Merge pull request #525 from creationix/add_unit_tests

Add unit tests
master
Jordan Harband 2014-09-15 20:46:31 -07:00
commit 5363ff8916
3 changed files with 52 additions and 9 deletions

23
nvm.sh
View File

@ -98,14 +98,17 @@ nvm_rc_version() {
if [ -e "$NVMRC_PATH" ]; then if [ -e "$NVMRC_PATH" ]; then
read NVM_RC_VERSION < "$NVMRC_PATH" read NVM_RC_VERSION < "$NVMRC_PATH"
echo "Found '$NVMRC_PATH' with version <$NVM_RC_VERSION>" echo "Found '$NVMRC_PATH' with version <$NVM_RC_VERSION>"
else
>&2 echo "No .nvmrc file found"
return 1
fi fi
} }
nvm_version_greater() { nvm_version_greater() {
local LHS local LHS
LHS=$(echo "$1" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}') LHS=$(nvm_normalize_version "$1")
local RHS local RHS
RHS=$(echo "$2" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}') RHS=$(nvm_normalize_version "$2")
[ $LHS -gt $RHS ]; [ $LHS -gt $RHS ];
} }
@ -171,7 +174,7 @@ nvm_remote_version() {
} }
nvm_normalize_version() { nvm_normalize_version() {
echo "$1" | sed -e 's/^v//' | \awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }' echo "$1" | sed -e 's/^v//' | \awk -F. '{ printf("%d%06d%06d\n", $1,$2,$3); }'
} }
nvm_format_version() { nvm_format_version() {
@ -192,11 +195,9 @@ nvm_prepend_path() {
nvm_binary_available() { nvm_binary_available() {
# binaries started with node 0.8.6 # binaries started with node 0.8.6
local MINIMAL local LAST_VERSION_WITHOUT_BINARY
MINIMAL="0.8.6" LAST_VERSION_WITHOUT_BINARY="0.8.5"
local VERSION nvm_version_greater "$1" "$LAST_VERSION_WITHOUT_BINARY"
VERSION=$1
[ $(nvm_normalize_version $VERSION) -ge $(nvm_normalize_version $MINIMAL) ]
} }
nvm_ls_current() { nvm_ls_current() {
@ -839,5 +840,9 @@ nvm() {
esac esac
} }
nvm ls default >/dev/null && nvm use default >/dev/null || true if nvm ls default >/dev/null; then
nvm use default >/dev/null
elif nvm_rc_version >/dev/null 2>&1; then
nvm use >/dev/null
fi

View File

@ -0,0 +1,26 @@
#!/bin/sh
die () { echo $@ ; cleanup ; exit 1; }
setup() {
cleanup
mkdir -p tmp_nvm_find_up/a/b/c/d
touch tmp_nvm_find_up/test
touch tmp_nvm_find_up/a/b/c/test
}
cleanup () {
rm -rf tmp_nvm_find_up
}
. ../../../nvm.sh
setup
TEST_DIR="$PWD"
[ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up" ] || die "failed to find 1 dir up"
[ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up" ] || die "failed to find 2 dirs up"
[ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b/c nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up/a/b/c" ] || die "failed to find in current dir"
[ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b/c/d nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up/a/b/c" ] || die "failed to find 1 level up from current dir"
cleanup

View File

@ -0,0 +1,12 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
nvm_has cat && type cat > /dev/null || die 'nvm_has locates "cat" properly'
[ "~$(nvm_has foobarbaz 2>&1)" = "~" ] || die "nvm_has does not suppress error output"
! nvm_has foobarbaz && ! type foobarbaz >/dev/null 2>&1 || die "nvm_has does not return a nonzero exit code when not found"