[Fix] `nvm_die_on_prefix`: use directory comparison rather than string

Dylan Armstrong 2020-10-09 09:43:21 -05:00 committed by Jordan Harband
parent 96069da0d5
commit e01060fa2c
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
2 changed files with 28 additions and 2 deletions

2
nvm.sh
View File

@ -2231,7 +2231,7 @@ nvm_die_on_prefix() {
local NVM_OS local NVM_OS
NVM_OS="$(nvm_get_os)" NVM_OS="$(nvm_get_os)"
NVM_NPM_PREFIX="$(npm config --loglevel=warn get prefix)" 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 if [ "_${NVM_DELETE_PREFIX}" = "_1" ]; then
npm config --loglevel=warn delete prefix npm config --loglevel=warn delete prefix
else else

View File

@ -1,11 +1,22 @@
#!/bin/sh #!/bin/sh
TEST_PWD=$(pwd)
TEST_DIR="$TEST_PWD/nvm_die_on_prefix_tmp"
cleanup () { cleanup () {
rm -rf "$TEST_DIR"
alias nvm_has='\nvm_has' alias nvm_has='\nvm_has'
alias npm='\npm' alias npm='\npm'
unset -f nvm_has npm unset -f nvm_has npm
} }
die () { echo "$@" ; exit 1; }
die () {
echo "$@";
cleanup;
exit 1;
}
[ ! -e "$TEST_DIR" ] && mkdir "$TEST_DIR"
\. ../../../nvm.sh \. ../../../nvm.sh
@ -40,9 +51,24 @@ npm() {
echo "$(nvm_version_dir new)/good prefix" echo "$(nvm_version_dir new)/good prefix"
fi fi
} }
OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)" 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'" [ -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)" 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" EXPECTED_OUTPUT='nvm is not compatible with the "PREFIX" environment variable: currently set to "bar"
Run `unset PREFIX` to unset it.' Run `unset PREFIX` to unset it.'