Skip to content
Myr

[Refactor] `nvm_alias`, `nvm_resolve_alias`: use builtins

Before adoption: this commit came in with the history the project adopted, so no landing records it.

[Tests] `nvm_alias`, `nvm_resolve_alias`: add edge-case tests nvm_alias() used a sed/awk pipeline to strip comments and blank lines from alias files that almost always contain a single word. A while-read loop with parameter expansion does the same filtering more directly. nvm_resolve_alias() piped nvm_alias through head and tail to extract one line, and used printf/grep for cycle detection. Parameter expansion and a case statement replace both without the extra plumbing. All replacements are POSIX (read -r, case, IFS=, parameter expansion). As a side effect, this also removes 4 external process invocations during shell init. [Fix] `nvm_resolve_alias`: detect cycles via newline-anchored `case` The original commit referenced above changed SEEN_ALIASES from `\n`-delimited storage (interpreted by `printf '%b' | nvm_grep -e "^${name}$"`) to space- delimited but left the line-anchored grep in place — without newlines in the haystack the anchored pattern can never match, so cycles never break. Switch to literal-newline storage and a `case` pattern anchored on those newlines. Newline anchoring also handles alias names containing spaces, which token-based patterns false-positive on (e.g. lookup of `bar` matches substring " bar " inside " foo bar midway " when the chain visits the multi-token alias `foo bar`). New test file covers self-loop, multi-hop loop, cycle through a space-bearing alias name, and a non-cycle through a space-bearing alias name. Existing `test/fast/Aliases/circular/` fixtures continue to pass.

authored by Jake Lodwick · committed by Jordan Harband · parent dd61ae4 · Browse files · 7 files+174 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+174 added−10 removed

  • tests:+141 added−0 removed
  • other:+33 added−10 removed

7 files+174 added−10 removed

