Skip to content

Commit

Permalink
Fix: nvm run should print the proper error
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakchethan committed Mar 29, 2022
1 parent ce938a6 commit 0d56b17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 6 additions & 2 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3657,6 +3657,8 @@ nvm() {
local provided_version
local has_checked_nvmrc
has_checked_nvmrc=0
local IS_VERSION_FROM_NVMRC
IS_VERSION_FROM_NVMRC=0
# run given version of node

local NVM_SILENT
Expand Down Expand Up @@ -3702,7 +3704,9 @@ nvm() {
if [ $has_checked_nvmrc -ne 1 ]; then
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version && has_checked_nvmrc=1
fi
VERSION="$(nvm_version "${NVM_RC_VERSION}")" ||:
provided_version="${NVM_RC_VERSION}"
IS_VERSION_FROM_NVMRC=1
VERSION="$(nvm_version "${provided_version}")" ||:
unset NVM_RC_VERSION
else
shift
Expand All @@ -3724,7 +3728,7 @@ nvm() {
VERSION=''
fi
if [ "_${VERSION}" = "_N/A" ]; then
nvm_ensure_version_installed "${provided_version}"
nvm_ensure_version_installed "${provided_version}" "${IS_VERSION_FROM_NVMRC}"
elif [ "${NVM_IOJS}" = true ]; then
nvm exec "${NVM_SILENT_ARG-}" "${LTS_ARG-}" "${VERSION}" iojs "$@"
else
Expand Down
13 changes: 11 additions & 2 deletions test/slow/nvm run/Running 'nvm run' should pick up .nvmrc version
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ die () { echo "$@" ; exit 1; }

echo "0.10.7" > .nvmrc

[ "$(nvm run --version | tail -1)" = "v0.10.7" ] || die "\`nvm run\` failed to run with the .nvmrc version"

[ "$(nvm run --version | head -1)" = "Found '$PWD/.nvmrc' with version <0.10.7>" ] || die "\`nvm run\` failed to print out the \"found in .nvmrc\" message"


# running nvm run with .nvmrc should not print the version information when not installed
OUTPUT="$(nvm run --version 2>&1)"
EXIT_CODE=$?
EXPECTED_OUTPUT="Found '$PWD/.nvmrc' with version <0.10.7>
N/A: version \"v0.10.7\" is not yet installed.
You need to run \`nvm install\` to install and use the node version specified in \`.nvmrc\`."
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm use' with nvmrc to give $EXPECTED_OUTPUT, got $OUTPUT"
[ "_$EXIT_CODE" = "_1" ] || die "expected 'nvm use' with nvmrc to exit with 1, got $EXIT_CODE"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ nvm deactivate 2>&1 >/dev/null || die 'deactivate failed'

echo "foo" > .nvmrc

# running nvm use should not print the version information
# running nvm use with .nvmrc should not print the version information
OUTPUT="$(nvm use 2>&1)"
EXIT_CODE=$?
EXPECTED_OUTPUT="Found '$PWD/.nvmrc' with version <foo>
Expand Down

0 comments on commit 0d56b17

Please sign in to comment.