1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
From: Philip Withnall <pwithnall@gnome.org>
Date: Fri, 19 Sep 2025 13:31:06 +0100
Subject: tests: Factor out a helper function in the utils tests
This introduces no functional changes, it just factors out a helper to
set a mock `user-dirs.dirs` file, so that we can do the same in multiple
tests.
It does add a little more error checking to the helper code though; in
particular it checks that the test is running with
`G_TEST_OPTION_ISOLATE_DIRS`.
Signed-off-by: Philip Withnall <pwithnall@gnome.org>
Origin: upstream, 2.86.1, commit:fa1ed222e2a91f7eca0171ff04f87070af7f1086
---
glib/tests/utils.c | 53 ++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 36 insertions(+), 17 deletions(-)
diff --git a/glib/tests/utils.c b/glib/tests/utils.c
index 00ba3e9..157e5b7 100644
--- a/glib/tests/utils.c
+++ b/glib/tests/utils.c
@@ -844,10 +844,42 @@ test_user_special_dirs_desktop (void)
g_assert_nonnull (dir2);
}
+#if !defined(HAVE_COCOA) && !defined(G_OS_WIN32)
+#define USES_USER_DIRS_DIRS 1
+#endif
+
+/* Write a mock user-dirs.dirs file with the given @content. This must be used
+ * with `G_TEST_OPTION_ISOLATE_DIRS`. See
+ * [`xdg-user-dirs-update(1)`](man:xdg-user-dirs-update(1)) for the specification. */
+#ifdef USES_USER_DIRS_DIRS
+static void
+set_mock_user_dirs_dirs_file (const char *content)
+{
+ char *user_dirs_file = NULL;
+ char *user_dirs_parent = NULL;
+ const char *config_dir;
+ GError *local_error = NULL;
+
+ config_dir = g_get_user_config_dir ();
+
+ /* Double check we’re running under G_TEST_OPTION_ISOLATE_DIRS and not about
+ * to overwrite actual user data. */
+ g_assert (g_str_has_prefix (config_dir, g_get_tmp_dir ()));
+
+ user_dirs_file = g_build_filename (config_dir, "user-dirs.dirs", NULL);
+ user_dirs_parent = g_path_get_dirname (user_dirs_file);
+ g_mkdir_with_parents (user_dirs_parent, 0700);
+ g_file_set_contents (user_dirs_file, content, -1, &local_error);
+ g_assert_no_error (local_error);
+ g_free (user_dirs_file);
+ g_free (user_dirs_parent);
+}
+#endif /* USES_USER_DIRS_DIRS */
+
static void
test_user_special_dirs_load_unlocked (void)
{
-#if defined(HAVE_COCOA) || defined(G_OS_WIN32)
+#ifndef USES_USER_DIRS_DIRS
g_test_skip ("The user-dirs.dirs parser is not used on this platform.");
#else
g_test_summary ("Tests error and corner cases of user-dirs.dirs content.");
@@ -855,28 +887,15 @@ test_user_special_dirs_load_unlocked (void)
if (g_test_subprocess ())
{
- gchar *user_dirs_file;
- char *user_dirs_parent = NULL;
- const gchar *config_dir;
const gchar *dir;
gchar *expected;
- gboolean result;
-
- config_dir = g_get_user_config_dir ();
- user_dirs_file = g_build_filename (config_dir, "user-dirs.dirs", NULL);
- user_dirs_parent = g_path_get_dirname (user_dirs_file);
- g_mkdir_with_parents (user_dirs_parent, 0700);
- result = g_file_set_contents (user_dirs_file,
- "XDG_DESKTOP_DIR = \"/root\"\nXDG_DESKTOP_DIR = \"$HOMER/Desktop\"\n"
+
+ set_mock_user_dirs_dirs_file ("XDG_DESKTOP_DIR = \"/root\"\nXDG_DESKTOP_DIR = \"$HOMER/Desktop\"\n"
"XDG_DOCUMENTS_DIR = \"$HOME\"\n"
"XDG_DOWNLOAD_DIR = \"$HOME/Downloads\"\n"
"XDG_MUSIC_DIR = \"///\"\n"
"XDG_PICTURES_DIR = \"$HOME/Pictures\"\n"
- "XDG_PICTURES_DIR = \"/\"\nXDG_DOWNLOAD_DIR = \"/dev/null\n",
- -1, NULL);
- g_assert_true (result);
- g_free (user_dirs_file);
- g_free (user_dirs_parent);
+ "XDG_PICTURES_DIR = \"/\"\nXDG_DOWNLOAD_DIR = \"/dev/null\n");
g_reload_user_special_dirs_cache ();
|