From: Phil Carmody Date: Wed, 1 Jun 2016 10:57:34 +0000 (+0300) Subject: lib-test: improve expected error handling X-Git-Tag: 2.2.26~290 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8154226c0c2bdc5f95e78d3b26287a50eecbd48d;p=thirdparty%2Fdovecot%2Fcore.git lib-test: improve expected error handling 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 Conflicts: src/lib-test/test-common.c --- diff --git a/src/lib-test/test-common.c b/src/lib-test/test-common.c index ffa627d3c0..5807b0bc98 100644 --- a/src/lib-test/test-common.c +++ b/src/lib-test/test-common.c @@ -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 diff --git a/src/lib-test/test-common.h b/src/lib-test/test-common.h index fc62afb52d..027c3c1a2f 100644 --- a/src/lib-test/test-common.h +++ b/src/lib-test/test-common.h @@ -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); diff --git a/src/lib/test-failures.c b/src/lib/test-failures.c index 47b3bd0ffe..90139b78e8 100644 --- a/src/lib/test-failures.c +++ b/src/lib/test-failures.c @@ -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(); }