[Fix] `nvm use`: fix `--silent` when version is omitted
Fixed a bug where --silent mode was failing for nvm use I ran a test
that just ran ```nvm use node --silent``` that I wrote by myself.
Unfortunately I noticed a bug where it still prints out some messages in
different cases. This pull request is to fix that bug. * Added in an argument
called ***quiet*** to the nvm_rc_version() function at *line 339* * Printed
anything inside the nvm_rc_version() only in the scenario where quiet mode is
off * Ran the nvm_rc_version() function in quiet mode only if silent mode is
on in the "use" command of nvm at *line 2990* * Ran *nvm_echo* and *nvm_err*
inside the "use" command of nvm only in the scenario where silent mode is off
(There were 4 scenarios where this was forgotten) * Edited the ```nvm
deactivate``` command to include a silent mode * Changed the help page to
include the --silent option for ```nvm deactivate``` * Added in aliases for
the --silent flag in ```nvm deactivate``` and ```nvm use``` * Used silent
mode inside the ```nvm use``` when running ```nvm deactivate``` inside it and
silent mode is on A test was attached in the 'test/slow/nvm use' directory.
It is named *Running "nvm use node --silent" doesn't print anything*. It runs
```nvm use node --silent and checks``` and succeeds if the OUTPUT matches the
EXPECTED_OUTPUT which is *null* or *''*. When the test was run locally, it
still ouputed results if someone deleted some contents of the *.nvm/* folder.
This output was given from the ```nvm deactivate command```. This was the
reason that the ```nvm deactivate``` was edited to include silent mode.
diff --git a/nvm.sh b/nvm.sh
index 843cebf..2e6c852 100644
--- a/nvm.sh
+++ b/nvm.sh
@@ -341,15 +341,21 @@
local NVMRC_PATH
NVMRC_PATH="$(nvm_find_nvmrc)"
if [ ! -e "${NVMRC_PATH}" ]; then
- nvm_err "No .nvmrc file found"
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
+ nvm_err "No .nvmrc file found"
+ fi
return 1
fi
NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf ''
if [ -z "${NVM_RC_VERSION}" ]; then
- nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
+ nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""
+ fi
return 2
fi
- nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
+ nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"
+ fi
}
nvm_clang_version() {
@@ -2995,15 +3001,18 @@
;;
"use")
local PROVIDED_VERSION
- local NVM_USE_SILENT
- NVM_USE_SILENT=0
+ local NVM_SILENT
+ local NVM_SILENT_ARG
local NVM_DELETE_PREFIX
NVM_DELETE_PREFIX=0
local NVM_LTS
while [ $# -ne 0 ]; do
case "$1" in
- --silent) NVM_USE_SILENT=1 ;;
+ --silent)
+ NVM_SILENT=1
+ NVM_SILENT_ARG='--silent'
+ ;;
--delete-prefix) NVM_DELETE_PREFIX=1 ;;
--) ;;
--lts) NVM_LTS='*' ;;
@@ -3021,7 +3030,7 @@
if [ -n "${NVM_LTS-}" ]; then
VERSION="$(nvm_match_version "lts/${NVM_LTS:-*}")"
elif [ -z "${PROVIDED_VERSION-}" ]; then
- nvm_rc_version
+ NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version
if [ -n "${NVM_RC_VERSION-}" ]; then
PROVIDED_VERSION="${NVM_RC_VERSION}"
VERSION="$(nvm_version "${PROVIDED_VERSION}")"
@@ -3041,30 +3050,32 @@
fi
if [ "_${VERSION}" = '_system' ]; then
- if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then
- if [ $NVM_USE_SILENT -ne 1 ]; then
+ if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)"
fi
return
- elif nvm_has_system_iojs && nvm deactivate >/dev/null 2>&1; then
- if [ $NVM_USE_SILENT -ne 1 ]; then
+ elif nvm_has_system_iojs && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)"
fi
return
- elif [ $NVM_USE_SILENT -ne 1 ]; then
+ elif [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err 'System version of node not found.'
fi
return 127
elif [ "_${VERSION}" = "_∞" ]; then
- if [ $NVM_USE_SILENT -ne 1 ]; then
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err "The alias \"${PROVIDED_VERSION}\" leads to an infinite loop. Aborting."
fi
return 8
fi
if [ "${VERSION}" = 'N/A' ]; then
- nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed."
- nvm_err ""
- nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it."
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
+ nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed."
+ nvm_err ""
+ nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it."
+ fi
return 3
# This nvm_ensure_version_installed call can be a performance bottleneck
# on shell startup. Perhaps we can optimize it away or make it faster.
@@ -3095,7 +3106,7 @@
fi
local NVM_USE_OUTPUT
NVM_USE_OUTPUT=''
- if [ $NVM_USE_SILENT -ne 1 ]; then
+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then
if nvm_is_iojs_version "${VERSION}"; then
NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)"
else
@@ -3108,14 +3119,14 @@
if [ -n "${PROVIDED_VERSION}" ]; then
NVM_USE_CMD="${NVM_USE_CMD} ${VERSION}"
fi
- if [ $NVM_USE_SILENT -eq 1 ]; then
+ if [ "${NVM_SILENT:-0}" -eq 1 ]; then
NVM_USE_CMD="${NVM_USE_CMD} --silent"
fi
if ! nvm_die_on_prefix "${NVM_DELETE_PREFIX}" "${NVM_USE_CMD}"; then
return 11
fi
fi
- if [ -n "${NVM_USE_OUTPUT-}" ]; then
+ if [ -n "${NVM_USE_OUTPUT-}" ] && [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_echo "${NVM_USE_OUTPUT}"
fi
;;
diff --git "a/test/slow/nvm use/Running \"nvm use node --silent\" doesn\047t print anything" "b/test/slow/nvm use/Running \"nvm use node --silent\" doesn\047t print anything"
new file mode 100755
index 0000000..1da47e8
--- /dev/null
+++ "b/test/slow/nvm use/Running \"nvm use node --silent\" doesn\047t print anything"
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+die () { echo "$@" ; exit 1; }
+
+\. ../../../nvm.sh
+
+nvm deactivate 2>&1 >/dev/null || die 'deactivate failed'
+
+OUTPUT=$(nvm use node --silent || die 'nvm use node failed')
+EXPECTED_OUTPUT=""
+
+[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
+ || die "'nvm use node --silent' output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"