diff --git a/libexec/pyenv-versions b/libexec/pyenv-versions index 03bbac1d..eb111884 100755 --- a/libexec/pyenv-versions +++ b/libexec/pyenv-versions @@ -11,7 +11,6 @@ if [ "$1" = "--bare" ]; then hit_prefix="" miss_prefix="" current_versions=() - version_origin="" include_system="" else hit_prefix="* " @@ -19,25 +18,30 @@ else OLDIFS="$IFS" IFS=: current_versions=($(pyenv-version-name || true)) IFS="$OLDIFS" - version_origin=" (set by $(pyenv-version-origin))" include_system="1" fi -array_exists() { - local x car="$1" +num_versions=0 + +exists() { + local car="$1" + local cdar shift - for x in "$@"; do - [ "${x}" = "${car}" ] && return 0 + for cdar in "$@"; do + if [ "${car}" == "${cdar}" ]; then + return 0 + fi done return 1 } print_version() { - if array_exists "$1" "${current_versions[@]}"; then - echo "${hit_prefix}$1${version_origin}" + if exists "$1" "${current_versions[@]}"; then + echo "${hit_prefix}$1 (set by $(pyenv-version-origin))" else echo "${miss_prefix}$1" fi + num_versions=$((num_versions + 1)) } # Include "system" in the non-bare output, if it exists @@ -45,8 +49,15 @@ if [ -n "$include_system" ] && PYENV_VERSION=system pyenv-which python >/dev/nul print_version system fi +shopt -s nullglob for path in "${PYENV_ROOT}/versions/"*; do if [ -d "$path" ]; then print_version "${path##*/}" fi done +shopt -u nullglob + +if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then + echo "Warning: no Python detected on the system" >&2 + exit 1 +fi diff --git a/test/versions.bats b/test/versions.bats index 840a0adc..6f7e4d2c 100644 --- a/test/versions.bats +++ b/test/versions.bats @@ -24,6 +24,12 @@ stub_system_python() { assert_success "* system (set by ${PYENV_ROOT}/version)" } +@test "not even system python available" { + PATH="$(path_without python)" run pyenv-versions + assert_failure + assert_output "Warning: no Python detected on the system" +} + @test "bare output no versions installed" { assert [ ! -d "${PYENV_ROOT}/versions" ] run pyenv-versions --bare @@ -113,3 +119,23 @@ OUT 3.4.0 OUT } + +@test "ignores non-directories under versions" { + create_version "3.3" + touch "${PYENV_ROOT}/versions/hello" + + run pyenv-versions --bare + assert_success "3.3" +} + +@test "lists symlinks under versions" { + create_version "2.7.8" + ln -s "2.7.8" "${PYENV_ROOT}/versions/2.7" + + run pyenv-versions --bare + assert_success + assert_output <