mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-04-03 19:44: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,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"
|
||||
|
||||
Reference in New Issue
Block a user