[Tests] nvm_print_color_legend: look up the function with PATH emptied

The presence checks were still satisfiable by this test file itself,
which is an executable named `nvm_print_color_legend`.
Filtering `command -v` output on a `/` was not enough:
dash and zsh report a bare name,
not a path,
for a current-directory match reached through an empty `PATH` component,
and that is indistinguishable from a function.
So `nvm unload` looked as though it had left the function behind in exactly the shells CI runs as `sh`, `dash`, and `zsh`,
while bash, which reports `./name`, passed.

Emptying `PATH` for the lookup asks about the function alone,
and is immune to whatever `PATH` happens to hold.

Reproduced first, then fixed:
with `PATH=":${PATH}"` the previous check fails under dash and zsh
and passes under bash, matching CI exactly.
The assertions now hold in sh, bash, dash, and zsh
with the directory absent from `PATH`, present on it, and via an empty entry,
and both dropping `nvm_print_color_legend` from `nvm unload`
and renaming the function out from under the guard
are still caught in all twelve combinations.
This commit is contained in:
Jordan Harband
2026-09-03 15:12:34 -07:00
parent 812a76bb41
commit 38f0f2cfdf
+6 -10
View File
@@ -27,17 +27,13 @@ ${1}
return 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.
# This test file is itself an executable named `nvm_print_color_legend`, so a
# plain `command -v` keeps finding it long after the function is gone. Emptying
# `PATH` for the lookup is the only reliable way to ask about the function
# alone: filtering on a `/` is not enough, because dash and zsh report a bare
# name for a current-directory match reached through an empty `PATH` component.
is_function () {
case "$(command -v "${1}" 2>/dev/null)" in
'') return 1 ;;
*/*) return 1 ;;
*) return 0 ;;
esac
[ -n "$(PATH=/nonexistent command -v "${1}" 2>/dev/null)" ]
}
is_function nvm_print_color_legend \