diff --git a/test/common.sh b/test/common.sh index f5bf6ff..203fa78 100644 --- a/test/common.sh +++ b/test/common.sh @@ -1,3 +1,29 @@ +# Runs a command once and captures stdout and exit code. +# Suppresses xtrace in the subshell. Discards stderr. +# +# Sets: CAPTURED_STDOUT, CAPTURED_EXIT_CODE +# +# Usage: +# try nvm_version current +# [ "$CAPTURED_STDOUT" = "v20.0.0" ] || die "wrong output" +# [ "$CAPTURED_EXIT_CODE" = 0 ] || die "wrong exit code" +try() { + CAPTURED_STDOUT="$(set +x; "$@" 2>/dev/null)" && CAPTURED_EXIT_CODE=0 || CAPTURED_EXIT_CODE=$? +} + +# Runs a command once and captures stderr and exit code. +# Suppresses xtrace in the subshell. Discards stdout. +# +# Sets: CAPTURED_STDERR, CAPTURED_EXIT_CODE +# +# Usage: +# try_err nvm_alias +# [ "$CAPTURED_STDERR" = "An alias is required." ] || die "wrong error" +# [ "$CAPTURED_EXIT_CODE" = 1 ] || die "wrong exit code" +try_err() { + CAPTURED_STDERR="$(set +x; "$@" 2>&1 >/dev/null)" && CAPTURED_EXIT_CODE=0 || CAPTURED_EXIT_CODE=$? +} + assert_ok() { local FUNCTION=$1 shift diff --git a/test/fast/Aliases/'nvm alias' should not accept aliases with a hash b/test/fast/Aliases/'nvm alias' should not accept aliases with a hash index a172a25..15ac230 100755 --- a/test/fast/Aliases/'nvm alias' should not accept aliases with a hash +++ b/test/fast/Aliases/'nvm alias' should not accept aliases with a hash @@ -3,25 +3,21 @@ : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + die () { echo "$@" ; exit 1; } -OUTPUT="$(nvm alias foo#bar baz 2>&1)" +try_err nvm alias foo#bar baz EXPECTED_OUTPUT="Aliases with a comment delimiter (#) are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias with a hash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias with a hash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to create an alias with a hash should fail with code 1, got '$CAPTURED_EXIT_CODE'" -EXIT_CODE="$(nvm alias foo#bar baz >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to create an alias with a hash should fail with code 1, got '$EXIT_CODE'" - -OUTPUT="$(nvm alias foo# baz 2>&1)" +try_err nvm alias foo# baz EXPECTED_OUTPUT="Aliases with a comment delimiter (#) are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias ending with a hash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias ending with a hash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to create an alias ending with a hash should fail with code 1, got '$CAPTURED_EXIT_CODE'" -EXIT_CODE="$(nvm alias foo# baz >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to create an alias ending with a hash should fail with code 1, got '$EXIT_CODE'" - -OUTPUT="$(nvm alias \#bar baz 2>&1)" +try_err nvm alias \#bar baz EXPECTED_OUTPUT="Aliases with a comment delimiter (#) are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias starting with a hash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" - -EXIT_CODE="$(nvm alias \#bar baz >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to create an alias starting with a hash should fail with code 1, got '$EXIT_CODE'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias starting with a hash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to create an alias starting with a hash should fail with code 1, got '$CAPTURED_EXIT_CODE'" diff --git a/test/fast/Aliases/'nvm alias' should not accept aliases with slashes b/test/fast/Aliases/'nvm alias' should not accept aliases with slashes index 53fc70e..5a363eb 100755 --- a/test/fast/Aliases/'nvm alias' should not accept aliases with slashes +++ b/test/fast/Aliases/'nvm alias' should not accept aliases with slashes @@ -3,25 +3,21 @@ : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + die () { echo "$@" ; exit 1; } -OUTPUT="$(nvm alias foo/bar baz 2>&1)" +try_err nvm alias foo/bar baz EXPECTED_OUTPUT="Aliases in subdirectories are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias with a slash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to create an alias with a slash should fail with code 1, got '$CAPTURED_EXIT_CODE'" -EXIT_CODE="$(nvm alias foo/bar baz >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to create an alias with a slash should fail with code 1, got '$EXIT_CODE'" - -OUTPUT="$(nvm alias foo/ baz 2>&1)" +try_err nvm alias foo/ baz EXPECTED_OUTPUT="Aliases in subdirectories are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias ending with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias ending with a slash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to create an alias ending with a slash should fail with code 1, got '$CAPTURED_EXIT_CODE'" -EXIT_CODE="$(nvm alias foo/ baz >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to create an alias ending with a slash should fail with code 1, got '$EXIT_CODE'" - -OUTPUT="$(nvm alias /bar baz 2>&1)" +try_err nvm alias /bar baz EXPECTED_OUTPUT="Aliases in subdirectories are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias starting with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" - -EXIT_CODE="$(nvm alias /bar baz >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to create an alias starting with a slash should fail with code 1, got '$EXIT_CODE'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias starting with a slash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to create an alias starting with a slash should fail with code 1, got '$CAPTURED_EXIT_CODE'" diff --git a/test/fast/Aliases/'nvm unalias' should not accept aliases with slashes b/test/fast/Aliases/'nvm unalias' should not accept aliases with slashes index 5e5fb52..b89cfa5 100755 --- a/test/fast/Aliases/'nvm unalias' should not accept aliases with slashes +++ b/test/fast/Aliases/'nvm unalias' should not accept aliases with slashes @@ -3,25 +3,21 @@ : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + die () { echo "$@" ; exit 1; } -OUTPUT="$(nvm unalias foo/bar 2>&1)" +try_err nvm unalias foo/bar EXPECTED_OUTPUT="Aliases in subdirectories are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias with a slash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to remove an alias with a slash should fail with code 1, got '$CAPTURED_EXIT_CODE'" -EXIT_CODE="$(nvm unalias foo/bar >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to remove an alias with a slash should fail with code 1, got '$EXIT_CODE'" - -OUTPUT="$(nvm unalias foo/ 2>&1)" +try_err nvm unalias foo/ EXPECTED_OUTPUT="Aliases in subdirectories are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias ending with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias ending with a slash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to remove an alias ending with a slash should fail with code 1, got '$CAPTURED_EXIT_CODE'" -EXIT_CODE="$(nvm unalias foo/ >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to remove an alias ending with a slash should fail with code 1, got '$EXIT_CODE'" - -OUTPUT="$(nvm unalias /bar 2>&1)" +try_err nvm unalias /bar EXPECTED_OUTPUT="Aliases in subdirectories are not supported." -[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias starting with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" - -EXIT_CODE="$(nvm unalias /bar >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = "1" ] || die "trying to remove an alias starting with a slash should fail with code 1, got '$EXIT_CODE'" +[ "$CAPTURED_STDERR" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias starting with a slash should fail with '$EXPECTED_OUTPUT', got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" = "1" ] || die "trying to remove an alias starting with a slash should fail with code 1, got '$CAPTURED_EXIT_CODE'" diff --git a/test/fast/Running 'nvm use foo' where 'foo' is circular aborts b/test/fast/Running 'nvm use foo' where 'foo' is circular aborts index 5c9e032..d2b36ac 100755 --- a/test/fast/Running 'nvm use foo' where 'foo' is circular aborts +++ b/test/fast/Running 'nvm use foo' where 'foo' is circular aborts @@ -11,30 +11,20 @@ cleanup() { : nvm.sh \. ../../nvm.sh +\. ../common.sh + nvm_make_alias foo foo -set +ex # needed for stderr -OUTPUT="$(nvm use foo 2>&1)" -set -ex +try_err nvm use foo 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}<" +[ "_${CAPTURED_STDERR}" = "_${EXPECTED_OUTPUT}" ] \ + || die "'nvm use foo' did not output >${EXPECTED_OUTPUT}<; got >${CAPTURED_STDERR}<" +[ "_$CAPTURED_EXIT_CODE" = "_8" ] || die "Expected exit code 8; got ${CAPTURED_EXIT_CODE}" -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 +try_err nvm use --silent foo 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}" +[ "_${CAPTURED_STDERR}" = "_${EXPECTED_OUTPUT}" ] \ + || die "'nvm use --silent foo' did not output >${EXPECTED_OUTPUT}<; got >${CAPTURED_STDERR}<" +[ $CAPTURED_EXIT_CODE -eq 8 ] || die "Expected exit code 8 from 'nvm use --silent foo'; got ${CAPTURED_EXIT_CODE}" cleanup diff --git a/test/fast/Unit tests/nvm_alias b/test/fast/Unit tests/nvm_alias index b05599a..90b5fca 100755 --- a/test/fast/Unit tests/nvm_alias +++ b/test/fast/Unit tests/nvm_alias @@ -9,21 +9,19 @@ cleanup () { : nvm.sh \. ../../../nvm.sh -OUTPUT="$(nvm_alias 2>&1)" -EXPECTED_OUTPUT='An alias is required.' -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias' produced wrong output; got $OUTPUT" +\. ../../common.sh -EXIT_CODE="$(nvm_alias >/dev/null 2>&1 ; echo $?)" -[ "_$EXIT_CODE" = "_1" ] || die "'nvm_alias' exited with $EXIT_CODE, expected 1" +try_err nvm_alias +EXPECTED_OUTPUT='An alias is required.' +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias' produced wrong output; got $CAPTURED_STDERR" +[ "_$CAPTURED_EXIT_CODE" = "_1" ] || die "'nvm_alias' exited with $CAPTURED_EXIT_CODE, expected 1" rm -rf ../../../alias/nonexistent -OUTPUT="$(nvm_alias nonexistent 2>&1)" +try_err nvm_alias nonexistent EXPECTED_OUTPUT='Alias does not exist.' -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias nonexistent' produced wrong output; got $OUTPUT" - -EXIT_CODE="$(nvm_alias nonexistent >/dev/null 2>&1 ; echo $?)" -[ "_$EXIT_CODE" = "_2" ] || die "'nvm_alias nonexistent' exited with $EXIT_CODE, expected 2" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias nonexistent' produced wrong output; got $CAPTURED_STDERR" +[ "_$CAPTURED_EXIT_CODE" = "_2" ] || die "'nvm_alias nonexistent' exited with $CAPTURED_EXIT_CODE, expected 2" EXPECTED_OUTPUT="0.10" nvm alias test "$EXPECTED_OUTPUT" || die "'nvm alias test $EXPECTED_OUTPUT' failed" diff --git a/test/fast/Unit tests/nvm_compare_checksum b/test/fast/Unit tests/nvm_compare_checksum index 99c3b79..c076984 100755 --- a/test/fast/Unit tests/nvm_compare_checksum +++ b/test/fast/Unit tests/nvm_compare_checksum @@ -8,66 +8,50 @@ die () { echo "$@" ; cleanup ; exit 1; } : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + set -ex nvm_compute_checksum() { echo } -set +x -OUTPUT="$(nvm_compare_checksum 2>&1 >/dev/null || echo)" -EXIT_CODE="$(nvm_compare_checksum >/dev/null 2>&1 || echo $?)" -set -x +try_err nvm_compare_checksum EXPECTED_OUTPUT='Provided file to checksum is empty.' -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 4 ] || die "expected to exit with code 4, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 4 ] || die "expected to exit with code 4, got ${CAPTURED_EXIT_CODE}" -set +x -OUTPUT="$(nvm_compare_checksum foo 2>&1 >/dev/null || echo)" -EXIT_CODE="$(nvm_compare_checksum foo >/dev/null 2>&1 || echo $?)" -set -x +try_err nvm_compare_checksum foo EXPECTED_OUTPUT='Provided file to checksum does not exist.' -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 3 ] || die "expected to exit with code 3, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 3 ] || die "expected to exit with code 3, got ${CAPTURED_EXIT_CODE}" -set +x -OUTPUT="$(nvm_compare_checksum ../../../nvm.sh 2>&1 >/dev/null || echo)" -EXIT_CODE="$(nvm_compare_checksum ../../../nvm.sh >/dev/null 2>&1 || echo $?)" -set -x +try_err nvm_compare_checksum ../../../nvm.sh EXPECTED_OUTPUT='Provided checksum to compare to is empty.' -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 2 ] || die "expected to exit with code 2, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 2 ] || die "expected to exit with code 2, got ${CAPTURED_EXIT_CODE}" -set +x -OUTPUT="$(nvm_compare_checksum ../../../nvm.sh checksum 2>&1 >/dev/null)" -EXIT_CODE="$(nvm_compare_checksum ../../../nvm.sh checksum >/dev/null 2>&1 ; echo $?)" -set -x +try_err nvm_compare_checksum ../../../nvm.sh checksum EXPECTED_OUTPUT="Computed checksum of '../../../nvm.sh' is empty. WARNING: Continuing *without checksum verification*" -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${CAPTURED_EXIT_CODE}" nvm_compute_checksum() { echo "not checksum: ${1}" } -set +x -OUTPUT="$(nvm_compare_checksum ../../../nvm.sh checksum 2>&1 >/dev/null || echo)" -EXIT_CODE="$(nvm_compare_checksum ../../../nvm.sh checksum >/dev/null 2>&1 || echo $?)" -set -x +try_err nvm_compare_checksum ../../../nvm.sh checksum EXPECTED_OUTPUT="Checksums do not match: 'not checksum: ../../../nvm.sh' found, 'checksum' expected." -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 1 ] || die "expected to exit with code 1, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 1 ] || die "expected to exit with code 1, got ${CAPTURED_EXIT_CODE}" nvm_compute_checksum() { echo checksum } -set +x -OUTPUT="$(nvm_compare_checksum ../../../nvm.sh checksum 2>&1 >/dev/null)" -EXIT_CODE="$(nvm_compare_checksum ../../../nvm.sh checksum >/dev/null 2>&1; echo $?)" -set -x +try_err nvm_compare_checksum ../../../nvm.sh checksum EXPECTED_OUTPUT='Checksums matched!' -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${CAPTURED_EXIT_CODE}" cleanup diff --git a/test/fast/Unit tests/nvm_compute_checksum b/test/fast/Unit tests/nvm_compute_checksum index 0e8ae0f..c149db4 100755 --- a/test/fast/Unit tests/nvm_compute_checksum +++ b/test/fast/Unit tests/nvm_compute_checksum @@ -7,18 +7,14 @@ die () { echo "$@" ; exit 1; } : nvm.sh \. ../../../nvm.sh -set +x -OUTPUT="$(nvm_compute_checksum 2>&1 >/dev/null || echo)" -EXIT_CODE="$(nvm_compute_checksum >/dev/null 2>&1 || echo $?)" -set -x -EXPECTED_OUTPUT='Provided file to checksum is empty.' -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 2 ] || die "expected to exit with code 2, got ${EXIT_CODE}" +\. ../../common.sh -set +x -OUTPUT="$(nvm_compute_checksum foo 2>&1 >/dev/null || echo)" -EXIT_CODE="$(nvm_compute_checksum foo >/dev/null 2>&1 || echo $?)" -set -x +try_err nvm_compute_checksum +EXPECTED_OUTPUT='Provided file to checksum is empty.' +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 2 ] || die "expected to exit with code 2, got ${CAPTURED_EXIT_CODE}" + +try_err nvm_compute_checksum foo EXPECTED_OUTPUT='Provided file to checksum does not exist.' -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 1 ] || die "expected to exit with code 1, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 1 ] || die "expected to exit with code 1, got ${CAPTURED_EXIT_CODE}" diff --git a/test/fast/Unit tests/nvm_die_on_prefix b/test/fast/Unit tests/nvm_die_on_prefix index 95f7b98..3be5a98 100755 --- a/test/fast/Unit tests/nvm_die_on_prefix +++ b/test/fast/Unit tests/nvm_die_on_prefix @@ -6,6 +6,8 @@ TEST_DIR="$TEST_PWD/nvm_die_on_prefix_tmp" : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + TEST_VERSION_DIR="${TEST_DIR}/version" cleanup () { @@ -23,23 +25,20 @@ die () { [ ! -e "$TEST_DIR" ] && mkdir "$TEST_DIR" -OUTPUT="$(nvm_die_on_prefix 2>&1)" +try_err nvm_die_on_prefix EXPECTED_OUTPUT="First argument \"delete the prefix\" must be zero or one" -EXIT_CODE="$(nvm_die_on_prefix >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "_$EXIT_CODE" = "_1" ] || die "'nvm_die_on_prefix' did not exit with 1; got "$EXIT_CODE"" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_1" ] || die "'nvm_die_on_prefix' did not exit with 1; got "$CAPTURED_EXIT_CODE"" -OUTPUT="$(nvm_die_on_prefix 2 2>&1)" +try_err nvm_die_on_prefix 2 EXPECTED_OUTPUT="First argument \"delete the prefix\" must be zero or one" -EXIT_CODE="$(nvm_die_on_prefix 2 >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 2' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "_$EXIT_CODE" = "_1" ] || die "'nvm_die_on_prefix' did not exit with 1; got "$EXIT_CODE"" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 2' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_1" ] || die "'nvm_die_on_prefix' did not exit with 1; got "$CAPTURED_EXIT_CODE"" -OUTPUT="$(nvm_die_on_prefix 0 2>&1)" +try_err nvm_die_on_prefix 0 EXPECTED_OUTPUT='Second argument "nvm command", and third argument "nvm version dir", must both be nonempty' -EXIT_CODE="$(nvm_die_on_prefix 0 >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "_$EXIT_CODE" = "_2" ] || die "'nvm_die_on_prefix 0' did not exit with 2; got '$EXIT_CODE'" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_2" ] || die "'nvm_die_on_prefix 0' did not exit with 2; got '$CAPTURED_EXIT_CODE'" nvm_has() { return 1; } # ie, npm is not installed OUTPUT="$(nvm_die_on_prefix 0 version_dir foo 2>&1)" @@ -70,26 +69,26 @@ node() { OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)" [ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when directory is equivalent; got '$OUTPUT'" -OUTPUT="$(PREFIX=bar nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)" +PREFIX=bar try_err nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" +unset PREFIX EXPECTED_OUTPUT='nvm is not compatible with the "PREFIX" environment variable: currently set to "bar" Run `unset PREFIX` to unset it.' -EXIT_CODE="$(export PREFIX=bar ; nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "_$EXIT_CODE" = "_3" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 3; got '$EXIT_CODE'" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 3; got '$CAPTURED_EXIT_CODE'" -OUTPUT="$(export NPM_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)" +NPM_CONFIG_PREFIX=bar try_err nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" +unset NPM_CONFIG_PREFIX EXPECTED_OUTPUT='nvm is not compatible with the "NPM_CONFIG_PREFIX" environment variable: currently set to "bar" Run `unset NPM_CONFIG_PREFIX` to unset it.' -EXIT_CODE="$(export NPM_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "_$EXIT_CODE" = "_4" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$EXIT_CODE'" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_4" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$CAPTURED_EXIT_CODE'" -OUTPUT="$(export npm_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)" +npm_CONFIG_PREFIX=bar try_err nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" +unset npm_CONFIG_PREFIX EXPECTED_OUTPUT='nvm is not compatible with the "npm_CONFIG_PREFIX" environment variable: currently set to "bar" Run `unset npm_CONFIG_PREFIX` to unset it.' -EXIT_CODE="$(export npm_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "_$EXIT_CODE" = "_4" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$EXIT_CODE'" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_4" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$CAPTURED_EXIT_CODE'" OUTPUT="$(export FOO='This contains NPM_CONFIG_PREFIX' ; nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)" [ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop; got '$OUTPUT'" @@ -101,23 +100,21 @@ OUTPUT="$(export FOO='This contains NPM_CONFIG_PREFIX' ; nvm_die_on_prefix 0 foo # project: prefix echo 'prefix=garbage' > .npmrc - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your project npmrc file ($(nvm_sanitize_path "${TEST_DIR}")/.npmrc) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has prefix did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has prefix did not exit with 10; got '$CAPTURED_EXIT_CODE'" # project: globalconfig echo 'globalconfig=garbage' > .npmrc - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your project npmrc file ($(nvm_sanitize_path "${TEST_DIR}")/.npmrc) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has globalconfig did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with project .npmrc that has globalconfig did not exit with 10; got '$CAPTURED_EXIT_CODE'" rm "${TEST_DIR}/.npmrc" || die '.npmrc could not be removed' @@ -133,67 +130,61 @@ Run \`foo\` to unset it." # global: prefix echo 'prefix=garbage' > "${GLOBAL_NPMRC}" - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your global npmrc file ($(nvm_sanitize_path "${GLOBAL_NPMRC}")) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has prefix did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has prefix did not exit with 10; got '$CAPTURED_EXIT_CODE'" # global: globalconfig echo 'globalconfig=garbage' > "${GLOBAL_NPMRC}" - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your global npmrc file ($(nvm_sanitize_path "${GLOBAL_NPMRC}")) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has globalconfig did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with global npmrc that has globalconfig did not exit with 10; got '$CAPTURED_EXIT_CODE'" rm "${GLOBAL_NPMRC}" || die "${GLOBAL_NPMRC} could not be removed" # builtin: prefix echo 'prefix=garbage' > "${BUILTIN_NPMRC}" - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your builtin npmrc file ($(nvm_sanitize_path "${BUILTIN_NPMRC}")) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has prefix did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has prefix did not exit with 10; got '$CAPTURED_EXIT_CODE'" # builtin: globalconfig echo 'globalconfig=garbage' > "${BUILTIN_NPMRC}" - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your builtin npmrc file ($(nvm_sanitize_path "${BUILTIN_NPMRC}")) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has globalconfig did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with builtin npmrc that has globalconfig did not exit with 10; got '$CAPTURED_EXIT_CODE'" rm "${BUILTIN_NPMRC}" || die "${BUILTIN_NPMRC} could not be removed" # user: prefix echo 'prefix=garbage' > "${USER_NPMRC}" - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your user’s .npmrc file ($(nvm_sanitize_path "${USER_NPMRC}")) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has prefix did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has prefix did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has prefix did not exit with 10; got '$CAPTURED_EXIT_CODE'" # user: globalconfig echo 'globalconfig=garbage' > "${USER_NPMRC}" - OUTPUT="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" 2>&1)" + try_err nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" EXPECTED_OUTPUT="Your user’s .npmrc file ($(nvm_sanitize_path "${USER_NPMRC}")) has a \`globalconfig\` and/or a \`prefix\` setting, which are incompatible with nvm. Run \`foo\` to unset it." - EXIT_CODE="$(nvm_die_on_prefix 0 foo "${TEST_VERSION_DIR}" >/dev/null 2>&1; echo $?)" - [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" - [ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has globalconfig did not exit with 10; got '$EXIT_CODE'" + [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" + [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has globalconfig did not exit with 10; got '$CAPTURED_EXIT_CODE'" ) cleanup diff --git a/test/fast/Unit tests/nvm_get_checksum b/test/fast/Unit tests/nvm_get_checksum index 3df06d4..559f781 100755 --- a/test/fast/Unit tests/nvm_get_checksum +++ b/test/fast/Unit tests/nvm_get_checksum @@ -12,17 +12,16 @@ set +e # TODO: fix \. ../../../nvm.sh set -e +\. ../../common.sh + nvm_get_mirror() { echo "mirror-${1}-${2}" } -set +ex # needed to capture error output -OUTPUT="$(nvm_get_checksum 2>&1 >/dev/null)" +try_err nvm_get_checksum EXPECTED_OUTPUT='supported flavors: node, iojs' -EXIT_CODE="$(nvm_get_checksum >/dev/null 2>&1 ; echo $?)" -set -ex -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected error output >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" -[ "${EXIT_CODE}" = 2 ] || die "expected exit code 2, got ${EXIT_CODE}" +[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected error output >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" +[ "${CAPTURED_EXIT_CODE}" = 2 ] || die "expected exit code 2, got ${CAPTURED_EXIT_CODE}" nvm_download() { echo "ERROR_FAILED_MATCH no_match more fields" diff --git a/test/fast/Unit tests/nvm_get_latest missing curl or wget b/test/fast/Unit tests/nvm_get_latest missing curl or wget index bc23d37..23788da 100755 --- a/test/fast/Unit tests/nvm_get_latest missing curl or wget +++ b/test/fast/Unit tests/nvm_get_latest missing curl or wget @@ -9,13 +9,14 @@ cleanup() { : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + nvm_has() { return 1 ; } -OUTPUT="$(nvm_get_latest 2>&1)" -EXIT_CODE="$(nvm_get_latest >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_nvm needs curl or wget to proceed." ] \ - || die "no curl/wget did not report correct error message, got '$OUTPUT'" -[ "_$EXIT_CODE" = "_1" ] \ - || die "no curl/wget did not exit with code 1, got $EXIT_CODE" +try_err nvm_get_latest +[ "_$CAPTURED_STDERR" = "_nvm needs curl or wget to proceed." ] \ + || die "no curl/wget did not report correct error message, got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_1" ] \ + || die "no curl/wget did not exit with code 1, got $CAPTURED_EXIT_CODE" cleanup diff --git a/test/fast/Unit tests/nvm_get_minor_version b/test/fast/Unit tests/nvm_get_minor_version index 546caf6..1d2a618 100755 --- a/test/fast/Unit tests/nvm_get_minor_version +++ b/test/fast/Unit tests/nvm_get_minor_version @@ -5,6 +5,8 @@ die () { echo "$@" ; exit 1; } : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + expect () { INPUT="$1" EXPECTED_OUTPUT="$2" @@ -20,8 +22,8 @@ fail_with () { INPUT="$1" EXPECTED_CODE="$2" - EXIT_CODE="$(nvm_get_minor_version "$INPUT" >/dev/null 2>&1; echo $?)" - [ "_$EXIT_CODE" = "_$EXPECTED_CODE" ] || die "nvm_get_minor_version "$INPUT" did not fail with code "$EXPECTED_CODE"; got $EXIT_CODE" + try nvm_get_minor_version "$INPUT" + [ "_$CAPTURED_EXIT_CODE" = "_$EXPECTED_CODE" ] || die "nvm_get_minor_version "$INPUT" did not fail with code "$EXPECTED_CODE"; got $CAPTURED_EXIT_CODE" } expect 1 1.0 diff --git a/test/fast/Unit tests/nvm_install_latest_npm b/test/fast/Unit tests/nvm_install_latest_npm index c65ccf3..a1f8f0f 100755 --- a/test/fast/Unit tests/nvm_install_latest_npm +++ b/test/fast/Unit tests/nvm_install_latest_npm @@ -22,11 +22,10 @@ npm() { echo '1.2.3' } -OUTPUT="$(nvm_install_latest_npm 2>&1 >/dev/null)" -EXIT_CODE="$(nvm_install_latest_npm >/dev/null 2>&1 ; echo $?)" +try_err nvm_install_latest_npm EXPECTED="Unable to obtain node version." -[ "${OUTPUT}" = "${EXPECTED}" ] || die "When node is unavailable, expected >${EXPECTED}<; got >${OUTPUT}" +[ "${CAPTURED_STDERR}" = "${EXPECTED}" ] || die "When node is unavailable, expected >${EXPECTED}<; got >${CAPTURED_STDERR}" node() { echo 'v4.5.6' @@ -37,11 +36,10 @@ nvm_ls_current() { npm() { return 1 } -OUTPUT="$(nvm_install_latest_npm 2>&1 >/dev/null)" -EXIT_CODE="$(nvm_install_latest_npm >/dev/null 2>&1 ; echo $?)" +try_err nvm_install_latest_npm EXPECTED="Unable to obtain npm version." -[ "${OUTPUT}" = "${EXPECTED}" ] || die "When node is available and npm is unavailable, expected >${EXPECTED}<; got >${OUTPUT}" +[ "${CAPTURED_STDERR}" = "${EXPECTED}" ] || die "When node is available and npm is unavailable, expected >${EXPECTED}<; got >${CAPTURED_STDERR}" node() { echo 'v4.5.6' @@ -52,8 +50,7 @@ nvm_ls_current() { npm() { return 1 } -OUTPUT="$(nvm_install_latest_npm 2>&1 >/dev/null)" -EXIT_CODE="$(nvm_install_latest_npm >/dev/null 2>&1 ; echo $?)" +try_err nvm_install_latest_npm EXPECTED="Unable to obtain npm version." -[ "${OUTPUT}" = "${EXPECTED}" ] || die "When node is system and npm is unavailable, expected >${EXPECTED}<; got >${OUTPUT}" +[ "${CAPTURED_STDERR}" = "${EXPECTED}" ] || die "When node is system and npm is unavailable, expected >${EXPECTED}<; got >${CAPTURED_STDERR}" diff --git a/test/fast/Unit tests/nvm_ls_remote b/test/fast/Unit tests/nvm_ls_remote index f0cf363..4bc3283 100755 --- a/test/fast/Unit tests/nvm_ls_remote +++ b/test/fast/Unit tests/nvm_ls_remote @@ -19,10 +19,11 @@ nvm_download() { EXPECTED_OUTPUT_PATH="$MOCKS_DIR/nvm_ls_remote.txt" -OUTPUT="$(nvm_ls_remote foo)" -EXIT_CODE="$(nvm_ls_remote foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A" -[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE" +\. ../../common.sh + +try nvm_ls_remote foo +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE" OUTPUT="$(nvm_ls_remote)" EXPECTED_OUTPUT="$(cat "$EXPECTED_OUTPUT_PATH")" diff --git a/test/fast/Unit tests/nvm_ls_remote nightly b/test/fast/Unit tests/nvm_ls_remote nightly index 83e95ec..c3393b7 100755 --- a/test/fast/Unit tests/nvm_ls_remote nightly +++ b/test/fast/Unit tests/nvm_ls_remote nightly @@ -19,10 +19,11 @@ nvm_download() { EXPECTED_OUTPUT_PATH="$MOCKS_DIR/nvm_ls_remote nightly.txt" -OUTPUT="$(nvm_ls_remote foo)" -EXIT_CODE="$(nvm_ls_remote foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A" -[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE" +\. ../../common.sh + +try nvm_ls_remote foo +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE" OUTPUT="$(nvm_ls_remote)" EXPECTED_OUTPUT="$(cat "$EXPECTED_OUTPUT_PATH")" diff --git a/test/fast/Unit tests/nvm_ls_remote_iojs b/test/fast/Unit tests/nvm_ls_remote_iojs index 7dd6ad3..668f5e9 100755 --- a/test/fast/Unit tests/nvm_ls_remote_iojs +++ b/test/fast/Unit tests/nvm_ls_remote_iojs @@ -17,10 +17,11 @@ nvm_download() { EXPECTED_OUTPUT_PATH="$PWD/mocks/nvm_ls_remote_iojs.txt" -OUTPUT="$(nvm_ls_remote_iojs foo)" -EXIT_CODE="$(nvm_ls_remote_iojs foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A" -[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE" +\. ../../common.sh + +try nvm_ls_remote_iojs foo +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE" OUTPUT="$(nvm_ls_remote_iojs)" EXPECTED_OUTPUT="$(cat "$EXPECTED_OUTPUT_PATH")" diff --git a/test/fast/Unit tests/nvm_make_alias b/test/fast/Unit tests/nvm_make_alias index dd3f794..d5e213a 100755 --- a/test/fast/Unit tests/nvm_make_alias +++ b/test/fast/Unit tests/nvm_make_alias @@ -5,16 +5,16 @@ die () { echo "$@" ; exit 1; } : nvm.sh \. ../../../nvm.sh -OUTPUT="$(nvm_make_alias 2>&1)" -EXIT_CODE="$(nvm_make_alias >/dev/null 2>&1 ; echo $?)" +\. ../../common.sh + +try_err nvm_make_alias EXPECTED_OUTPUT='an alias name is required' -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "\`nvm_make_alias\` did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "$EXIT_CODE" -eq 1 ] || die "\`nvm_make_alias\` did not exit with 1, got '$EXIT_CODE'" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "\`nvm_make_alias\` did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" -eq 1 ] || die "\`nvm_make_alias\` did not exit with 1, got '$CAPTURED_EXIT_CODE'" -OUTPUT="$(nvm_make_alias foo 2>&1)" -EXIT_CODE="$(nvm_make_alias foo >/dev/null 2>&1 ; echo $?)" +try_err nvm_make_alias foo EXPECTED_OUTPUT='an alias target version is required' -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "\`nvm_make_alias foo\` did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'" -[ "$EXIT_CODE" -eq 2 ] || die "\`nvm_make_alias foo\` did not exit with 2, got '$EXIT_CODE'" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "\`nvm_make_alias foo\` did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'" +[ "$CAPTURED_EXIT_CODE" -eq 2 ] || die "\`nvm_make_alias foo\` did not exit with 2, got '$CAPTURED_EXIT_CODE'" diff --git a/test/fast/Unit tests/nvm_print_alias_path b/test/fast/Unit tests/nvm_print_alias_path index 29be663..bf0eea9 100755 --- a/test/fast/Unit tests/nvm_print_alias_path +++ b/test/fast/Unit tests/nvm_print_alias_path @@ -24,12 +24,11 @@ nvm_alias() { echo '' } -OUTPUT="$(nvm_print_alias_path "$NVM_ALIAS_DIR" foo | strip_colors)" +try nvm_print_alias_path "$NVM_ALIAS_DIR" foo +OUTPUT="$(echo "$CAPTURED_STDOUT" | strip_colors)" EXPECTED_OUTPUT='' [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\" foo' should produce no output when nvm_alias does not; got '$OUTPUT'" - -EXIT_CODE="$(nvm_print_alias_path "$NVM_ALIAS_DIR" foo >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = '0' ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\" foo' should exit zero when nvm_alias produces no output; got $EXIT_CODE" +[ "$CAPTURED_EXIT_CODE" = '0' ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\" foo' should exit zero when nvm_alias produces no output; got $CAPTURED_EXIT_CODE" nvm_alias() { echo "\"$1\"" diff --git a/test/fast/Unit tests/nvm_print_default_alias b/test/fast/Unit tests/nvm_print_default_alias index f9d8bef..b126f06 100755 --- a/test/fast/Unit tests/nvm_print_default_alias +++ b/test/fast/Unit tests/nvm_print_default_alias @@ -18,12 +18,11 @@ OUTPUT="$(nvm_print_default_alias 2>&1)" EXPECTED_OUTPUT='A default alias is required.' [ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_print_default_alias' produced wrong output; got '$OUTPUT', expected '$EXPECTED_OUTPUT'" -OUTPUT="$(nvm_print_default_alias foo | strip_colors)" +try nvm_print_default_alias foo +OUTPUT="$(echo "$CAPTURED_STDOUT" | strip_colors)" EXPECTED_OUTPUT='' [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_print_default_alias foo' should produce no output when nvm_print_implicit_alias does not; got '$OUTPUT'" - -EXIT_CODE="$(nvm_print_default_alias foo >/dev/null 2>&1 ; echo $?)" -[ "$EXIT_CODE" = '0' ] || die "'nvm_print_default_alias foo' should exit zero when nvm_print_implicit_alias produces no output; got $EXIT_CODE" +[ "$CAPTURED_EXIT_CODE" = '0' ] || die "'nvm_print_default_alias foo' should exit zero when nvm_print_implicit_alias produces no output; got $CAPTURED_EXIT_CODE" nvm_print_implicit_alias() { echo "\"$1-$2\"" diff --git a/test/fast/Unit tests/nvm_print_implicit_alias errors b/test/fast/Unit tests/nvm_print_implicit_alias errors index c142406..ffff6e0 100755 --- a/test/fast/Unit tests/nvm_print_implicit_alias errors +++ b/test/fast/Unit tests/nvm_print_implicit_alias errors @@ -5,15 +5,17 @@ die () { echo "$@" ; exit 1; } : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + EXPECTED_FIRST_MSG="nvm_print_implicit_alias must be specified with local or remote as the first argument." [ "_$(nvm_print_implicit_alias 2>&1)" = "_$EXPECTED_FIRST_MSG" ] \ || die "nvm_print_implicit_alias did not require local|remote as first argument" [ "_$(nvm_print_implicit_alias foo 2>&1)" = "_$EXPECTED_FIRST_MSG" ] \ || die "nvm_print_implicit_alias did not require local|remote as first argument" -FIRST_EXIT_CODE="$(nvm_print_implicit_alias > /dev/null 2>&1 ; echo $?)" -[ "_$FIRST_EXIT_CODE" = "_1" ] \ - || die "nvm_print_implicit_alias without local|remote had wrong exit code: expected 1, got $FIRST_EXIT_CODE" +try_err nvm_print_implicit_alias +[ "_$CAPTURED_EXIT_CODE" = "_1" ] \ + || die "nvm_print_implicit_alias without local|remote had wrong exit code: expected 1, got $CAPTURED_EXIT_CODE" EXPECTED_SECOND_MSG="Only implicit aliases 'stable', 'unstable', 'iojs', and 'node' are supported." [ "_$(nvm_print_implicit_alias local 2>&1)" = "_$EXPECTED_SECOND_MSG" ] \ @@ -21,6 +23,6 @@ EXPECTED_SECOND_MSG="Only implicit aliases 'stable', 'unstable', 'iojs', and 'no [ "_$(nvm_print_implicit_alias local foo 2>&1)" = "_$EXPECTED_SECOND_MSG" ] \ || die "nvm_print_implicit_alias did not require stable|unstable|iojs|node as second argument" -SECOND_EXIT_CODE="$(nvm_print_implicit_alias local > /dev/null 2>&1 ; echo $?)" -[ "_$SECOND_EXIT_CODE" = "_2" ] \ - || die "nvm_print_implicit_alias without stable|unstable|iojs|node had wrong exit code: expected 2, got $SECOND_EXIT_CODE" +try_err nvm_print_implicit_alias local +[ "_$CAPTURED_EXIT_CODE" = "_2" ] \ + || die "nvm_print_implicit_alias without stable|unstable|iojs|node had wrong exit code: expected 2, got $CAPTURED_EXIT_CODE" diff --git a/test/fast/Unit tests/nvm_process_nvmrc b/test/fast/Unit tests/nvm_process_nvmrc index 5423cb3..701477e 100755 --- a/test/fast/Unit tests/nvm_process_nvmrc +++ b/test/fast/Unit tests/nvm_process_nvmrc @@ -12,24 +12,23 @@ cleanup() { \. ../../common.sh for f in ../../../test/fixtures/nvmrc/test/fixtures/valid/*; do - STDOUT="$(nvm_process_nvmrc $f/.nvmrc 2>/dev/null)" - EXIT_CODE="$(nvm_process_nvmrc $f/.nvmrc >/dev/null 2>/dev/null; echo $?)" + try nvm_process_nvmrc $f/.nvmrc EXPECTED="$(nvm_json_extract node < "${f}/expected.json" | tr -d '"')" - [ "${EXIT_CODE}" = "0" ] || die "$(basename "${f}"): expected exit code of 0 but got ${EXIT_CODE}" + [ "${CAPTURED_EXIT_CODE}" = "0" ] || die "$(basename "${f}"): expected exit code of 0 but got ${CAPTURED_EXIT_CODE}" - [ "${STDOUT}" = "${EXPECTED}" ] || die "$(basename "${f}"): expected STDOUT of \`${EXPECTED}\` but got \`${STDOUT}\`" + [ "${CAPTURED_STDOUT}" = "${EXPECTED}" ] || die "$(basename "${f}"): expected STDOUT of \`${EXPECTED}\` but got \`${CAPTURED_STDOUT}\`" done for f in ../../../test/fixtures/nvmrc/test/fixtures/invalid/*; do - STDOUT="$(nvm_process_nvmrc $f/.nvmrc 2>/dev/null)" - STDERR="$(nvm_process_nvmrc $f/.nvmrc 2>&1 >/dev/null | awk '{if(NR > 8) print $0}' | strip_colors)" - EXIT_CODE="$(nvm_process_nvmrc $f/.nvmrc >/dev/null 2>/dev/null; echo $?)" + try nvm_process_nvmrc $f/.nvmrc + try_err nvm_process_nvmrc $f/.nvmrc + STDERR="$(echo "$CAPTURED_STDERR" | awk '{if(NR > 8) print $0}' | strip_colors)" EXPECTED="$(nvm_json_extract < "${f}/expected.json" | tr -d '"')" - [ "${EXIT_CODE}" != "0" ] || die "$(basename "${f}"): expected exit code of 'not 0' but got ${EXIT_CODE}" + [ "${CAPTURED_EXIT_CODE}" != "0" ] || die "$(basename "${f}"): expected exit code of 'not 0' but got ${CAPTURED_EXIT_CODE}" [ "${STDERR}" = "${EXPECTED}" ] || die "$(basename "${f}"): expected STDERR of \`${EXPECTED}\` but got \`${STDERR}\`" done diff --git a/test/fast/Unit tests/nvm_remote_version b/test/fast/Unit tests/nvm_remote_version index 3aa5c11..11ceb98 100755 --- a/test/fast/Unit tests/nvm_remote_version +++ b/test/fast/Unit tests/nvm_remote_version @@ -9,21 +9,21 @@ cleanup() { : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + nvm_ls_remote() { echo "N/A" } -OUTPUT="$(nvm_remote_version foo)" -EXIT_CODE="$(nvm_remote_version foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A" -[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE" +try nvm_remote_version foo +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE" nvm_ls_remote_iojs() { echo "N/A" } -OUTPUT="$(nvm_remote_version iojs-foo)" -EXIT_CODE="$(nvm_remote_version iojs-foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A" -[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE" +try nvm_remote_version iojs-foo +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE" nvm_ls_remote() { @@ -40,41 +40,35 @@ nvm_ls_remote_iojs() { echo "iojs_pattern_received:_$1_" fi } -OUTPUT="$(nvm_remote_version foo)" -EXIT_CODE="$(nvm_remote_version foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_pattern_received:_foo_" ] \ - || die "nvm_remote_version foo did not return last line only of nvm_ls_remote foo; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version foo did not exit with 0, got $EXIT_CODE" +try nvm_remote_version foo +[ "_$CAPTURED_STDOUT" = "_pattern_received:_foo_" ] \ + || die "nvm_remote_version foo did not return last line only of nvm_ls_remote foo; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version foo did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_version iojs-foo)" -EXIT_CODE="$(nvm_remote_version iojs-foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_iojs_pattern_received:_iojs-foo_" ] \ - || die "nvm_remote_version iojs-foo did not return last line only of nvm_ls_remote_iojs foo; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version iojs-foo did not exit with 0, got $EXIT_CODE" +try nvm_remote_version iojs-foo +[ "_$CAPTURED_STDOUT" = "_iojs_pattern_received:_iojs-foo_" ] \ + || die "nvm_remote_version iojs-foo did not return last line only of nvm_ls_remote_iojs foo; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version iojs-foo did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_version iojs)" -EXIT_CODE="$(nvm_remote_version iojs >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_iojs_pattern_received:__" ] \ - || die "nvm_remote_version iojs did not return last line only of nvm_ls_remote_iojs; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version iojs did not exit with 0, got $EXIT_CODE" +try nvm_remote_version iojs +[ "_$CAPTURED_STDOUT" = "_iojs_pattern_received:__" ] \ + || die "nvm_remote_version iojs did not return last line only of nvm_ls_remote_iojs; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version iojs did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_version stable)" -EXIT_CODE="$(nvm_remote_version stable >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$(nvm_ls_remote stable)" ] \ - || die "nvm_remote_version stable did not return contents of nvm_ls_remote stable; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version stable did not exit with 0, got $EXIT_CODE" +try nvm_remote_version stable +[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote stable)" ] \ + || die "nvm_remote_version stable did not return contents of nvm_ls_remote stable; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version stable did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_version unstable)" -EXIT_CODE="$(nvm_remote_version unstable >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$(nvm_ls_remote unstable)" ] \ - || die "nvm_remote_version unstable did not return contents of nvm_ls_remote unstable; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version unstable did not exit with 0, got $EXIT_CODE" +try nvm_remote_version unstable +[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote unstable)" ] \ + || die "nvm_remote_version unstable did not return contents of nvm_ls_remote unstable; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version unstable did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_version node)" -EXIT_CODE="$(nvm_remote_version node >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$(nvm_ls_remote node)" ] \ - || die "nvm_remote_version node did not return contents of nvm_ls_remote node; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version node did not exit with 0, got $EXIT_CODE" +try nvm_remote_version node +[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote node)" ] \ + || die "nvm_remote_version node did not return contents of nvm_ls_remote node; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version node did not exit with 0, got $CAPTURED_EXIT_CODE" # Test LTS name rejection (Issue #3474) # When nvm_remote_versions returns a line with LTS name in description, @@ -83,17 +77,15 @@ EXIT_CODE="$(nvm_remote_version node >/dev/null 2>&1 ; echo $?)" nvm_remote_versions() { echo "v4.9.1 Argon *" } -OUTPUT="$(nvm_remote_version Argon)" -EXIT_CODE="$(nvm_remote_version Argon >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nvm_remote_version Argon should return N/A (LTS name not in version), got $OUTPUT" -[ "_$EXIT_CODE" = "_3" ] || die "nvm_remote_version Argon should exit with code 3, got $EXIT_CODE" +try nvm_remote_version Argon +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nvm_remote_version Argon should return N/A (LTS name not in version), got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nvm_remote_version Argon should exit with code 3, got $CAPTURED_EXIT_CODE" nvm_remote_versions() { echo "v4.9.1" } -OUTPUT="$(nvm_remote_version 4)" -EXIT_CODE="$(nvm_remote_version 4 >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_v4.9.1" ] || die "nvm_remote_version 4 should return v4.9.1, got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version 4 should exit with code 0, got $EXIT_CODE" +try nvm_remote_version 4 +[ "_$CAPTURED_STDOUT" = "_v4.9.1" ] || die "nvm_remote_version 4 should return v4.9.1, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version 4 should exit with code 0, got $CAPTURED_EXIT_CODE" cleanup diff --git a/test/fast/Unit tests/nvm_remote_versions b/test/fast/Unit tests/nvm_remote_versions index 02a0464..ba3f718 100755 --- a/test/fast/Unit tests/nvm_remote_versions +++ b/test/fast/Unit tests/nvm_remote_versions @@ -9,33 +9,31 @@ cleanup() { : nvm.sh \. ../../../nvm.sh -OUTPUT="$(nvm_remote_versions stable 2>&1)" -EXPECTED_OUTPUT="Implicit aliases are not supported in nvm_remote_versions." -EXIT_CODE="$(nvm_remote_versions stable >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'stable' did not error out with correct message, got $OUTPUT" -[ "_$EXIT_CODE" = "_1" ] || die "implicit alias 'stable' did not exit with code 1, got $EXIT_CODE" +\. ../../common.sh -OUTPUT="$(nvm_remote_versions unstable 2>&1)" +try_err nvm_remote_versions stable EXPECTED_OUTPUT="Implicit aliases are not supported in nvm_remote_versions." -EXIT_CODE="$(nvm_remote_versions unstable >/dev/null 2>&1; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'unstable' did not error out with correct message, got $OUTPUT" -[ "_$EXIT_CODE" = "_1" ] || die "implicit alias 'unstable' did not exit with code 1, got $EXIT_CODE" +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'stable' did not error out with correct message, got $CAPTURED_STDERR" +[ "_$CAPTURED_EXIT_CODE" = "_1" ] || die "implicit alias 'stable' did not exit with code 1, got $CAPTURED_EXIT_CODE" + +try_err nvm_remote_versions unstable +EXPECTED_OUTPUT="Implicit aliases are not supported in nvm_remote_versions." +[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'unstable' did not error out with correct message, got $CAPTURED_STDERR" +[ "_$CAPTURED_EXIT_CODE" = "_1" ] || die "implicit alias 'unstable' did not exit with code 1, got $CAPTURED_EXIT_CODE" nvm_ls_remote() { echo "N/A" } -OUTPUT="$(nvm_remote_versions foo)" -EXIT_CODE="$(nvm_remote_versions foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A" -[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE" +try nvm_remote_versions foo +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE" nvm_ls_remote_iojs() { echo "N/A" } -OUTPUT="$(nvm_remote_versions iojs-foo)" -EXIT_CODE="$(nvm_remote_versions iojs-foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A" -[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE" +try nvm_remote_versions iojs-foo +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE" nvm_ls_remote() { @@ -49,30 +47,26 @@ nvm_ls_remote_iojs() { echo "iojs pattern received: _$1_" } -OUTPUT="$(nvm_remote_versions foo)" -EXIT_CODE="$(nvm_remote_versions foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$(nvm_ls_remote foo) +try nvm_remote_versions foo +[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote foo) $(nvm_ls_remote_iojs foo)" ] \ - || die "nvm_remote_versions foo did not return contents of nvm_ls_remote foo combined with nvm_ls_remote_iojs foo; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions foo did not exit with 0, got $EXIT_CODE" + || die "nvm_remote_versions foo did not return contents of nvm_ls_remote foo combined with nvm_ls_remote_iojs foo; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions foo did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_versions node)" -EXIT_CODE="$(nvm_remote_versions node >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$(nvm_ls_remote)" ] \ - || die "nvm_remote_versions node did not return contents of nvm_ls_remote; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions node did not exit with 0, got $EXIT_CODE" +try nvm_remote_versions node +[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote)" ] \ + || die "nvm_remote_versions node did not return contents of nvm_ls_remote; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions node did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_versions iojs-foo)" -EXIT_CODE="$(nvm_remote_versions iojs-foo >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$(nvm_ls_remote iojs-foo) +try nvm_remote_versions iojs-foo +[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote iojs-foo) $(nvm_ls_remote_iojs iojs-foo)" ] \ - || die "nvm_remote_versions iojs-foo did not return contents of nvm_ls_remote iojs-foo combined with nvm_ls_remote_iojs iojs-foo; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs-foo did not exit with 0, got $EXIT_CODE" + || die "nvm_remote_versions iojs-foo did not return contents of nvm_ls_remote iojs-foo combined with nvm_ls_remote_iojs iojs-foo; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs-foo did not exit with 0, got $CAPTURED_EXIT_CODE" -OUTPUT="$(nvm_remote_versions iojs)" -EXIT_CODE="$(nvm_remote_versions iojs >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$(nvm_ls_remote_iojs)" ] \ - || die "nvm_remote_versions iojs did not return contents of nvm_ls_remote_iojs; got $OUTPUT" -[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs did not exit with 0, got $EXIT_CODE" +try nvm_remote_versions iojs +[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote_iojs)" ] \ + || die "nvm_remote_versions iojs did not return contents of nvm_ls_remote_iojs; got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs did not exit with 0, got $CAPTURED_EXIT_CODE" cleanup diff --git a/test/fast/Unit tests/nvm_validate_implicit_alias b/test/fast/Unit tests/nvm_validate_implicit_alias index 961fb31..8c35fed 100755 --- a/test/fast/Unit tests/nvm_validate_implicit_alias +++ b/test/fast/Unit tests/nvm_validate_implicit_alias @@ -5,15 +5,17 @@ die () { echo "$@" ; exit 1; } : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + EXPECTED_MSG="Only implicit aliases 'stable', 'unstable', 'iojs', and 'node' are supported." [ "_$(nvm_validate_implicit_alias 2>&1)" = "_$EXPECTED_MSG" ] \ || die "nvm_validate_implicit_alias did not require stable|unstable|iojs|node" [ "_$(nvm_validate_implicit_alias foo 2>&1)" = "_$EXPECTED_MSG" ] \ || die "nvm_validate_implicit_alias did not require stable|unstable|iojs|node" -EXIT_CODE="$(nvm_validate_implicit_alias >/dev/null 2>&1 ; echo $?)" -[ "_$EXIT_CODE" = "_1" ] \ - || die "nvm_validate_implicit_alias without stable|unstable|iojs|node had wrong exit code: expected 1, got $EXIT_CODE" +try_err nvm_validate_implicit_alias +[ "_$CAPTURED_EXIT_CODE" = "_1" ] \ + || die "nvm_validate_implicit_alias without stable|unstable|iojs|node had wrong exit code: expected 1, got $CAPTURED_EXIT_CODE" nvm_validate_implicit_alias stable || die "nvm_validate_implicit_alias stable did not exit 0" nvm_validate_implicit_alias unstable || die "nvm_validate_implicit_alias unstable did not exit 0" diff --git a/test/fast/Unit tests/nvm_version b/test/fast/Unit tests/nvm_version index 49fc5d0..9334f40 100755 --- a/test/fast/Unit tests/nvm_version +++ b/test/fast/Unit tests/nvm_version @@ -8,22 +8,22 @@ cleanup () { : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + nvm_ls_current() { echo "CURRENT!" return 7 } -OUTPUT="$(nvm_version current)" +try nvm_version current EXPECTED_OUTPUT="CURRENT!" -EXIT_CODE="$(nvm_version current 2>&1 >/dev/null ; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version current" did not return nvm_ls_current output' -[ "_$EXIT_CODE" = "_7" ] || die '"nvm_version current" did not return nvm_ls_current exit code' +[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version current" did not return nvm_ls_current output' +[ "_$CAPTURED_EXIT_CODE" = "_7" ] || die '"nvm_version current" did not return nvm_ls_current exit code' -OUTPUT="$(nvm_version)" +try nvm_version EXPECTED_OUTPUT="CURRENT!" -EXIT_CODE="$(nvm_version 2>&1 >/dev/null ; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version" did not return nvm_ls_current output' -[ "_$EXIT_CODE" = "_7" ] || die '"nvm_version" did not return nvm_ls_current exit code' +[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version" did not return nvm_ls_current output' +[ "_$CAPTURED_EXIT_CODE" = "_7" ] || die '"nvm_version" did not return nvm_ls_current exit code' nvm_ls() { echo "line 1" @@ -38,15 +38,13 @@ nvm_ls() { echo "system v20.0.0"; } [ "_$(nvm_version system)" = "_system" ] || die '"nvm_version system" did not return "system" when "nvm_ls" returns extra columns' nvm_ls() { echo "N/A"; } -OUTPUT="$(nvm_version foo)" +try nvm_version foo EXPECTED_OUTPUT="N/A" -EXIT_CODE="$(nvm_version foo 2>&1 >/dev/null ; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version" did not return N/A when nvm_ls returns N/A' -[ "_$EXIT_CODE" = "_3" ] || die '"nvm_version" returning N/A did not exit code with code 3' +[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version" did not return N/A when nvm_ls returns N/A' +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die '"nvm_version" returning N/A did not exit code with code 3' nvm_ls() { echo; } -OUTPUT="$(nvm_version foo)" +try nvm_version foo EXPECTED_OUTPUT="N/A" -EXIT_CODE="$(nvm_version foo 2>&1 >/dev/null ; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version" did not return N/A when nvm_ls returns nothing' -[ "_$EXIT_CODE" = "_3" ] || die '"nvm_version" returning N/A did not exit code with code 3' +[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version" did not return N/A when nvm_ls returns nothing' +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die '"nvm_version" returning N/A did not exit code with code 3' diff --git a/test/slow/nvm use/Running 'nvm use' calls 'nvm_die_on_prefix' b/test/slow/nvm use/Running 'nvm use' calls 'nvm_die_on_prefix' index 2fe0de7..1a3d74a 100755 --- a/test/slow/nvm use/Running 'nvm use' calls 'nvm_die_on_prefix' +++ b/test/slow/nvm use/Running 'nvm use' calls 'nvm_die_on_prefix' @@ -4,6 +4,8 @@ die () { echo "$@" ; exit 1; } \. ../../../nvm.sh +\. ../../common.sh + nvm deactivate >/dev/null 2>&1 || die 'deactivate failed' nvm_die_on_prefix() { @@ -11,12 +13,12 @@ nvm_die_on_prefix() { return 3 } -OUTPUT="$(nvm use --silent node)" +try nvm use --silent node EXPECTED_OUTPUT="" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \ - || die "'nvm use --silent node' did not call through to 'nvm_die_on_prefix' and give output '$EXPECTED_OUTPUT'; got '$OUTPUT'" +[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] \ + || die "'nvm use --silent node' did not call through to 'nvm_die_on_prefix' and give output '$EXPECTED_OUTPUT'; got '$CAPTURED_STDOUT'" -EXIT_CODE="$(nvm use --silent node >/dev/null 2>&1; echo $?)" +try_err nvm use --silent node EXPECTED_CODE="11" -[ "_$EXIT_CODE" = "_$EXPECTED_CODE" ] \ - || die "'nvm use --silent node' when 'nvm_die_on_prefix' fails did not return '$EXPECTED_CODE'; got '$EXIT_CODE'" +[ "_$CAPTURED_EXIT_CODE" = "_$EXPECTED_CODE" ] \ + || die "'nvm use --silent node' when 'nvm_die_on_prefix' fails did not return '$EXPECTED_CODE'; got '$CAPTURED_EXIT_CODE'" diff --git a/test/slow/nvm_get_latest/nvm_get_latest b/test/slow/nvm_get_latest/nvm_get_latest index 32b85e7..870c6c2 100755 --- a/test/slow/nvm_get_latest/nvm_get_latest +++ b/test/slow/nvm_get_latest/nvm_get_latest @@ -90,12 +90,13 @@ wget() { fi } -OUTPUT="$(nvm_get_latest)" -EXIT_CODE="$(nvm_get_latest >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_$EXPECTED_VERSION" ] \ - || die "success path did not return version '$EXPECTED_VERSION', got '$OUTPUT'" -[ "_$EXIT_CODE" = "_0" ] \ - || die "success path did not exit with code 0, got $EXIT_CODE" +\. ../../common.sh + +try nvm_get_latest +[ "_$CAPTURED_STDOUT" = "_$EXPECTED_VERSION" ] \ + || die "success path did not return version '$EXPECTED_VERSION', got '$CAPTURED_STDOUT'" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] \ + || die "success path did not exit with code 0, got $CAPTURED_EXIT_CODE" cleanup diff --git a/test/slow/nvm_get_latest/nvm_get_latest failed redirect b/test/slow/nvm_get_latest/nvm_get_latest failed redirect index 2548b06..f03cf6f 100755 --- a/test/slow/nvm_get_latest/nvm_get_latest failed redirect +++ b/test/slow/nvm_get_latest/nvm_get_latest failed redirect @@ -8,6 +8,8 @@ cleanup() { \. ../../../nvm.sh +\. ../../common.sh + curl() { return 1 } @@ -15,11 +17,10 @@ wget() { return 1 } -OUTPUT="$(nvm_get_latest 2>&1)" -EXIT_CODE="$(nvm_get_latest >/dev/null 2>&1 ; echo $?)" -[ "_$OUTPUT" = "_https://latest.nvm.sh did not redirect to the latest release on GitHub" ] \ - || die "failed redirect did not report correct error message, got '$OUTPUT'" -[ "_$EXIT_CODE" = "_2" ] \ - || die "failed redirect did not exit with code 2, got $EXIT_CODE" +try_err nvm_get_latest +[ "_$CAPTURED_STDERR" = "_https://latest.nvm.sh did not redirect to the latest release on GitHub" ] \ + || die "failed redirect did not report correct error message, got '$CAPTURED_STDERR'" +[ "_$CAPTURED_EXIT_CODE" = "_2" ] \ + || die "failed redirect did not exit with code 2, got $CAPTURED_EXIT_CODE" cleanup