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.
This commit is contained in:
Alex Hedges 2024-02-25 16:07:52 -05:00 committed by GitHub
parent f9a2bb81b6
commit 02e1d4a293
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View file

@ -297,7 +297,7 @@ EOS
fi fi
case "\$command" in case "\$command" in
${commands[*]}) ${commands[*]:-/})
eval "\$(pyenv "sh-\$command" "\$@")" eval "\$(pyenv "sh-\$command" "\$@")"
;; ;;
*) *)

View file

@ -2,6 +2,18 @@
load test_helper 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" { @test "creates shims and versions directories" {
assert [ ! -d "${PYENV_ROOT}/shims" ] assert [ ! -d "${PYENV_ROOT}/shims" ]
assert [ ! -d "${PYENV_ROOT}/versions" ] assert [ ! -d "${PYENV_ROOT}/versions" ]
@ -167,6 +179,24 @@ echo "\$PATH"
assert_line ' case "$command" in' assert_line ' case "$command" in'
} }
@test "outputs sh-compatible case syntax" {
create_executable pyenv-commands <<!
#!$BASH
echo -e 'activate\ndeactivate\nrehash\nshell'
!
run pyenv-init - bash
assert_success
assert_line ' activate|deactivate|rehash|shell)'
create_executable pyenv-commands <<!
#!$BASH
echo
!
run pyenv-init - bash
assert_success
assert_line ' /)'
}
@test "outputs fish-specific syntax (fish)" { @test "outputs fish-specific syntax (fish)" {
run pyenv-init - fish run pyenv-init - fish
assert_success assert_success