1
0
mirror of https://github.com/pyenv/pyenv.git synced 2025-11-13 13:53:53 -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

@@ -378,6 +378,9 @@ OUT
stub port "-q installed libyaml : echo ' libyaml @0.2.5_0 (active)'" stub port "-q installed libyaml : echo ' libyaml @0.2.5_0 (active)'"
for i in {1..3}; do stub port false; done for i in {1..3}; do stub port false; done
stub_make_install stub_make_install
export PYTHON_BUILD_SKIP_HOMEBREW=1
PORT_PREFIX="$(which port)"
PORT_PREFIX="${PORT_PREFIX%/bin/port}"
install_fixture definitions/needs-yaml install_fixture definitions/needs-yaml
assert_success assert_success
@@ -403,6 +406,9 @@ OUT
stub port "-q installed readline : echo ' readline @8.2.013_0 (active)'" stub port "-q installed readline : echo ' readline @8.2.013_0 (active)'"
for i in {1..2}; do stub port false; done for i in {1..2}; do stub port false; done
stub_make_install stub_make_install
export PYTHON_BUILD_SKIP_HOMEBREW=1
PORT_PREFIX="$(which port)"
PORT_PREFIX="${PORT_PREFIX%/bin/port}"
run_inline_definition <<DEF run_inline_definition <<DEF
install_package "Python-3.6.2" "http://python.org/ftp/python/3.6.2/Python-3.6.2.tar.gz" install_package "Python-3.6.2" "http://python.org/ftp/python/3.6.2/Python-3.6.2.tar.gz"
@@ -431,6 +437,9 @@ OUT
stub port "-q installed ncurses : echo '$ncurses_libdir'" stub port "-q installed ncurses : echo '$ncurses_libdir'"
stub port false stub port false
stub_make_install stub_make_install
export PYTHON_BUILD_SKIP_HOMEBREW=1
PORT_PREFIX="$(which port)"
PORT_PREFIX="${PORT_PREFIX%/bin/port}"
run_inline_definition <<DEF run_inline_definition <<DEF
install_package "Python-3.6.2" "http://python.org/ftp/python/3.6.2/Python-3.6.2.tar.gz" install_package "Python-3.6.2" "http://python.org/ftp/python/3.6.2/Python-3.6.2.tar.gz"
@@ -845,6 +854,7 @@ OUT
stub uname '-s : echo Darwin' stub uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.10' stub sw_vers '-productVersion : echo 10.10'
for i in {1..6}; do stub brew false; done
stub sysctl false stub sysctl false
stub_make_install stub_make_install
@@ -857,6 +867,7 @@ DEF
unstub uname unstub uname
unstub sw_vers unstub sw_vers
unstub brew
unstub make unstub make
assert_build_log <<OUT assert_build_log <<OUT
@@ -872,6 +883,7 @@ OUT
stub uname '-s : echo Darwin' stub uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.10' stub sw_vers '-productVersion : echo 10.10'
for i in {1..6}; do stub brew false; done
stub sysctl '-n hw.ncpu : echo 4' stub sysctl '-n hw.ncpu : echo 4'
stub_make_install stub_make_install
@@ -884,6 +896,7 @@ DEF
unstub uname unstub uname
unstub sw_vers unstub sw_vers
unstub brew
unstub sysctl unstub sysctl
unstub make unstub make

View File

@@ -87,9 +87,9 @@ jython-2.5.4-rc1
jython-2.7-beta1 jython-2.7-beta1
jython-2.7-beta2 jython-2.7-beta2
jython-2.7-beta3" jython-2.7-beta3"
for ver in "$expected"; do while IFS=$'\n' read -r ver; do
touch "${PYTHON_BUILD_ROOT}/share/python-build/$ver" touch "${PYTHON_BUILD_ROOT}/share/python-build/$ver"
done done <<<"$expected"
run python-build --definitions run python-build --definitions
assert_success "$expected" assert_success "$expected"
} }

