lib/tests: Implement generalized checkConfigCodeOutErr for module tests

This commit is contained in:
Silvan Mosberger 2020-09-02 22:33:29 +02:00
parent c4fb54e92a
commit 900c4a5abd
No known key found for this signature in database
GPG Key ID: E8F1E9EAD284E17D

View File

@ -27,37 +27,50 @@ reportFailure() {
fail=$((fail + 1)) fail=$((fail + 1))
} }
checkConfigOutput() { checkConfigCodeOutErr() {
local expectedExit=$1
shift;
local outputContains=$1 local outputContains=$1
shift; shift;
if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then local errorContains=$1
pass=$((pass + 1)) shift;
return 0; out=$(mktemp)
else err=$(mktemp)
echo 2>&1 "error: Expected result matching '$outputContains', while evaluating" evalConfig "$@" 1>"$out" 2>"$err"
if [[ "$?" -ne "$expectedExit" ]]; then
echo 2>&1 "error: Expected exit code $expectedExit while evaluating"
reportFailure "$@" reportFailure "$@"
return 1 return 1
fi fi
}
checkConfigError() { if [[ -n "$outputContains" ]] && ! grep -zP --silent "$outputContains" "$out"; then
local errorContains=$1 echo 2>&1 "error: Expected output matching '$outputContains', while evaluating"
local err=""
shift;
if err==$(evalConfig "$@" 2>&1 >/dev/null); then
echo 2>&1 "error: Expected error code, got exit code 0, while evaluating"
reportFailure "$@" reportFailure "$@"
return 1 return 1
else fi
if echo "$err" | grep -zP --silent "$errorContains" ; then
pass=$((pass + 1)) if [[ -n "$errorContains" ]] && ! grep -zP --silent "$errorContains" "$err"; then
return 0;
else
echo 2>&1 "error: Expected error matching '$errorContains', while evaluating" echo 2>&1 "error: Expected error matching '$errorContains', while evaluating"
reportFailure "$@" reportFailure "$@"
return 1 return 1
fi fi
fi
pass=$((pass + 1))
# Clean up temp files
rm "$out" "$err"
}
checkConfigOutput() {
local outputContains=$1
shift;
checkConfigCodeOutErr 0 "$outputContains" "" "$@"
}
checkConfigError() {
local errorContains=$1
shift;
checkConfigCodeOutErr 1 "" "$errorContains" "$@"
} }
# Check boolean option. # Check boolean option.