[gnome-software] tests: Enable G_TEST_OPTION_ISOLATE_DIRS for all tests



commit 1536efc75bd4c554a2615a8e2eb56ce25e17ce60
Author: Philip Withnall <withnall endlessm com>
Date:   Thu May 14 22:40:25 2020 +0100

    tests: Enable G_TEST_OPTION_ISOLATE_DIRS for all tests
    
    This gives stronger guarantees that the tests are not using the user’s
    real home directory, or system paths outside the build root, without the
    test harness explicitly setting that up. This makes the tests less
    likely to cause harm, and more portable and reproducable.
    
    A few tests need paths for access to the system icon theme and the
    shared-mime-info database to be set up, as they need those. The
    shared-mime-info database is needed for detecting content types like
    `.tar.gz` for decompression of appdata or firmware archives, for
    example.
    
    The new APIs in use come from GLib 2.60, but since the CI is still
    running on Debian Stable (GLib 2.58), make the dependency optional for
    now. That’s OK, as this is a ‘safety net’ rather than a hard requirement
    of the tests at the moment.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 lib/gs-self-test.c                      |  6 +++++-
 lib/gs-test.c                           | 33 +++++++++++++++++++++++++++++++++
 lib/gs-test.h                           |  1 +
 plugins/core/gs-self-test.c             | 17 ++++++++++++++++-
 plugins/dpkg/gs-self-test.c             | 13 ++++++++++++-
 plugins/dummy/gs-self-test.c            | 17 ++++++++++++++++-
 plugins/fedora-langpacks/gs-self-test.c |  6 +++++-
 plugins/flatpak/gs-self-test.c          | 17 ++++++++++++++++-
 plugins/fwupd/gs-self-test.c            | 13 ++++++++++++-
 plugins/modalias/gs-self-test.c         |  6 +++++-
 plugins/packagekit/gs-self-test.c       |  6 +++++-
 plugins/repos/gs-self-test.c            |  6 +++++-
 plugins/snap/gs-self-test.c             |  6 +++++-
 src/gs-self-test.c                      |  6 +++++-
 14 files changed, 141 insertions(+), 12 deletions(-)
