[glib/wip/test-fail-convenience: 1/4] gtestutils: Allow failing a test with a printf-style message




commit 7d28a2b36a69ac74d0fa91342c4c56fb1d9f0b43
Author: Simon McVittie <smcv collabora com>
Date:   Thu Aug 5 11:42:22 2021 +0100

    gtestutils: Allow failing a test with a printf-style message
    
    This allows a pattern like
    
        g_test_message ("cannot reticulate splines: %s", error->message);
        g_test_fail ();
    
    to be replaced by the simpler
    
        g_test_fail_message ("cannot reticulate splines: %s", error->message);
    
    with the secondary benefit of making the message available to TAP
    consumers as part of the "not ok" message.
    
    Signed-off-by: Simon McVittie <smcv collabora com>

 glib/gtestutils.c           | 25 +++++++++++++++++++++++++
 glib/gtestutils.h           |  3 +++
 glib/tests/testing-helper.c | 10 ++++++++++
 glib/tests/testing.c        | 20 ++++++++++++++++++++
 4 files changed, 58 insertions(+)
---
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index 8aafc703b..c56ae8ddd 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -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");
         }
@@ -2407,6 +2409,29 @@ void
 g_test_fail (void)
 {
   test_run_success = G_TEST_RUN_FAILURE;
+  g_clear_pointer (&test_run_msg, g_free);
+}
+
+/**
+ * g_test_fail_message:
+ * @format: the format string
+ * @...:    printf-like arguments to @format
+ *
+ * Equivalent to g_test_fail(), but also record a message like g_test_skip().
+ *
+ * Since: 2.70
+ **/
+void
+g_test_fail_message (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);
 }
 
 /**
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 5be4ce215..5acdf3f2c 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -345,6 +345,9 @@ 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_message             (const char *format,
+                                         ...) G_GNUC_PRINTF (1, 2);
 GLIB_AVAILABLE_IN_2_38
 void    g_test_incomplete               (const gchar *msg);
 GLIB_AVAILABLE_IN_2_38
diff --git a/glib/tests/testing-helper.c b/glib/tests/testing-helper.c
index cc6afe589..a3a2089c7 100644
--- a/glib/tests/testing-helper.c
+++ b/glib/tests/testing-helper.c
@@ -41,6 +41,12 @@ test_fail (void)
   g_test_fail ();
 }
 
+static void
+test_fail_message (void)
+{
+  g_test_fail_message ("this test intentionally left failing");
+}
+
 static void
 test_incomplete (void)
 {
@@ -99,6 +105,10 @@ main (int   argc,
     {
       g_test_add_func ("/fail", test_fail);
     }
+  else if (g_strcmp0 (argv1, "fail-message") == 0)
+    {
+      g_test_add_func ("/fail-message", test_fail_message);
+    }
   else if (g_strcmp0 (argv1, "all-non-failures") == 0)
     {
       g_test_add_func ("/pass", test_pass);
diff --git a/glib/tests/testing.c b/glib/tests/testing.c
index fd10f4891..7554fc5aa 100644
--- a/glib/tests/testing.c
+++ b/glib/tests/testing.c
@@ -1139,6 +1139,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-message");
+  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-message - 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);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]