From fdaeaf1f97fc13f15b5294100f43179f4e6aedbd Mon Sep 17 00:00:00 2001 From: Pedro Fonini Date: Sun, 10 Jul 2022 17:00:51 -0300 Subject: [PATCH] Use version sort in `pyenv versions` if available (#2405) --- libexec/pyenv-versions | 14 +++++++++++- test/versions.bats | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/libexec/pyenv-versions b/libexec/pyenv-versions index 273eb20a..e4bb6264 100755 --- a/libexec/pyenv-versions +++ b/libexec/pyenv-versions @@ -123,7 +123,19 @@ if [ -n "$include_system" ] && \ fi shopt -s dotglob nullglob -for path in "$versions_dir"/*; do +versions_dir_entries=("$versions_dir"/*) +if sort --version-sort /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 [ -n "$skip_aliases" ] && [ -L "$path" ]; then target="$(realpath "$path")" diff --git a/test/versions.bats b/test/versions.bats index 932883c7..fb381dbe 100644 --- a/test/versions.bats +++ b/test/versions.bats @@ -17,6 +17,14 @@ stub_system_python() { 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" { stub_system_python assert [ ! -d "${PYENV_ROOT}/versions" ] @@ -161,3 +169,44 @@ OUT run pyenv-versions --bare 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 <