Ensure that if `npm config get prefix` is set to something outside `nvm`, that `nvm use` refuses to work.

Fixes #606.
Jordan Harband 2015-09-21 04:47:37 -07:00
parent a1def71062
commit 1458de7293
4 changed files with 57 additions and 3 deletions

27
nvm.sh
View File

@ -1267,9 +1267,17 @@ nvm_die_on_prefix() {
return 2 return 2
fi 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 if ! nvm_has 'npm'; then
return return
fi fi
local NVM_NPM_PREFIX local NVM_NPM_PREFIX
NVM_NPM_PREFIX="$(npm config get prefix)" NVM_NPM_PREFIX="$(npm config get prefix)"
if ! (nvm_tree_contains_path "$NVM_DIR" "$NVM_NPM_PREFIX" >/dev/null 2>&1); then if ! (nvm_tree_contains_path "$NVM_DIR" "$NVM_NPM_PREFIX" >/dev/null 2>&1); then
@ -1283,7 +1291,7 @@ nvm_die_on_prefix() {
else else
echo >&2 "Run \`$NVM_COMMAND\` to unset it." echo >&2 "Run \`$NVM_COMMAND\` to unset it."
fi fi
return 3 return 4
fi fi
fi fi
} }
@ -1596,11 +1604,15 @@ nvm() {
local PROVIDED_VERSION local PROVIDED_VERSION
local NVM_USE_SILENT local NVM_USE_SILENT
NVM_USE_SILENT=0 NVM_USE_SILENT=0
local NVM_DELETE_PREFIX
NVM_DELETE_PREFIX=0
shift # remove "use" shift # remove "use"
while [ $# -ne 0 ] while [ $# -ne 0 ]
do do
case "$1" in case "$1" in
--silent) NVM_USE_SILENT=1 ;; --silent) NVM_USE_SILENT=1 ;;
--delete-prefix) NVM_DELETE_PREFIX=1 ;;
*) *)
if [ -n "$1" ]; then if [ -n "$1" ]; then
PROVIDED_VERSION="$1" PROVIDED_VERSION="$1"
@ -1690,6 +1702,19 @@ nvm() {
echo "Now using node $VERSION$(nvm_print_npm_version)" echo "Now using node $VERSION$(nvm_print_npm_version)"
fi fi
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" ) "run" )
local provided_version local provided_version

View File

@ -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 [ `expr $PATH : ".*v0.2.3/.*/bin"` = 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2
. ../../nvm.sh . ../../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 "$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)" [ `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 # ^ note: NODE_PATH should not contain `npm root -g` since globals should not be requireable

View File

@ -43,6 +43,13 @@ npm() {
OUTPUT="$(nvm_die_on_prefix 0 foo 2>&1)" 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'" [ -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() { npm() {
local args local args
args="$@" args="$@"
@ -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." Run \`npm config delete prefix\` or \`foo\` to unset it."
EXIT_CODE="$(nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)" 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'" [ "_$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 cleanup

View File

@ -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'"