mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-04-03 11:34:50 +08:00
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.
35 lines
1.1 KiB
Bash
Executable File
35 lines
1.1 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
die () { echo "$@" ; cleanup ; exit 1; }
|
|
|
|
cleanup() {
|
|
echo 'cleaned up'
|
|
}
|
|
|
|
: nvm.sh
|
|
\. ../../../nvm.sh
|
|
|
|
\. ../../common.sh
|
|
|
|
for f in ../../../test/fixtures/nvmrc/test/fixtures/valid/*; do
|
|
try nvm_process_nvmrc $f/.nvmrc
|
|
|
|
EXPECTED="$(nvm_json_extract node < "${f}/expected.json" | tr -d '"')"
|
|
|
|
[ "${CAPTURED_EXIT_CODE}" = "0" ] || die "$(basename "${f}"): expected exit code of 0 but got ${CAPTURED_EXIT_CODE}"
|
|
|
|
[ "${CAPTURED_STDOUT}" = "${EXPECTED}" ] || die "$(basename "${f}"): expected STDOUT of \`${EXPECTED}\` but got \`${CAPTURED_STDOUT}\`"
|
|
done
|
|
|
|
for f in ../../../test/fixtures/nvmrc/test/fixtures/invalid/*; do
|
|
try nvm_process_nvmrc $f/.nvmrc
|
|
try_err nvm_process_nvmrc $f/.nvmrc
|
|
STDERR="$(echo "$CAPTURED_STDERR" | awk '{if(NR > 8) print $0}' | strip_colors)"
|
|
|
|
EXPECTED="$(nvm_json_extract < "${f}/expected.json" | tr -d '"')"
|
|
|
|
[ "${CAPTURED_EXIT_CODE}" != "0" ] || die "$(basename "${f}"): expected exit code of 'not 0' but got ${CAPTURED_EXIT_CODE}"
|
|
|
|
[ "${STDERR}" = "${EXPECTED}" ] || die "$(basename "${f}"): expected STDERR of \`${EXPECTED}\` but got \`${STDERR}\`"
|
|
done
|