Merge branch 'wip/test-fail-convenience' into 'main'

gtestutils: Add more convenience functions

See merge request GNOME/glib!2215
This commit is contained in:
Philip Withnall 2021-08-19 09:35:48 +00:00
commit 4961c1c3b5
9 changed files with 198 additions and 52 deletions

View File

@ -3569,8 +3569,11 @@ g_test_get_filename
g_test_get_dir
g_test_fail
g_test_fail_printf
g_test_skip
g_test_skip_printf
g_test_incomplete
g_test_incomplete_printf
g_test_failed
g_test_message
g_test_bug_base

View File

@ -482,8 +482,7 @@ assert_strings_equivalent (const gchar *expected,
if (g_str_equal (expected_words[i], result_words[j]))
goto got_it;
g_test_message ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
g_test_fail ();
g_test_fail_printf ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
got_it:
continue;

View File

@ -341,9 +341,7 @@ test_ip_async (GSocketFamily family)
data = create_server (family, echo_server_thread, FALSE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -454,9 +452,7 @@ test_ip_sync (GSocketFamily family)
data = create_server (family, echo_server_thread, FALSE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -594,9 +590,7 @@ test_ip_sync_dgram (GSocketFamily family)
echo_server_dgram_thread, FALSE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -974,9 +968,7 @@ test_close_graceful (void)
data = create_server (family, graceful_server_thread, FALSE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -1084,9 +1076,7 @@ test_ipv6_v4mapped (void)
data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -1145,9 +1135,7 @@ test_timed_wait (void)
data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -1232,9 +1220,7 @@ test_fd_reuse (void)
data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -1938,9 +1924,7 @@ test_credentials_tcp_client (void)
data = create_server (family, echo_server_thread, FALSE, &error);
if (error != NULL)
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
g_clear_error (&error);
return;
}
@ -2045,13 +2029,9 @@ test_credentials_tcp_server (void)
goto beach;
skip:
{
gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Failed to create server: %s", error->message);
goto beach;
goto beach;
}
beach:
{
g_clear_error (&error);

View File

@ -111,11 +111,7 @@ test_trash_symlinks (void)
if (!g_file_test (target, G_FILE_TEST_IS_DIR))
{
gchar *message;
message = g_strdup_printf ("Directory '%s' does not exist", target);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Directory '%s' does not exist", target);
g_free (target);
return;
}
@ -124,12 +120,7 @@ test_trash_symlinks (void)
if (target_mount == NULL)
{
gchar *message;
message = g_strdup_printf ("Unable to determine mount point for %s",
target);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Unable to determine mount point for %s", target);
g_free (target);
return;
}
@ -144,11 +135,7 @@ test_trash_symlinks (void)
if (tmp_mount == NULL)
{
gchar *message;
message = g_strdup_printf ("Unable to determine mount point for %s", tmp);
g_test_skip (message);
g_free (message);
g_test_skip_printf ("Unable to determine mount point for %s", tmp);
g_unix_mount_free (target_mount);
g_free (target);
g_free (tmp);

View File

@ -1039,6 +1039,8 @@ g_test_log (GTestLogType lbit,
g_print (" # TODO %s\n", string2 ? string2 : "");
else if (result == G_TEST_RUN_SKIPPED)
g_print (" # SKIP %s\n", string2 ? string2 : "");
else if (result == G_TEST_RUN_FAILURE && string2 != NULL)
g_print (" - %s\n", string2);
else
g_print ("\n");
}
@ -2401,12 +2403,42 @@ g_test_add_vtable (const char *testpath,
*
* If not called from inside a test, this function does nothing.
*
* Note that unlike g_test_skip() and g_test_incomplete(), this
* function does not log a message alongside the test failure.
* If details of the test failure are available, either log them with
* g_test_message() before g_test_fail(), or use g_test_fail_printf()
* instead.
*
* Since: 2.30
**/
void
g_test_fail (void)
{
test_run_success = G_TEST_RUN_FAILURE;
g_clear_pointer (&test_run_msg, g_free);
}
/**
* g_test_fail_printf:
* @format: the format string
* @...: printf-like arguments to @format
*
* Equivalent to g_test_fail(), but also record a message like
* g_test_skip_printf().
*
* Since: 2.70
**/
void
g_test_fail_printf (const char *format,
...)
{
va_list args;
test_run_success = G_TEST_RUN_FAILURE;
va_start (args, format);
g_free (test_run_msg);
test_run_msg = g_strdup_vprintf (format, args);
va_end (args);
}
/**
@ -2434,6 +2466,29 @@ g_test_incomplete (const gchar *msg)
test_run_msg = g_strdup (msg);
}
/**
* g_test_incomplete_printf:
* @format: the format string
* @...: printf-like arguments to @format
*
* Equivalent to g_test_incomplete(), but the explanation is formatted
* as if by g_strdup_printf().
*
* Since: 2.70
*/
void
g_test_incomplete_printf (const char *format,
...)
{
va_list args;
test_run_success = G_TEST_RUN_INCOMPLETE;
va_start (args, format);
g_free (test_run_msg);
test_run_msg = g_strdup_vprintf (format, args);
va_end (args);
}
/**
* g_test_skip:
* @msg: (nullable): explanation
@ -2457,6 +2512,29 @@ g_test_skip (const gchar *msg)
test_run_msg = g_strdup (msg);
}
/**
* g_test_skip_printf:
* @format: the format string
* @...: printf-like arguments to @format
*
* Equivalent to g_test_skip(), but the explanation is formatted
* as if by g_strdup_printf().
*
* Since: 2.70
*/
void
g_test_skip_printf (const char *format,
...)
{
va_list args;
test_run_success = G_TEST_RUN_SKIPPED;
va_start (args, format);
g_free (test_run_msg);
test_run_msg = g_strdup_vprintf (format, args);
va_end (args);
}
/**
* g_test_failed:
*

View File

@ -345,10 +345,19 @@ const char * g_test_get_path (void);
/* tell about failure */
GLIB_AVAILABLE_IN_2_30
void g_test_fail (void);
GLIB_AVAILABLE_IN_2_70
void g_test_fail_printf (const char *format,
...) G_GNUC_PRINTF (1, 2);
GLIB_AVAILABLE_IN_2_38
void g_test_incomplete (const gchar *msg);
GLIB_AVAILABLE_IN_2_70
void g_test_incomplete_printf (const char *format,
...) G_GNUC_PRINTF (1, 2);
GLIB_AVAILABLE_IN_2_38
void g_test_skip (const gchar *msg);
GLIB_AVAILABLE_IN_2_70
void g_test_skip_printf (const char *format,
...) G_GNUC_PRINTF (1, 2);
GLIB_AVAILABLE_IN_2_38
gboolean g_test_failed (void);
GLIB_AVAILABLE_IN_2_38

View File

@ -435,9 +435,8 @@ expect_log_writer (GLogLevelFlags log_level,
char *str;
str = g_log_writer_format_fields (log_level, fields, n_fields, FALSE);
g_test_message ("Unexpected message: %s", str);
g_test_fail_printf ("Unexpected message: %s", str);
g_free (str);
g_test_fail ();
}
return G_LOG_WRITER_HANDLED;
@ -529,9 +528,8 @@ test_structured_logging_roundtrip1 (void)
ExpectedMessage *expected = expected_messages->data;
str = g_log_writer_format_fields (0, expected->fields, expected->n_fields, FALSE);
g_test_message ("Unexpected message: %s", str);
g_test_fail_printf ("Unexpected message: %s", str);
g_free (str);
g_test_fail ();
}
}

View File

@ -35,18 +35,40 @@ test_skip (void)
g_test_skip ("not enough tea");
}
static void
test_skip_printf (void)
{
const char *beverage = "coffee";
g_test_skip_printf ("not enough %s", beverage);
}
static void
test_fail (void)
{
g_test_fail ();
}
static void
test_fail_printf (void)
{
g_test_fail_printf ("this test intentionally left failing");
}
static void
test_incomplete (void)
{
g_test_incomplete ("mind reading not implemented yet");
}
static void
test_incomplete_printf (void)
{
const char *operation = "telekinesis";
g_test_incomplete_printf ("%s not implemented yet", operation);
}
static void
test_summary (void)
{
@ -91,14 +113,26 @@ main (int argc,
{
g_test_add_func ("/skip", test_skip);
}
else if (g_strcmp0 (argv1, "skip-printf") == 0)
{
g_test_add_func ("/skip-printf", test_skip_printf);
}
else if (g_strcmp0 (argv1, "incomplete") == 0)
{
g_test_add_func ("/incomplete", test_incomplete);
}
else if (g_strcmp0 (argv1, "incomplete-printf") == 0)
{
g_test_add_func ("/incomplete-printf", test_incomplete_printf);
}
else if (g_strcmp0 (argv1, "fail") == 0)
{
g_test_add_func ("/fail", test_fail);
}
else if (g_strcmp0 (argv1, "fail-printf") == 0)
{
g_test_add_func ("/fail-printf", test_fail_printf);
}
else if (g_strcmp0 (argv1, "all-non-failures") == 0)
{
g_test_add_func ("/pass", test_pass);

View File

@ -1100,6 +1100,25 @@ test_tap (void)
g_free (output);
g_ptr_array_unref (argv);
g_test_message ("skip with printf format");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);
g_ptr_array_add (argv, "skip-printf");
g_ptr_array_add (argv, "--tap");
g_ptr_array_add (argv, NULL);
g_spawn_sync (NULL, (char **) argv->pdata, NULL,
G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL, &output, NULL, &status,
&error);
g_assert_no_error (error);
g_spawn_check_wait_status (status, &error);
g_assert_no_error (error);
g_assert_nonnull (strstr (output, "\nok 1 /skip-printf # SKIP not enough coffee\n"));
g_free (output);
g_ptr_array_unref (argv);
g_test_message ("incomplete");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);
@ -1119,6 +1138,25 @@ test_tap (void)
g_free (output);
g_ptr_array_unref (argv);
g_test_message ("incomplete with printf format");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);
g_ptr_array_add (argv, "incomplete-printf");
g_ptr_array_add (argv, "--tap");
g_ptr_array_add (argv, NULL);
g_spawn_sync (NULL, (char **) argv->pdata, NULL,
G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL, &output, NULL, &status,
&error);
g_assert_no_error (error);
g_spawn_check_wait_status (status, &error);
g_assert_no_error (error);
g_assert_nonnull (strstr (output, "\nnot ok 1 /incomplete-printf # TODO telekinesis not implemented yet\n"));
g_free (output);
g_ptr_array_unref (argv);
g_test_message ("fail");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);
@ -1139,6 +1177,26 @@ test_tap (void)
g_clear_error (&error);
g_ptr_array_unref (argv);
g_test_message ("fail with message");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);
g_ptr_array_add (argv, "fail-printf");
g_ptr_array_add (argv, "--tap");
g_ptr_array_add (argv, NULL);
g_spawn_sync (NULL, (char **) argv->pdata, NULL,
G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL, &output, NULL, &status,
&error);
g_assert_no_error (error);
g_spawn_check_wait_status (status, &error);
g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
g_assert_nonnull (strstr (output, "\nnot ok 1 /fail-printf - this test intentionally left failing\n"));
g_free (output);
g_clear_error (&error);
g_ptr_array_unref (argv);
g_test_message ("all");
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);