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.
57 lines
1.1 KiB
Bash
Executable File
57 lines
1.1 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
\. ../../common.sh
|
|
|
|
die () { echo "$@" ; exit 1; }
|
|
|
|
: nvm.sh
|
|
\. ../../../nvm.sh
|
|
|
|
nvm deactivate >/dev/null 2>&1
|
|
|
|
CURRENT="$(nvm current)"
|
|
[ "$CURRENT" = 'none' ] || [ "$CURRENT" = 'system' ] || die "nvm should be using none or system; got $CURRENT"
|
|
|
|
nvm_ls_current() {
|
|
echo 'none'
|
|
}
|
|
node() {
|
|
return 1
|
|
}
|
|
npm() {
|
|
echo '1.2.3'
|
|
}
|
|
|
|
try_err nvm_install_latest_npm
|
|
|
|
EXPECTED="Unable to obtain node version."
|
|
[ "${CAPTURED_STDERR}" = "${EXPECTED}" ] || die "When node is unavailable, expected >${EXPECTED}<; got >${CAPTURED_STDERR}"
|
|
|
|
node() {
|
|
echo 'v4.5.6'
|
|
}
|
|
nvm_ls_current() {
|
|
node --version
|
|
}
|
|
npm() {
|
|
return 1
|
|
}
|
|
try_err nvm_install_latest_npm
|
|
|
|
EXPECTED="Unable to obtain npm version."
|
|
[ "${CAPTURED_STDERR}" = "${EXPECTED}" ] || die "When node is available and npm is unavailable, expected >${EXPECTED}<; got >${CAPTURED_STDERR}"
|
|
|
|
node() {
|
|
echo 'v4.5.6'
|
|
}
|
|
nvm_ls_current() {
|
|
echo 'system'
|
|
}
|
|
npm() {
|
|
return 1
|
|
}
|
|
try_err nvm_install_latest_npm
|
|
|
|
EXPECTED="Unable to obtain npm version."
|
|
[ "${CAPTURED_STDERR}" = "${EXPECTED}" ] || die "When node is system and npm is unavailable, expected >${EXPECTED}<; got >${CAPTURED_STDERR}"
|