Skip to content

Commit

Permalink
[Tests] use -ex option during tests when possible
Browse files Browse the repository at this point in the history
Relates to #1130
  • Loading branch information
ljharb committed Jul 11, 2016
1 parent 186eb88 commit ecbd7d4
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 40 deletions.
2 changes: 1 addition & 1 deletion nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2576,7 +2576,7 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
nvm_version_greater nvm_version_greater_than_or_equal_to \
nvm_print_npm_version nvm_npm_global_modules \
nvm_has_system_node nvm_has_system_iojs \
nvm_download nvm_get_latest nvm_has nvm_get_latest \
nvm_download nvm_get_latest nvm_has \
nvm_supports_source_options nvm_auto nvm_supports_xz \
nvm_echo nvm_err nvm_grep \
nvm_has_colors nvm_process_parameters > /dev/null 2>&1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/sh

set -ex

. ../../nvm.sh

nvm alias test v0.1.2
[ "$(cat ../../alias/test)" = "v0.1.2" ]

[ "$(cat "$(nvm_alias_path)/test")" = "v0.1.2" ]
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/sh

set -ex

die () { echo $@ ; exit 1; }

. ../../nvm.sh

nvm deactivate 2>&1

[ "$(nvm current)" = "system" ] || [ "$(nvm current)" = "none" ] || die '"nvm current" did not report "system" or "none" when deactivated'

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#!/bin/sh

set -ex

mkdir -p ../../v0.2.3

die () { echo $@ ; exit 1; }

