tests/functional: Support negative codes in expect, expectStderr

This commit is contained in:
Robert Hensing 2024-07-16 01:28:28 +02:00
parent 03326d606f
commit 783a8341ee
2 changed files with 23 additions and 2 deletions

View File

@ -236,7 +236,8 @@ expect() {
expected="$1"
shift
"$@" && res=0 || res="$?"
if [[ $res -ne $expected ]]; then
# also match "negative" codes, which wrap around to >127
if [[ $res -ne $expected && $res -ne $[256 + expected] ]]; then
echo "Expected exit code '$expected' but got '$res' from command ${*@Q}" >&2
return 1
fi
@ -250,7 +251,8 @@ expectStderr() {
expected="$1"
shift
"$@" 2>&1 && res=0 || res="$?"
if [[ $res -ne $expected ]]; then
# also match "negative" codes, which wrap around to >127
if [[ $res -ne $expected && $res -ne $[256 + expected] ]]; then
echo "Expected exit code '$expected' but got '$res' from command ${*@Q}" >&2
return 1
fi

View File

@ -13,6 +13,25 @@ expect 1 false
# `expect` will fail when we get it wrong
expect 1 expect 0 false
function ret() {
return $1
}
# `expect` can call functions, not just executables
expect 0 ret 0
expect 1 ret 1
# `expect` supports negative exit codes
expect -1 ret -1
# or high positive ones, equivalent to negative ones
expect 255 ret 255
expect 255 ret -1
expect -1 ret 255
# but it doesn't confuse negative exit codes with positive ones
expect 1 expect -10 ret 10
noisyTrue () {
echo YAY! >&2
true