Skip to content

Commit

Permalink
[New] nvm_resolve_alias: only resolve one line of a multiline alias…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
ljharb committed Oct 23, 2023
1 parent 10cdda0 commit 6743aef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion test/fast/Aliases/nvm_resolve_alias
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}"
1 change: 1 addition & 0 deletions test/fast/Aliases/teardown
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 6743aef

Please sign in to comment.