[dconf: 6/9] tests: Stop using deprecated g_test_trap_fork() API



commit a8dcd4f0decf78afdd22caaaad4ac746e075c3b0
Author: Philip Withnall <withnall endlessm com>
Date:   Tue Aug 14 16:03:03 2018 +0100

    tests: Stop using deprecated g_test_trap_fork() API
    
    Use g_test_subprocess() and g_test_trap_subprocess() instead. This
    requires splitting up some of the unit tests, since subprocesses are
    handled at a per-test level, and you can’t trivially fork multiple
    different subprocesses from a single test (like you could with fork()).
    
    While fork was safe to use on POSIX (and dconf only targets POSIX), the
    fact that we had to redefine GLIB_VERSION_MIN_REQUIRED to hide the
    deprecation errors was not doing wonders for the maintainability of the
    tests.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 tests/dbus.c   | 45 ++++++++++++++++++++++++++++---------
 tests/engine.c | 71 +++++++++++++++++++++++++++++++---------------------------
 tests/shm.c    | 26 ++++++++++++---------
 3 files changed, 89 insertions(+), 53 deletions(-)
---
diff --git a/tests/dbus.c b/tests/dbus.c
index 980d2b0..032cb04 100644
--- a/tests/dbus.c
+++ b/tests/dbus.c
@@ -1,5 +1,3 @@
-#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_36 /* Suppress deprecation warnings */
-
 #include <string.h>
 #include <glib.h>
 #include <stdlib.h>
@@ -147,7 +145,7 @@ dconf_engine_handle_dbus_signal (GBusType     bus_type,
 }
 
 static void
-test_creation_error (void)
+test_creation_error_sync_with_error (void)
 {
   if (g_getenv ("DISPLAY") == NULL || g_strcmp0 (g_getenv ("DISPLAY"), "") == 0)
     {
@@ -156,7 +154,7 @@ test_creation_error (void)
     }
 
   /* Sync with 'error' */
-  if (g_test_trap_fork (0, 0))
+  if (g_test_subprocess ())
     {
       GError *error = NULL;
       GVariant *reply;
@@ -170,13 +168,24 @@ test_creation_error (void)
       g_assert (reply == NULL);
       g_assert (error != NULL);
       g_assert (strstr (error->message, "some nonsense"));
-      exit (0);
+      return;
     }
 
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
+}
+
+static void
+test_creation_error_sync_without_error (void)
+{
+  if (g_getenv ("DISPLAY") == NULL || g_strcmp0 (g_getenv ("DISPLAY"), "") == 0)
+    {
+      g_test_skip ("FIXME: D-Bus tests do not work on CI at the moment");
+      return;
+    }
 
   /* Sync without 'error' */
-  if (g_test_trap_fork (0, 0))
+  if (g_test_subprocess ())
     {
       GVariant *reply;
 
@@ -187,13 +196,24 @@ test_creation_error (void)
                                                 g_variant_new ("()"), G_VARIANT_TYPE ("(as)"), NULL);
 
       g_assert (reply == NULL);
-      exit (0);
+      return;
     }
 
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
+}
+
+static void
+test_creation_error_async (void)
+{
+  if (g_getenv ("DISPLAY") == NULL || g_strcmp0 (g_getenv ("DISPLAY"), "") == 0)
+    {
+      g_test_skip ("FIXME: D-Bus tests do not work on CI at the moment");
+      return;
+    }
 
   /* Async */
-  if (g_test_trap_fork (0, 0))
+  if (g_test_subprocess ())
     {
       DConfEngineCallHandle *handle;
       GError *error = NULL;
@@ -222,9 +242,10 @@ test_creation_error (void)
       else
         g_assert (error != NULL);
 
-      exit (0);
+      return;
     }
 
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
 }
 
@@ -506,7 +527,11 @@ main (int argc, char **argv)
 
   /* test_creation_error absolutely must come first */
   if (!g_str_equal (DBUS_BACKEND, "/libdbus-1"))
