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"
#avoid failure due to a localized error message
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-rehash: line *: ${PYENV_ROOT}/shims/.pyenv-shim: cannot overwrite existing file
!

View file

@ -58,33 +58,64 @@ assert_success() {
if [ "$status" -ne 0 ]; then
flunk "command failed with exit status $status" $'\n'\
"output: $output"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
_assert_output_if_provided "$@"
}
assert_failure() {
if [ "$status" -eq 0 ]; then
flunk "expected failed exit status" $'\n'\
"output: $output"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
_assert_output_if_provided "$@"
}
assert_equal() {
if [ "$1" != "$2" ]; then
{ diff -u --label expected --label actual <(echo "$1") <(echo "$2") | cat -te
_assert_equal_as text "$@"
}
_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
fi
}
assert_output() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
else expected="$1"
_assert_output_if_provided() {
if [ "$#" -gt 0 ]; then
assert_output "$1"
elif [ ! -t 0 ]; then
local expected=$(cat -)
if [[ -n $expected ]]; then
assert_output "$expected"
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() {

View file

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

View file

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

View file

@ -64,7 +64,7 @@ load test_helper
assert_failure <<OUT
pyenv: version \`3.3' is not installed (set by PYENV_VERSION environment variable)
pyenv: py.test: command not found
The \`py.test' command exists in these Python versions:
3.4
@ -81,7 +81,7 @@ OUT
pyenv: version \`2.7' is not installed (set by PYENV_VERSION environment variable)
pyenv: version \`3.3' is not installed (set by PYENV_VERSION environment variable)
pyenv: py.test: command not found
The \`py.test' command exists in these Python versions:
3.4