Skip to content

Commit

Permalink
[Fix] allow nvm unalias x when x is a default alias, but shadowed
Browse files Browse the repository at this point in the history
Fixes #2122.
  • Loading branch information
ljharb committed Nov 12, 2019
1 parent 04ad1b5 commit 73a513c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
24 changes: 17 additions & 7 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3372,14 +3372,24 @@ nvm() {
local NVM_NODE_PREFIX
NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"
NVM_NODE_PREFIX="$(nvm_node_prefix)"
case "$1" in
"stable" | "unstable" | "${NVM_IOJS_PREFIX}" | "${NVM_NODE_PREFIX}" | "system")
nvm_err "${1-} is a default (built-in) alias and cannot be deleted."
return 1
;;
esac
local NVM_ALIAS_EXISTS
NVM_ALIAS_EXISTS=0
if [ -f "${NVM_ALIAS_DIR}/${1-}" ]; then
NVM_ALIAS_EXISTS=1
fi

if [ $NVM_ALIAS_EXISTS -eq 0 ]; then
case "$1" in
"stable" | "unstable" | "${NVM_IOJS_PREFIX}" | "${NVM_NODE_PREFIX}" | "system")
nvm_err "${1-} is a default (built-in) alias and cannot be deleted."
return 1
;;
esac

nvm_err "Alias ${1-} doesn't exist!"
return
fi

[ ! -f "${NVM_ALIAS_DIR}/${1-}" ] && nvm_err "Alias ${1-} doesn't exist!" && return
local NVM_ALIAS_ORIGINAL
NVM_ALIAS_ORIGINAL="$(nvm_alias "${1}")"
command rm -f "${NVM_ALIAS_DIR}/${1}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

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

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

OUTPUT="$(nvm unalias node 2>&1)"
EXPECTED_OUTPUT="node is a default (built-in) alias and cannot be deleted."
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove a built-in alias should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'"

nvm alias node stable || die '`nvm alias node stable` failed'

nvm unalias node || die '`nvm unalias node` failed'

OUTPUT="$(nvm unalias node 2>&1)"
EXPECTED_OUTPUT="node is a default (built-in) alias and cannot be deleted."
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove a built-in alias should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'"

0 comments on commit 73a513c

Please sign in to comment.