-    g_test_add_func (DBUS_BACKEND "/creation/error", test_creation_error);
+    {
+      g_test_add_func (DBUS_BACKEND "/creation/error/sync-with-error", test_creation_error_sync_with_error);
+      g_test_add_func (DBUS_BACKEND "/creation/error/sync-without-error", 
test_creation_error_sync_without_error);
+      g_test_add_func (DBUS_BACKEND "/creation/error/async", test_creation_error_async);
+    }
 
   g_test_add_func (DBUS_BACKEND "/sync-call/success", test_sync_call_success);
   g_test_add_func (DBUS_BACKEND "/sync-call/error", test_sync_call_error);
diff --git a/tests/engine.c b/tests/engine.c
index b351126..32b43f3 100644
--- a/tests/engine.c
+++ b/tests/engine.c
@@ -1,7 +1,5 @@
 #define _GNU_SOURCE
 
-#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_36 /* Suppress deprecation warnings */
-
 #include "../engine/dconf-engine.h"
 #include "../engine/dconf-engine-profile.h"
 #include "../common/dconf-enums.h"
@@ -144,47 +142,39 @@ test_five_times (const gchar *filename,
   g_free (expected_names);
 }
 
+typedef struct
+{
+  const gchar *profile_path;
+  const gchar *expected_stderr_pattern;
+} ProfileParserOpenData;
+
 static void
