Fix pyenv which to support auto-resolved prefixes (#2601)

* Resolve version name that hooks see
* Avoid a 2nd iteration over configured versions
This commit is contained in:
native-api 2023-01-22 18:22:26 +03:00 committed by GitHub
parent f1a1f59c87
commit c20fc7bd6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 11 deletions

View file

@ -47,12 +47,20 @@ OLDIFS="$IFS"
IFS=: versions=(${PYENV_VERSION:-$(pyenv-version-name)}) IFS=: versions=(${PYENV_VERSION:-$(pyenv-version-name)})
IFS="$OLDIFS" IFS="$OLDIFS"
declare -a nonexistent_versions
for version in "${versions[@]}" "$system"; do for version in "${versions[@]}" "$system"; do
if [ "$version" = "system" ]; then if [ "$version" = "system" ]; then
PATH="$(remove_from_path "${PYENV_ROOT}/shims")" PATH="$(remove_from_path "${PYENV_ROOT}/shims")"
PYENV_COMMAND_PATH="$(command -v "$PYENV_COMMAND" || true)" PYENV_COMMAND_PATH="$(command -v "$PYENV_COMMAND" || true)"
else else
PYENV_COMMAND_PATH="${PYENV_ROOT}/versions/${version}/bin/${PYENV_COMMAND}" # $version may be a prefix to be resolved by pyenv-latest
version_path="$(pyenv-prefix "${version}" 2>/dev/null)" || \
{ nonexistent_versions+=("$version"); continue; }
# resolve $version for hooks
version="$(basename "$version_path")"
PYENV_COMMAND_PATH="$version_path/bin/${PYENV_COMMAND}"
unset version_path
fi fi
if [ -x "$PYENV_COMMAND_PATH" ]; then if [ -x "$PYENV_COMMAND_PATH" ]; then
break break
@ -69,17 +77,10 @@ done
if [ -x "$PYENV_COMMAND_PATH" ]; then if [ -x "$PYENV_COMMAND_PATH" ]; then
echo "$PYENV_COMMAND_PATH" echo "$PYENV_COMMAND_PATH"
else else
any_not_installed=0 if (( ${#nonexistent_versions[@]} )); then
for version in "${versions[@]}"; do for version in "${nonexistent_versions[@]}"; do
if [ "$version" = "system" ]; then
continue
fi
if ! [ -d "${PYENV_ROOT}/versions/${version}" ]; then
echo "pyenv: version \`$version' is not installed (set by $(pyenv-version-origin))" >&2 echo "pyenv: version \`$version' is not installed (set by $(pyenv-version-origin))" >&2
any_not_installed=1
fi
done done
if [ "$any_not_installed" = 1 ]; then
exit 1 exit 1
fi fi

View file

@ -136,3 +136,22 @@ SH
PYENV_VERSION= run pyenv-which python PYENV_VERSION= run pyenv-which python
assert_success "${PYENV_ROOT}/versions/3.4/bin/python" assert_success "${PYENV_ROOT}/versions/3.4/bin/python"
} }
@test "resolves pyenv-latest prefixes" {
create_executable "3.4.2" "python"
PYENV_VERSION=3.4 run pyenv-which python
assert_success "${PYENV_ROOT}/versions/3.4.2/bin/python"
}
@test "hooks get resolved version name" {
create_hook which echo.bash <<!
echo version=\$version
exit
!
create_executable "3.4.2" "python"
PYENV_VERSION=3.4 run pyenv-which python
assert_success "version=3.4.2"
}