Fix up failure-to-see-expected-message logging

When GLib had been told to expect message X, but then actually saw
message Y, it would log the "did not see expected message" error with
message Y's log level and domain, which makes no sense. Change it to
log with domain "GLib" and G_LOG_LEVEL_CRITICAL instead.

Also, include the expected domain in the error message, so that if
that's the reason why the expectation didn't match, you can tell that
from the error message.

Update glib/tests/testing.c for these changes; for all other test
programs in GLib and elsewhere, this change should not break any
existing tests, it should only improve the output on failure.

https://bugzilla.gnome.org/show_bug.cgi?id=727974
This commit is contained in:
Dan Winship 2014-04-10 11:37:23 -04:00
parent 5463c8cedb
commit be0b921115
2 changed files with 15 additions and 7 deletions

View File

@ -969,9 +969,10 @@ g_logv (const gchar *log_domain,
gchar *expected_message;
mklevel_prefix (level_prefix, expected->log_level);
expected_message = g_strdup_printf ("Did not see expected message %s: %s",
expected_message = g_strdup_printf ("Did not see expected message %s-%s: %s",
expected->log_domain ? expected->log_domain : "**",
level_prefix, expected->pattern);
g_log_default_handler (log_domain, log_level, expected_message, NULL);
g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, expected_message, NULL);
g_free (expected_message);
log_level |= G_LOG_FLAG_FATAL;
@ -1211,9 +1212,10 @@ g_test_assert_expected_messages_internal (const char *domain,
expected = expected_messages->data;
mklevel_prefix (level_prefix, expected->log_level);
message = g_strdup_printf ("Did not see expected message %s: %s",
message = g_strdup_printf ("Did not see expected message %s-%s: %s",
expected->log_domain ? expected->log_domain : "**",
level_prefix, expected->pattern);
g_assertion_message (domain, file, line, func, message);
g_assertion_message (G_LOG_DOMAIN, file, line, func, message);
g_free (message);
}
}

View File

@ -20,6 +20,12 @@
* if advised of the possibility of such damage.
*/
/* We want to distinguish between messages originating from libglib
* and messages originating from this program.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "testing"
#include <glib.h>
#include <stdlib.h>
@ -380,7 +386,7 @@ static void
test_expected_messages_expect_error (void)
{
/* make sure we can't try to expect a g_error() */
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*G_LOG_LEVEL_ERROR*");
g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*G_LOG_LEVEL_ERROR*");
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "this won't work");
g_test_assert_expected_messages ();
}
@ -433,7 +439,7 @@ test_expected_messages (void)
g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0, 0);
g_test_trap_assert_failed ();
g_test_trap_assert_stderr_unmatched ("*should not be reached*");
g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
g_test_trap_assert_stderr ("*GLib-CRITICAL*Did not see expected message testing-CRITICAL*should not be *WARNING*This is a * warning*");
g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0, 0);
g_test_trap_assert_passed ();
@ -449,7 +455,7 @@ test_expected_messages (void)
g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0, 0);
g_test_trap_assert_failed ();
g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
g_test_trap_assert_stderr ("*GLib:ERROR*Did not see expected message testing-CRITICAL*nope*");
}
static void