1
0
mirror of https://github.com/pyenv/pyenv.git synced 2025-11-17 07:43:46 -05:00

fix test isolation

Make sur that PYENV_TEST_DIR is created if mktemp failed
mock python3 usage by updating create_executable function
fix test by adding the right PATH
update path in plugin test
replace for loop with while read
don't use symlink for stub
This commit is contained in:
rockandska
2025-09-08 15:36:12 +02:00
parent 857806e6ca
commit 0d987b0e4b
7 changed files with 43 additions and 17 deletions

View File

@@ -8,7 +8,7 @@ create_executable() {
bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin"
mkdir -p "$bin"
{ if [ $# -eq 0 ]; then cat -
else echo "$@"
else printf '%s\n' "$@"
fi
} | sed -Ee '1s/^ +//' > "${bin}/$name"
chmod +x "${bin}/$name"
@@ -85,18 +85,25 @@ OUT
}
@test "sys.executable with system version (#98)" {
system_python="$(python3 -c 'import sys; print(sys.executable)')"
export PATH="${PYENV_ROOT}/versions/bin:${PATH}"
create_executable "python3" <<SH
#!$BASH
echo system
SH
system_python="$(python3)"
assert_equal "${system_python}" "system"
PYENV_VERSION="custom"
create_executable "python3" ""
unset PYENV_VERSION
export PYENV_VERSION="custom"
create_executable "python3" "#!/bin/sh" "echo custom"
pyenv-rehash
run pyenv-exec python3 -c 'import sys; print(sys.executable)'
assert_success "${system_python}"
custom_python="$(pyenv-exec python3)"
assert_equal "${custom_python}" "custom"
}
@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;}'
@@ -104,7 +111,13 @@ OUT
pyenv-rehash
# Path is not modified with system Python.
run pyenv-exec python3 -c 'import os; print(os.getenv("PATH"))'
cat > "${PYENV_TEST_DIR}/python3" <<SH
#!$BASH
echo \$PATH
SH
chmod +x "${PYENV_TEST_DIR}/python3"
pyenv-rehash
run pyenv-exec python3
assert_success "$PATH"
# Path is modified with custom Python.
@@ -116,6 +129,6 @@ OUT
assert_success
# Path is not modified with system:custom Python.
PYENV_VERSION=system:custom run pyenv-exec python3 -c 'import os; print(os.getenv("PATH"))'
PYENV_VERSION=system:custom run pyenv-exec python3
assert_success "$PATH"
}

View File

@@ -20,7 +20,7 @@ load test_helper
mkdir -p "${PYENV_TEST_DIR}/bin"
touch "${PYENV_TEST_DIR}/bin/python"
chmod +x "${PYENV_TEST_DIR}/bin/python"
PYENV_VERSION="system" run pyenv-prefix
PATH="${PYENV_TEST_DIR}/libexec:$PATH" PYENV_VERSION="system" run pyenv-prefix
assert_success "$PYENV_TEST_DIR"
}
@@ -40,7 +40,7 @@ load test_helper
echo /bin/python
OUT
chmod +x "${BATS_TEST_DIRNAME}/libexec/pyenv-which"
PYENV_VERSION="system" run pyenv-prefix
PATH="${PYENV_TEST_DIR}/libexec:$PATH" PYENV_VERSION="system" run pyenv-prefix
assert_success "/"
rm -f "${BATS_TEST_DIRNAME}/libexec/pyenv-which"
}

View File

@@ -5,6 +5,7 @@ unset PYENV_DIR
if [ -z "$PYENV_TEST_DIR" ]; then
PYENV_TEST_DIR="${BATS_TMPDIR}/pyenv"
export PYENV_TEST_DIR="$(mktemp -d "${PYENV_TEST_DIR}.XXX" 2>/dev/null || echo "$PYENV_TEST_DIR")"
mkdir -p "${PYENV_TEST_DIR}"
if enable -f "${BATS_TEST_DIRNAME}"/../libexec/pyenv-realpath.dylib realpath 2>/dev/null; then
export PYENV_TEST_DIR="$(realpath "$PYENV_TEST_DIR")"

View File

@@ -238,6 +238,7 @@ OUT
}
@test "non-bare output shows symlink contents" {
stub_system_python
create_version "1.9.0"
create_alias "link" "1.9.0"