Merge pull request #526 from creationix/add_nvm_num_version_groups
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Replace `expr` with `nvm_num_version_groups`
authored by Jordan Harband · committed by Jordan Harband · parents 5363ff8, 3546618 · Browse files · 7 files·+57 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+57 added−22 removed
- tests:+36 added−19 removed
- other:+21 added−3 removed
Provenance
- Commit
edf36a8- Tree
bd1e0f5- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent 1
5363ff8- Parent 2
3546618
7 files changed+57 added−22 removed
modified:test/fast/Listing versions/Running "nvm ls system" should include "system" when appropriate+1 added-2 removed
1 #!/bin/sh2 3-cleanup () { unset -f nvm_has_system_node; }4-die () { cleanup ; echo $@ ; exit 1; }3+die () { echo $@ ; exit 1; }4 5 . ../../../nvm.sh6 7 mkdir -p ../../../v0.0.114 unmodified linesmodified:test/fast/Listing versions/Running "nvm ls" should filter out "versions"+1 added-4 removed
1 #!/bin/sh2 3 mkdir ../../../v0.1.34 mkdir ../../../v0.2.35+mkdir ../../../versions6 7 . ../../../nvm.sh8 8-mkdir ../../../versions9-9 [ -z "$(nvm ls | \grep 'versions')" ]10 # The result should contain only the appropriate version numbers.11 13-rmdir ../../../versions14-modified:test/fast/Listing versions/Running "nvm ls" with node-like versioning vx.x.x should only list a matched version+1 added-1 removed
4 unmodified lines5 . ../../../nvm.sh6 7 nvm ls v0.1 | grep v0.1.2 &&8 nvm ls v0.1.2 | grep v0.1.2 &&9-nvm ls v0.1. | grep N/A &&9+nvm ls v0.1. | grep v0.1.2 &&10 nvm ls v0.1.1 | grep N/A11 modified:test/fast/Listing versions/teardown+11 added-11 removed
1-rmdir ../../../v0.0.12-rmdir ../../../v0.0.33-rmdir ../../../v0.0.94-rmdir ../../../v0.1.25-rmdir ../../../v0.1.36-rmdir ../../../v0.2.37-rmdir ../../../v0.3.18-rmdir ../../../v0.3.39-rmdir ../../../v0.3.910-rmdir ../../../versions11-unalias nvm_has_system_node1+rmdir ../../../v0.0.1 >/dev/null 2>&12+rmdir ../../../v0.0.3 >/dev/null 2>&13+rmdir ../../../v0.0.9 >/dev/null 2>&14+rmdir ../../../v0.1.2 >/dev/null 2>&15+rmdir ../../../v0.1.3 >/dev/null 2>&16+rmdir ../../../v0.2.3 >/dev/null 2>&17+rmdir ../../../v0.3.1 >/dev/null 2>&18+rmdir ../../../v0.3.3 >/dev/null 2>&19+rmdir ../../../v0.3.9 >/dev/null 2>&110+rmdir ../../../versions >/dev/null 2>&111+unalias nvm_has_system_node >/dev/null 2>&112 modified:test/fast/Unit tests/nvm_ls_current+1 added-1 removed
4 unmodified lines5 . ../../../nvm.sh6 7 return_zero () { return 0; }8 9-[ "$( (nvm deactivate > /dev/null 2>&1) && nvm_ls_current)" = "system" ] || die 'when deactivated, did not return "system"'9+[ "$(nvm deactivate > /dev/null 2>&1 ; nvm_ls_current)" = "system" ] || die 'when deactivated, did not return "system"'10 11 TEST_PWD=$(pwd)12 TEST_DIR="$TEST_PWD/nvm_ls_current_tmp"13 rm -rf "$TEST_DIR"15 unmodified linesadded:test/fast/Unit tests/nvm_num_version_groups+21 added
1+#!/bin/sh2+3+die () { echo $@ ; exit 1; }4+5+. ../../../nvm.sh6+7+[ "~$(nvm_num_version_groups)" = "~0" ] || die "no args should give 0"8+[ "~$(nvm_num_version_groups a)" = "~1" ] || die "one letter should give 1"9+10+[ "~$(nvm_num_version_groups 1)" = "~1" ] || die "1 should give 1"11+[ "~$(nvm_num_version_groups v1)" = "~1" ] || die "v1 should give 1"12+[ "~$(nvm_num_version_groups v1.)" = "~1" ] || die "v1. should give 1"13+14+[ "~$(nvm_num_version_groups 1.2)" = "~2" ] || die "1.2 should give 2"15+[ "~$(nvm_num_version_groups v1.2)" = "~2" ] || die "v1.2 should give 2"16+[ "~$(nvm_num_version_groups v1.2.)" = "~2" ] || die "v1.2. should give 2"17+18+[ "~$(nvm_num_version_groups 1.2.3)" = "~3" ] || die "1.2.3 should give 3"19+[ "~$(nvm_num_version_groups v1.2.3)" = "~3" ] || die "v1.2.3 should give 3"20+[ "~$(nvm_num_version_groups v1.2.3.)" = "~3" ] || die "v1.2.3. should give 3"21+modified:nvm.sh+21 added-3 removed
179 unmodified lines180 nvm_format_version() {181 echo "$1" | sed -e 's/^\([0-9]\)/v\1/g'182 }183 184+nvm_num_version_groups() {185+ local VERSION186+ VERSION="$1"187+ if [ -z "$VERSION" ]; then188+ echo "0"189+ return190+ fi191+ local NVM_NUM_DOTS192+ NVM_NUM_DOTS=$(echo "$VERSION" | sed -e 's/^v//' | sed -e 's/\.$//' | sed -e 's/[^\.]//g')193+ local NVM_NUM_GROUPS194+ NVM_NUM_GROUPS=".$NVM_NUM_DOTS"195+ echo "${#NVM_NUM_GROUPS}"196+}197+198 nvm_strip_path() {199 echo "$1" | sed -e "s#$NVM_DIR/[^/]*$2[^:]*:##g" -e "s#:$NVM_DIR/[^/]*$2[^:]*##g" -e "s#$NVM_DIR/[^/]*$2[^:]*##g"200 }201 44 unmodified lines246 nvm_version `cat $NVM_DIR/alias/$PATTERN`247 return248 fi249 # If it looks like an explicit version, don't do anything funny236- if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*\.[0-9]*$"` != 0 ]; then250+ if [ "~$(echo "$PATTERN" | cut -c1-1)" = "~v" ] && [ "~$(nvm_num_version_groups "$PATTERN")" = "~3" ]; then251 if [ -d "$(nvm_version_path "$PATTERN")" ]; then252 VERSIONS="$PATTERN"253 fi254 else255 PATTERN=$(nvm_format_version $PATTERN)242- if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*$"` != 0 ]; then243- PATTERN="$PATTERN."256+ if [ "~$PATTERN" != "~system" ]; then257+ local NUM_VERSION_GROUPS258+ NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")"259+ if [ "~$NUM_VERSION_GROUPS" = "~2" ] || [ "~$NUM_VERSION_GROUPS" = "~1" ]; then260+ PATTERN="$(echo "$PATTERN" | sed -e 's/\.*$//g')."261+ fi262 fi263 if [ -d "$(nvm_version_dir new)" ]; then264 VERSIONS=`find "$(nvm_version_dir new)/" "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \265 | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v' | \grep -v -e '^versions$'`601 unmodified lines