View File

@@ -6,9 +6,7 @@ if [ "$FIXTURE_ROOT" != "$BATS_TEST_DIRNAME/fixtures" ]; then
export FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures" export FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures"
export INSTALL_ROOT="$TMP/install" export INSTALL_ROOT="$TMP/install"
PATH="/usr/bin:/bin:/usr/sbin:/sbin" PATH="/usr/bin:/bin:/usr/sbin:/sbin"
if [ "FreeBSD" = "$(uname -s)" ]; then
PATH="/usr/local/bin:$PATH" PATH="/usr/local/bin:$PATH"
fi
PATH="$BATS_TEST_DIRNAME/../bin:$PATH" PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
PATH="$TMP/bin:$PATH" PATH="$TMP/bin:$PATH"
export PATH export PATH
@@ -29,7 +27,7 @@ stub() {
export "${prefix}_STUB_END"= export "${prefix}_STUB_END"=
mkdir -p "${TMP}/bin" mkdir -p "${TMP}/bin"
ln -sf "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}" cp "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}"
touch "${TMP}/${program}-stub-plan" touch "${TMP}/${program}-stub-plan"
for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done

View File

@@ -8,7 +8,7 @@ create_executable() {
bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin" bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin"
mkdir -p "$bin" mkdir -p "$bin"
{ if [ $# -eq 0 ]; then cat - { if [ $# -eq 0 ]; then cat -
else echo "$@" else printf '%s\n' "$@"
fi fi
} | sed -Ee '1s/^ +//' > "${bin}/$name" } | sed -Ee '1s/^ +//' > "${bin}/$name"
chmod +x "${bin}/$name" chmod +x "${bin}/$name"
@@ -85,18 +85,25 @@ OUT
} }
@test "sys.executable with system version (#98)" { @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" export PYENV_VERSION="custom"
create_executable "python3" "" create_executable "python3" "#!/bin/sh" "echo custom"
unset PYENV_VERSION
pyenv-rehash 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' { @test 'PATH is not modified with system Python' {
export PATH="${PYENV_TEST_DIR}:${PATH}"
# Create a wrapper executable that verifies PATH. # Create a wrapper executable that verifies PATH.
PYENV_VERSION="custom" PYENV_VERSION="custom"
create_executable "python3" '[[ "$PATH" == "${PYENV_TEST_DIR}/root/versions/custom/bin:"* ]] || { echo "unexpected:$PATH"; exit 2;}' create_executable "python3" '[[ "$PATH" == "${PYENV_TEST_DIR}/root/versions/custom/bin:"* ]] || { echo "unexpected:$PATH"; exit 2;}'
@@ -104,7 +111,13 @@ OUT
pyenv-rehash pyenv-rehash
# Path is not modified with system Python. # 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" assert_success "$PATH"
# Path is modified with custom Python. # Path is modified with custom Python.
@@ -116,6 +129,6 @@ OUT
assert_success assert_success
# Path is not modified with system:custom Python. # 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" assert_success "$PATH"
} }

View File

@@ -20,7 +20,7 @@ load test_helper
mkdir -p "${PYENV_TEST_DIR}/bin" mkdir -p "${PYENV_TEST_DIR}/bin"
touch "${PYENV_TEST_DIR}/bin/python" touch "${PYENV_TEST_DIR}/bin/python"
chmod +x "${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" assert_success "$PYENV_TEST_DIR"
} }
@@ -40,7 +40,7 @@ load test_helper
echo /bin/python echo /bin/python
OUT OUT
chmod +x "${BATS_TEST_DIRNAME}/libexec/pyenv-which" 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 "/" assert_success "/"
rm -f "${BATS_TEST_DIRNAME}/libexec/pyenv-which" rm -f "${BATS_TEST_DIRNAME}/libexec/pyenv-which"
} }

View File

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

View File

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