]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
testprogs: Add testit_grep_count() helper
authorVolker Lendecke <vl@samba.org>
Wed, 9 Nov 2022 13:04:23 +0000 (14:04 +0100)
committerJule Anger <janger@samba.org>
Fri, 13 Jan 2023 09:27:09 +0000 (09:27 +0000)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
(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

index 75a9b5ec7e3cf58397f1af3c1243ed298d90a9f4..ba4e997c5253a0926ddb5ea17b4ead4cee344197 100755 (executable)
@@ -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"