diff options
author | Silvan Mosberger <contact@infinisil.com> | 2020-09-02 22:33:29 +0200 |
---|---|---|
committer | Silvan Mosberger <contact@infinisil.com> | 2020-11-30 23:51:23 +0100 |
commit | 900c4a5abd1061db2390224849cbc5eb8eb07059 (patch) | |
tree | f75dd4a8180e9eb847473abb41dafcf7a3048ae3 /lib | |
parent | c4fb54e92a10f04bb70b31b397a50fdbc203bc66 (diff) |
lib/tests: Implement generalized checkConfigCodeOutErr for module tests
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/tests/modules.sh | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 309c5311361c..012835e48c13 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -27,37 +27,50 @@ reportFailure() { fail=$((fail + 1)) } -checkConfigOutput() { +checkConfigCodeOutErr() { + local expectedExit=$1 + shift; local outputContains=$1 shift; - if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then - pass=$((pass + 1)) - return 0; - else - echo 2>&1 "error: Expected result matching '$outputContains', while evaluating" + local errorContains=$1 + shift; + out=$(mktemp) + err=$(mktemp) + evalConfig "$@" 1>"$out" 2>"$err" + if [[ "$?" -ne "$expectedExit" ]]; then + echo 2>&1 "error: Expected exit code $expectedExit while evaluating" + reportFailure "$@" + return 1 + fi + + if [[ -n "$outputContains" ]] && ! grep -zP --silent "$outputContains" "$out"; then + echo 2>&1 "error: Expected output matching '$outputContains', while evaluating" + reportFailure "$@" + return 1 + fi + + if [[ -n "$errorContains" ]] && ! grep -zP --silent "$errorContains" "$err"; then + echo 2>&1 "error: Expected error matching '$errorContains', while evaluating" reportFailure "$@" return 1 fi + + pass=$((pass + 1)) + + # Clean up temp files + rm "$out" "$err" +} + +checkConfigOutput() { + local outputContains=$1 + shift; + checkConfigCodeOutErr 0 "$outputContains" "" "$@" } checkConfigError() { local errorContains=$1 - 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 "$@" - return 1 - else - if echo "$err" | grep -zP --silent "$errorContains" ; then - pass=$((pass + 1)) - return 0; - else - echo 2>&1 "error: Expected error matching '$errorContains', while evaluating" - reportFailure "$@" - return 1 - fi - fi + checkConfigCodeOutErr 1 "" "$errorContains" "$@" } # Check boolean option. |