test_helper: support here-document to assert_success/failure; + assert_output_glob

This commit is contained in:
Ivan Pozdeev 2026-08-10 21:19:47 +03:00
parent bd7c969e7f
commit f9ffb73a7e
5 changed files with 53 additions and 23 deletions

View File

@ -23,7 +23,8 @@ load test_helper
touch "${PYENV_ROOT}/shims/.pyenv-shim" touch "${PYENV_ROOT}/shims/.pyenv-shim"
#avoid failure due to a localized error message #avoid failure due to a localized error message
LANG=C run pyenv-rehash LANG=C run pyenv-rehash
assert_failure <<! assert_failure
assert_output_glob <<!
pyenv: cannot rehash: couldn't acquire lock ${PYENV_ROOT}/shims/.pyenv-shim for 1 seconds. Last error message: pyenv: cannot rehash: couldn't acquire lock ${PYENV_ROOT}/shims/.pyenv-shim for 1 seconds. Last error message:
*/pyenv-rehash: line *: ${PYENV_ROOT}/shims/.pyenv-shim: cannot overwrite existing file */pyenv-rehash: line *: ${PYENV_ROOT}/shims/.pyenv-shim: cannot overwrite existing file
! !

View File

@ -58,33 +58,64 @@ assert_success() {
if [ "$status" -ne 0 ]; then if [ "$status" -ne 0 ]; then
flunk "command failed with exit status $status" $'\n'\ flunk "command failed with exit status $status" $'\n'\
"output: $output" "output: $output"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi fi
_assert_output_if_provided "$@"
} }
assert_failure() { assert_failure() {
if [ "$status" -eq 0 ]; then if [ "$status" -eq 0 ]; then
flunk "expected failed exit status" $'\n'\ flunk "expected failed exit status" $'\n'\
"output: $output" "output: $output"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi fi
_assert_output_if_provided "$@"
} }
assert_equal() { assert_equal() {
if [ "$1" != "$2" ]; then _assert_equal_as text "$@"
{ diff -u --label expected --label actual <(echo "$1") <(echo "$2") | cat -te }
_assert_equal_as() {
if case "$1" in
text)
[[ "$2" != "$3" ]];;
glob)
[[ "$3" != $2 ]];;
*)
echo "invalid comparison type: \`$1'" >&2; return 1;;
esac
then
{ diff -u --label expected --label actual <(echo "$2") <(echo "$3") | cat -te
} | flunk } | flunk
fi fi
} }
assert_output() { _assert_output_if_provided() {
local expected if [ "$#" -gt 0 ]; then
if [ $# -eq 0 ]; then expected="$(cat -)" assert_output "$1"
else expected="$1" elif [ ! -t 0 ]; then
local expected=$(cat -)
if [[ -n $expected ]]; then
assert_output "$expected"
fi
fi fi
assert_equal "$expected" "$output" }
assert_output() {
_assert_output_as text "$@"
}
assert_output_glob() {
_assert_output_as glob "$@"
}
_assert_output_as() {
local kind="${1:?}"; shift
local expected
if [ $# -eq 0 ]
then expected="$(cat -)"
else expected="$1"
fi
_assert_equal_as "$kind" "$expected" "$output"
} }
assert_line() { assert_line() {

View File

@ -90,13 +90,13 @@ IN
} }
@test "skips glob path traversal" { @test "skips glob path traversal" {
cat > my-version <<IN cat > my-version <<'IN'
../* ../*
3.9.3 3.9.3
IN IN
run pyenv-version-file-read my-version run pyenv-version-file-read my-version
assert_success <<OUT assert_success <<'OUT'
pyenv: invalid version \`../\*' ignored in \`my-version' pyenv: invalid version `../*' ignored in `my-version'
3.9.3 3.9.3
OUT OUT
} }

View File

@ -83,8 +83,7 @@ OUT
create_version "3.4.0/envs/bar" create_version "3.4.0/envs/bar"
create_version "3.5.2" create_version "3.5.2"
run pyenv-versions run pyenv-versions
assert_success assert_success <<OUT
assert_output <<OUT
* system (set by ${PYENV_ROOT}/version) * system (set by ${PYENV_ROOT}/version)
2.7.6 2.7.6
3.4.0 3.4.0
@ -95,6 +94,7 @@ OUT
} }
@test "skips envs with --skip-envs" { @test "skips envs with --skip-envs" {
stub_system_python
create_version "3.3.3" create_version "3.3.3"
create_version "3.4.0" create_version "3.4.0"
create_version "3.4.0/envs/foo" create_version "3.4.0/envs/foo"
@ -102,7 +102,7 @@ OUT
create_version "3.5.0" create_version "3.5.0"
run pyenv-versions --skip-envs run pyenv-versions --skip-envs
assert_success <<OUT assert_success <<OUT
* system (set by ${PYENV_ROOT}/version) * system (set by ${PYENV_ROOT}/version)
3.3.3 3.3.3
3.4.0 3.4.0
@ -188,9 +188,7 @@ OUT
create_external_version "moo" create_external_version "moo"
run pyenv-versions --bare --skip-aliases run pyenv-versions --bare --skip-aliases
assert_success assert_success <<OUT
assert_output <<OUT
1.8.7 1.8.7
moo moo
OUT OUT