Skip to content

Commit

Permalink
[Fix] nvm_die_on_prefix: use directory comparison rather than string
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanarmstrong authored and ljharb committed Oct 9, 2020
1 parent 96069da commit e01060f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,7 @@ nvm_die_on_prefix() {
local NVM_OS
NVM_OS="$(nvm_get_os)"
NVM_NPM_PREFIX="$(npm config --loglevel=warn get prefix)"
if [ "${NVM_VERSION_DIR}" != "${NVM_NPM_PREFIX}" ] && ! (nvm_tree_contains_path "${NVM_VERSION_DIR}" "${NVM_NPM_PREFIX}" >/dev/null 2>&1); then
if [ ! "${NVM_VERSION_DIR}" -ef "${NVM_NPM_PREFIX}" ] && ! (nvm_tree_contains_path "${NVM_VERSION_DIR}" "${NVM_NPM_PREFIX}" >/dev/null 2>&1); then
if [ "_${NVM_DELETE_PREFIX}" = "_1" ]; then
npm config --loglevel=warn delete prefix
else
Expand Down
28 changes: 27 additions & 1 deletion test/fast/Unit tests/nvm_die_on_prefix
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#!/bin/sh

TEST_PWD=$(pwd)
TEST_DIR="$TEST_PWD/nvm_die_on_prefix_tmp"

cleanup () {
rm -rf "$TEST_DIR"
alias nvm_has='\nvm_has'
alias npm='\npm'
unset -f nvm_has npm
}
die () { echo "$@" ; exit 1; }

die () {
echo "$@";
cleanup;
exit 1;
}

[ ! -e "$TEST_DIR" ] && mkdir "$TEST_DIR"

\. ../../../nvm.sh

Expand Down Expand Up @@ -40,9 +51,24 @@ npm() {
echo "$(nvm_version_dir new)/good prefix"
fi
}

OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when prefix is good; got '$OUTPUT'"

mkdir -p "$(nvm_version_dir new)"
ln -s "$(nvm_version_dir new)" "$TEST_DIR/node"

npm() {
local args
args="$@"
if [ "_$args" = "_config --loglevel=warn get prefix" ]; then
echo "$TEST_DIR/node"
fi
}

OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when directory is equivalent; got '$OUTPUT'"

OUTPUT="$(PREFIX=bar nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
EXPECTED_OUTPUT='nvm is not compatible with the "PREFIX" environment variable: currently set to "bar"
Run `unset PREFIX` to unset it.'
Expand Down

0 comments on commit e01060f

Please sign in to comment.