From eabd7ab13d4823928fd61629c32c9bf14accdeb2 Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Mon, 14 Nov 2016 19:18:24 +0200 Subject: [PATCH] Respect previously nvm-loaded node version when sourcing --- nvm.sh | 16 ++++++++++----- ...vm.sh should keep version if one is active | 20 +++++++++++++++++++ ...lt if available and no nvm node is loaded} | 19 ++++++++++++++---- test/sourcing/setup | 1 + test/sourcing/teardown | 1 + 5 files changed, 48 insertions(+), 9 deletions(-) create mode 100755 test/sourcing/Sourcing nvm.sh should keep version if one is active rename test/sourcing/{Sourcing nvm.sh should use the default if available => Sourcing nvm.sh should use the default if available and no nvm node is loaded} (53%) diff --git a/nvm.sh b/nvm.sh index 5d488fe..d854605 100644 --- a/nvm.sh +++ b/nvm.sh @@ -3606,6 +3606,8 @@ nvm_supports_xz() { } nvm_auto() { + local NVM_CURRENT + NVM_CURRENT="$(nvm_ls_current)" local NVM_MODE NVM_MODE="${1-}" local VERSION @@ -3617,11 +3619,15 @@ nvm_auto() { nvm install >/dev/null fi elif [ "_$NVM_MODE" = '_use' ]; then - VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)" - if [ -n "$VERSION" ]; then - nvm use --silent "$VERSION" >/dev/null - elif nvm_rc_version >/dev/null 2>&1; then - nvm use --silent >/dev/null + if [ "_${NVM_CURRENT}" = '_none' ] || [ "_${NVM_CURRENT}" = '_system' ]; then + VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)" + if [ -n "${VERSION}" ]; then + nvm use --silent "${VERSION}" >/dev/null + elif nvm_rc_version >/dev/null 2>&1; then + nvm use --silent >/dev/null + fi + else + nvm use --silent "${NVM_CURRENT}" >/dev/null fi elif [ "_$NVM_MODE" != '_none' ]; then nvm_err 'Invalid auto mode supplied.' diff --git a/test/sourcing/Sourcing nvm.sh should keep version if one is active b/test/sourcing/Sourcing nvm.sh should keep version if one is active new file mode 100755 index 0000000..4254159 --- /dev/null +++ b/test/sourcing/Sourcing nvm.sh should keep version if one is active @@ -0,0 +1,20 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +echo '0.10.1' > ../../alias/default || die "couldn't create default alias" + +\. ../../nvm.sh || die 'sourcing returned nonzero exit code' + +NVM_TARGET=0.10.3 +NVM_DEFAULT="$(nvm_resolve_local_alias default)" + +[ "_$NVM_DEFAULT" != "_$NVM_TARGET" ] || die "default $NVM_DEFAULT is the same as target $NVM_TARGET" + +nvm use $NVM_TARGET || die "nvm use $NVM_TARGET failed" + +\. ../../nvm.sh || die 'sourcing returned nonzero exit code' + +NVM_CURRENT="$(nvm current)" + +[ "_${NVM_CURRENT#v}" = "_$NVM_TARGET" ] || die "node version not retained after sourcing" diff --git a/test/sourcing/Sourcing nvm.sh should use the default if available b/test/sourcing/Sourcing nvm.sh should use the default if available and no nvm node is loaded similarity index 53% rename from test/sourcing/Sourcing nvm.sh should use the default if available rename to test/sourcing/Sourcing nvm.sh should use the default if available and no nvm node is loaded index 67e3048..fe837f2 100755 --- a/test/sourcing/Sourcing nvm.sh should use the default if available +++ b/test/sourcing/Sourcing nvm.sh should use the default if available and no nvm node is loaded @@ -2,13 +2,24 @@ die () { echo "$@" ; exit 1; } -echo '0.10.1' > ../../alias/default || die 'creation of default alias failed' - -\. ../../nvm.sh || die 'sourcing returned nonzero exit code' \. ../common.sh +# We need to unload nvm again first, as by the time this test is run, +# despite being unloaded in setup, the inherited PATH still contains +# an nvm-installed node version. We have to reset NVM_DIR after the unload. +\. ../../nvm.sh || die 'sourcing returned nonzero exit code' +NVM_DIR_CACHED="$NVM_DIR" +nvm unload || die 'unloading returned nonzero exit code' +NVM_DIR="$NVM_DIR_CACHED" + +echo '0.10.1' > ../../alias/default || die 'creation of default alias failed' + +# Now to begin the real test +\. ../../nvm.sh || die 'sourcing returned nonzero exit code' + +NVM_LS_CURRENT_NOT_GREPPED="$(nvm ls current | strip_colors)" NVM_LS_CURRENT="$(nvm ls current | strip_colors | \grep -o v0.10.1)" -[ "_$NVM_LS_CURRENT" = '_v0.10.1' ] || die "'nvm ls current' did not return '-> v0.10.1', got '$NVM_LS_CURRENT'" +[ "_$NVM_LS_CURRENT" = '_v0.10.1' ] || die "'nvm ls current' did not return '-> v0.10.1', got '$NVM_LS_CURRENT_NOT_GREPPED'" # NVM_LS_CURRENT_COLORED="$(nvm ls current | sed -n l)" diff --git a/test/sourcing/setup b/test/sourcing/setup index 44b688e..a7f6be8 100755 --- a/test/sourcing/setup +++ b/test/sourcing/setup @@ -9,5 +9,6 @@ rm -rf ../../v0.9.12 \. ../../nvm.sh nvm install 0.10.1 || echo >&2 'nvm install 0.10.1 failed' +nvm install 0.10.3 || echo >&2 'nvm install 0.10.3 failed' nvm unalias default || 'removing default alias failed' nvm unload || echo >&2 'nvm unload failed' diff --git a/test/sourcing/teardown b/test/sourcing/teardown index 00a8749..9bb52b0 100755 --- a/test/sourcing/teardown +++ b/test/sourcing/teardown @@ -3,4 +3,5 @@ rm -rf ../../alias rm -rf ../../v0.10.1 rm -rf ../../v0.10.2 +rm -rf ../../v0.10.3 rm -f ../../.nvmrc