Skip to content

Commit

Permalink
lib-test: improve expected error handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Phil Carmody authored and sirainen committed Sep 9, 2016
1 parent 3e2ec5a commit 8154226
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/lib-test/test-common.c
Expand Up @@ -287,15 +287,16 @@ 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;
}

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) {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/lib-test/test-common.h
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/test-failures.c
Expand Up @@ -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();
}
Expand Down

0 comments on commit 8154226

Please sign in to comment.