diff --git a/nvm.sh b/nvm.sh index f6e9511..0e2a8a0 100644 --- a/nvm.sh +++ b/nvm.sh @@ -181,6 +181,20 @@ nvm_format_version() { echo "$1" | sed -e 's/^\([0-9]\)/v\1/g' } +nvm_num_version_groups() { + local VERSION + VERSION="$1" + if [ -z "$VERSION" ]; then + echo "0" + return + fi + local NVM_NUM_DOTS + NVM_NUM_DOTS=$(echo "$VERSION" | sed -e 's/^v//' | sed -e 's/\.$//' | sed -e 's/[^\.]//g') + local NVM_NUM_GROUPS + NVM_NUM_GROUPS=".$NVM_NUM_DOTS" + echo "${#NVM_NUM_GROUPS}" +} + nvm_strip_path() { echo "$1" | sed -e "s#$NVM_DIR/[^/]*$2[^:]*:##g" -e "s#:$NVM_DIR/[^/]*$2[^:]*##g" -e "s#$NVM_DIR/[^/]*$2[^:]*##g" } @@ -233,14 +247,18 @@ nvm_ls() { return fi # If it looks like an explicit version, don't do anything funny - if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*\.[0-9]*$"` != 0 ]; then + if [ "~$(echo "$PATTERN" | cut -c1-1)" = "~v" ] && [ "~$(nvm_num_version_groups "$PATTERN")" = "~3" ]; then if [ -d "$(nvm_version_path "$PATTERN")" ]; then VERSIONS="$PATTERN" fi else PATTERN=$(nvm_format_version $PATTERN) - if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*$"` != 0 ]; then - PATTERN="$PATTERN." + if [ "~$PATTERN" != "~system" ]; then + local NUM_VERSION_GROUPS + NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")" + if [ "~$NUM_VERSION_GROUPS" = "~2" ] || [ "~$NUM_VERSION_GROUPS" = "~1" ]; then + PATTERN="$(echo "$PATTERN" | sed -e 's/\.*$//g')." + fi fi if [ -d "$(nvm_version_dir new)" ]; then VERSIONS=`find "$(nvm_version_dir new)/" "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \ diff --git "a/test/fast/Listing versions/Running \"nvm ls system\" should include \"system\" when appropriate" "b/test/fast/Listing versions/Running \"nvm ls system\" should include \"system\" when appropriate" index 3acaf3f..f1e5ef6 100755 --- "a/test/fast/Listing versions/Running \"nvm ls system\" should include \"system\" when appropriate" +++ "b/test/fast/Listing versions/Running \"nvm ls system\" should include \"system\" when appropriate" @@ -1,7 +1,6 @@ #!/bin/sh -cleanup () { unset -f nvm_has_system_node; } -die () { cleanup ; echo $@ ; exit 1; } +die () { echo $@ ; exit 1; } . ../../../nvm.sh diff --git "a/test/fast/Listing versions/Running \"nvm ls\" should filter out \"versions\"" "b/test/fast/Listing versions/Running \"nvm ls\" should filter out \"versions\"" index c9858df..0652c6b 100755 --- "a/test/fast/Listing versions/Running \"nvm ls\" should filter out \"versions\"" +++ "b/test/fast/Listing versions/Running \"nvm ls\" should filter out \"versions\"" @@ -2,13 +2,10 @@ mkdir ../../../v0.1.3 mkdir ../../../v0.2.3 +mkdir ../../../versions . ../../../nvm.sh -mkdir ../../../versions - [ -z "$(nvm ls | \grep 'versions')" ] # The result should contain only the appropriate version numbers. -rmdir ../../../versions - diff --git "a/test/fast/Listing versions/Running \"nvm ls\" with node-like versioning vx.x.x should only list a matched version" "b/test/fast/Listing versions/Running \"nvm ls\" with node-like versioning vx.x.x should only list a matched version" index 443b305..43c77d8 100755 --- "a/test/fast/Listing versions/Running \"nvm ls\" with node-like versioning vx.x.x should only list a matched version" +++ "b/test/fast/Listing versions/Running \"nvm ls\" with node-like versioning vx.x.x should only list a matched version" @@ -6,6 +6,6 @@ mkdir ../../../v0.1.2 nvm ls v0.1 | grep v0.1.2 && nvm ls v0.1.2 | grep v0.1.2 && -nvm ls v0.1. | grep N/A && +nvm ls v0.1. | grep v0.1.2 && nvm ls v0.1.1 | grep N/A diff --git a/test/fast/Listing versions/teardown b/test/fast/Listing versions/teardown index a8fc501..c230c3c 100755 --- a/test/fast/Listing versions/teardown +++ b/test/fast/Listing versions/teardown @@ -1,12 +1,12 @@ -rmdir ../../../v0.0.1 -rmdir ../../../v0.0.3 -rmdir ../../../v0.0.9 -rmdir ../../../v0.1.2 -rmdir ../../../v0.1.3 -rmdir ../../../v0.2.3 -rmdir ../../../v0.3.1 -rmdir ../../../v0.3.3 -rmdir ../../../v0.3.9 -rmdir ../../../versions -unalias nvm_has_system_node +rmdir ../../../v0.0.1 >/dev/null 2>&1 +rmdir ../../../v0.0.3 >/dev/null 2>&1 +rmdir ../../../v0.0.9 >/dev/null 2>&1 +rmdir ../../../v0.1.2 >/dev/null 2>&1 +rmdir ../../../v0.1.3 >/dev/null 2>&1 +rmdir ../../../v0.2.3 >/dev/null 2>&1 +rmdir ../../../v0.3.1 >/dev/null 2>&1 +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 diff --git a/test/fast/Unit tests/nvm_ls_current b/test/fast/Unit tests/nvm_ls_current index c475157..59f0a60 100755 --- a/test/fast/Unit tests/nvm_ls_current +++ b/test/fast/Unit tests/nvm_ls_current @@ -6,7 +6,7 @@ die () { echo $@ ; exit 1; } return_zero () { return 0; } -[ "$( (nvm deactivate > /dev/null 2>&1) && nvm_ls_current)" = "system" ] || die 'when deactivated, did not return "system"' +[ "$(nvm deactivate > /dev/null 2>&1 ; nvm_ls_current)" = "system" ] || die 'when deactivated, did not return "system"' TEST_PWD=$(pwd) TEST_DIR="$TEST_PWD/nvm_ls_current_tmp" diff --git a/test/fast/Unit tests/nvm_num_version_groups b/test/fast/Unit tests/nvm_num_version_groups new file mode 100755 index 0000000..893da75 --- /dev/null +++ b/test/fast/Unit tests/nvm_num_version_groups @@ -0,0 +1,21 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +[ "~$(nvm_num_version_groups)" = "~0" ] || die "no args should give 0" +[ "~$(nvm_num_version_groups a)" = "~1" ] || die "one letter should give 1" + +[ "~$(nvm_num_version_groups 1)" = "~1" ] || die "1 should give 1" +[ "~$(nvm_num_version_groups v1)" = "~1" ] || die "v1 should give 1" +[ "~$(nvm_num_version_groups v1.)" = "~1" ] || die "v1. should give 1" + +[ "~$(nvm_num_version_groups 1.2)" = "~2" ] || die "1.2 should give 2" +[ "~$(nvm_num_version_groups v1.2)" = "~2" ] || die "v1.2 should give 2" +[ "~$(nvm_num_version_groups v1.2.)" = "~2" ] || die "v1.2. should give 2" + +[ "~$(nvm_num_version_groups 1.2.3)" = "~3" ] || die "1.2.3 should give 3" +[ "~$(nvm_num_version_groups v1.2.3)" = "~3" ] || die "v1.2.3 should give 3" +[ "~$(nvm_num_version_groups v1.2.3.)" = "~3" ] || die "v1.2.3. should give 3" +