1
0
mirror of https://github.com/nvm-sh/nvm.git synced 2026-10-23 23:03:46 +08:00
Files
nvm/test/sourcing/Sourcing nvm.sh with zsh extendedglob should use the default
debaditya 80e700ec21 [Fix] nvm_alias: unset zsh's extendedglob while reading an alias file
`#` is a repetition operator under `extendedglob`,
so `${VAR%%#*}` is an invalid pattern:
zsh aborts the expansion and `nvm_alias` silently prints nothing,
leaving the `default` alias unresolved when nvm.sh is sourced.

The read loop moves into `nvm_print_alias_file` so that `local_options`
scopes the option change to that section alone,
rather than to everything `nvm_alias` does,
and restores whatever the caller had set.

Fixes #3885

Co-authored-by: debaditya <debaditya2005hait@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-09-03 20:54:28 -07:00

34 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
die () { echo "$@" ; exit 1; }
\. ../common.sh
# `#` is a pattern operator only under zsh's `extendedglob`
[ -n "${ZSH_VERSION-}" ] || exit 0
# We need to unload nvm again first, as by the time this test is run,
# despite being unloaded in setup, the inherited PATH still contains
# an nvm-installed node version. `nvm unload` unsets NVM_DIR, so cache it.
\. ../../nvm.sh || die 'sourcing returned nonzero exit code'
NVM_DIR_CACHED="${NVM_DIR}"
nvm unload || die 'unloading returned nonzero exit code'
NVM_DIR="${NVM_DIR_CACHED}"
# the comment is the point: stripping it is what needs a `#` pattern
echo '0.10.1 # comment' > "${NVM_DIR}/alias/default" || die 'creation of default alias failed'
setopt extendedglob || die 'enabling extendedglob failed'
\. ../../nvm.sh || die 'sourcing returned nonzero exit code'
[[ -o extendedglob ]] || die 'sourcing nvm.sh disabled extendedglob'
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_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}'"