Skip to content

Commit

Permalink
Show "∞" when there's a circular alias reference.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 23, 2014
1 parent 0f5997f commit 9b91734
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
31 changes: 29 additions & 2 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions test/fast/Aliases/circular/nvm_resolve_alias
Original file line number Diff line number Diff line change
@@ -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"

10 changes: 10 additions & 0 deletions test/fast/Aliases/circular/setup
Original file line number Diff line number Diff line change
@@ -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

10 changes: 10 additions & 0 deletions test/fast/Aliases/circular/teardown
Original file line number Diff line number Diff line change
@@ -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

File renamed without changes.
File renamed without changes.

2 comments on commit 9b91734

@koenpunt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to rant or anything, but this much code for only displaying a infinity symbol seems excessive..

@ljharb
Copy link
Member Author

@ljharb ljharb commented on 9b91734 Nov 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about the infinity symbol, it's about preventing a circular loop. In order to do it, I needed a semaphore/signal value, and seemed logical enough.

Please sign in to comment.