Skip to content

Commit f6757b1

Browse files
committed
Normalize paths in nvm_tree_contains_path before comparing
It's possible that `tree` and `node_path` both refer to the same directory, but are lexically different. This converts both paths into a canonical form so that any lexical-only differences are removed. This solves a problem where setting `NVM_DIR` to /home/user//.nvm instead of /home/user/.nvm results in the infamous: nvm is not compatible with the npm config "prefix" option This adds a test that the above case no longer results in an error. An existing test stopped failing as expected as a result, as the test set the npm prefix to a local-directory-relative path, which resolved to a directory inside the `NVM_DIR` when running the tests from within the `NVM_DIR`. This test is updated to use an absolute path.
1 parent 45c1b84 commit f6757b1

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

nvm.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ unset NVM_SCRIPT_SOURCE 2>/dev/null
278278

279279
nvm_tree_contains_path() {
280280
local tree
281-
tree="${1-}"
281+
tree=$(realpath --canonicalize-missing "${1-}")
282282
local node_path
283-
node_path="${2-}"
283+
node_path=$(realpath --canonicalize-missing "${2-}")
284284

285285
if [ "@${tree}@" = "@@" ] || [ "@${node_path}@" = "@@" ]; then
286286
nvm_err "both the tree and the node path are required"

test/fast/Unit tests/nvm_die_on_prefix

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,28 @@ npm() {
6868
local args
6969
args="$@"
7070
if [ "_$args" = "_config --loglevel=warn get prefix" ]; then
71-
echo "./bad prefix"
71+
echo "/bad/prefix"
7272
fi
7373
}
7474
OUTPUT="$(nvm_die_on_prefix 0 foo 2>&1)"
75-
EXPECTED_OUTPUT="nvm is not compatible with the npm config \"prefix\" option: currently set to \"./bad prefix\"
75+
EXPECTED_OUTPUT="nvm is not compatible with the npm config \"prefix\" option: currently set to \"/bad/prefix\"
7676
Run \`npm config delete prefix\` or \`foo\` to unset it."
7777
EXIT_CODE="$(nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
7878
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT' with bad prefix set; got '$OUTPUT'"
7979
[ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 10 with bad prefix set; got '$EXIT_CODE'"
8080

81+
npm() {
82+
local args
83+
args="$@"
84+
if [ "_$args" = "_config --loglevel=warn get prefix" ]; then
85+
echo "/home/foo/.nvm/path/to/version"
86+
fi
87+
}
88+
NON_NORMALIZED_NVM_DIR="//home//foo//.nvm/"
89+
OUTPUT="$(export NVM_DIR="$NON_NORMALIZED_NVM_DIR" ; nvm_die_on_prefix 0 foo 2>&1)"
90+
EXPECTED_OUTPUT=''
91+
EXIT_CODE="$(export NVM_DIR="$NON_NORMALIZED_NVM_DIR" ; nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
92+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT' with non-normalized NVM_DIR; got '$OUTPUT'"
93+
[ "_$EXIT_CODE" = "_0" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 0 with non-normalized NVM_DIR; got '$EXIT_CODE'"
94+
8195
cleanup

0 commit comments

Comments
 (0)