[gnome-software/wip/mcrha/plugin-loader-self-test: 24/24] tests: Introduce and use gs_test_init()




commit 059632c0fec6b323c89f8b37571257f41374a625
Author: Milan Crha <mcrha redhat com>
Date:   Mon Oct 11 13:56:20 2021 +0200

    tests: Introduce and use gs_test_init()
    
    To be used to initialize self tests, covering common parts of the setup.
    
    It also disables the ODRS data download during the tests by unsetting
    the server address in the settings.

 lib/gs-self-test.c                      |  9 +--------
 lib/gs-test.c                           | 34 +++++++++++++++++++++++++++++++--
 lib/gs-test.h                           |  2 ++
 plugins/core/gs-self-test.c             | 10 +---------
 plugins/dpkg/gs-self-test.c             | 10 +---------
 plugins/dummy/gs-self-test.c            | 10 +---------
 plugins/fedora-langpacks/gs-self-test.c | 10 +---------
 plugins/flatpak/gs-self-test.c          | 10 +---------
 plugins/fwupd/gs-self-test.c            | 10 +---------
 plugins/modalias/gs-self-test.c         | 10 +---------
 plugins/packagekit/gs-self-test.c       | 10 +---------
 plugins/repos/gs-self-test.c            | 10 +---------
 plugins/snap/gs-self-test.c             | 10 +---------
 src/gs-self-test.c                      | 10 +---------
 14 files changed, 46 insertions(+), 109 deletions(-)
---
diff --git a/lib/gs-self-test.c b/lib/gs-self-test.c
index 97e636a6a..ee4da9ed8 100644
--- a/lib/gs-self-test.c
+++ b/lib/gs-self-test.c
@@ -819,14 +819,7 @@ main (int argc, char **argv)
 {
        g_autoptr(GsDebug) debug = gs_debug_new (NULL, TRUE, FALSE);
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+       gs_test_init (&argc, &argv);
 
        /* tests go here */
        g_test_add_func ("/gnome-software/lib/utils{url}", gs_utils_url_func);
diff --git a/lib/gs-test.c b/lib/gs-test.c
index 1f79e6af7..2b7d54743 100644
--- a/lib/gs-test.c
+++ b/lib/gs-test.c
@@ -10,6 +10,37 @@
 
 #include "gs-test.h"
 
+/**
+ * gs_test_init:
+ *
+ * Initializes the environment with the common settings for the test,
+ * as a replacement for the g_test_init(), which is called as well.
+ *
+ * Since: 42
+ **/
+void
+gs_test_init (gint *pargc,
+             gchar ***pargv)
+{
+       g_autoptr(GSettings) settings = NULL;
+
+       g_setenv ("GSETTINGS_BACKEND", "memory", FALSE);
+       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+
+       /* To not download ODRS data during the test */
+       settings = g_settings_new ("org.gnome.software");
+       g_settings_set_string (settings, "review-server", "");
+
+       g_test_init (pargc, pargv,
+#if GLIB_CHECK_VERSION(2, 60, 0)
+                    G_TEST_OPTION_ISOLATE_DIRS,
+#endif
+                    NULL);
+
+       /* only critical and error are fatal */
+       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+}
+
 gchar *
 gs_test_get_filename (const gchar *testdatadir, const gchar *filename)
 {
@@ -42,8 +73,7 @@ gs_test_flush_main_context (void)
  *
  * Calculate and set the `GS_SELF_TEST_ICON_THEME_PATH` environment variable
  * to include the current system icon theme paths. This is designed to be called
- * before calling `g_test_init()` with `G_TEST_OPTION_ISOLATE_DIRS`, which will
- * clear the system icon theme paths.
+ * before calling `gs_test_init()`, which will clear the system icon theme paths.
  *
  * As this function calls `g_setenv()`, it must not be called after threads have
  * been spawned.
diff --git a/lib/gs-test.h b/lib/gs-test.h
index 82a33b4f8..2d97fb964 100644
--- a/lib/gs-test.h
+++ b/lib/gs-test.h
@@ -12,6 +12,8 @@
 
 G_BEGIN_DECLS
 
+void    gs_test_init                           (gint           *pargc,
+                                                gchar        ***pargv);
 void    gs_test_flush_main_context             (void);
 gchar  *gs_test_get_filename                   (const gchar    *testdatadir,
                                                 const gchar    *filename);
diff --git a/plugins/core/gs-self-test.c b/plugins/core/gs-self-test.c
index 16894b915..63204a373 100644
--- a/plugins/core/gs-self-test.c
+++ b/plugins/core/gs-self-test.c
@@ -212,12 +212,7 @@ main (int argc, char **argv)
         * overwritten by %G_TEST_OPTION_ISOLATE_DIRS. */
        gs_test_expose_icon_theme_paths ();
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+       gs_test_init (&argc, &argv);
 
        /* Use a common cache directory for all tests, since the appstream
         * plugin uses it and cannot be reinitialised for each test. */
@@ -251,9 +246,6 @@ main (int argc, char **argv)
                "</components>\n";
        g_setenv ("GS_SELF_TEST_APPSTREAM_XML", xml, TRUE);
 
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
-
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
        gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR);
diff --git a/plugins/dpkg/gs-self-test.c b/plugins/dpkg/gs-self-test.c
index e3a76b230..09c7971b8 100644
--- a/plugins/dpkg/gs-self-test.c
+++ b/plugins/dpkg/gs-self-test.c
@@ -67,15 +67,7 @@ main (int argc, char **argv)
        g_content_type_set_mime_dirs (NULL);
 #endif
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
-
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+       gs_test_init (&argc, &argv);
 
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
diff --git a/plugins/dummy/gs-self-test.c b/plugins/dummy/gs-self-test.c
index 8d80f08d2..92d054f1e 100644
--- a/plugins/dummy/gs-self-test.c
+++ b/plugins/dummy/gs-self-test.c
@@ -749,12 +749,7 @@ main (int argc, char **argv)
         * overwritten by %G_TEST_OPTION_ISOLATE_DIRS. */
        gs_test_expose_icon_theme_paths ();
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+       gs_test_init (&argc, &argv);
        g_setenv ("GS_XMLB_VERBOSE", "1", TRUE);
 
        /* set all the things required as a dummy test harness */
@@ -825,9 +820,6 @@ main (int argc, char **argv)
                "</components>\n");
        g_setenv ("GS_SELF_TEST_APPSTREAM_XML", xml, TRUE);
 
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
-
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
        g_signal_connect (plugin_loader, "status-changed",
diff --git a/plugins/fedora-langpacks/gs-self-test.c b/plugins/fedora-langpacks/gs-self-test.c
index 895f2f8b0..f9268758b 100644
--- a/plugins/fedora-langpacks/gs-self-test.c
+++ b/plugins/fedora-langpacks/gs-self-test.c
@@ -67,15 +67,7 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
-
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+       gs_test_init (&argc, &argv);
 
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
diff --git a/plugins/flatpak/gs-self-test.c b/plugins/flatpak/gs-self-test.c
index 2789d4f70..fccc11027 100644
--- a/plugins/flatpak/gs-self-test.c
+++ b/plugins/flatpak/gs-self-test.c
@@ -1910,12 +1910,7 @@ main (int argc, char **argv)
         * overwritten by %G_TEST_OPTION_ISOLATE_DIRS. */
        gs_test_expose_icon_theme_paths ();
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+       gs_test_init (&argc, &argv);
        g_setenv ("GS_XMLB_VERBOSE", "1", TRUE);
        g_setenv ("GS_SELF_TEST_PLUGIN_ERROR_FAIL_HARD", "1", TRUE);
 
@@ -1940,9 +1935,6 @@ main (int argc, char **argv)
                "</components>\n");
        g_setenv ("GS_SELF_TEST_APPSTREAM_XML", xml, TRUE);
 
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
-
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
        gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR);
