From 6743aef70cc1911970e24a83223234af3655c5c7 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 23 Oct 2023 14:41:14 -0700 Subject: [PATCH] [New] `nvm_resolve_alias`: only resolve one line of a multiline alias file --- nvm.sh | 4 +++- test/fast/Aliases/nvm_resolve_alias | 18 +++++++++++++++++- test/fast/Aliases/teardown | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index bee8456f81..800e61d3c2 100644 --- a/nvm.sh +++ b/nvm.sh @@ -1177,8 +1177,10 @@ nvm_resolve_alias() { local SEEN_ALIASES SEEN_ALIASES="${ALIAS}" + local NVM_ALIAS_INDEX + NVM_ALIAS_INDEX=1 while true; do - ALIAS_TEMP="$(nvm_alias "${ALIAS}" 2>/dev/null || nvm_echo)" + ALIAS_TEMP="$( (nvm_alias "${ALIAS}" 2>/dev/null | command head -n "${NVM_ALIAS_INDEX}" | command tail -n 1) || nvm_echo)" if [ -z "${ALIAS_TEMP}" ]; then break diff --git a/test/fast/Aliases/nvm_resolve_alias b/test/fast/Aliases/nvm_resolve_alias index 4520aa14c1..307c3cbd94 100755 --- a/test/fast/Aliases/nvm_resolve_alias +++ b/test/fast/Aliases/nvm_resolve_alias @@ -2,7 +2,9 @@ die () { echo "$@" ; exit 1; } -\. ../../../nvm.sh +export NVM_DIR="$(cd ../../.. && pwd)" + +\. "${NVM_DIR}/nvm.sh" EXIT_CODE=$(nvm_resolve_alias ; echo $?) [ $EXIT_CODE = "1" ] || die "nvm_resolve_alias without an argument did not return 1; got $EXIT_CODE" @@ -31,3 +33,17 @@ UNSTABLE="$(nvm_resolve_alias unstable)" IOJS="$(nvm_resolve_alias iojs)" [ "_$IOJS" = "_iojs-v0.2" ] || die "'nvm_resolve_alias iojs' was not iojs-v0.2; got $IOJS" + +echo " + +v0.0.1 +v0.0.2 +v0.0.3 +" > ../../../alias/test-multi-lines + +EXPECTED='v0.0.1' +ACTUAL="$(nvm_resolve_alias test-multi-lines)" +EXIT_CODE="$(nvm_resolve_alias test-multi-lines 2>&1 >/dev/null; echo $?)" + +[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<" +[ "${EXIT_CODE}" = '0' ] || die "expected exit code 0, got ${EXIT_CODE}" diff --git a/test/fast/Aliases/teardown b/test/fast/Aliases/teardown index 33e1b02470..08724ade0a 100755 --- a/test/fast/Aliases/teardown +++ b/test/fast/Aliases/teardown @@ -16,3 +16,4 @@ rm -f "../../../alias/node" rm -f "../../../alias/iojs" rm -f "../../../alias/default" rm -f "../../../alias/test-blank-lines" +rm -f "../../../alias/test-multi-lines"