Skip to content

Commit

Permalink
Ensure that if npm config get prefix is set to something outside `n…
Browse files Browse the repository at this point in the history
…vm`, that `nvm use` refuses to work.

Fixes #606.
  • Loading branch information
ljharb committed Sep 21, 2015
1 parent a1def71 commit 1458de7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
27 changes: 26 additions & 1 deletion nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1267,9 +1267,17 @@ nvm_die_on_prefix() {
return 2
fi

if [ -n "$PREFIX" ] && ! (nvm_tree_contains_path "$NVM_DIR" "$PREFIX" >/dev/null 2>&1); then
nvm deactivate >/dev/null 2>&1
echo >&2 "nvm is not compatible with the \"PREFIX\" environment variable: currently set to \"$PREFIX\""
echo >&2 "Run \`unset PREFIX\` to unset it."
return 3
fi

if ! nvm_has 'npm'; then
return
fi

local NVM_NPM_PREFIX
NVM_NPM_PREFIX="$(npm config get prefix)"
if ! (nvm_tree_contains_path "$NVM_DIR" "$NVM_NPM_PREFIX" >/dev/null 2>&1); then
Expand All @@ -1283,7 +1291,7 @@ nvm_die_on_prefix() {
else
echo >&2 "Run \`$NVM_COMMAND\` to unset it."
fi
return 3
return 4
fi
fi
}
Expand Down Expand Up @@ -1596,11 +1604,15 @@ nvm() {
local PROVIDED_VERSION
local NVM_USE_SILENT
NVM_USE_SILENT=0
local NVM_DELETE_PREFIX
NVM_DELETE_PREFIX=0

shift # remove "use"
while [ $# -ne 0 ]
do
case "$1" in
--silent) NVM_USE_SILENT=1 ;;
--delete-prefix) NVM_DELETE_PREFIX=1 ;;
*)
if [ -n "$1" ]; then
PROVIDED_VERSION="$1"
Expand Down Expand Up @@ -1690,6 +1702,19 @@ nvm() {
echo "Now using node $VERSION$(nvm_print_npm_version)"
fi
fi
if [ "_$VERSION" != "_system" ]; then
local NVM_USE_CMD
NVM_USE_CMD="nvm use --delete-prefix"
if [ -n "$PROVIDED_VERSION" ]; then
NVM_USE_CMD="$NVM_USE_CMD $VERSION"
fi
if [ $NVM_USE_SILENT -eq 1 ]; then
NVM_USE_CMD="$NVM_USE_CMD --silent"
fi
if ! nvm_die_on_prefix "$NVM_DELETE_PREFIX" "$NVM_USE_CMD"; then
return 11
fi
fi
;;
"run" )
local provided_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ die () { echo $@ ; exit 1; }
[ `expr $PATH : ".*v0.2.3/.*/bin"` = 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2

. ../../nvm.sh
nvm use v0.2.3 || die "Failed to activate v0.2.3"
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)"
# ^ note: NODE_PATH should not contain `npm root -g` since globals should not be requireable
Expand Down
9 changes: 8 additions & 1 deletion test/fast/Unit tests/nvm_die_on_prefix
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ npm() {
OUTPUT="$(nvm_die_on_prefix 0 foo 2>&1)"
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when prefix is good; got '$OUTPUT'"

OUTPUT="$(PREFIX=bar nvm_die_on_prefix 0 foo 2>&1)"
EXPECTED_OUTPUT='nvm is not compatible with the "PREFIX" environment variable: currently set to "bar"
Run `unset PREFIX` to unset it.'
EXIT_CODE="$(PREFIX=bar nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
[ "_$EXIT_CODE" = "_3" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 3; got '$EXIT_CODE'"

npm() {
local args
args="$@"
Expand All @@ -55,6 +62,6 @@ EXPECTED_OUTPUT="nvm is not compatible with the npm config \"prefix\" option: cu
Run \`npm config delete prefix\` or \`foo\` to unset it."
EXIT_CODE="$(nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT' with bad prefix set; got '$OUTPUT'"
[ "_$EXIT_CODE" = "_3" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 3 with bad prefix set; got '$EXIT_CODE'"
[ "_$EXIT_CODE" = "_4" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 4 with bad prefix set; got '$EXIT_CODE'"

cleanup
22 changes: 22 additions & 0 deletions test/slow/nvm use/Running "nvm use" calls "nvm_die_on_prefix"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

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

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

nvm deactivate >/dev/null 2>&1 || die 'deactivate failed'

nvm_die_on_prefix() {
echo >&2 "| $1 | $2 |"
return 3
}

OUTPUT="$(nvm use --silent node)"
EXPECTED_OUTPUT=""
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm use --silent node' did not call through to 'nvm_die_on_prefix' and give output '$EXPECTED_OUTPUT'; got '$OUTPUT'"

EXIT_CODE="$(nvm use --silent node >/dev/null 2>&1; echo $?)"
EXPECTED_CODE="11"
[ "_$EXIT_CODE" = "_$EXPECTED_CODE" ] \
|| die "'nvm use --silent node' when 'nvm_die_on_prefix' fails did not return '$EXPECTED_CODE'; got '$EXIT_CODE'"

0 comments on commit 1458de7

Please sign in to comment.