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