---
diff --git a/lib/gs-self-test.c b/lib/gs-self-test.c
index 0eb5d380..32951698 100644
--- a/lib/gs-self-test.c
+++ b/lib/gs-self-test.c
@@ -800,7 +800,11 @@ gs_app_list_related_func (void)
 int
 main (int argc, char **argv)
 {
-       g_test_init (&argc, &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 */
diff --git a/lib/gs-test.c b/lib/gs-test.c
index 007273d7..6d9c35c4 100644
--- a/lib/gs-test.c
+++ b/lib/gs-test.c
@@ -35,3 +35,36 @@ gs_test_flush_main_context (void)
        if (cnt > 0)
                g_debug ("cleared %u events", cnt);
 }
+
+/**
+ * gs_test_expose_icon_theme_paths:
+ *
+ * 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.
+ *
+ * As this function calls `g_setenv()`, it must not be called after threads have
+ * been spawned.
+ *
+ * Calling this function is an explicit acknowledgement that the code under test
+ * should be accessing the icon theme.
+ *
+ * Since: 3.38
+ */
+void
+gs_test_expose_icon_theme_paths (void)
+{
+       const gchar * const *data_dirs;
+       g_autoptr(GString) data_dirs_str = NULL;
+       g_autofree gchar *data_dirs_joined = NULL;
+
+       data_dirs = g_get_system_data_dirs ();
+       data_dirs_str = g_string_new ("");
+       for (gsize i = 0; data_dirs[i] != NULL; i++)
+               g_string_append_printf (data_dirs_str, "%s%s/icons",
+                                       (data_dirs_str->len > 0) ? ":" : "",
+                                       data_dirs[i]);
+       data_dirs_joined = g_string_free (g_steal_pointer (&data_dirs_str), FALSE);
+       g_setenv ("GS_SELF_TEST_ICON_THEME_PATH", data_dirs_joined, TRUE);
+}
diff --git a/lib/gs-test.h b/lib/gs-test.h
index 4e620bc5..ff54939f 100644
--- a/lib/gs-test.h
+++ b/lib/gs-test.h
@@ -14,5 +14,6 @@ G_BEGIN_DECLS
 void    gs_test_flush_main_context             (void);
 gchar  *gs_test_get_filename                   (const gchar    *testdatadir,
                                                 const gchar    *filename);
+void    gs_test_expose_icon_theme_paths        (void);
 
 G_END_DECLS
diff --git a/plugins/core/gs-self-test.c b/plugins/core/gs-self-test.c
index efc291c6..cc7cf5cb 100644
--- a/plugins/core/gs-self-test.c
+++ b/plugins/core/gs-self-test.c
@@ -193,7 +193,22 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv, NULL);
+       /* While we use %G_TEST_OPTION_ISOLATE_DIRS to create temporary directories
+        * for each of the tests, we want to use the system MIME registry, assuming
+        * that it exists and correctly has shared-mime-info installed. */
+#if GLIB_CHECK_VERSION(2, 60, 0)
+       g_content_type_set_mime_dirs (NULL);
+#endif
+
+       /* Similarly, add the system-wide icon theme path before it’s
+        * 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);
 
        /* Use a common cache directory for all tests, since the appstream
diff --git a/plugins/dpkg/gs-self-test.c b/plugins/dpkg/gs-self-test.c
index e37778de..38d561e5 100644
--- a/plugins/dpkg/gs-self-test.c
+++ b/plugins/dpkg/gs-self-test.c
@@ -59,7 +59,18 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv, NULL);
+       /* While we use %G_TEST_OPTION_ISOLATE_DIRS to create temporary directories
+        * for each of the tests, we want to use the system MIME registry, assuming
+        * that it exists and correctly has shared-mime-info installed. */
+#if GLIB_CHECK_VERSION(2, 60, 0)
+       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 */
diff --git a/plugins/dummy/gs-self-test.c b/plugins/dummy/gs-self-test.c
index 70e85839..69b74591 100644
--- a/plugins/dummy/gs-self-test.c
+++ b/plugins/dummy/gs-self-test.c
@@ -732,7 +732,22 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv, NULL);
+       /* While we use %G_TEST_OPTION_ISOLATE_DIRS to create temporary directories
+        * for each of the tests, we want to use the system MIME registry, assuming
+        * that it exists and correctly has shared-mime-info installed. */
+#if GLIB_CHECK_VERSION(2, 60, 0)
+       g_content_type_set_mime_dirs (NULL);
+#endif
+
+       /* Similarly, add the system-wide icon theme path before it’s
+        * 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);
        g_setenv ("GS_XMLB_VERBOSE", "1", TRUE);
 
diff --git a/plugins/fedora-langpacks/gs-self-test.c b/plugins/fedora-langpacks/gs-self-test.c
index 895b4947..f476939c 100644
--- a/plugins/fedora-langpacks/gs-self-test.c
+++ b/plugins/fedora-langpacks/gs-self-test.c
@@ -56,7 +56,11 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &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 */
diff --git a/plugins/flatpak/gs-self-test.c b/plugins/flatpak/gs-self-test.c
index 31466bdd..1a1ad445 100644
--- a/plugins/flatpak/gs-self-test.c
+++ b/plugins/flatpak/gs-self-test.c
@@ -1727,7 +1727,22 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv, NULL);
+       /* While we use %G_TEST_OPTION_ISOLATE_DIRS to create temporary directories
+        * for each of the tests, we want to use the system MIME registry, assuming
+        * that it exists and correctly has shared-mime-info installed. */
+#if GLIB_CHECK_VERSION(2, 60, 0)
+       g_content_type_set_mime_dirs (NULL);
+#endif
+
+       /* Similarly, add the system-wide icon theme path before it’s
+        * 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);
        g_setenv ("GS_XMLB_VERBOSE", "1", TRUE);
        g_setenv ("GS_SELF_TEST_PLUGIN_ERROR_FAIL_HARD", "1", TRUE);
diff --git a/plugins/fwupd/gs-self-test.c b/plugins/fwupd/gs-self-test.c
index 8628043d..5078a30a 100644
--- a/plugins/fwupd/gs-self-test.c
+++ b/plugins/fwupd/gs-self-test.c
@@ -67,7 +67,18 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &argv, NULL);
+       /* While we use %G_TEST_OPTION_ISOLATE_DIRS to create temporary directories
+        * for each of the tests, we want to use the system MIME registry, assuming
+        * that it exists and correctly has shared-mime-info installed. */
+#if GLIB_CHECK_VERSION(2, 60, 0)
+       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 */
diff --git a/plugins/modalias/gs-self-test.c b/plugins/modalias/gs-self-test.c
index 8cf00fa9..23a58fda 100644
--- a/plugins/modalias/gs-self-test.c
+++ b/plugins/modalias/gs-self-test.c
@@ -54,7 +54,11 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &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);
        g_setenv ("GS_SELF_TEST_DUMMY_ENABLE", "1", TRUE);
 
diff --git a/plugins/packagekit/gs-self-test.c b/plugins/packagekit/gs-self-test.c
index af142a8b..b3b7cfc8 100644
--- a/plugins/packagekit/gs-self-test.c
+++ b/plugins/packagekit/gs-self-test.c
@@ -243,7 +243,11 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &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 */
diff --git a/plugins/repos/gs-self-test.c b/plugins/repos/gs-self-test.c
index 236cb3fa..d4778601 100644
--- a/plugins/repos/gs-self-test.c
+++ b/plugins/repos/gs-self-test.c
@@ -46,7 +46,11 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &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);
 
        /* dummy data */
diff --git a/plugins/snap/gs-self-test.c b/plugins/snap/gs-self-test.c
index cb8ce07d..99180455 100644
--- a/plugins/snap/gs-self-test.c
+++ b/plugins/snap/gs-self-test.c
@@ -343,7 +343,11 @@ main (int argc, char **argv)
                NULL
        };
 
-       g_test_init (&argc, &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 */
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index be6408df..0ed05c5b 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -94,7 +94,11 @@ gs_content_rating_from_locale (void)
 int
 main (int argc, char **argv)
 {
-       g_test_init (&argc, &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 */


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