1
0
mirror of https://github.com/pyenv/pyenv.git synced 2025-11-16 07:13:53 -05:00

use BATS_FILE_TMPDIR in test/plugin test

use global setup with bats with possibility to add specific test file _setup
use readlink in helper if realpath is not working as expected
This commit is contained in:
rockandska
2025-09-08 16:47:50 +02:00
committed by Tester
parent 8990110dcf
commit 92a28e531b
29 changed files with 234 additions and 233 deletions

View File

@@ -2,9 +2,8 @@
load test_helper
export GIT_DIR="${PYENV_TEST_DIR}/.git"
setup() {
_setup() {
export GIT_DIR="${PYENV_TEST_DIR}/.git"
mkdir -p "$HOME"
git config --global user.name "Tester"
git config --global user.email "tester@test.local"

View File

@@ -2,7 +2,7 @@
load test_helper
setup() {
_setup() {
export PATH="${PYENV_TEST_DIR}/bin:$PATH"
}

View File

@@ -2,7 +2,7 @@
load test_helper
setup() {
_setup() {
export PATH="${PYENV_TEST_DIR}/bin:$PATH"
}

View File

@@ -2,7 +2,7 @@
load test_helper
setup() {
_setup() {
mkdir -p "${PYENV_TEST_DIR}/myproject"
cd "${PYENV_TEST_DIR}/myproject"
}

View File

@@ -34,15 +34,15 @@ load test_helper
}
@test "prefix for system in /" {
mkdir -p "${BATS_TEST_DIRNAME}/libexec"
cat >"${BATS_TEST_DIRNAME}/libexec/pyenv-which" <<OUT
mkdir -p "${PYENV_TEST_DIR}/libexec"
cat >"${PYENV_TEST_DIR}/libexec/pyenv-which" <<OUT
#!/bin/sh
echo /bin/python
OUT
chmod +x "${BATS_TEST_DIRNAME}/libexec/pyenv-which"
chmod +x "${PYENV_TEST_DIR}/libexec/pyenv-which"
PATH="${PYENV_TEST_DIR}/libexec:$PATH" PYENV_VERSION="system" run pyenv-prefix
assert_success "/"
rm -f "${BATS_TEST_DIRNAME}/libexec/pyenv-which"
rm -f "${PYENV_TEST_DIR}/libexec/pyenv-which"
}
@test "prefix for invalid system" {

View File

@@ -32,14 +32,14 @@ load test_helper
}
@test "inherited PYENV_DIR" {
dir="${BATS_TMPDIR}/myproject"
dir="${BATS_TEST_TMPDIR}/myproject"
mkdir -p "$dir"
PYENV_DIR="$dir" run pyenv echo PYENV_DIR
assert_output "$dir"
}
@test "invalid PYENV_DIR" {
dir="${BATS_TMPDIR}/does-not-exist"
dir="${BATS_TEST_TMPDIR}/does-not-exist"
assert [ ! -d "$dir" ]
PYENV_DIR="$dir" run pyenv echo PYENV_DIR
assert_failure

View File

@@ -1,21 +1,23 @@
unset PYENV_VERSION
unset PYENV_DIR
# guard against executing this block twice due to bats internals
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")"
else
setup() {
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
exit 1
fi
fi
local bats_test_tmpdir="$(realpath "${BATS_TEST_TMPDIR}")"
if [ -z "${bats_test_tmpdir}" ];then
# Use readlink if running in a container instead of realpath lib
bats_test_tmpdir="$(readlink -f "${BATS_TEST_TMPDIR}")"
fi
# update BATS_TEST_TMPDIR discover by realpath/readlink to avoid "//"
export BATS_TEST_TMPDIR="${bats_test_tmpdir}"
export PYENV_TEST_DIR="${BATS_TEST_TMPDIR}/pyenv"
export PYENV_ROOT="${PYENV_TEST_DIR}/root"
export HOME="${PYENV_TEST_DIR}/home"
export PYENV_HOOK_PATH="${PYENV_ROOT}/pyenv.d"
@@ -29,10 +31,11 @@ if [ -z "$PYENV_TEST_DIR" ]; then
for xdg_var in `env 2>/dev/null | grep ^XDG_ | cut -d= -f1`; do unset "$xdg_var"; done
unset xdg_var
fi
teardown() {
rm -rf "$PYENV_TEST_DIR"
# If test specific setup exist, run it
if [[ $(type -t _setup) == function ]];then
_setup
fi
}
flunk() {

View File

@@ -2,7 +2,7 @@
load test_helper
setup() {
_setup() {
mkdir -p "${PYENV_TEST_DIR}/myproject"
cd "${PYENV_TEST_DIR}/myproject"
}

View File

@@ -2,8 +2,8 @@
load test_helper
setup() {
mkdir -p "$PYENV_TEST_DIR"
_setup() {
mkdir -p "${PYENV_TEST_DIR}"
cd "$PYENV_TEST_DIR"
}

View File

@@ -2,8 +2,8 @@
load test_helper
setup() {
mkdir -p "$PYENV_TEST_DIR"
_setup() {
mkdir -p "${PYENV_TEST_DIR}"
cd "$PYENV_TEST_DIR"
}

View File

@@ -6,8 +6,8 @@ create_version() {
mkdir -p "${PYENV_ROOT}/versions/$1"
}
setup() {
mkdir -p "$PYENV_TEST_DIR"
_setup() {
mkdir -p "${PYENV_TEST_DIR}"
cd "$PYENV_TEST_DIR"
}

View File

@@ -2,8 +2,8 @@
load test_helper
setup() {
mkdir -p "$PYENV_TEST_DIR"
_setup() {
mkdir -p "${PYENV_TEST_DIR}"
cd "$PYENV_TEST_DIR"
}

View File

@@ -6,8 +6,8 @@ create_version() {
mkdir -p "${PYENV_ROOT}/versions/$1"
}
setup() {
mkdir -p "$PYENV_TEST_DIR"
_setup() {
mkdir -p "${PYENV_ROOT}"
cd "$PYENV_TEST_DIR"
}
@@ -78,4 +78,4 @@ OUT
assert_output <<OUT
3.3.3
OUT
}
}

View File

@@ -11,7 +11,7 @@ create_alias() {
ln -s "$2" "${PYENV_ROOT}/versions/$1"
}
setup() {
_setup() {
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
}