From 0798f7413ce924edc80db138ffd65255caa481c9 Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Mon, 14 Nov 2016 19:18:24 +0200 Subject: [PATCH 1/7] Upon load, don't change current node version if already set by nvm --- nvm.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 1a6798f..29e2acd 100755 --- a/nvm.sh +++ b/nvm.sh @@ -3216,6 +3216,8 @@ nvm_supports_xz() { } nvm_auto() { + local NVM_CURRENT + NVM_CURRENT="$(nvm_ls_current)" local NVM_MODE NVM_MODE="${1-}" local VERSION @@ -3227,7 +3229,11 @@ nvm_auto() { nvm install >/dev/null fi elif [ "_$NVM_MODE" = '_use' ]; then - VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)" + if [ "_$NVM_CURRENT" != '_system' ]; then + VERSION="$NVM_CURRENT" + else + VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)" + fi if [ -n "$VERSION" ]; then nvm use --silent "$VERSION" >/dev/null elif nvm_rc_version >/dev/null 2>&1; then From 4b5df30460ec3cae04bb8eaedabbde0b90c05298 Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Mon, 14 Nov 2016 19:19:39 +0200 Subject: [PATCH 2/7] Don't mess with the order of the path locations when changing versions --- nvm.sh | 33 +++++++++++++++++---------- test/fast/Unit tests/nvm_change_path | 25 ++++++++++++++++++++ test/fast/Unit tests/nvm_prepend_path | 18 --------------- 3 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 test/fast/Unit tests/nvm_change_path delete mode 100755 test/fast/Unit tests/nvm_prepend_path diff --git a/nvm.sh b/nvm.sh index 29e2acd..688583c 100755 --- a/nvm.sh +++ b/nvm.sh @@ -484,11 +484,24 @@ nvm_strip_path() { -e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*##g" } -nvm_prepend_path() { +nvm_change_path() { + # if there’s no initial path, just return the supplementary path if [ -z "${1-}" ]; then - nvm_echo "${2-}" + nvm_echo "${3-}${2-}" + # if the initial path doesn’t contain an nvm path, prepend the supplementary + # path + elif [ "${1#*$NVM_DIR}" == "$1" ]; then + nvm_echo "${3-}${2-}:${1-}" + # use sed to replace the existing nvm path with the supplementary path. This + # preserves the order of the path. else - nvm_echo "${2-}:${1-}" + nvm_echo "${1-}" | command sed \ + -e "s#${NVM_DIR}/[^/]*${2-}[^:]*:#${3-}${2-}:#g" \ + -e "s#:${NVM_DIR}/[^/]*${2-}[^:]*#:${3-}${2-}#g" \ + -e "s#${NVM_DIR}/[^/]*${2-}[^:]*#${3-}${2-}#g" \ + -e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*:#${3-}${2-}:#g" \ + -e "s#:${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*#:${3-}${2-}#g" \ + -e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*#${3-}${2-}#g" fi } @@ -2636,18 +2649,14 @@ nvm() { local NVM_VERSION_DIR NVM_VERSION_DIR="$(nvm_version_path "$VERSION")" - # Strip other version from PATH - PATH="$(nvm_strip_path "$PATH" "/bin")" - # Prepend current version - PATH="$(nvm_prepend_path "$PATH" "$NVM_VERSION_DIR/bin")" + # Change current version + PATH="$(nvm_change_path "$PATH" "/bin" "$NVM_VERSION_DIR")" if nvm_has manpath; then if [ -z "$MANPATH" ]; then MANPATH=$(manpath) fi - # Strip other version from MANPATH - MANPATH="$(nvm_strip_path "$MANPATH" "/share/man")" - # Prepend current version - MANPATH="$(nvm_prepend_path "$MANPATH" "$NVM_VERSION_DIR/share/man")" + # Change current version + MANPATH="$(nvm_change_path "$MANPATH" "/share/man" "$NVM_VERSION_DIR")" export MANPATH fi export PATH @@ -3146,7 +3155,7 @@ nvm() { nvm_ensure_default_set nvm_get_arch nvm_get_os \ nvm_print_implicit_alias nvm_validate_implicit_alias \ nvm_resolve_alias nvm_ls_current nvm_alias \ - nvm_binary_available nvm_prepend_path nvm_strip_path \ + nvm_binary_available nvm_change_path nvm_strip_path \ nvm_num_version_groups nvm_format_version nvm_ensure_version_prefix \ nvm_normalize_version nvm_is_valid_version \ nvm_ensure_version_installed \ diff --git a/test/fast/Unit tests/nvm_change_path b/test/fast/Unit tests/nvm_change_path new file mode 100644 index 0000000..41e0d0b --- /dev/null +++ b/test/fast/Unit tests/nvm_change_path @@ -0,0 +1,25 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +\. ../../../nvm.sh + +TEST_PATH=/usr/bin:/usr/local/bin + +NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0/bin"` + +[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0:/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " + + +TEST_PATH=/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v4.5.0:/usr/bin:/usr/local/bin + +NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0/bin"` + +[ "$NEW_PATH" = "/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v7.1.0:/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " + + +EMPTY_PATH= + +NEW_PATH=`nvm_change_path "$EMPTY_PATH" "/bin" "$NVM_DIR/versions/node/v4.5.0/bin"` + +[ "$NEW_PATH" = "$NVM_DIR/versions/node/v4.5.0/bin" ] || die "Not correctly prepended: $NEW_PATH " diff --git a/test/fast/Unit tests/nvm_prepend_path b/test/fast/Unit tests/nvm_prepend_path deleted file mode 100755 index 37df3f9..0000000 --- a/test/fast/Unit tests/nvm_prepend_path +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -die () { echo "$@" ; exit 1; } - -\. ../../../nvm.sh - -TEST_PATH=/usr/bin:/usr/local/bin - -NEW_PATH=`nvm_prepend_path "$TEST_PATH" "$NVM_DIR/v0.2.5/bin"` - -[ "$NEW_PATH" = "$NVM_DIR/v0.2.5/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly prepended: $NEW_PATH " - - -EMPTY_PATH= - -NEW_PATH=`nvm_prepend_path "$EMPTY_PATH" "$NVM_DIR/v0.2.5/bin"` - -[ "$NEW_PATH" = "$NVM_DIR/v0.2.5/bin" ] || die "Not correctly prepended: $NEW_PATH " From 06e5336b56fd1363a643965387bc20c4b6ddba33 Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Tue, 15 Nov 2016 18:43:02 +0200 Subject: [PATCH 3/7] Performance enhancement If nvm has already set up env, don't `nvm use` when running `nvm_auto`. Incidentally, failing tests from 0798f74 are now gone, too. --- nvm.sh | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/nvm.sh b/nvm.sh index 688583c..3c4eb69 100755 --- a/nvm.sh +++ b/nvm.sh @@ -3238,15 +3238,13 @@ nvm_auto() { nvm install >/dev/null fi elif [ "_$NVM_MODE" = '_use' ]; then - if [ "_$NVM_CURRENT" != '_system' ]; then - VERSION="$NVM_CURRENT" - else + if [ "_$NVM_CURRENT" = '_system' ]; then VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)" - fi - 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 [ -n "$VERSION" ]; then + nvm use --silent "$VERSION" >/dev/null + elif nvm_rc_version >/dev/null 2>&1; then + nvm use --silent >/dev/null + fi fi elif [ "_$NVM_MODE" != '_none' ]; then nvm_err 'Invalid auto mode supplied.' From 88998c35f28161f1ed054efa5b0ff3779a1d7477 Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Tue, 15 Nov 2016 21:47:23 +0200 Subject: [PATCH 4/7] Better pre-managed PATH check in nvm_change_path --- nvm.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 3c4eb69..2dd6bfe 100755 --- a/nvm.sh +++ b/nvm.sh @@ -490,7 +490,8 @@ nvm_change_path() { nvm_echo "${3-}${2-}" # if the initial path doesn’t contain an nvm path, prepend the supplementary # path - elif [ "${1#*$NVM_DIR}" == "$1" ]; then + elif [ `expr "${1-}" : ".*${NVM_DIR}/[^:]*${2-}.*"` = 0 ] || \ + [ `expr "${1-}" : ".*${NVM_DIR}/versions/[^/]*/[^/]*${2-}.*"` = 0 ]; then nvm_echo "${3-}${2-}:${1-}" # use sed to replace the existing nvm path with the supplementary path. This # preserves the order of the path. From 19288e279b5ac849e3361e20f6e8bb0269b36ae6 Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Wed, 16 Nov 2016 07:48:45 +0200 Subject: [PATCH 5/7] nvm's paths aren't always in the same place in PATH anymore --- ...\" should unset the nvm environment variables." | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git "a/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." "b/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." index af9ecef..3eb1b3f 100755 --- "a/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." +++ "b/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." @@ -6,19 +6,19 @@ mkdir -p ../../v0.2.3 die () { echo "$@" ; exit 1; } -[ `expr $PATH : ".*v0.2.3/.*/bin"` = 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2 +[ `expr $PATH : ".*v0.2.3/.*/bin.*"` = 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2 \. ../../nvm.sh nvm use --delete-prefix v0.2.3 || die "Failed to activate v0.2.3" -[ `expr "$PATH" : ".*v0.2.3/.*/bin"` != 0 ] || die "PATH not set up properly" -[ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules"` = 0 ] || die "NODE_PATH should not contain (npm root -g)" -[ `expr "$NVM_BIN" : ".*v0.2.3/bin"` != 0 ] || die "NODE_BIN should contain bin directory path" -[ `expr "$NVM_PATH" : ".*v0.2.3/lib/node"` != 0 ] || die "NODE_PATH should contain lib node directory path" +[ `expr "$PATH" : ".*v0.2.3/.*/bin.*"` != 0 ] || die "PATH not set up properly" +[ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules.*"` = 0 ] || die "NODE_PATH should not contain (npm root -g)" +[ `expr "$NVM_BIN" : ".*v0.2.3/bin.*"` != 0 ] || die "NODE_BIN should contain bin directory path" +[ `expr "$NVM_PATH" : ".*v0.2.3/lib/node.*"` != 0 ] || die "NODE_PATH should contain lib node directory path" # ^ note: NODE_PATH should not contain `npm root -g` since globals should not be requireable nvm deactivate || die "Failed to deactivate v0.2.3" -[ `expr "$PATH" : ".*v0.2.3/.*/bin"` = 0 ] || die "PATH not cleaned properly" -[ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules"` = 0 ] || die "NODE_PATH not cleaned properly" +[ `expr "$PATH" : ".*v0.2.3/.*/bin.*"` = 0 ] || die "PATH not cleaned properly" +[ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules.*"` = 0 ] || die "NODE_PATH not cleaned properly" [ "_$NVM_BIN" = "_" ] || die "NVM_BIN should be unset: got '$NVM_BIN'" [ "_$NVM_PATH" = "_" ] || die "NVM_PATH should be unset: got '$NVM_PATH'" From a41e8018f41655ca3efd3675bc041d562a7c673c Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Wed, 16 Nov 2016 09:50:20 +0200 Subject: [PATCH 6/7] Compliance with shellcheck --- nvm.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index 2dd6bfe..27fdbf2 100755 --- a/nvm.sh +++ b/nvm.sh @@ -490,8 +490,8 @@ nvm_change_path() { nvm_echo "${3-}${2-}" # if the initial path doesn’t contain an nvm path, prepend the supplementary # path - elif [ `expr "${1-}" : ".*${NVM_DIR}/[^:]*${2-}.*"` = 0 ] || \ - [ `expr "${1-}" : ".*${NVM_DIR}/versions/[^/]*/[^/]*${2-}.*"` = 0 ]; then + elif [ "$(expr "${1-}" : ".*${NVM_DIR}/[^:]*${2-}.*")" = 0 ] || \ + [ "$(expr "${1-}" : ".*${NVM_DIR}/versions/[^/]*/[^/]*${2-}.*")" = 0 ]; then nvm_echo "${3-}${2-}:${1-}" # use sed to replace the existing nvm path with the supplementary path. This # preserves the order of the path. From 3aef580a861a4d6b527a65ed5bd268f1ebe7fe34 Mon Sep 17 00:00:00 2001 From: Xandor Schiefer Date: Wed, 16 Nov 2016 14:17:12 +0200 Subject: [PATCH 7/7] Improved nvm_change_path unit test --- test/fast/Unit tests/nvm_change_path | 32 ++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) mode change 100644 => 100755 test/fast/Unit tests/nvm_change_path diff --git a/test/fast/Unit tests/nvm_change_path b/test/fast/Unit tests/nvm_change_path old mode 100644 new mode 100755 index 41e0d0b..621729f --- a/test/fast/Unit tests/nvm_change_path +++ b/test/fast/Unit tests/nvm_change_path @@ -6,20 +6,38 @@ die () { echo "$@" ; exit 1; } TEST_PATH=/usr/bin:/usr/local/bin -NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0/bin"` +# New version dir +NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0"` -[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0:/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " +[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " + +# Old version dir +NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/v0.1.2"` + +[ "$NEW_PATH" = "$NVM_DIR/v0.1.2/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " -TEST_PATH=/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v4.5.0:/usr/bin:/usr/local/bin +TEST_PATH=/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v4.5.0/bin:/usr/bin:/usr/local/bin -NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0/bin"` +# New version dir +NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0"` -[ "$NEW_PATH" = "/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v7.1.0:/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " +[ "$NEW_PATH" = "/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v7.1.0/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " + +# Old version dir +NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/v0.1.2"` + +[ "$NEW_PATH" = "/home/user/code/test/node_modules/.bin:$NVM_DIR/v0.1.2/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH " EMPTY_PATH= -NEW_PATH=`nvm_change_path "$EMPTY_PATH" "/bin" "$NVM_DIR/versions/node/v4.5.0/bin"` +# New version dir +NEW_PATH=`nvm_change_path "$EMPTY_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0"` -[ "$NEW_PATH" = "$NVM_DIR/versions/node/v4.5.0/bin" ] || die "Not correctly prepended: $NEW_PATH " +[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0/bin" ] || die "Not correctly prepended: $NEW_PATH " + +# Old version dir +NEW_PATH=`nvm_change_path "$EMPTY_PATH" "/bin" "$NVM_DIR/v0.1.2"` + +[ "$NEW_PATH" = "$NVM_DIR/v0.1.2/bin" ] || die "Not correctly prepended: $NEW_PATH "