[Fix] `nvm_alias`: unset zsh's `extendedglob` while reading an alias file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
`#` 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
authored by debaditya · committed by Jordan Harband · parent 25b3783 · Browse files · 3 files·+105 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: debaditya <debaditya2005hait@gmail.com>
- Co-authored-by: Jordan Harband <ljharb@gmail.com>
Diff stats
3 files+105 added−11 removed
- tests:+86 added−0 removed
- other:+19 added−11 removed
Provenance
- Commit
80e700e- Tree
cd3e27c- Author
debaditya <debaditya2005hait@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
25b3783
3 files changed+105 added−11 removed
added:test/fast/Unit tests/nvm_print_alias_file+53 added
1+#!/bin/sh2+3+die () { echo "$@" ; cleanup ; exit 1; }4+5+cleanup () {6+ rm -rf "${TEST_DIR-}"7+ unset TEST_DIR OUTPUT EXPECTED8+}9+10+: nvm.sh11+\. ../../../nvm.sh12+13+TEST_DIR="${PWD}/nvm_print_alias_file_tmp"14+mkdir -p "${TEST_DIR}" || die 'failed to create test dir'15+16+printf 'v0.10 # inline comment\n' > "${TEST_DIR}/inline"17+OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/inline")"18+[ "_${OUTPUT}" = '_v0.10' ] || die "inline comment not stripped; got '${OUTPUT}'"19+20+printf '# leading comment\nv0.11\n' > "${TEST_DIR}/comment-first"21+OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/comment-first")"22+[ "_${OUTPUT}" = '_v0.11' ] || die "comment-only line not skipped; got '${OUTPUT}'"23+24+printf 'v0.12\t \nv0.13\n' > "${TEST_DIR}/trailing-space"25+OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/trailing-space")"26+EXPECTED="$(printf 'v0.12\nv0.13')"27+[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "trailing whitespace not stripped, or a line was dropped; got '${OUTPUT}'"28+29+printf '\n \n\t\n# only a comment\n' > "${TEST_DIR}/nothing-usable"30+OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/nothing-usable")"31+[ "_${OUTPUT}" = '_' ] || die "blank and comment-only lines should produce no output; got '${OUTPUT}'"32+33+nvm_print_alias_file "${TEST_DIR}/nothing-usable" >/dev/null 2>&1 \34+ || die 'a file with no usable lines should still exit zero'35+36+printf 'v0.14' > "${TEST_DIR}/no-trailing-newline"37+OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/no-trailing-newline")"38+[ "_${OUTPUT}" = '_v0.14' ] || die "a final line with no trailing newline should be read; got '${OUTPUT}'"39+40+# a `#` pattern is a repetition operator under zsh's `extendedglob`, which is what41+# made `nvm_alias` print nothing at all: https://github.com/nvm-sh/nvm/issues/388542+if [ -n "${ZSH_VERSION-}" ]; then43+ setopt extendedglob || die 'enabling extendedglob failed'44+ OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/inline")"45+ [ "_${OUTPUT}" = '_v0.10' ] || die "with extendedglob set, expected 'v0.10', got '${OUTPUT}'"46+ [[ -o extendedglob ]] || die 'nvm_print_alias_file left extendedglob unset'47+48+ unsetopt extendedglob49+ nvm_print_alias_file "${TEST_DIR}/inline" > /dev/null50+ [[ -o extendedglob ]] && die 'nvm_print_alias_file left extendedglob set'51+fi52+53+cleanupadded:test/sourcing/Sourcing nvm.sh with zsh extendedglob should use the default+33 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+\. ../common.sh6+7+# `#` is a pattern operator only under zsh's `extendedglob`8+[ -n "${ZSH_VERSION-}" ] || exit 09+10+# We need to unload nvm again first, as by the time this test is run,11+# despite being unloaded in setup, the inherited PATH still contains12+# an nvm-installed node version. `nvm unload` unsets NVM_DIR, so cache it.13+\. ../../nvm.sh || die 'sourcing returned nonzero exit code'14+NVM_DIR_CACHED="${NVM_DIR}"15+nvm unload || die 'unloading returned nonzero exit code'16+NVM_DIR="${NVM_DIR_CACHED}"17+18+# the comment is the point: stripping it is what needs a `#` pattern19+echo '0.10.1 # comment' > "${NVM_DIR}/alias/default" || die 'creation of default alias failed'20+21+setopt extendedglob || die 'enabling extendedglob failed'22+23+\. ../../nvm.sh || die 'sourcing returned nonzero exit code'24+25+[[ -o extendedglob ]] || die 'sourcing nvm.sh disabled extendedglob'26+27+NVM_LS_CURRENT_NOT_GREPPED="$(nvm ls current | strip_colors)"28+NVM_LS_CURRENT="$(nvm ls current | strip_colors | \grep -o 'v0\.10\.1')"29+[ "_${NVM_LS_CURRENT}" = '_v0.10.1' ] || die "'nvm ls current' did not return '-> v0.10.1', got '${NVM_LS_CURRENT_NOT_GREPPED}'"30+31+NVM_ALIAS_DEFAULT="$(nvm alias default | strip_colors)"32+[ "_${NVM_ALIAS_DEFAULT}" = '_default -> 0.10.1 (-> v0.10.1 *)' ] \33+ || die "'nvm alias default' did not return 'default -> 0.10.1 (-> v0.10.1 *)', got '${NVM_ALIAS_DEFAULT}'"modified:nvm.sh+19 added-11 removed
1425 unmodified lines1426 ) | command sort1427 return1428 }1429 1430+nvm_print_alias_file() {1431+ # a `#` pattern is a repetition operator under zsh's `extendedglob`, so it is1432+ # unset for the length of this function and restored by `local_options`1433+ nvm_is_zsh && setopt local_options noextendedglob1434+1435+ local NVM_ALIAS_LINE1436+ while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do1437+ NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}"1438+ case "${NVM_ALIAS_LINE}" in1439+ *[![:space:]]*) ;;1440+ *) continue ;;1441+ esac1442+ NVM_ALIAS_LINE="${NVM_ALIAS_LINE%"${NVM_ALIAS_LINE##*[![:space:]]}"}"1443+ nvm_echo "${NVM_ALIAS_LINE}"1444+ done < "$1"1445+}1446+1447 nvm_alias() {1448 local ALIAS1449 ALIAS="${1-}"1450 if [ -z "${ALIAS}" ]; then22 unmodified lines1473 nvm_err "Alias file is not readable: ${NVM_ALIAS_PATH}"1474 return 01475 fi1476 1460- local NVM_ALIAS_LINE1461- while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do1462- NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}"1463- case "${NVM_ALIAS_LINE}" in1464- *[![:space:]]*) ;;1465- *) continue ;;1466- esac1467- NVM_ALIAS_LINE="${NVM_ALIAS_LINE%"${NVM_ALIAS_LINE##*[![:space:]]}"}"1468- nvm_echo "${NVM_ALIAS_LINE}"1469- done < "${NVM_ALIAS_PATH}"1477+ nvm_print_alias_file "${NVM_ALIAS_PATH}"1478 }1479 1480 nvm_ls_current() {1481 local NVM_LS_CURRENT_NODE_PATH3486 unmodified lines4968 nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \4969 nvm_has_solaris_binary nvm_is_merged_node_version \4970 nvm_is_natural_num nvm_is_version_installed nvm_validate_install \4971 nvm_install_lock_name nvm_acquire_install_lock nvm_release_install_lock \4964- nvm_list_aliases nvm_make_alias nvm_print_alias_path \4972+ nvm_list_aliases nvm_make_alias nvm_print_alias_file nvm_print_alias_path \4973 nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \4974 nvm_sanitize_path nvm_has_colors nvm_process_parameters \4975 nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \4976 nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \181 unmodified lines