mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-09-19 00:00:09 +08:00
[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:
@@ -27,17 +27,13 @@ ${1}
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# `command -v` reports a bare name for a shell function but a path for an
|
# This test file is itself an executable named `nvm_print_color_legend`, so a
|
||||||
# executable, and this test file is itself an executable named
|
# plain `command -v` keeps finding it long after the function is gone. Emptying
|
||||||
# `nvm_print_color_legend`. Whenever its directory is on PATH, as it is under
|
# `PATH` for the lookup is the only reliable way to ask about the function
|
||||||
# CI, a plain `command -v` keeps finding the file long after the function is
|
# alone: filtering on a `/` is not enough, because dash and zsh report a bare
|
||||||
# gone, so the presence checks below have to insist on the function.
|
# name for a current-directory match reached through an empty `PATH` component.
|
||||||
is_function () {
|
is_function () {
|
||||||
case "$(command -v "${1}" 2>/dev/null)" in
|
[ -n "$(PATH=/nonexistent command -v "${1}" 2>/dev/null)" ]
|
||||||
'') return 1 ;;
|
|
||||||
*/*) return 1 ;;
|
|
||||||
*) return 0 ;;
|
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
is_function nvm_print_color_legend \
|
is_function nvm_print_color_legend \
|
||||||
|
|||||||
Reference in New Issue
Block a user