From 9068dda1107328b87ac1b9c7fefaaffd0cc9297a Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 19 Aug 2014 01:53:39 -0700 Subject: [PATCH 1/7] Adding `nvm_version_dir` --- nvm.sh | 13 +++++++++++++ test/fast/Unit tests/nvm_version_dir | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100755 test/fast/Unit tests/nvm_version_dir diff --git a/nvm.sh b/nvm.sh index 98e6228..67794cb 100644 --- a/nvm.sh +++ b/nvm.sh @@ -95,6 +95,19 @@ nvm_rc_version() { fi } +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" 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' + From a2da9abd6d3f9c7d77c64208519b5732e866b9ba Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 19 Aug 2014 01:56:34 -0700 Subject: [PATCH 2/7] Use nvm_version_dir in nvm_version_path --- nvm.sh | 5 +++-- test/fast/Unit tests/nvm_version_path | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/nvm.sh b/nvm.sh index 67794cb..8d8133f 100644 --- a/nvm.sh +++ b/nvm.sh @@ -112,9 +112,10 @@ nvm_version_path() { local VERSION VERSION="$1" if [ -z "$VERSION" ]; then - echo "$NVM_DIR" + echo "version is required" >&2 + return 3 elif [ ! -z "$VERSION" ]; then - echo "$NVM_DIR/$VERSION" + echo "$(nvm_version_dir old)/$VERSION" fi } diff --git a/test/fast/Unit tests/nvm_version_path b/test/fast/Unit tests/nvm_version_path index adb6338..ba38e37 100755 --- a/test/fast/Unit tests/nvm_version_path +++ b/test/fast/Unit tests/nvm_version_path @@ -4,6 +4,6 @@ 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' From 8fe677a6443cd959e5b1ed1fc29cc9025719c626 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 20 Aug 2014 00:01:06 -0700 Subject: [PATCH 3/7] Add `nvm_version_greater` --- nvm.sh | 10 +++++++++- test/fast/Unit tests/nvm_version_greater | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100755 test/fast/Unit tests/nvm_version_greater diff --git a/nvm.sh b/nvm.sh index 8d8133f..8860919 100644 --- a/nvm.sh +++ b/nvm.sh @@ -95,6 +95,14 @@ 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" @@ -777,7 +785,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/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' + From 0a695c5d2a85c0613ea088bd9ebb7acaae95fa77 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 20 Aug 2014 00:18:29 -0700 Subject: [PATCH 4/7] Modify nvm_version_path to use nvm_version_greater. --- nvm.sh | 4 +++- test/fast/Unit tests/nvm_version_path | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 8860919..f5216b1 100644 --- a/nvm.sh +++ b/nvm.sh @@ -122,8 +122,10 @@ nvm_version_path() { if [ -z "$VERSION" ]; then echo "version is required" >&2 return 3 - elif [ ! -z "$VERSION" ]; then + elif nvm_version_greater 0.12.0 "$VERSION"; then echo "$(nvm_version_dir old)/$VERSION" + else + echo "$(nvm_version_dir new)/$VERSION" fi } diff --git a/test/fast/Unit tests/nvm_version_path b/test/fast/Unit tests/nvm_version_path index ba38e37..b9fa2e5 100755 --- a/test/fast/Unit tests/nvm_version_path +++ b/test/fast/Unit tests/nvm_version_path @@ -6,4 +6,6 @@ die () { echo $@ ; exit 1; } [ "$(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' From 65c2435418850731dd3d0a0a4762a5285f566898 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 20 Aug 2014 00:37:38 -0700 Subject: [PATCH 5/7] Making sure that versions are backed up when running tests. --- test/fast/setup_dir | 3 +++ 1 file changed, 3 insertions(+) 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 ) From 5dd2d7e5d429397a89eb27afec3394ae30f9a1d6 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 20 Aug 2014 00:42:50 -0700 Subject: [PATCH 6/7] Make `nvm ls` support listing the "versions" directory when it's present. --- nvm.sh | 9 +++++++-- ...ould list versions in the \"versions\" directory" | 12 ++++++++++++ test/fast/Listing versions/teardown | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100755 "test/fast/Listing versions/Running \"nvm ls\" should list versions in the \"versions\" directory" diff --git a/nvm.sh b/nvm.sh index f5216b1..9e13892 100644 --- a/nvm.sh +++ b/nvm.sh @@ -229,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" 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 From 37477359802073d8c807a9662cce43cd17d53378 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 20 Aug 2014 00:50:15 -0700 Subject: [PATCH 7/7] Make sure `nvm clear-cache` clears the versions directory as well. --- nvm.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 9e13892..6e9da50 100644 --- a/nvm.sh +++ b/nvm.sh @@ -782,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" )