-test_profile_parser (void)
+test_profile_parser_errors (gconstpointer test_data)
 {
+  const ProfileParserOpenData *data = test_data;
   DConfEngineSource **sources;
   gint n_sources;
 
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+  if (g_test_subprocess ())
     {
       g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
 
-      sources = dconf_engine_profile_open (SRCDIR "/profile/this-file-does-not-exist", &n_sources);
+      sources = dconf_engine_profile_open (data->profile_path, &n_sources);
       g_assert_cmpint (n_sources, ==, 0);
       g_assert (sources == NULL);
-      exit (0);
+      return;
     }
-  g_test_trap_assert_passed ();
-  g_test_trap_assert_stderr ("*WARNING*: unable to open named profile*");
-
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-    {
-      g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
 
-      sources = dconf_engine_profile_open (SRCDIR "/profile/broken-profile", &n_sources);
-      g_assert_cmpint (n_sources, ==, 0);
-      g_assert (sources == NULL);
-      exit (0);
-    }
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
-  g_test_trap_assert_stderr ("*WARNING*: unknown dconf database*unknown dconf database*");
-
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-    {
-      g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
+  g_test_trap_assert_stderr (data->expected_stderr_pattern);
+}
 
-      sources = dconf_engine_profile_open (SRCDIR "/profile/gdm", &n_sources);
-      g_assert_cmpint (n_sources, ==, 0);
-      g_assert (sources == NULL);
-      exit (0);
-    }
-  g_test_trap_assert_passed ();
-  g_test_trap_assert_stderr ("*WARNING*: unknown dconf database*unknown dconf database*");
+static void
+test_profile_parser (void)
+{
+  DConfEngineSource **sources;
+  gint n_sources;
 
   test_five_times (SRCDIR "/profile/empty-profile", 0);
   test_five_times (SRCDIR "/profile/test-profile", 1, "test");
@@ -459,7 +449,7 @@ test_service_source (void)
   gboolean reopened;
 
   /* Make sure we deal with errors from the service sensibly */
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+  if (g_test_subprocess ())
     {
       g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
 
@@ -470,8 +460,10 @@ test_service_source (void)
       g_assert (source->locks == NULL);
       reopened = dconf_engine_source_refresh (source);
 
-      exit (0);
+      return;
     }
+
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
   g_test_trap_assert_stderr ("*WARNING*: unable to open file*unknown/nil*expect degraded performance*");
 
@@ -546,7 +538,7 @@ test_system_source (void)
   g_assert (source != NULL);
 
   /* Check to see that we get the warning about the missing file. */
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+  if (g_test_subprocess ())
     {
       g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
 
@@ -571,8 +563,10 @@ test_system_source (void)
 
       dconf_engine_source_free (source);
 
-      exit (0);
+      return;
     }
+
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
   /* Check that we only saw the warning, but only one time. */
   g_test_trap_assert_stderr ("*this gvdb does not exist; expect degraded performance*");
@@ -1960,6 +1954,14 @@ test_sync (void)
 int
 main (int argc, char **argv)
 {
+  const ProfileParserOpenData profile_parser0 =
+    { SRCDIR "/profile/this-file-does-not-exist", "*WARNING*: unable to open named profile*" };
+  const ProfileParserOpenData profile_parser1 =
+    { SRCDIR "/profile/broken-profile", "*WARNING*: unknown dconf database*unknown dconf database*" };
+  const ProfileParserOpenData profile_parser2 =
+    { SRCDIR "/profile/gdm", "*WARNING*: unknown dconf database*unknown dconf database*" };
+  int retval;
+
   g_setenv ("XDG_RUNTIME_DIR", "/RUNTIME/", TRUE);
   g_setenv ("XDG_CONFIG_HOME", "/HOME/.config", TRUE);
   g_unsetenv ("DCONF_PROFILE");
@@ -1968,6 +1970,9 @@ main (int argc, char **argv)
 
   g_test_init (&argc, &argv, NULL);
 
+  g_test_add_data_func ("/engine/profile-parser/errors/0", &profile_parser0, test_profile_parser_errors);
+  g_test_add_data_func ("/engine/profile-parser/errors/1", &profile_parser1, test_profile_parser_errors);
+  g_test_add_data_func ("/engine/profile-parser/errors/2", &profile_parser2, test_profile_parser_errors);
   g_test_add_func ("/engine/profile-parser", test_profile_parser);
   g_test_add_func ("/engine/signal-threadsafety", test_signal_threadsafety);
   g_test_add_func ("/engine/sources/user", test_user_source);
diff --git a/tests/shm.c b/tests/shm.c
index 66e67a2..a0cf67e 100644
--- a/tests/shm.c
+++ b/tests/shm.c
@@ -1,7 +1,5 @@
 #define _GNU_SOURCE
 
-#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_36 /* Suppress deprecation warnings */
-
 #include "../common/dconf-paths.h"
 #include <glib/gstdio.h>
 #include <sys/stat.h>
@@ -19,7 +17,7 @@ test_mkdir_fail (void)
 {
   guint8 *shm;
 
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+  if (g_test_subprocess ())
     {
       gchar *evil;
       gint fd;
@@ -36,8 +34,10 @@ test_mkdir_fail (void)
       g_unlink (evil);
       g_free (evil);
 
-      exit (0);
+      return;
     }
+
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
   g_test_trap_assert_stderr ("*unable to create directory*");
 }
@@ -64,7 +64,7 @@ test_open_and_flag (void)
 static void
 test_invalid_name (void)
 {
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+  if (g_test_subprocess ())
     {
       guint8 *shm;
 
@@ -73,8 +73,10 @@ test_invalid_name (void)
       shm = dconf_shm_open ("foo/bar");
       g_assert (shm == NULL);
       g_assert (dconf_shm_is_flagged (shm));
-      exit (0);
+      return;
     }
+
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
   g_test_trap_assert_stderr ("*unable to create*foo/bar*");
 }
@@ -107,7 +109,7 @@ pwrite (int fd, const void *buf, size_t count, off_t offset)
 static void
 test_out_of_space_open (void)
 {
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+  if (g_test_subprocess ())
     {
       guint8 *shm;
 
@@ -117,8 +119,10 @@ test_out_of_space_open (void)
       shm = dconf_shm_open ("foo");
       g_assert (shm == NULL);
       g_assert (dconf_shm_is_flagged (shm));
-      exit (0);
+      return;
     }
+
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
   g_test_trap_assert_stderr ("*failed to allocate*foo*");
 }
@@ -126,14 +130,16 @@ test_out_of_space_open (void)
 static void
 test_out_of_space_flag (void)
 {
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+  if (g_test_subprocess ())
     {
       g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
       should_fail_pwrite = TRUE;
 
       dconf_shm_flag ("foo");
-      exit (0);
+      return;
     }
+
+  g_test_trap_subprocess (NULL, 0, 0);
   g_test_trap_assert_passed ();
 }
 


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