Files
nvm/test/fast/Unit tests/nvm_print_alias_path
Jordan Harband 14d01c6877 [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.
2026-03-13 15:26:07 -04:00

45 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
\. ../../common.sh
die () { echo "$@" ; cleanup ; exit 1; }
cleanup () {
unset -f nvm_alias nvm_version
}
: nvm.sh
\. ../../../nvm.sh
NVM_ALIAS_DIR='path/to/the alias/dir'
OUTPUT="$(nvm_print_alias_path 2>&1)"
EXPECTED_OUTPUT='An alias dir is required.'
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_print_alias_path' produced wrong output; got '$OUTPUT', expected '$EXPECTED_OUTPUT'"
OUTPUT="$(nvm_print_alias_path "$NVM_ALIAS_DIR" 2>&1)"
EXPECTED_OUTPUT='An alias path is required.'
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\"' produced wrong output; got '$OUTPUT', expected '$EXPECTED_OUTPUT'"
nvm_alias() {
echo ''
}
try nvm_print_alias_path "$NVM_ALIAS_DIR" foo
OUTPUT="$(echo "$CAPTURED_STDOUT" | strip_colors)"
EXPECTED_OUTPUT=''
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\" foo' should produce no output when nvm_alias does not; got '$OUTPUT'"
[ "$CAPTURED_EXIT_CODE" = '0' ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\" foo' should exit zero when nvm_alias produces no output; got $CAPTURED_EXIT_CODE"
nvm_alias() {
echo "\"$1\""
}
nvm_version() {
echo "v$1"
}
OUTPUT="$(nvm_print_alias_path "$NVM_ALIAS_DIR" "$NVM_ALIAS_DIR/blah" | strip_colors)"
EXPECTED_OUTPUT='blah -> "blah" (-> v"blah" *)'
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\" \"\$NVM_ALIAS_DIR/blah\"' should strip alias dir and print nvm_alias output; got '$OUTPUT', expected '$EXPECTED_OUTPUT'"
cleanup