From 26fef4fe1a033e5da8a114b85cc4d8e5acc66367 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 3 Sep 2026 12:08:18 -0700 Subject: [PATCH] [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. --- test/fast/Unit tests/nvm_print_color_legend | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/fast/Unit tests/nvm_print_color_legend b/test/fast/Unit tests/nvm_print_color_legend index 53474174..3a50935c 100755 --- a/test/fast/Unit tests/nvm_print_color_legend +++ b/test/fast/Unit tests/nvm_print_color_legend @@ -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