From 658a590b3532ffab79d463d28749a480b923384e Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Wed, 9 Nov 2022 14:04:23 +0100 Subject: [PATCH] testprogs: Add testit_grep_count() helper Signed-off-by: Volker Lendecke Reviewed-by: Stefan Metzmacher (cherry picked from commit 55feb593012fc5b24e795a00081666fca740429c) BUG: https://bugzilla.samba.org/show_bug.cgi?id=15243 BUG: https://bugzilla.samba.org/show_bug.cgi?id=15266 --- testprogs/blackbox/subunit.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/testprogs/blackbox/subunit.sh b/testprogs/blackbox/subunit.sh index 75a9b5ec7e3..ba4e997c525 100755 --- a/testprogs/blackbox/subunit.sh +++ b/testprogs/blackbox/subunit.sh @@ -121,6 +121,35 @@ testit_grep() return $status } +# This returns 0 if the command gave success and the grep value was found +# num times all other cases return != 0 +testit_grep_count() +{ + name="$1" + shift + grep="$1" + shift + num="$1" + shift + cmdline="$@" + subunit_start_test "$name" + output=$($cmdline 2>&1) + status=$? + if [ x$status != x0 ]; then + printf '%s' "$output" | subunit_fail_test "$name" + return $status + fi + found=$(printf '%s' "$output" | grep -c "$grep") + if [ x"$found" = x"$num" ]; then + subunit_pass_test "$name" + else + printf 'GREP: "%s" found "%d" times, expected "%d" in output:\n%s'\ + "$grep" "$found" "$num" "$output" | + subunit_fail_test "$name" + fi + return $status +} + testit_expect_failure() { name="$1"