diff --git a/plugins/fwupd/gs-self-test.c b/plugins/fwupd/gs-self-test.c
index 2c0bf10b2..f5f222b6c 100644
--- a/plugins/fwupd/gs-self-test.c
+++ b/plugins/fwupd/gs-self-test.c
@@ -75,15 +75,7 @@ main (int argc, char **argv)
        g_content_type_set_mime_dirs (NULL);
 #endif
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
-
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+       gs_test_init (&argc, &argv);
 
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
diff --git a/plugins/modalias/gs-self-test.c b/plugins/modalias/gs-self-test.c
index 92bf88f00..bf9e7cd96 100644
--- a/plugins/modalias/gs-self-test.c
+++ b/plugins/modalias/gs-self-test.c
@@ -55,12 +55,7 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+       gs_test_init (&argc, &argv);
        g_setenv ("GS_SELF_TEST_DUMMY_ENABLE", "1", TRUE);
 
        xml = g_strdup_printf ("<?xml version=\"1.0\"?>\n"
@@ -86,9 +81,6 @@ main (int argc, char **argv)
        g_assert (tmp_root != NULL);
        g_setenv ("GS_SELF_TEST_CACHEDIR", tmp_root, TRUE);
 
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
-
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
        gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR);
diff --git a/plugins/packagekit/gs-self-test.c b/plugins/packagekit/gs-self-test.c
index 4a9cff58e..5bea5d0d8 100644
--- a/plugins/packagekit/gs-self-test.c
+++ b/plugins/packagekit/gs-self-test.c
@@ -248,15 +248,7 @@ main (int argc, char **argv)
         * %G_TEST_OPTION_ISOLATE_DIRS resets the XDG system dirs. */
        g_settings_schema_source_get_default ();
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
-
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+       gs_test_init (&argc, &argv);
 
        /* generic tests go here */
        g_test_add_func ("/gnome-software/markdown", gs_markdown_func);
diff --git a/plugins/repos/gs-self-test.c b/plugins/repos/gs-self-test.c
index ff5b3e726..259d0fb64 100644
--- a/plugins/repos/gs-self-test.c
+++ b/plugins/repos/gs-self-test.c
@@ -47,21 +47,13 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+       gs_test_init (&argc, &argv);
 
        /* dummy data */
        reposdir = gs_test_get_filename (TESTDATADIR, "yum.repos.d");
        g_assert (reposdir != NULL);
        g_setenv ("GS_SELF_TEST_REPOS_DIR", reposdir, TRUE);
 
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
-
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
        gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR);
diff --git a/plugins/snap/gs-self-test.c b/plugins/snap/gs-self-test.c
index 621f6f8f5..061b57b98 100644
--- a/plugins/snap/gs-self-test.c
+++ b/plugins/snap/gs-self-test.c
@@ -354,15 +354,7 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
-
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+       gs_test_init (&argc, &argv);
 
        /* we can only load this once per process */
        plugin_loader = gs_plugin_loader_new ();
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index b664656c0..83fce99f0 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -43,15 +43,7 @@ gs_css_func (void)
 int
 main (int argc, char **argv)
 {
-       g_test_init (&argc, &argv,
-#if GLIB_CHECK_VERSION(2, 60, 0)
-                    G_TEST_OPTION_ISOLATE_DIRS,
-#endif
-                    NULL);
-       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
-
-       /* only critical and error are fatal */
-       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+       gs_test_init (&argc, &argv);
 
        /* tests go here */
        g_test_add_func ("/gnome-software/src/css", gs_css_func);


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