diff --git a/nvm.sh b/nvm.sh index b31f217e..a95b7884 100755 --- a/nvm.sh +++ b/nvm.sh @@ -273,7 +273,9 @@ nvm_is_version_installed() { if [ "_$(nvm_get_os)" = '_win' ]; then NVM_NODE_BINARY='node.exe' fi - if [ -x "$(nvm_version_path "$1" 2>/dev/null)/bin/${NVM_NODE_BINARY}" ]; then + local VERSION_PATH + VERSION_PATH="$(nvm_version_path "$1" 2>/dev/null)" || return 1 + if [ -x "${VERSION_PATH}/bin/${NVM_NODE_BINARY}" ]; then return 0 fi return 1 diff --git a/test/fast/Unit tests/nvm_is_version_installed b/test/fast/Unit tests/nvm_is_version_installed index 7fe01313..0e130b6a 100755 --- a/test/fast/Unit tests/nvm_is_version_installed +++ b/test/fast/Unit tests/nvm_is_version_installed @@ -67,6 +67,11 @@ touch "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin/node" chmod +x "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin/node" nvm_is_version_installed "iojs-$IOJS_VERSION" || die "nvm_is_version_installed iojs-$IOJS_VERSION should work via the fallback" +# a version that nvm_version_path rejects is never installed +for BAD_VERSION in '..' 'v/../x' '../node/12.0.0' 'iojs-v/../x'; do + ! nvm_is_version_installed "$BAD_VERSION" || die "nvm_is_version_installed $BAD_VERSION should fail with a '..' path component" +done + # the modern layout is found without shelling out to detect the OS FAST_VERSION='14.0.0' ! nvm_is_version_installed "$FAST_VERSION" || die "nvm_is_version_installed $FAST_VERSION should fail before it is installed" @@ -92,5 +97,14 @@ nvm_get_os() { } check_version '13.0.0' 'node.exe' +# nvm_version_path's failure must be honored, not just its (usually empty) output, +# which would otherwise turn the check into one for `/bin/node` +nvm_version_path() { + echo "$NODE_PATH/13.0.0" + return 3 +} +! nvm_is_version_installed 'v/../x' || die "nvm_is_version_installed must fail when nvm_version_path fails" +unset -f nvm_version_path + cleanup