diff --git a/nvm.sh b/nvm.sh index 98e6228..6e9da50 100644 --- a/nvm.sh +++ b/nvm.sh @@ -95,13 +95,37 @@ nvm_rc_version() { fi } +nvm_version_greater() { + local LHS + LHS=$(echo "$1" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}') + local RHS + RHS=$(echo "$2" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}') + [ $LHS \> $RHS ]; +} + +nvm_version_dir() { + local NVM_USE_NEW_DIR + NVM_USE_NEW_DIR="$1" + if [ -z "$NVM_USE_NEW_DIR" ] || [ "$NVM_USE_NEW_DIR" = "new" ]; then + echo "$NVM_DIR/versions" + elif [ "$NVM_USE_NEW_DIR" = "old" ]; then + echo "$NVM_DIR" + else + echo "unknown version dir" >&2 + return 3 + fi +} + nvm_version_path() { local VERSION VERSION="$1" if [ -z "$VERSION" ]; then - echo "$NVM_DIR" - elif [ ! -z "$VERSION" ]; then - echo "$NVM_DIR/$VERSION" + echo "version is required" >&2 + return 3 + elif nvm_version_greater 0.12.0 "$VERSION"; then + echo "$(nvm_version_dir old)/$VERSION" + else + echo "$(nvm_version_dir new)/$VERSION" fi } @@ -205,8 +229,13 @@ nvm_ls() { if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*$"` != 0 ]; then PATTERN="$PATTERN." fi - VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \ - | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'` + 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 '{}' ';' \ + | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'` + else + VERSIONS=`find "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \ + | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'` + fi fi if [ -z "$VERSIONS" ]; then echo "N/A" @@ -753,7 +782,7 @@ nvm() { npm install -g --quiet $INSTALLS ;; "clear-cache" ) - rm -f $NVM_DIR/v* 2>/dev/null + rm -f $NVM_DIR/v* "$(nvm_version_dir)" 2>/dev/null echo "Cache cleared." ;; "version" ) @@ -763,7 +792,7 @@ nvm() { echo "0.13.1" ;; "unload" ) - unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version > /dev/null 2>&1 + unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version nvm_version_greater > /dev/null 2>&1 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1 ;; * ) diff --git "a/test/fast/Listing versions/Running \"nvm ls\" should list versions in the \"versions\" directory" "b/test/fast/Listing versions/Running \"nvm ls\" should list versions in the \"versions\" directory" new file mode 100755 index 0000000..eb8f2ba --- /dev/null +++ "b/test/fast/Listing versions/Running \"nvm ls\" should list versions in the \"versions\" directory" @@ -0,0 +1,12 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +mkdir -p ../../../versions/v0.12.1 +mkdir ../../../v0.1.3 + +. ../../../nvm.sh + +nvm ls 0.12 | grep v0.12.1 || die '"nvm ls" did not list a version in the versions/ directory' +nvm ls 0.1 | grep v0.1.3 || die '"nvm ls" did not list a version not in the versions/ directory' + diff --git a/test/fast/Listing versions/teardown b/test/fast/Listing versions/teardown index 191d12b..a8fc501 100644 --- a/test/fast/Listing versions/teardown +++ b/test/fast/Listing versions/teardown @@ -7,5 +7,6 @@ rmdir ../../../v0.2.3 rmdir ../../../v0.3.1 rmdir ../../../v0.3.3 rmdir ../../../v0.3.9 +rmdir ../../../versions unalias nvm_has_system_node diff --git a/test/fast/Unit tests/nvm_version_dir b/test/fast/Unit tests/nvm_version_dir new file mode 100755 index 0000000..fb070ce --- /dev/null +++ b/test/fast/Unit tests/nvm_version_dir @@ -0,0 +1,11 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +[ "$(nvm_version_dir)" = "$NVM_DIR/versions" ] || die '"nvm_version_dir" did not return new dir path' +[ "$(nvm_version_dir new)" = "$(nvm_version_dir)" ] || die '"nvm_version_dir new" did not return new dir path' +[ "$(nvm_version_dir old)" = "$NVM_DIR" ] || die '"nvm_version_dir old" did not return old dir path' +[ "$(nvm_version_dir foo 2>&1)" = "unknown version dir" ] || die '"nvm_version_dir foo" did not error out' + diff --git a/test/fast/Unit tests/nvm_version_greater b/test/fast/Unit tests/nvm_version_greater new file mode 100755 index 0000000..8f8f551 --- /dev/null +++ b/test/fast/Unit tests/nvm_version_greater @@ -0,0 +1,10 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +nvm_version_greater 0.10.0 0.2.12 || die '"nvm_version_greater 0.10.0 0.2.12" did not return true' +! nvm_version_greater 0.10.0 0.20.12 || die '"nvm_version_greater 0.10.0 0.20.12" returned true' +! nvm_version_greater 0.10.0 0.10.0 || die '"nvm_version_greater" returned false for the same two versions' + diff --git a/test/fast/Unit tests/nvm_version_path b/test/fast/Unit tests/nvm_version_path index adb6338..b9fa2e5 100755 --- a/test/fast/Unit tests/nvm_version_path +++ b/test/fast/Unit tests/nvm_version_path @@ -4,6 +4,8 @@ die () { echo $@ ; exit 1; } . ../../../nvm.sh -[ "$(nvm_version_path)" = "$NVM_DIR" ] && -[ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ] +[ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ] || die '"nvm_version_path foo" did not return correct location' +[ "$(nvm_version_path 2>&1)" = "version is required" ] || die '"nvm_version_path" did not error out' +[ "$(nvm_version_path v0.11.0)" = "$NVM_DIR/v0.11.0" ] || die 'old version has the wrong path' +[ "$(nvm_version_path v0.12.0)" = "$NVM_DIR/versions/v0.12.0" ] || die 'new version has the wrong path' diff --git a/test/fast/setup_dir b/test/fast/setup_dir index 64e097c..5e2a208 100755 --- a/test/fast/setup_dir +++ b/test/fast/setup_dir @@ -9,5 +9,8 @@ for SRC in v* src alias; do [ -e "$SRC" ] && mv "$SRC" bak done + if [ -d versions ]; then + mv versions bak + fi true )