[Tests] nvm_print_color_legend: check for the function, not a same-named file

The presence checks used `command -v`,
which finds executables as readily as functions,
and this test file is itself an executable named `nvm_print_color_legend`.
Whenever its own directory is on `PATH`, as it is under CI,
`command -v` kept resolving to the file
long after `nvm unload` had removed the function,
so the unload assertion could never pass there
and the "is it defined" guard could never fail.

`command -v` prints a bare name for a function
and a path for an executable,
in sh, bash, dash, zsh, and ksh alike,
so requiring the result to contain no slash
distinguishes the two.

Verified both ways round:
with the test's directory on `PATH`
the suite passes in all four shells,
and removing `nvm_print_color_legend` from `nvm unload`'s list
is still caught in all four.
This commit is contained in:
Jordan Harband
2026-09-03 12:08:18 -07:00
parent 17f12a1207
commit 26fef4fe1a
+15 -2
View File
@@ -27,7 +27,20 @@ ${1}
return 1
}
command -v nvm_print_color_legend >/dev/null 2>&1 \
# `command -v` reports a bare name for a shell function but a path for an
# executable, and this test file is itself an executable named
# `nvm_print_color_legend`. Whenever its directory is on PATH, as it is under
# CI, a plain `command -v` keeps finding the file long after the function is
# gone, so the presence checks below have to insist on the function.
is_function () {
case "$(command -v "${1}" 2>/dev/null)" in
'') return 1 ;;
*/*) return 1 ;;
*) return 0 ;;
esac
}
is_function nvm_print_color_legend \
|| die 'nvm_print_color_legend is not defined'
OUTPUT="$(nvm_print_color_legend)"
@@ -77,7 +90,7 @@ UNLOAD_OUT="$(
nvm unload >/dev/null 2>&1
# plain `echo`: `nvm unload` has just unset `nvm_echo`
echo "unload_rc=$?"
if command -v nvm_print_color_legend >/dev/null 2>&1; then
if is_function nvm_print_color_legend; then
echo present
else
echo gone