[Tests] add try/try_err helpers; convert tests to use them

Add `try` and `try_err` helper functions to `test/common.sh` that capture stdout/stderr and exit code from a single invocation, eliminating duplicate command executions in tests.
Convert all existing tests that used the `OUTPUT`/`EXIT_CODE` double-invocation pattern to use the new helpers.
Also fixes a pre-existing bug in the `nvm_die_on_prefix` test where ASCII apostrophes were used instead of U+2019 to match nvm.sh output.
This commit is contained in:
Jordan Harband
2026-03-13 15:26:07 -04:00
parent 4c556a19b0
commit 14d01c6877
28 changed files with 329 additions and 365 deletions

View File

@@ -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