Skip to content
Myr

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

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

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.

authored by Jordan Harband · committed by Jordan Harband · parent 812a76b · Browse files · 1 file+6 added−10 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+6 added−10 removed

  • tests:+6 added−10 removed

1 file+6 added−10 removed

modified:test/fast/Unit tests/nvm_print_color_legend+6 added-10 removed
25 unmodified lines
26 esac27 return 128}2930# `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.30# This test file is itself an executable named `nvm_print_color_legend`, so a31# plain `command -v` keeps finding it long after the function is gone. Emptying32# `PATH` for the lookup is the only reliable way to ask about the function33# alone: filtering on a `/` is not enough, because dash and zsh report a bare34# name for a current-directory match reached through an empty `PATH` component.35is_function () {36 case "$(command -v "${1}" 2>/dev/null)" in37 '') return 1 ;;38 */*) return 1 ;;39 *) return 0 ;;40 esac36 [ -n "$(PATH=/nonexistent command -v "${1}" 2>/dev/null)" ]37}3839is_function nvm_print_color_legend \40 || die 'nvm_print_color_legend is not defined'
60 unmodified lines