[ `expr $PATH : ".*v0.2.3/.*/bin"` = 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2

. ../../nvm.sh

nvm use --delete-prefix v0.2.3 || die "Failed to activate v0.2.3"
[ `expr "$PATH" : ".*v0.2.3/.*/bin"` != 0 ] || die "PATH not set up properly"
[ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules"` = 0 ] || die "NODE_PATH should not contain (npm root -g)"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

die () { echo $@ ; cleanup ; exit 1; }
cleanup () {
rm -rf "${NVM_DIR}/v0.10.4"
Expand All @@ -11,22 +13,30 @@ mkdir "${NVM_DIR}/v0.10.4"

nvm deactivate >/dev/null 2>&1

set +ex # needed for stderr
INSTALL_ERROR_MSG="$(nvm install v0.10.5 --reinstall-packages-from=0.11 2>&1)"
set -ex
EXPECTED_ERROR_MSG="If --reinstall-packages-from is provided, it must point to an installed version of node."
[ "~$INSTALL_ERROR_MSG" = "~$EXPECTED_ERROR_MSG" ] \
|| die ""nvm install --reinstall-packages-from" should fail when given an uninstalled version: expected '$EXPECTED_ERROR_MSG', got '$INSTALL_ERROR_MSG'"

set +e # TODO: fix
INSTALL_EXIT_CODE="$(nvm install v0.10.5 --reinstall-packages-from=0.11 >/dev/null 2>&1; echo $?)"
[ "~$INSTALL_EXIT_CODE" = "~5" ] \
|| die ""nvm install --reinstall-packages-from" should exit with code 5 when given an uninstalled version, got $INSTALL_EXIT_CODE"
set -e
[ $INSTALL_EXIT_CODE -eq 5 ] \
|| die ""nvm install --reinstall-packages-from" should exit with code 5 when given an uninstalled version, got ${INSTALL_EXIT_CODE}"

set +ex # needed for stderr
INSTALL_ERROR_MSG="$(nvm install v0.10.5 --reinstall-packages-from=0.10.5 2>&1)"
set -ex
EXPECTED_ERROR_MSG="You can't reinstall global packages from the same version of node you're installing."
[ "~$INSTALL_ERROR_MSG" = "~$EXPECTED_ERROR_MSG" ] \
|| die ""nvm install --reinstall-packages-from" should fail when given the same version: expected '$EXPECTED_ERROR_MSG', got '$INSTALL_ERROR_MSG'"

set +e # TODO: fix
INSTALL_EXIT_CODE="$(nvm install v0.10.5 --reinstall-packages-from=0.10.5 >/dev/null 2>&1; echo $?)"
[ "~$INSTALL_EXIT_CODE" = "~4" ] \
set -e
[ $INSTALL_EXIT_CODE -eq 4 ] \
|| die ""nvm install --reinstall-packages-from" should exit with code 4 when given the same version, got $INSTALL_EXIT_CODE"

cleanup
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/bin/sh

set -ex

die () { echo $@ ; exit 1; }

. ../../nvm.sh
[ "$(nvm install invalid.invalid 2>&1)" = "Version 'invalid.invalid' not found - try \`nvm ls-remote\` to browse available versions." ] || die "nvm installing an invalid version did not print a nice error message"

set +ex # needed for stderr
OUTPUT="$(nvm install invalid.invalid 2>&1)"
set -ex
EXPECTED_OUTPUT="Version 'invalid.invalid' not found - try \`nvm ls-remote\` to browse available versions."
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "nvm installing an invalid version did not print a nice error message"
11 changes: 9 additions & 2 deletions test/fast/Running "nvm unalias" should remove the alias file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/bin/sh

echo v0.1.2 > ../../alias/test
set -ex

ALIAS_PATH="../../alias"

echo v0.1.2 > "${ALIAS_PATH}/test"

. ../../nvm.sh

nvm unalias test
! [ -e ../../alias/test ]

! [ -e "${ALIAS_PATH}/test" ]
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

cd ../..
mkdir v0.0.1
mkdir src/node-v0.0.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

cd ../..
mkdir v0.0.1
mkdir src/node-v0.0.1
Expand All @@ -8,7 +10,7 @@ sudo touch v0.0.1/sudo

. ./nvm.sh

RETURN_MESSAGE="$(nvm uninstall v0.0.1 2>&1)"
RETURN_MESSAGE="$(nvm uninstall v0.0.1 2>&1 || echo)"
CHECK_FOR="Cannot uninstall, incorrect permissions on installation folder"

test "${RETURN_MESSAGE#*$CHECK_FOR}" != "$RETURN_MESSAGE" || exit 1
[ "${RETURN_MESSAGE#*$CHECK_FOR}" != "$RETURN_MESSAGE" ] || exit 1
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/sh

fail () { echo $@ ; exit 1; }
set -ex

die () { echo $@ ; exit 1; }

. ../../nvm.sh

type nvm > /dev/null 2>&1 || fail "NVM not loaded"
type nvm > /dev/null 2>&1 || die "nvm not loaded"

nvm unload

type nvm > /dev/null 2>&1 && fail "NVM not unloaded" || exit 0

! type nvm > /dev/null 2>&1 || die "nvm not unloaded"
10 changes: 10 additions & 0 deletions test/fast/Running "nvm use foo" where "foo" is circular aborts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

die () { echo $@ ; cleanup ; exit 1; }

cleanup() {
Expand All @@ -10,20 +12,28 @@ cleanup() {

nvm_make_alias foo foo

set +ex # needed for stderr
OUTPUT="$(nvm use foo 2>&1)"
set -ex
EXPECTED_OUTPUT='The alias "foo" leads to an infinite loop. Aborting.'
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "'nvm use foo' did not output >${EXPECTED_OUTPUT}<; got >${OUTPUT}<"

set +ex # needed for stderr
EXIT_CODE="$(nvm use foo 2>/dev/null ; echo $?)"
set -ex
[ "_$EXIT_CODE" = "_8" ] || die "Expected exit code 8; got ${EXIT_CODE}"

set +ex # needed for stderr
OUTPUT="$(nvm use --silent foo 2>&1)"
set -ex
EXPECTED_OUTPUT=''
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "'nvm use --silent foo' did not output >${EXPECTED_OUTPUT}<; got >${OUTPUT}<"

set +ex # needed for stderr
EXIT_CODE="$(nvm use --silent foo 2>/dev/null ; echo $?)"
set -ex
[ $EXIT_CODE -eq 8 ] || die "Expected exit code 8 from 'nvm use --silent foo'; got ${EXIT_CODE}"

cleanup
2 changes: 0 additions & 2 deletions test/fast/Running "nvm use iojs" uses latest io.js version
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ cleanup() {
rm -rf "$(nvm_version_path "iojs-${VERSION}")"
}

set +ex # TODO
. ../../nvm.sh
set -ex

nvm deactivate || die "unable to deactivate; current: >$(nvm current)<"

Expand Down
21 changes: 18 additions & 3 deletions test/fast/Running "nvm use system" should work as expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

cleanup() {
unset -f nvm_has_system_node nvm_print_npm_version
}
Expand All @@ -11,19 +13,32 @@ nvm_has_system_node() { return 0; }
nvm_print_npm_version() { command printf ' (npm v1.2.3)'; }

EXPECTED_OUTPUT="Now using system version of node: $(node -v)$(nvm_print_npm_version)"
set +x # since stderr is needed
OUTPUT="$(nvm use system 2>&1)"
set -x
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "Could not use system version of node. Got >${OUTPUT}<, expected >${EXPECTED_OUTPUT}<"

EXPECTED_OUTPUT=""
[ "$(nvm use --silent system 2>&1 | tail -1)" = "${EXPECTED_OUTPUT}" ] || die "Could not use system version of node or --silent was not silent"
set +x # since stderr is needed
OUTPUT="$(nvm use --silent system 2>&1 | tail -1)"
set -x
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "Could not use system version of node or --silent was not silent"

nvm_has_system_node() { return 1; }
nvm_print_npm_version() { command printf ''; }

EXPECTED_OUTPUT="System version of node not found."
[ "$(nvm use system 2>&1 | tail -1)" = "${EXPECTED_OUTPUT}" ] || die "Did not report error, system node not found"
set +x # since stderr is needed
OUTPUT="$(nvm use system 2>&1 | tail -1)"
set -x
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "Did not report error, system node not found"

nvm use system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found"
EXPECTED_OUTPUT=""
[ "$(nvm use --silent system 2>&1 | tail -1)" = "${EXPECTED_OUTPUT}" ] || die "Did not report error, system node not found or --silent was not silent"
set +x # since stderr is needed
OUTPUT="$(nvm use --silent system 2>&1 | tail -1)"
set -x
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "Did not report error, system node not found or --silent was not silent"
nvm use --silent system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found or --silent was not silent"

cleanup
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env bash
#!/bin/sh

set -ex

export NVM_SYMLINK_CURRENT=true
. ../../nvm.sh

rm -rf "${NVM_DIR}/v0.10.29"
mkdir "${NVM_DIR}/v0.10.29"
nvm use 0.10.29
nvm use --delete-prefix 0.10.29
rmdir "${NVM_DIR}/v0.10.29"

if [ ! -L "${NVM_DIR}/current" ];then
Expand All @@ -22,7 +24,7 @@ fi

rm -rf "${NVM_DIR}/v0.11.13"
mkdir "${NVM_DIR}/v0.11.13"
nvm use 0.11.13
nvm use --delete-prefix 0.11.13
rmdir "${NVM_DIR}/v0.11.13"

newLink="$(readlink "${NVM_DIR}/current")"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
#!/bin/sh

set -ex

. ../../nvm.sh

Expand All @@ -8,17 +10,17 @@ TEST_COUNT=0
TEST_PASSED=0
TEST_FAILED=0

function registerExpectedSymlink() {
registerExpectedSymlink() {
registerResult ${1}
}

function registerExpectedNoSymlink() {
[ ${1} -ne 0 ]
registerExpectedNoSymlink() {
[ $1 -ne 0 ]
registerResult $?
}

function registerResult() {
result=${1}
registerResult() {
result="${1}"

TEST_COUNT=$(($TEST_COUNT + 1))

Expand All @@ -27,19 +29,19 @@ function registerResult() {
|| TEST_FAILED=$(($TEST_FAILED + 1))
}

function cleanup() {
rm -rf ../../${TEST_NODE_VERSION}
rm -f ../../current
cleanup() {
rm -rf "${NVM_DIR}/${TEST_NODE_VERSION}"
rm -f "${NVM_DIR}/current"
}

function runNvmUse() {
mkdir ../../${TEST_NODE_VERSION}
nvm use ${TEST_NODE_VERSION} > /dev/null 2>&1
rmdir ../../${TEST_NODE_VERSION}
runNvmUse() {
mkdir "${NVM_DIR}/${TEST_NODE_VERSION}"
nvm use --delete-prefix "${TEST_NODE_VERSION}" > /dev/null 2>&1
rmdir "${NVM_DIR}/${TEST_NODE_VERSION}"
}

function isCurrentSymlinkPresent() {
[ -L ../../current ]
isCurrentSymlinkPresent() {
[ -L "${NVM_DIR}/current" ]
}

NVM_SYMLINK_CURRENT=false
Expand Down Expand Up @@ -80,5 +82,4 @@ registerExpectedNoSymlink $?

cleanup

[ ${TEST_FAILED} -ne 0 ] && echo "${TEST_COUNT} tested, ${TEST_PASSED} passed, ${TEST_FAILED} failed" && exit 1 || true

[ $TEST_FAILED -ne 0 ] && echo "${TEST_COUNT} tested, ${TEST_PASSED} passed, ${TEST_FAILED} failed" && exit 1 || true
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/sh

set -ex

. ../../nvm.sh

nvm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

set -- yes
. ../../nvm.sh
[ "$1" = yes ]
2 changes: 2 additions & 0 deletions test/fast/setup
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

(
cd ../..

Expand Down
2 changes: 2 additions & 0 deletions test/fast/setup_dir
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

(
cd ../..

Expand Down
2 changes: 2 additions & 0 deletions test/fast/teardown
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

# Remove temporary files
(
cd ../..
Expand Down
2 changes: 2 additions & 0 deletions test/fast/teardown_dir
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -ex

(
cd ../..

Expand Down

0 comments on commit ecbd7d4

Please sign in to comment.