Skip to content

Commit

Permalink
[Fix] nvm_auto: Fix bug introduced in c31a867
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 21, 2024
1 parent c31a867 commit df70535
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
25 changes: 14 additions & 11 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4559,33 +4559,36 @@ nvm_auto() {

case "${NVM_MODE}" in
none) return 0 ;;
use | install)
use)
local VERSION
local NVM_CURRENT
NVM_CURRENT="$(nvm_ls_current)"
if [ "_${NVM_CURRENT}" = '_none' ] || [ "_${NVM_CURRENT}" = '_system' ]; then
VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"
if [ -n "${VERSION}" ]; then
if [ "_${VERSION}" != '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
if [ "_${NVM_MODE}" = '_install' ]; then
nvm install "${VERSION}" >/dev/null
else
nvm use --silent "${VERSION}" >/dev/null
fi
nvm use --silent "${VERSION}" >/dev/null
else
return 0
fi
elif nvm_rc_version >/dev/null 2>&1; then
if [ "_${NVM_MODE}" = '_install' ]; then
nvm install >/dev/null
else
nvm use --silent >/dev/null
fi
nvm use --silent >/dev/null
fi
else
nvm use --silent "${NVM_CURRENT}" >/dev/null
fi
;;
install)
local VERSION
VERSION="$(nvm_alias default 2>/dev/null || nvm_echo)"
if [ -n "${VERSION}" ] && [ "_${VERSION}" != '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
nvm install "${VERSION}" >/dev/null
elif nvm_rc_version >/dev/null 2>&1; then
nvm install >/dev/null
else
return 0
fi
;;
*)
nvm_err 'Invalid auto mode supplied.'
return 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ echo '0.10.1' > ../../alias/default || die "couldn't create default alias"
NVM_TARGET=0.10.3
NVM_DEFAULT="$(nvm_resolve_local_alias default)"

[ "_$NVM_DEFAULT" != "_$NVM_TARGET" ] || die "default $NVM_DEFAULT is the same as target $NVM_TARGET"
[ "_${NVM_DEFAULT}" != "_${NVM_TARGET}" ] || die "default ${NVM_DEFAULT} is the same as target ${NVM_TARGET}"

nvm use $NVM_TARGET || die "nvm use $NVM_TARGET failed"
nvm use "${NVM_TARGET}" || die "nvm use ${NVM_TARGET} failed"

\. ../../nvm.sh || die 'sourcing returned nonzero exit code'

NVM_CURRENT="$(nvm current)"

[ "_${NVM_CURRENT#v}" = "_$NVM_TARGET" ] || die "node version not retained after sourcing"
[ "_${NVM_CURRENT#v}" = "_${NVM_TARGET}" ] || die "node version not retained after sourcing"
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ die () { echo "$@" ; exit 1; }
# despite being unloaded in setup, the inherited PATH still contains
# an nvm-installed node version. We have to reset NVM_DIR after the unload.
\. ../../nvm.sh || die 'sourcing returned nonzero exit code'
NVM_DIR_CACHED="$NVM_DIR"
NVM_DIR_CACHED="${NVM_DIR}"
nvm unload || die 'unloading returned nonzero exit code'
NVM_DIR="$NVM_DIR_CACHED"
NVM_DIR="${NVM_DIR_CACHED}"

echo '0.10.1' > ../../alias/default || die 'creation of default alias failed'

which node && die 'node was found in PATH after unloading nvm'

# Now to begin the real test
\. ../../nvm.sh || die 'sourcing returned nonzero exit code'

NVM_LS_CURRENT_NOT_GREPPED="$(nvm ls current | strip_colors)"
NVM_LS_CURRENT="$(nvm ls current | strip_colors | \grep -o v0.10.1)"
[ "_$NVM_LS_CURRENT" = '_v0.10.1' ] || die "'nvm ls current' did not return '-> v0.10.1', got '$NVM_LS_CURRENT_NOT_GREPPED'"
[ "_${NVM_LS_CURRENT}" = '_v0.10.1' ] || die "'nvm ls current' did not return '-> v0.10.1', got '${NVM_LS_CURRENT_NOT_GREPPED}'"

# NVM_LS_CURRENT_COLORED="$(nvm ls current | sed -n l)"

NVM_ALIAS_DEFAULT="$(nvm alias default | strip_colors)"
[ "_$NVM_ALIAS_DEFAULT" = "_default -> 0.10.1 (-> v0.10.1)" ] \
|| die "'nvm alias default did not return 'default -> 0.10.1 (-> v0.10.1)', got '$NVM_ALIAS_DEFAULT'"
[ "_${NVM_ALIAS_DEFAULT}" = "_default -> 0.10.1 (-> v0.10.1)" ] \
|| die "'nvm alias default did not return 'default -> 0.10.1 (-> v0.10.1)', got '${NVM_ALIAS_DEFAULT}'"
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ fi
echo '0.10.2' > ../../.nvmrc || die 'creation of .nvmrc failed'

\. ../../nvm.sh --install
EXIT_CODE="$(echo $?)"
EXIT_CODE="$?"

echo 'sourcing complete.'

nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: $(nvm ls)"
nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: \n$(nvm_ls)"

[ "_$(nvm_rc_version | \grep -o -e 'with version .*$')" = "_with version <0.10.2>" ] || die "nvm_rc_version $(nvm_rc_version)"

[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
[ "_${EXIT_CODE}" = '_0' ] || die "sourcing returned nonzero exit code: ${EXIT_CODE}"

NVM_LS_CURRENT="$(nvm ls current | \grep -o v0.10.2)"
[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', got '$NVM_LS_CURRENT' `nvm ls`"
[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', got >${NVM_LS_CURRENT}<\n$(nvm_ls)"
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ echo '0.10.2' > ../../alias/default || die 'creation of default alias failed'
echo 'sourcing nvm with --install...'

\. ../../nvm.sh --install
EXIT_CODE="$(echo $?)"
EXIT_CODE="$?"

echo 'sourcing complete.'

nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: $(nvm ls)"
nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: \n$(nvm_ls)"

[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
[ "_$EXIT_CODE" = '_0' ] || die "sourcing returned nonzero exit code: ${EXIT_CODE}"

NVM_LS_CURRENT="$(nvm ls current | strip_colors | command grep -o v0.10.2)"
[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', got '$NVM_LS_CURRENT'"
[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', >${NVM_LS_CURRENT}<\n$(nvm_ls)"

NVM_ALIAS_DEFAULT="$(nvm alias default | strip_colors)"
[ "_$NVM_ALIAS_DEFAULT" = "_default -> 0.10.2 (-> v0.10.2)" ] \
|| die "'nvm alias default did not return 'default -> 0.10.2 (-> v0.10.2)', got '$NVM_ALIAS_DEFAULT'"
|| die "'nvm alias default did not return 'default -> 0.10.2 (-> v0.10.2)', got >${NVM_ALIAS_DEFAULT}<\n$(nvm_ls)"

0 comments on commit df70535

Please sign in to comment.