From 02e1d4a293139ecf2c94206ee9c00b388550366e Mon Sep 17 00:00:00 2001 From: Alex Hedges Date: Sun, 25 Feb 2024 16:07:52 -0500 Subject: [PATCH] Handle case where `pyenv-commands --sh` returns nothing (#2908) In exceptional cases (custom installation, malfunctions elsewhere), `pyenv-commands --sh` may return nothing. In non-Fish, this would cause "syntax error near unexpected token `)'" in `pyenv()`. Bash does not allow to specify a `case` option that would never match. This works around it by defaulting to `/`. Commands, being filenames, can never match it. In Fish, nothing needs to be done: it apparently does interpret a `case` without argument as one that never matches. --- libexec/pyenv-init | 2 +- test/init.bats | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libexec/pyenv-init b/libexec/pyenv-init index a875bb31..492360e6 100755 --- a/libexec/pyenv-init +++ b/libexec/pyenv-init @@ -297,7 +297,7 @@ EOS fi case "\$command" in - ${commands[*]}) + ${commands[*]:-/}) eval "\$(pyenv "sh-\$command" "\$@")" ;; *) diff --git a/test/init.bats b/test/init.bats index 21109c94..53d131cc 100755 --- a/test/init.bats +++ b/test/init.bats @@ -2,6 +2,18 @@ load test_helper +setup() { + export PATH="${PYENV_TEST_DIR}/bin:$PATH" +} + +create_executable() { + local name="$1" + local bin="${PYENV_TEST_DIR}/bin" + mkdir -p "$bin" + sed -Ee '1s/^ +//' > "${bin}/$name" + chmod +x "${bin}/$name" +} + @test "creates shims and versions directories" { assert [ ! -d "${PYENV_ROOT}/shims" ] assert [ ! -d "${PYENV_ROOT}/versions" ] @@ -167,6 +179,24 @@ echo "\$PATH" assert_line ' case "$command" in' } +@test "outputs sh-compatible case syntax" { + create_executable pyenv-commands <