mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-04-08 14:04:52 +08:00
[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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user