added:test/fast/Aliases/nvm_alias handles comment-only alias file+17 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file containing only comments11printf '# this is a comment\n# another comment\n' > ../../../alias/test-edge-comments12ACTUAL="$(nvm_alias test-edge-comments 2>/dev/null)"13EXIT_CODE="$(nvm_alias test-edge-comments 2>/dev/null; echo $?)"14[ -z "${ACTUAL}" ] || die "expected empty output for comment-only alias file, got >${ACTUAL}<"15[ "${EXIT_CODE}" = '0' ] || die "expected exit code 0, got ${EXIT_CODE}"1617rm -f ../../../alias/test-edge-comments
added:test/fast/Aliases/nvm_alias handles empty alias file+17 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an empty alias file11printf '' > ../../../alias/test-edge-empty12ACTUAL="$(nvm_alias test-edge-empty 2>/dev/null)"13EXIT_CODE="$(nvm_alias test-edge-empty 2>/dev/null; echo $?)"14[ -z "${ACTUAL}" ] || die "expected empty output for empty alias file, got >${ACTUAL}<"15[ "${EXIT_CODE}" = '0' ] || die "expected exit code 0, got ${EXIT_CODE}"1617rm -f ../../../alias/test-edge-empty
added:test/fast/Aliases/nvm_alias strips trailing whitespace+16 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file with trailing spaces and tabs11printf '22.1.0 \t \n' > ../../../alias/test-edge-trailing-ws12ACTUAL="$(nvm_alias test-edge-trailing-ws)"13EXPECTED='22.1.0'14[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"1516rm -f ../../../alias/test-edge-trailing-ws
added:test/fast/Aliases/nvm_resolve_alias handles cycles+52 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# 1-hop self-reference: alias points to itself11echo 'self' > '../../../alias/self'1213ACTUAL="$(nvm_resolve_alias self)"14EXPECTED='∞'15[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for self-reference, got >${ACTUAL}<"1617rm -f '../../../alias/self'1819# Multi-hop loop: link1 -> link2 -> link3 -> link120echo 'link2' > '../../../alias/link1'21echo 'link3' > '../../../alias/link2'22echo 'link1' > '../../../alias/link3'2324ACTUAL="$(nvm_resolve_alias link1)"25EXPECTED='∞'26[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for 3-hop cycle, got >${ACTUAL}<"2728rm -f '../../../alias/link1' '../../../alias/link2' '../../../alias/link3'2930# Cycle through an alias name containing a space31echo 'midway' > '../../../alias/foo bar'32echo 'foo bar' > '../../../alias/midway'3334ACTUAL="$(nvm_resolve_alias 'foo bar')"35EXPECTED='∞'36[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for space-name cycle, got >${ACTUAL}<"3738rm -f '../../../alias/foo bar' '../../../alias/midway'3940# Non-cycle through an alias name containing a space.41# Token-based cycle detection would false-positive on this chain because42# 'bar' appears as a substring of the multi-token alias name 'foo bar'.43# Resolves: 'foo bar' -> 'midway' -> 'bar' -> 0.0.9944echo 'midway' > '../../../alias/foo bar'45echo 'bar' > '../../../alias/midway'46echo '0.0.99' > '../../../alias/bar'4748ACTUAL="$(nvm_resolve_alias 'foo bar')"49EXPECTED='v0.0.99'50[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for space-name chain, got >${ACTUAL}<"5152rm -f '../../../alias/foo bar' '../../../alias/midway' '../../../alias/bar'
added:test/fast/Aliases/nvm_resolve_alias handles deep chain+20 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create a 4-deep alias chain: hop1 -> hop2 -> hop3 -> hop4 -> 0.0.9911echo 'hop2' > ../../../alias/hop112echo 'hop3' > ../../../alias/hop213echo 'hop4' > ../../../alias/hop314echo '0.0.99' > ../../../alias/hop41516ACTUAL="$(nvm_resolve_alias hop1)"17EXPECTED='v0.0.99'18[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for 4-deep chain, got >${ACTUAL}<"1920rm -f ../../../alias/hop1 ../../../alias/hop2 ../../../alias/hop3 ../../../alias/hop4
added:test/fast/Aliases/nvm_resolve_alias with nonexistent target+19 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias pointing to a version that does not exist as an alias11echo '99.99.99' > ../../../alias/test-edge-noexist1213ACTUAL="$(nvm_resolve_alias test-edge-noexist)"14EXPECTED='v99.99.99'15EXIT_CODE="$(nvm_resolve_alias test-edge-noexist >/dev/null 2>&1; echo $?)"16[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"17[ "${EXIT_CODE}" = '0' ] || die "expected exit code 0, got ${EXIT_CODE}"1819rm -f ../../../alias/test-edge-noexist
modified:nvm.sh+33 added-10 removed
1368 unmodified lines
1369 nvm_err "Alias file is not readable: ${NVM_ALIAS_PATH}"1370 return 01371 fi13721373 command sed 's/#.*//; s/[[:space:]]*$//' "${NVM_ALIAS_PATH}" | command awk 'NF'1373 local NVM_ALIAS_LINE1374 while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do1375 NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}"1376 case "${NVM_ALIAS_LINE}" in1377 *[!\ \ ]*) ;;1378 *) continue ;;1379 esac1380 while : ; do1381 case "${NVM_ALIAS_LINE}" in1382 *' ') NVM_ALIAS_LINE="${NVM_ALIAS_LINE% }" ;;1383 *' ') NVM_ALIAS_LINE="${NVM_ALIAS_LINE% }" ;;1384 *) break ;;1385 esac1386 done1387 nvm_echo "${NVM_ALIAS_LINE}"1388 done < "${NVM_ALIAS_PATH}"1389}13901391nvm_ls_current() {1392 local NVM_LS_CURRENT_NODE_PATH
24 unmodified lines
14171418 local ALIAS1419 ALIAS="${PATTERN}"1420 local ALIAS_TEMP1421 local ALIAS_OUTPUT14221423 local SEEN_ALIASES1408 SEEN_ALIASES="${ALIAS}"1409 local NVM_ALIAS_INDEX1410 NVM_ALIAS_INDEX=11424 SEEN_ALIASES="1425${ALIAS}1426"1427 while true; do1412 ALIAS_TEMP="$( (nvm_alias "${ALIAS}" 2>/dev/null | command head -n "${NVM_ALIAS_INDEX}" | command tail -n 1) || nvm_echo)"1428 ALIAS_OUTPUT="$(nvm_alias "${ALIAS}" 2>/dev/null)" || ALIAS_OUTPUT=''1429 ALIAS_TEMP="${ALIAS_OUTPUT%%1430*}"14311432 if [ -z "${ALIAS_TEMP}" ]; then1433 break1434 fi14351418 if command printf '%b' "${SEEN_ALIASES}" | nvm_grep -q -e "^${ALIAS_TEMP}$"; then1419 ALIAS="∞"1420 break1421 fi1436 case "${SEEN_ALIASES}" in1437 *"1438${ALIAS_TEMP}1439"*)1440 ALIAS="∞"1441 break1442 ;;1443 esac14441423 SEEN_ALIASES="${SEEN_ALIASES}\\n${ALIAS_TEMP}"1445 SEEN_ALIASES="${SEEN_ALIASES}${ALIAS_TEMP}1446"1447 ALIAS="${ALIAS_TEMP}"1448 done14491450 if [ -n "${ALIAS}" ] && [ "_${ALIAS}" != "_${PATTERN}" ]; then
3494 unmodified lines