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" expected="$1"
shift shift
"$@" && res=0 || res="$?" "$@" && 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 echo "Expected exit code '$expected' but got '$res' from command ${*@Q}" >&2
return 1 return 1
fi fi
@ -250,7 +251,8 @@ expectStderr() {
expected="$1" expected="$1"
shift shift
"$@" 2>&1 && res=0 || res="$?" "$@" 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 echo "Expected exit code '$expected' but got '$res' from command ${*@Q}" >&2
return 1 return 1
fi fi

View file

@ -13,6 +13,25 @@ expect 1 false
# `expect` will fail when we get it wrong # `expect` will fail when we get it wrong
expect 1 expect 0 false 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 () { noisyTrue () {
echo YAY! >&2 echo YAY! >&2
true true