tests: refactor: rename and consolidate create_executable() functions

This commit is contained in:
Ivan Pozdeev 2025-12-19 20:20:20 +03:00
parent 22b192913e
commit b259813357
No known key found for this signature in database
GPG Key ID: FB6A628DCF06DCD7
9 changed files with 105 additions and 146 deletions

View File

@ -2,18 +2,6 @@
load test_helper
create_executable() {
name="${1?}"
shift 1
bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin"
mkdir -p "$bin"
{ if [ $# -eq 0 ]; then cat -
else printf '%s\n' "$@"
fi
} | sed -Ee '1s/^ +//' > "${bin}/$name"
chmod +x "${bin}/$name"
}
@test "fails with invalid version" {
bats_require_minimum_version 1.5.0
export PYENV_VERSION="3.4"
@ -38,8 +26,8 @@ EOF
@test "completes with names of executables" {
export PYENV_VERSION="3.4"
create_executable "fab" "#!/bin/sh"
create_executable "python" "#!/bin/sh"
create_alt_executable "fab"
create_alt_executable "python"
pyenv-rehash
run pyenv-completions exec
@ -65,7 +53,7 @@ SH
@test "forwards all arguments" {
export PYENV_VERSION="3.4"
create_executable "python" <<SH
create_alt_executable "python" <<SH
#!$BASH
echo \$0
for arg; do
@ -87,16 +75,13 @@ OUT
}
@test "sys.executable with system version (#98)" {
export PATH="${PYENV_ROOT}/versions/bin:${PATH}"
create_executable "python3" <<SH
#!$BASH
echo system
SH
system_python="$(python3)"
create_path_executable "python3" "echo system"
system_python="$(python3 </dev/null)"
assert_equal "${system_python}" "system"
export PYENV_VERSION="custom"
create_executable "python3" "#!/bin/sh" "echo custom"
create_alt_executable "python3" "echo custom"
pyenv-rehash
@ -105,19 +90,16 @@ SH
}
@test 'PATH is not modified with system Python' {
export PATH="${PYENV_TEST_DIR}:${PATH}"
# Create a wrapper executable that verifies PATH.
PYENV_VERSION="custom"
create_executable "python3" '[[ "$PATH" == "${PYENV_TEST_DIR}/root/versions/custom/bin:"* ]] || { echo "unexpected:$PATH"; exit 2;}'
unset PYENV_VERSION
create_alt_executable_in_version "custom" "python3" <<!
[[ \$PATH == "\${PYENV_ROOT}/versions/custom/bin:"* ]] \
|| { echo "unexpected:\$PATH"; exit 2;}
!
pyenv-rehash
# Path is not modified with system Python.
cat > "${PYENV_TEST_DIR}/python3" <<SH
#!$BASH
echo \$PATH
SH
chmod +x "${PYENV_TEST_DIR}/python3"
create_path_executable "python3" "echo \$PATH"
pyenv-rehash
run pyenv-exec python3
assert_success "$PATH"

View File

@ -6,14 +6,6 @@ _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" {
assert [ ! -d "${PYENV_ROOT}/shims" ]
assert [ ! -d "${PYENV_ROOT}/versions" ]
@ -179,7 +171,7 @@ echo "\$PATH"
}
@test "outputs sh-compatible case syntax" {
create_executable pyenv-commands <<!
create_path_executable pyenv-commands <<!
#!$BASH
echo -e 'activate\ndeactivate\nrehash\nshell'
!
@ -187,7 +179,7 @@ echo -e 'activate\ndeactivate\nrehash\nshell'
assert_success
assert_line ' activate|deactivate|rehash|shell)'
create_executable pyenv-commands <<!
create_path_executable pyenv-commands <<!
#!$BASH
echo
!

View File

@ -6,16 +6,8 @@ _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 "read from installed" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
echo 4.5.6
!
@ -27,7 +19,7 @@ echo 4.5.6
}
@test "read from known" {
create_executable python-build <<!
create_path_executable python-build <<!
#!$BASH
echo 4.5.6
!
@ -39,7 +31,7 @@ echo 4.5.6
}
@test "installed version not found" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
echo 3.5.6
echo 3.10.8
@ -52,7 +44,7 @@ pyenv: no installed versions match the prefix \`3.8'
}
@test "known version not found" {
create_executable python-build <<!
create_path_executable python-build <<!
#!$BASH
echo 3.5.6
echo 3.10.8
@ -65,7 +57,7 @@ pyenv: no known versions match the prefix \`3.8'
}
@test "complete name resolves to itself" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
echo foo
echo foo.bar
@ -80,7 +72,7 @@ foo
}
@test "sort CPython" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
echo 2.7.18
echo 3.5.6
@ -95,7 +87,7 @@ echo 3.10.6
}
@test "ignores rolling releases, branch tips, alternative srcs, prereleases, virtualenvs; 't' versions if prefix without 't'" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
echo 3.8.5-dev
echo 3.8.5-src
@ -117,7 +109,7 @@ echo 3.8.1/envs/foo
}
@test "resolves to a 't' version if prefix has 't'" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
echo 3.13.2t
echo 3.13.5
@ -132,7 +124,7 @@ echo 3.14.6
}
@test "falls back to argument with -b" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
!
run pyenv-latest -b nonexistent
@ -143,7 +135,7 @@ nonexistent
}
@test "falls back to argument and succeeds with -f" {
create_executable pyenv-versions <<!
create_path_executable pyenv-versions <<!
#!$BASH
!
run pyenv-latest -f nonexistent

View File

@ -2,13 +2,6 @@
load test_helper
create_executable() {
local bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin"
mkdir -p "$bin"
echo "#!/bin/sh" > "${bin}/$1"
chmod +x "${bin}/$1"
}
copy_src_pyenvd() {
mkdir -p "${PYENV_ROOT}"
cp -r "${BATS_TEST_DIRNAME}/../pyenv.d" "${PYENV_ROOT}"
@ -16,8 +9,8 @@ copy_src_pyenvd() {
@test "pip-rehash triggered when using 'pip'" {
export PYENV_VERSION="3.7.14"
create_executable "example"
create_executable "pip"
create_alt_executable "example"
create_alt_executable "pip"
copy_src_pyenvd
run command -v example 2> /dev/null
assert_failure
@ -29,8 +22,8 @@ copy_src_pyenvd() {
@test "pip-rehash triggered when using 'pip3'" {
export PYENV_VERSION="3.7.14"
create_executable "example"
create_executable "pip3"
create_alt_executable "example"
create_alt_executable "pip3"
copy_src_pyenvd
run command -v example 2> /dev/null
assert_failure
@ -42,8 +35,8 @@ copy_src_pyenvd() {
@test "pip-rehash triggered when using 'pip3.x'" {
export PYENV_VERSION="3.7.14"
create_executable "example"
create_executable "pip3.7"
create_alt_executable "example"
create_alt_executable "pip3.7"
copy_src_pyenvd
run command -v example 2> /dev/null
assert_failure
@ -55,8 +48,8 @@ copy_src_pyenvd() {
@test "pip-rehash triggered when using 'python -m pip install'" {
export PYENV_VERSION="3.7.14"
create_executable "example"
create_executable "python"
create_alt_executable "example"
create_alt_executable "python"
copy_src_pyenvd
run command -v example 2> /dev/null
assert_failure

View File

@ -2,13 +2,6 @@
load test_helper
create_executable() {
local bin="${PYENV_ROOT}/versions/${1}/bin"
mkdir -p "$bin"
touch "${bin}/$2"
chmod +x "${bin}/$2"
}
@test "empty rehash" {
assert [ ! -d "${PYENV_ROOT}/shims" ]
run pyenv-rehash
@ -42,10 +35,10 @@ create_executable() {
}
@test "creates shims" {
create_executable "2.7" "python"
create_executable "2.7" "fab"
create_executable "3.4" "python"
create_executable "3.4" "py.test"
create_alt_executable_in_version "2.7" "python"
create_alt_executable_in_version "2.7" "fab"
create_alt_executable_in_version "3.4" "python"
create_alt_executable_in_version "3.4" "py.test"
assert [ ! -e "${PYENV_ROOT}/shims/fab" ]
assert [ ! -e "${PYENV_ROOT}/shims/python" ]
@ -68,8 +61,8 @@ OUT
touch "${PYENV_ROOT}/shims/oldshim1"
chmod +x "${PYENV_ROOT}/shims/oldshim1"
create_executable "3.4" "fab"
create_executable "3.4" "python"
create_alt_executable_in_version "3.4" "fab"
create_alt_executable_in_version "3.4" "python"
run pyenv-rehash
assert_success ""
@ -78,8 +71,8 @@ OUT
}
@test "binary install locations containing spaces" {
create_executable "dirname1 p247" "python"
create_executable "dirname2 preview1" "py.test"
create_alt_executable_in_version "dirname1 p247" "python"
create_alt_executable_in_version "dirname2 preview1" "py.test"
assert [ ! -e "${PYENV_ROOT}/shims/python" ]
assert [ ! -e "${PYENV_ROOT}/shims/py.test" ]
@ -108,28 +101,28 @@ SH
}
@test "sh-rehash in bash" {
create_executable "3.4" "python"
create_alt_executable_in_version "3.4" "python"
PYENV_SHELL=bash run pyenv-sh-rehash
assert_success "command pyenv rehash
hash -r 2>/dev/null || true"
}
@test "sh-rehash in bash (integration)" {
create_executable "3.4" "python"
create_alt_executable_in_version "3.4" "python"
run eval "$(pyenv-sh-rehash)"
assert_success
assert [ -x "${PYENV_ROOT}/shims/python" ]
}
@test "sh-rehash in fish" {
create_executable "3.4" "python"
create_alt_executable_in_version "3.4" "python"
PYENV_SHELL=fish run pyenv-sh-rehash
assert_success "command pyenv rehash"
}
@test "sh-rehash in fish (integration)" {
command -v fish >/dev/null || skip "-- fish not installed"
create_executable "3.4" "python"
create_alt_executable_in_version "3.4" "python"
run fish -Nc "eval (pyenv-sh-rehash)"
assert_success
assert [ -x "${PYENV_ROOT}/shims/python" ]

View File

@ -2,6 +2,7 @@ unset PYENV_VERSION
unset PYENV_DIR
setup() {
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
if ! enable -f "${BATS_TEST_DIRNAME}"/../libexec/pyenv-realpath.dylib realpath 2>/dev/null; then
if [ -n "$PYENV_NATIVE_EXT" ]; then
echo "pyenv: failed to load \`realpath' builtin" >&2
@ -141,6 +142,39 @@ path_without() {
echo "$path"
}
create_path_executable() {
create_executable "${PYENV_TEST_DIR}/bin" "$@"
}
create_alt_executable() {
create_alt_executable_in_version "${PYENV_VERSION}" "$@"
}
create_alt_executable_in_version() {
local version="${1:?}"
shift 1
create_executable "${PYENV_ROOT}/versions/$version/bin" "$@"
}
create_executable() {
bin="${1:?}"
name="${2:?}"
shift 2
mkdir -p "$bin"
local payload
# Bats doesn't redirect stdin
if [[ $# -eq 0 && ! -t 0 ]]; then
payload="$(cat -)"
else
payload="$(printf '%s\n' "$@")"
fi
if [[ $payload != "#!/"* ]]; then
payload="#!$BASH"$'\n'"$payload"
fi
echo "$payload" > "${bin}/$name"
chmod +x "${bin}/$name"
}
create_hook() {
mkdir -p "${PYENV_HOOK_PATH}/$1"
touch "${PYENV_HOOK_PATH}/$1/$2"

View File

@ -22,14 +22,6 @@ 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" ]
@ -199,7 +191,7 @@ OUT
create_version "1.9.0"
create_version "1.53.0"
create_version "1.218.0"
create_executable sort <<SH
create_path_executable sort <<SH
#!$BASH
cat >/dev/null
if [ "\$1" == "--version-sort" ]; then
@ -223,7 +215,7 @@ OUT
create_version "1.9.0"
create_version "1.53.0"
create_version "1.218.0"
create_executable sort <<SH
create_path_executable sort <<SH
#!$BASH
exit 1
SH

View File

@ -2,18 +2,11 @@
load test_helper
create_executable() {
local bin="${PYENV_ROOT}/versions/${1}/bin"
mkdir -p "$bin"
touch "${bin}/$2"
chmod +x "${bin}/$2"
}
@test "finds versions where present" {
create_executable "2.7" "python"
create_executable "2.7" "fab"
create_executable "3.4" "python"
create_executable "3.4" "py.test"
create_alt_executable_in_version "2.7" "python"
create_alt_executable_in_version "2.7" "fab"
create_alt_executable_in_version "3.4" "python"
create_alt_executable_in_version "3.4" "py.test"
run pyenv-whence python
assert_success

View File

@ -2,19 +2,9 @@
load test_helper
create_executable() {
local bin
if [[ $1 == */* ]]; then bin="$1"
else bin="${PYENV_ROOT}/versions/${1}/bin"
fi
mkdir -p "$bin"
touch "${bin}/$2"
chmod +x "${bin}/$2"
}
@test "outputs path to executable" {
create_executable "2.7" "python"
create_executable "3.4" "py.test"
create_alt_executable_in_version "2.7" "python"
create_alt_executable_in_version "3.4" "py.test"
PYENV_VERSION=2.7 run pyenv-which python
assert_success "${PYENV_ROOT}/versions/2.7/bin/python"
@ -27,7 +17,7 @@ create_executable() {
}
@test "searches PATH for system version" {
create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
create_path_executable "kill-all-humans"
create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
PYENV_VERSION=system run pyenv-which kill-all-humans
@ -35,7 +25,7 @@ create_executable() {
}
@test "searches PATH for system version (shims prepended)" {
create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
create_path_executable "kill-all-humans"
create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
PATH="${PYENV_ROOT}/shims:$PATH" PYENV_VERSION=system run pyenv-which kill-all-humans
@ -43,7 +33,7 @@ create_executable() {
}
@test "searches PATH for system version (shims appended)" {
create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
create_path_executable "kill-all-humans"
create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
PATH="$PATH:${PYENV_ROOT}/shims" PYENV_VERSION=system run pyenv-which kill-all-humans
@ -51,7 +41,7 @@ create_executable() {
}
@test "searches PATH for system version (shims spread)" {
create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
create_path_executable "kill-all-humans"
create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/shims:/tmp/non-existent:$PATH:${PYENV_ROOT}/shims" \
@ -61,17 +51,15 @@ create_executable() {
@test "doesn't include current directory in PATH search" {
bats_require_minimum_version 1.5.0
mkdir -p "$PYENV_TEST_DIR"
create_executable "$PYENV_TEST_DIR" kill-all-humans
cd "$PYENV_TEST_DIR"
touch kill-all-humans
chmod +x kill-all-humans
PATH="$(path_without "kill-all-humans")" PYENV_VERSION=system run -127 pyenv-which kill-all-humans
assert_failure "pyenv: kill-all-humans: command not found"
}
@test "version not installed" {
bats_require_minimum_version 1.5.0
create_executable "3.4" "py.test"
create_alt_executable_in_version "3.4" "py.test"
PYENV_VERSION=3.3 run -127 pyenv-which py.test
assert_failure <<OUT
pyenv: version \`3.3' is not installed (set by PYENV_VERSION environment variable)
@ -87,7 +75,7 @@ OUT
@test "versions not installed" {
bats_require_minimum_version 1.5.0
create_executable "3.4" "py.test"
create_alt_executable_in_version "3.4" "py.test"
PYENV_VERSION=2.7:3.3 run -127 pyenv-which py.test
assert_failure <<OUT
pyenv: version \`2.7' is not installed (set by PYENV_VERSION environment variable)
@ -104,7 +92,7 @@ OUT
@test "no executable found" {
bats_require_minimum_version 1.5.0
create_executable "2.7" "py.test"
create_alt_executable_in_version "2.7" "py.test"
PYENV_VERSION=2.7 run -127 pyenv-which fab
assert_failure "pyenv: fab: command not found"
}
@ -117,9 +105,9 @@ OUT
@test "executable found in other versions" {
bats_require_minimum_version 1.5.0
create_executable "2.7" "python"
create_executable "3.3" "py.test"
create_executable "3.4" "py.test"
create_alt_executable_in_version "2.7" "python"
create_alt_executable_in_version "3.3" "py.test"
create_alt_executable_in_version "3.4" "py.test"
PYENV_VERSION=2.7 run -127 pyenv-which py.test
assert_failure
@ -150,7 +138,7 @@ SH
@test "discovers version from pyenv-version-name" {
mkdir -p "$PYENV_ROOT"
cat > "${PYENV_ROOT}/version" <<<"3.4"
create_executable "3.4" "python"
create_alt_executable_in_version "3.4" "python"
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
@ -165,7 +153,7 @@ SH
2.7
3.4
EOF
create_executable "3.4" "python"
create_alt_executable_in_version "3.4" "python"
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
@ -175,7 +163,7 @@ EOF
}
@test "resolves pyenv-latest prefixes" {
create_executable "3.4.2" "python"
create_alt_executable_in_version "3.4.2" "python"
PYENV_VERSION=3.4 run pyenv-which python
assert_success "${PYENV_ROOT}/versions/3.4.2/bin/python"
@ -187,7 +175,7 @@ echo version=\$version
exit
!
create_executable "3.4.2" "python"
create_alt_executable_in_version "3.4.2" "python"
PYENV_VERSION=3.4 run pyenv-which python
assert_success "version=3.4.2"
@ -195,9 +183,9 @@ exit
@test "skip advice supresses error messages" {
bats_require_minimum_version 1.5.0
create_executable "2.7" "python"
create_executable "3.3" "py.test"
create_executable "3.4" "py.test"
create_alt_executable_in_version "2.7" "python"
create_alt_executable_in_version "3.3" "py.test"
create_alt_executable_in_version "3.4" "py.test"
PYENV_VERSION=2.7 run -127 pyenv-which py.test --skip-advice
assert_failure