From a3f26bd50974d127022150c166bcce3fecb17219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 16 Jan 2023 20:23:22 +0100 Subject: [PATCH] testing: Add TAP test for Bail out! messages --- glib/tests/testing-helper.c | 15 +++++++ glib/tests/testing.c | 84 +++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/glib/tests/testing-helper.c b/glib/tests/testing-helper.c index 4479bc7be..c4cbe0058 100644 --- a/glib/tests/testing-helper.c +++ b/glib/tests/testing-helper.c @@ -51,6 +51,12 @@ test_fail (void) g_test_fail (); } +static void +test_error (void) +{ + g_error ("This should error out\nBecause it's just wrong!"); +} + static void test_fail_printf (void) { @@ -157,6 +163,15 @@ main (int argc, { g_test_add_func ("/fail", test_fail); } + else if (g_strcmp0 (argv1, "error") == 0) + { + g_test_add_func ("/error", test_error); + } + else if (g_strcmp0 (argv1, "error-and-pass") == 0) + { + g_test_add_func ("/error", test_error); + g_test_add_func ("/pass", test_pass); + } else if (g_strcmp0 (argv1, "fail-printf") == 0) { g_test_add_func ("/fail-printf", test_fail_printf); diff --git a/glib/tests/testing.c b/glib/tests/testing.c index d46100296..5854c3cf5 100644 --- a/glib/tests/testing.c +++ b/glib/tests/testing.c @@ -1697,6 +1697,88 @@ test_tap_message (void) g_ptr_array_unref (argv); } +static void +test_tap_error (void) +{ + const char *testing_helper; + GPtrArray *argv; + GError *error = NULL; + int status; + gchar *output; + + g_test_summary ("Test that g_error() generates Bail out TAP output of a test."); + + testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL); + + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "error"); + 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_nonnull (error); + + const char *expected_tap_header = "\n# 1..1\n"; + const char *interesting_lines = strstr (output, expected_tap_header); + g_assert_nonnull (interesting_lines); + interesting_lines += strlen (expected_tap_header); + + g_assert_cmpstr (interesting_lines, ==, "# Bail out! GLib-FATAL-ERROR: This should error out\n" + "Because it's just wrong!\n"); + + g_free (output); + g_ptr_array_unref (argv); + g_clear_error (&error); +} + +static void +test_tap_error_and_pass (void) +{ + const char *testing_helper; + GPtrArray *argv; + GError *error = NULL; + int status; + gchar *output; + + g_test_summary ("Test that g_error() generates Bail out TAP output of a test."); + + testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL); + + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "error-and-pass"); + 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_nonnull (error); + + const char *expected_tap_header = "\n# 1..2\n"; + const char *interesting_lines = strstr (output, expected_tap_header); + g_assert_nonnull (interesting_lines); + interesting_lines += strlen (expected_tap_header); + + g_assert_cmpstr (interesting_lines, ==, "# Bail out! GLib-FATAL-ERROR: This should error out\n" + "Because it's just wrong!\n"); + + g_free (output); + g_ptr_array_unref (argv); + g_clear_error (&error); +} + static void test_init_no_argv0 (void) { @@ -1828,6 +1910,8 @@ main (int argc, g_test_add_func ("/tap", test_tap); g_test_add_func ("/tap/summary", test_tap_summary); g_test_add_func ("/tap/message", test_tap_message); + g_test_add_func ("/tap/error", test_tap_error); + g_test_add_func ("/tap/error-and-pass", test_tap_error_and_pass); g_test_add_func ("/init/no_argv0", test_init_no_argv0);