Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Jordan Harband · committed by Jordan Harband · parent 17f12a1 · Browse files · 1 file+15 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−2 removed

  • tests:+15 added−2 removed

1 file+15 added−2 removed

modified:test/fast/Unit tests/nvm_print_color_legend+15 added-2 removed
25 unmodified lines
26 esac27 return 128}2930command -v nvm_print_color_legend >/dev/null 2>&1 \30# `command -v` reports a bare name for a shell function but a path for an31# executable, and this test file is itself an executable named32# `nvm_print_color_legend`. Whenever its directory is on PATH, as it is under33# CI, a plain `command -v` keeps finding the file long after the function is34# gone, so the presence checks below have to insist on the function.35is_function () {36 case "$(command -v "${1}" 2>/dev/null)" in37 '') return 1 ;;38 */*) return 1 ;;39 *) return 0 ;;40 esac41}4243is_function nvm_print_color_legend \44 || die 'nvm_print_color_legend is not defined'4546OUTPUT="$(nvm_print_color_legend)"47
41 unmodified lines
89 set +e90 nvm unload >/dev/null 2>&191 # plain `echo`: `nvm unload` has just unset `nvm_echo`92 echo "unload_rc=$?"80 if command -v nvm_print_color_legend >/dev/null 2>&1; then93 if is_function nvm_print_color_legend; then94 echo present95 else96 echo gone97 fi
7 unmodified lines