[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

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