Use version sort in pyenv versions if available (#2405)

This commit is contained in:
Pedro Fonini 2022-07-10 17:00:51 -03:00 committed by GitHub
parent 207f33fc5e
commit fdaeaf1f97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 1 deletions

View file

@ -123,7 +123,19 @@ if [ -n "$include_system" ] && \
fi fi
shopt -s dotglob nullglob shopt -s dotglob nullglob
for path in "$versions_dir"/*; do versions_dir_entries=("$versions_dir"/*)
if sort --version-sort </dev/null >/dev/null 2>&1; then
# system sort supports version sorting
OLDIFS="$IFS"
IFS=$'\n'
versions_dir_entries=($(
printf "%s\n" "${versions_dir_entries[@]}" |
sort --version-sort
))
IFS="$OLDIFS"
fi
for path in "${versions_dir_entries[@]}"; do
if [ -d "$path" ]; then if [ -d "$path" ]; then
if [ -n "$skip_aliases" ] && [ -L "$path" ]; then if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
target="$(realpath "$path")" target="$(realpath "$path")"

View file

@ -17,6 +17,14 @@ stub_system_python() {
touch "$stub" && chmod +x "$stub" touch "$stub" && chmod +x "$stub"
} }
create_executable() {
local name="$1"
local bin="${PYENV_TEST_DIR}/bin"
mkdir -p "$bin"
sed -Ee '1s/^ +//' > "${bin}/$name"
chmod +x "${bin}/$name"
}
@test "no versions installed" { @test "no versions installed" {
stub_system_python stub_system_python
assert [ ! -d "${PYENV_ROOT}/versions" ] assert [ ! -d "${PYENV_ROOT}/versions" ]
@ -161,3 +169,44 @@ OUT
run pyenv-versions --bare run pyenv-versions --bare
assert_success ".venv" assert_success ".venv"
} }
@test "sort supports version sorting" {
create_version "1.9.0"
create_version "1.53.0"
create_version "1.218.0"
create_executable sort <<SH
#!$BASH
if [ "\$1" == "--version-sort" ]; then
echo "${PYENV_ROOT}/versions/1.9.0"
echo "${PYENV_ROOT}/versions/1.53.0"
echo "${PYENV_ROOT}/versions/1.218.0"
else exit 1
fi
SH
run pyenv-versions --bare
assert_success
assert_output <<OUT
1.9.0
1.53.0
1.218.0
OUT
}
@test "sort doesn't support version sorting" {
create_version "1.9.0"
create_version "1.53.0"
create_version "1.218.0"
create_executable sort <<SH
#!$BASH
exit 1
SH
run pyenv-versions --bare
assert_success
assert_output <<OUT
1.218.0
1.53.0
1.9.0
OUT
}