Show "∞" when there's a circular alias reference.

master
Jordan Harband 2014-10-22 01:48:02 -07:00
parent 0f5997f754
commit 9b91734f0b
6 changed files with 85 additions and 2 deletions

31
nvm.sh
View File

@ -278,8 +278,35 @@ nvm_resolve_alias() {
local PATTERN
PATTERN="$1"
if [ -f "$NVM_DIR/alias/$PATTERN" ]; then
nvm_version "$(nvm_alias "$PATTERN" 2> /dev/null)"
local ALIAS
ALIAS="$PATTERN"
local ALIAS_TEMP
local SEEN_ALIASES
SEEN_ALIASES="$ALIAS"
while true; do
ALIAS_TEMP="$(nvm_alias "$ALIAS" 2> /dev/null)"
if [ -z "$ALIAS_TEMP" ]; then
break
fi
if [ -n "$ALIAS_TEMP" ] \
&& printf "$SEEN_ALIASES" | \grep -e "^$ALIAS_TEMP$" > /dev/null; then
ALIAS="∞"
break
fi
SEEN_ALIASES="$SEEN_ALIASES\n$ALIAS_TEMP"
ALIAS="$ALIAS_TEMP"
done
if [ -n "$ALIAS" ] && [ "_$ALIAS" != "_$PATTERN" ]; then
if [ "_$ALIAS" = "_∞" ]; then
echo "$ALIAS"
else
nvm_version "$ALIAS"
fi
return 0
fi

View File

@ -0,0 +1,36 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../../nvm.sh
ALIAS="$(nvm_resolve_alias loopback)"
[ "_$ALIAS" = "_∞" ] || die "nvm_resolve_alias loopback was not ∞; got $ALIAS"
OUTPUT="$(nvm alias loopback)"
EXPECTED_OUTPUT="loopback -> loopback (-> ∞)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm alias loopback was not $EXPECTED_OUTPUT; got $OUTPUT"
ALIAS="$(nvm_resolve_alias one)"
[ "_$ALIAS" = "_∞" ] || die "nvm_resolve_alias one was not ∞; got $ALIAS"
OUTPUT="$(nvm alias one)"
EXPECTED_OUTPUT="one -> two (-> ∞)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm alias one was not $EXPECTED_OUTPUT; got $OUTPUT"
ALIAS="$(nvm_resolve_alias two)"
[ "_$ALIAS" = "_∞" ] || die "nvm_resolve_alias two was not ∞; got $ALIAS"
OUTPUT="$(nvm alias two)"
EXPECTED_OUTPUT="two -> three (-> ∞)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm alias two was not $EXPECTED_OUTPUT; got $OUTPUT"
ALIAS="$(nvm_resolve_alias three)"
[ "_$ALIAS" = "_∞" ] || die "nvm_resolve_alias three was not ∞; got $ALIAS"
OUTPUT="$(nvm alias three)"
EXPECTED_OUTPUT="three -> one (-> ∞)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm alias three was not $EXPECTED_OUTPUT; got $OUTPUT"
ALIAS="$(nvm_resolve_alias four)"
[ "_$ALIAS" = "_∞" ] || die "nvm_resolve_alias four was not ∞; got $ALIAS"
OUTPUT="$(nvm alias four)"
EXPECTED_OUTPUT="four -> two (-> ∞)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm alias four was not $EXPECTED_OUTPUT; got $OUTPUT"

View File

@ -0,0 +1,10 @@
#!/bin/sh
echo loopback > ../../../../alias/loopback
echo two > ../../../../alias/one
echo three > ../../../../alias/two
echo one > ../../../../alias/three
echo two > ../../../../alias/four

View File

@ -0,0 +1,10 @@
#!/bin/sh
rm -f ../../../../alias/loopback
rm -f ../../../../alias/one
rm -f ../../../../alias/two
rm -f ../../../../alias/three
rm -f ../../../../alias/four