]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-test: improve expected error handling
authorPhil Carmody <phil@dovecot.fi>
Wed, 1 Jun 2016 10:57:34 +0000 (13:57 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 9 Sep 2016 11:28:09 +0000 (14:28 +0300)
If we expect a specific error string, then when we see it, suppress it.

We do not suppress errors expected by count, because if we get unexpected
errors, then we do not want them suppressed, and we have no way of
distinguishing between the expected and unexpected errors.

This of course favours the use of the expected string version of the helper,
but alas that's not always usable, as you can only expect one at a time.

Additionally, if we failed to see an expected message, then when we no longer
expect to see it, reset the expected message state to not cascade further
test assertion failures.

Signed-off-by: Phil Carmody <phil@dovecot.fi>
Conflicts:
src/lib-test/test-common.c

src/lib-test/test-common.c
src/lib-test/test-common.h
src/lib/test-failures.c

index ffa627d3c0c16b3dd50427173c1d4cfae1c972ca..5807b0bc98c79bcaac28a74eb33b27a98b631b9e 100644 (file)
@@ -287,6 +287,7 @@ void
 test_expect_no_more_errors(void)
 {
        test_assert(expected_errors == 0 && expected_error_str == NULL);
+       i_free_and_null(expected_error_str);
        expected_errors = 0;
 }
 
@@ -294,8 +295,8 @@ static void ATTR_FORMAT(2, 0)
 test_error_handler(const struct failure_context *ctx,
                   const char *format, va_list args)
 {
-       test_dump_rand_state();
-       default_error_handler(ctx, format, args);
+       bool suppress = FALSE;
+
 #ifdef DEBUG
        if (ctx->type == LOG_TYPE_WARNING &&
            strstr(format, "Growing") != NULL) {
@@ -306,13 +307,20 @@ test_error_handler(const struct failure_context *ctx,
 #endif
        if (expected_errors > 0) {
                if (expected_error_str != NULL) {
-                       test_assert(strstr(format, expected_error_str) != NULL);
-                       i_free(expected_error_str);
+                       /* test_assert() will reset test_success if need be. */
+                       suppress = strstr(format, expected_error_str) != NULL;
+                       test_assert(suppress == TRUE);
+                       i_free_and_null(expected_error_str);
                }
                expected_errors--;
-               return;
+       } else {
+               test_success = FALSE;
+       }
+
+       if (!suppress) {
+               test_dump_rand_state();
+               default_error_handler(ctx, format, args);
        }
-       test_success = FALSE;
 }
 
 static void ATTR_FORMAT(2, 0) ATTR_NORETURN
index fc62afb52da2e6cad7b3c3b394da9af57334f269..027c3c1a2f1de3ee4ebb85562a780503f538d14d 100644 (file)
@@ -33,6 +33,10 @@ bool test_has_failed(void);
 void test_expect_errors(unsigned int expected);
 void test_expect_error_string(const char *substr); /* expect just 1 message matching the printf format */
 void test_expect_no_more_errors(void);
+/* Note that test_expect_error{s,_string}() effectively begin with a check equivalent
+   to test_expect_no_more_errors(), so you don't need the latter explicitly if following
+   it with either of the former.*/
+
 void test_end(void);
 
 void test_out(const char *name, bool success);
index 47b3bd0ffe63c5510aacf2df3e01a3a60418d31f..90139b78e8da779b65f07d2a964f1d59a111a3ef 100644 (file)
@@ -50,15 +50,15 @@ static void test_expected(void)
 {
        test_begin("expected messages");
        test_expect_errors(1);
-       i_warning("deliberate warning - be happy you're seeing this");
+       i_warning("deliberate warning - not suppressed");
        test_expect_no_more_errors();
        test_end();
 }
 static void test_expected_str(void)
 {
        test_begin("expected strings in messages");
-       test_expect_error_string("be happy");
-       i_error("deliberate error - be happy you're seeing this");
+       test_expect_error_string("be unhappy");
+       i_error("deliberate error - suppressed - be unhappy if you see this");
        test_expect_no_more_errors();
        test_end();
 }