From 38f0f2cfdf73a5ab131c0da6299cf42c0b814ef4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 3 Sep 2026 15:12:34 -0700 Subject: [PATCH] [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. --- test/fast/Unit tests/nvm_print_color_legend | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/test/fast/Unit tests/nvm_print_color_legend b/test/fast/Unit tests/nvm_print_color_legend index 3a50935c..b82cc8ab 100755 --- a/test/fast/Unit tests/nvm_print_color_legend +++ b/test/fast/Unit tests/nvm_print_color_legend @@ -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 \