diff --git a/nvm.sh b/nvm.sh index 4553a85..69de1be 100644 --- a/nvm.sh +++ b/nvm.sh @@ -274,6 +274,11 @@ nvm_ls() { fi else if [ "_$PATTERN" != "_system" ]; then + if nvm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then + nvm_ls "$(nvm_print_implicit_alias local "$PATTERN" 2> /dev/null)" + return $? + fi + local NUM_VERSION_GROUPS NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")" if [ "_$NUM_VERSION_GROUPS" = "_2" ] || [ "_$NUM_VERSION_GROUPS" = "_1" ]; then @@ -312,7 +317,9 @@ nvm_ls_remote() { local VERSIONS local GREP_OPTIONS GREP_OPTIONS='' - if [ -n "$PATTERN" ]; then + if nvm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then + PATTERN="$(nvm_remote_version "$(nvm_print_implicit_alias remote "$PATTERN")")" + elif [ -n "$PATTERN" ]; then PATTERN="$(nvm_ensure_version_prefix "$PATTERN")" else PATTERN=".*" @@ -368,6 +375,48 @@ nvm_print_versions() { done } +nvm_validate_implicit_alias() { + if [ "_$1" != "_stable" ] && [ "_$1" != "_unstable" ]; then + echo "Only implicit aliases 'stable' and 'unstable' are supported." >&2 + return 1 + fi +} + +nvm_print_implicit_alias() { + if [ "_$1" != "_local" ] && [ "_$1" != "_remote" ]; then + echo "nvm_print_implicit_alias must be specified with local or remote as the first argument." >&2 + return 1 + fi + + if ! nvm_validate_implicit_alias "$2"; then + return 2 + fi + + local LAST_TWO + if [ "_$1" = "_local" ]; then + LAST_TWO="$(nvm_ls | \grep -e '^v' | cut -c2- | cut -d . -f 1,2 | uniq)" + else + LAST_TWO="$(nvm_ls_remote | \grep -e '^v' | cut -c2- | cut -d . -f 1,2 | uniq)" + fi + local MINOR + local STABLE + local UNSTABLE + local MOD + for MINOR in $LAST_TWO; do + MOD=$(expr "$(nvm_normalize_version "$MINOR")" \/ 1000000 \% 2) + if [ $MOD -eq 0 ]; then + STABLE="$MINOR" + elif [ $MOD -eq 1 ]; then + UNSTABLE="$MINOR" + fi + done + if [ "_$2" = "_stable" ]; then + echo $STABLE + elif [ "_$2" = "_unstable" ]; then + echo $UNSTABLE + fi +} + nvm() { if [ $# -lt 1 ]; then nvm help @@ -471,7 +520,7 @@ nvm() { nobinary=1 fi - provided_version=$1 + provided_version="$1" if [ -z "$provided_version" ]; then if [ $version_not_provided -ne 1 ]; then @@ -834,6 +883,16 @@ nvm() { fi fi done + + for ALIAS in "stable" "unstable"; do + if [ ! -f "$NVM_DIR/alias/$ALIAS" ]; then + if [ $# -lt 2 ] || [ "~$ALIAS" = "~$2" ]; then + DEST="$(nvm_print_implicit_alias local "$ALIAS")" + VERSION="$(nvm_version "$DEST")" + echo "$ALIAS -> $DEST (-> $VERSION) (default)" + fi + fi + done return fi if [ -z "$3" ]; then diff --git "a/test/fast/Aliases/Running \"nvm alias\" lists implicit aliases when they do not exist" "b/test/fast/Aliases/Running \"nvm alias\" lists implicit aliases when they do not exist" new file mode 100755 index 0000000..da91f11 --- /dev/null +++ "b/test/fast/Aliases/Running \"nvm alias\" lists implicit aliases when they do not exist" @@ -0,0 +1,18 @@ +#!/bin/sh + +. ../../../nvm.sh + +die () { echo $@ ; exit 1; } + +NVM_ALIAS_OUTPUT=$(nvm alias) + +EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)" +STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")" +echo "$NVM_ALIAS_OUTPUT" | \grep -e "^stable -> $EXPECTED_STABLE (-> $STABLE_VERSION) (default)$" \ + || die "nvm alias did not contain the default local stable node version" + +EXPECTED_UNSTABLE="$(nvm_print_implicit_alias local unstable)" +UNSTABLE_VERSION="$(nvm_version "$EXPECTED_UNSTABLE")" +echo "$NVM_ALIAS_OUTPUT" | \grep -e "^unstable -> $EXPECTED_UNSTABLE (-> $UNSTABLE_VERSION) (default)$" \ + || die "nvm alias did not contain the default local unstable node version" + diff --git "a/test/fast/Aliases/Running \"nvm alias\" lists manual aliases instead of implicit aliases when present" "b/test/fast/Aliases/Running \"nvm alias\" lists manual aliases instead of implicit aliases when present" new file mode 100755 index 0000000..691ba66 --- /dev/null +++ "b/test/fast/Aliases/Running \"nvm alias\" lists manual aliases instead of implicit aliases when present" @@ -0,0 +1,35 @@ +#!/bin/sh + +. ../../../nvm.sh + +die () { echo $@ ; cleanup ; exit 1; } +cleanup () { + rm -rf ../../../v0.8.1 + rm -rf ../../../v0.9.1 +} + +mkdir ../../../v0.8.1 +mkdir ../../../v0.9.1 + +EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)" +STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")" + +EXPECTED_UNSTABLE="$(nvm_print_implicit_alias local unstable)" +UNSTABLE_VERSION="$(nvm_version "$EXPECTED_UNSTABLE")" + +[ "_$STABLE_VERSION" != "_$UNSTABLE_VERSION" ] \ + || die "stable and unstable versions are the same!" + +nvm alias stable "$EXPECTED_UNSTABLE" +nvm alias unstable "$EXPECTED_STABLE" + +NVM_ALIAS_OUTPUT=$(nvm alias) + +echo "$NVM_ALIAS_OUTPUT" | \grep -e "^stable -> $EXPECTED_UNSTABLE (-> $UNSTABLE_VERSION)$" \ + || die "nvm alias did not contain the overridden 'stable' alias" + +echo "$NVM_ALIAS_OUTPUT" | \grep -e "^unstable -> $EXPECTED_STABLE (-> $STABLE_VERSION)$" \ + || die "nvm alias did not contain the overridden 'unstable' alias" + +cleanup + diff --git a/test/fast/Aliases/teardown_dir b/test/fast/Aliases/teardown_dir index 8ea87ce..f6a01c6 100755 --- a/test/fast/Aliases/teardown_dir +++ b/test/fast/Aliases/teardown_dir @@ -6,3 +6,6 @@ for i in $(seq 1 10) rm -rf "../../../v0.0.$i" done +rm -f "../../../alias/stable" +rm -f "../../../alias/unstable" + diff --git "a/test/fast/Listing versions/Running \"nvm ls stable\" and \"nvm ls unstable\" should return the appropriate implicit alias" "b/test/fast/Listing versions/Running \"nvm ls stable\" and \"nvm ls unstable\" should return the appropriate implicit alias" new file mode 100755 index 0000000..f744ffe --- /dev/null +++ "b/test/fast/Listing versions/Running \"nvm ls stable\" and \"nvm ls unstable\" should return the appropriate implicit alias" @@ -0,0 +1,25 @@ + #!/bin/sh + +. ../../../nvm.sh + +die () { echo $@ ; exit 1; } + +EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)" +STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")" + +EXPECTED_UNSTABLE="$(nvm_print_implicit_alias local unstable)" +UNSTABLE_VERSION="$(nvm_version "$EXPECTED_UNSTABLE")" + +nvm ls stable | \grep "$STABLE_VERSION" >/dev/null \ + || die "expected 'nvm ls stable' to give $STABLE_VERSION, got $(nvm ls stable)" + +nvm ls unstable | \grep "$UNSTABLE_VERSION" >/dev/null \ + || die "expected 'nvm ls unstable' to give $UNSTABLE_VERSION, got $(nvm ls unstable)" + +mkdir ../../../v0.1.2 +nvm alias stable 0.1 + +nvm ls stable | \grep v0.1.2 >/dev/null \ + && nvm ls stable | \grep -v "$STABLE_VERSION" >/dev/null \ + || die "'nvm ls stable' did not contain an explicit 'stable' alias" + diff --git a/test/fast/Listing versions/teardown b/test/fast/Listing versions/teardown index 3c5f013..b1c31a5 100755 --- a/test/fast/Listing versions/teardown +++ b/test/fast/Listing versions/teardown @@ -11,4 +11,6 @@ rmdir ../../../v0.3.3 >/dev/null 2>&1 rmdir ../../../v0.3.9 >/dev/null 2>&1 rmdir ../../../versions >/dev/null 2>&1 unalias nvm_has_system_node >/dev/null 2>&1 +rm -f ../../../alias/stable >/dev/null 2>&1 +rm -f ../../../alias/unstable >/dev/null 2>&1 diff --git a/test/fast/Unit tests/nvm_ls_remote b/test/fast/Unit tests/nvm_ls_remote index c6f2e7c..8b7da02 100755 --- a/test/fast/Unit tests/nvm_ls_remote +++ b/test/fast/Unit tests/nvm_ls_remote @@ -464,5 +464,22 @@ v0.3.8" [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote 0.3 did not output 0.3.x versions; got $OUTPUT" +# Sanity checks +OUTPUT="$(nvm_print_implicit_alias remote stable)" +EXPECTED_OUTPUT="0.10" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_implicit_alias remote stable did not output $EXPECTED_OUTPUT; got $OUTPUT" + +OUTPUT="$(nvm_print_implicit_alias remote unstable)" +EXPECTED_OUTPUT="0.11" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_implicit_alias remote unstable did not output $EXPECTED_OUTPUT; got $OUTPUT" + +OUTPUT="$(nvm_ls_remote stable)" +EXPECTED_OUTPUT="v0.10.32" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote stable did not output $EXPECTED_OUTPUT; got $OUTPUT" + +OUTPUT="$(nvm_ls_remote unstable)" +EXPECTED_OUTPUT="v0.11.14" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote unstable did not output $EXPECTED_OUTPUT; got $OUTPUT" + cleanup diff --git a/test/fast/Unit tests/nvm_print_implicit_alias errors b/test/fast/Unit tests/nvm_print_implicit_alias errors new file mode 100755 index 0000000..44db71c --- /dev/null +++ b/test/fast/Unit tests/nvm_print_implicit_alias errors @@ -0,0 +1,26 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +EXPECTED_FIRST_MSG="nvm_print_implicit_alias must be specified with local or remote as the first argument." +[ "_$(nvm_print_implicit_alias 2>&1)" = "_$EXPECTED_FIRST_MSG" ] \ + || die "nvm_print_implicit_alias did not require local|remote as first argument" +[ "_$(nvm_print_implicit_alias foo 2>&1)" = "_$EXPECTED_FIRST_MSG" ] \ + || die "nvm_print_implicit_alias did not require local|remote as first argument" + +FIRST_EXIT_CODE="$(nvm_print_implicit_alias > /dev/null 2>&1 ; echo $?)" +[ "_$FIRST_EXIT_CODE" = "_1" ] \ + || die "nvm_print_implicit_alias without local|remote had wrong exit code: expected 1, got $FIRST_EXIT_CODE" + +EXPECTED_SECOND_MSG="Only implicit aliases 'stable' and 'unstable' are supported." +[ "_$(nvm_print_implicit_alias local 2>&1)" = "_$EXPECTED_SECOND_MSG" ] \ + || die "nvm_print_implicit_alias did not require stable|unstable as second argument" +[ "_$(nvm_print_implicit_alias local foo 2>&1)" = "_$EXPECTED_SECOND_MSG" ] \ + || die "nvm_print_implicit_alias did not require stable|unstable as second argument" + +SECOND_EXIT_CODE="$(nvm_print_implicit_alias local > /dev/null 2>&1 ; echo $?)" +[ "_$SECOND_EXIT_CODE" = "_2" ] \ + || die "nvm_print_implicit_alias without stable|unstable had wrong exit code: expected 2, got $SECOND_EXIT_CODE" + diff --git a/test/fast/Unit tests/nvm_print_implicit_alias success b/test/fast/Unit tests/nvm_print_implicit_alias success new file mode 100755 index 0000000..95f5fa9 --- /dev/null +++ b/test/fast/Unit tests/nvm_print_implicit_alias success @@ -0,0 +1,42 @@ +#!/bin/sh + +die () { echo $@ ; cleanup ; exit 1; } +cleanup() { + rm -rf ../../../v0.2.3 + rm -rf ../../../v0.3.4 + rm -rf ../../../v0.4.6 + rm -rf ../../../v0.5.7 + rm -rf ../../../v0.7.7 + unset -f nvm_ls_remote +} + +. ../../../nvm.sh + +mkdir ../../../v0.2.3 +mkdir ../../../v0.3.4 +mkdir ../../../v0.4.6 +mkdir ../../../v0.5.7 +mkdir ../../../v0.7.7 + +LATEST_STABLE="$(nvm_print_implicit_alias local stable)" +[ "_$LATEST_STABLE" = "_0.4" ] || die "local stable is not latest even minor: expected 0.4, got $LATEST_STABLE" + +LATEST_UNSTABLE="$(nvm_print_implicit_alias local unstable)" +[ "_$LATEST_UNSTABLE" = "_0.7" ] || die "local unstable is not latest odd minor: expected 0.7, got $LATEST_UNSTABLE" + +nvm_ls_remote() { + echo "v0.4.3" + echo "v0.5.4" + echo "v0.6.6" + echo "v0.7.7" + echo "v0.9.7" +} + +LATEST_STABLE="$(nvm_print_implicit_alias remote stable)" +[ "_$LATEST_STABLE" = "_0.6" ] || die "remote stable is not latest even minor: expected 0.6, got $LATEST_STABLE" + +LATEST_UNSTABLE="$(nvm_print_implicit_alias remote unstable)" +[ "_$LATEST_UNSTABLE" = "_0.9" ] || die "remote unstable is not latest odd minor: expected 0.9, got $LATEST_UNSTABLE" + +cleanup + diff --git a/test/fast/Unit tests/nvm_validate_implicit_alias b/test/fast/Unit tests/nvm_validate_implicit_alias new file mode 100755 index 0000000..ffc4247 --- /dev/null +++ b/test/fast/Unit tests/nvm_validate_implicit_alias @@ -0,0 +1,19 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +EXPECTED_MSG="Only implicit aliases 'stable' and 'unstable' are supported." +[ "_$(nvm_validate_implicit_alias 2>&1)" = "_$EXPECTED_MSG" ] \ + || die "nvm_validate_implicit_alias did not require stable|unstable" +[ "_$(nvm_validate_implicit_alias foo 2>&1)" = "_$EXPECTED_MSG" ] \ + || die "nvm_validate_implicit_alias did not require stable|unstable" + +EXIT_CODE="$(nvm_validate_implicit_alias >/dev/null 2>&1 ; echo $?)" +[ "_$EXIT_CODE" = "_1" ] \ + || die "nvm_validate_implicit_alias without stable|unstable had wrong exit code: expected 1, got $EXIT_CODE" + +nvm_validate_implicit_alias stable || die "nvm_validate_implicit_alias stable did not exit 0" +nvm_validate_implicit_alias unstable || die "nvm_validate_implicit_alias unstable did not exit 0" +