From 133aed18752f0d7af761a38f39f8dbb7ba90c967 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 19 Sep 2025 11:42:46 +0100 Subject: [PATCH] tests: Use G_TEST_OPTION_ISOLATE_DIRS for utils tests Another test isolated. This does mean moving one of the test cases from the suite out into a separate suite, as it explicitly relies on running without `G_TEST_OPTION_ISOLATE_DIRS`, and I think that makes sense. The other test cases run fine when isolated. Signed-off-by: Philip Withnall --- glib/tests/meson.build | 1 + glib/tests/utils-unisolated.c | 110 ++++++++++++++++++++++++++++++++++ glib/tests/utils.c | 73 +--------------------- 3 files changed, 112 insertions(+), 72 deletions(-) create mode 100644 glib/tests/utils-unisolated.c diff --git a/glib/tests/meson.build b/glib/tests/meson.build index 76b031106..564497c90 100644 --- a/glib/tests/meson.build +++ b/glib/tests/meson.build @@ -185,6 +185,7 @@ glib_tests = { 'c_standards': c_standards.keys(), }, 'utils-isolated' : {}, + 'utils-unisolated' : {}, 'unicode' : {}, 'unicode-encoding' : {}, 'unicode-normalize': {}, diff --git a/glib/tests/utils-unisolated.c b/glib/tests/utils-unisolated.c new file mode 100644 index 000000000..01d4a3bc7 --- /dev/null +++ b/glib/tests/utils-unisolated.c @@ -0,0 +1,110 @@ +/* Unit tests for utilities + * Copyright (C) 2010 Red Hat, Inc. + * + * SPDX-License-Identifier: LicenseRef-old-glib-tests + * + * This work is provided "as is"; redistribution and modification + * in whole or in part, in any medium, physical or electronic is + * permitted without restriction. + * + * This work is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In no event shall the authors or contributors be liable for any + * direct, indirect, incidental, special, exemplary, or consequential + * damages (including, but not limited to, procurement of substitute + * goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether + * in contract, strict liability, or tort (including negligence or + * otherwise) arising in any way out of the use of this software, even + * if advised of the possibility of such damage. + * + * Author: Matthias Clasen + */ + +#include "glib.h" + +static void +test_xdg_dirs (void) +{ +#ifdef G_OS_UNIX + char *xdg = NULL; + const char *dir; + const char * const *dirs; + char *s = NULL; + + xdg = g_strdup (g_getenv ("XDG_CONFIG_HOME")); + if (!xdg) + xdg = g_build_filename (g_get_home_dir (), ".config", NULL); + + dir = g_get_user_config_dir (); + + g_assert_cmpstr (dir, ==, xdg); + g_free (xdg); + + xdg = g_strdup (g_getenv ("XDG_DATA_HOME")); + if (!xdg) + xdg = g_build_filename (g_get_home_dir (), ".local", "share", NULL); + + dir = g_get_user_data_dir (); + + g_assert_cmpstr (dir, ==, xdg); + g_free (xdg); + + xdg = g_strdup (g_getenv ("XDG_CACHE_HOME")); + if (!xdg) + xdg = g_build_filename (g_get_home_dir (), ".cache", NULL); + + dir = g_get_user_cache_dir (); + + g_assert_cmpstr (dir, ==, xdg); + g_free (xdg); + + xdg = g_strdup (g_getenv ("XDG_STATE_HOME")); + if (!xdg) + xdg = g_build_filename (g_get_home_dir (), ".local/state", NULL); + + dir = g_get_user_state_dir (); + + g_assert_cmpstr (dir, ==, xdg); + g_free (xdg); + + xdg = g_strdup (g_getenv ("XDG_RUNTIME_DIR")); + if (!xdg) + xdg = g_strdup (g_get_user_cache_dir ()); + + dir = g_get_user_runtime_dir (); + + g_assert_cmpstr (dir, ==, xdg); + g_free (xdg); + + xdg = (gchar *)g_getenv ("XDG_CONFIG_DIRS"); + if (!xdg) + xdg = "/etc/xdg"; + + dirs = g_get_system_config_dirs (); + + s = g_strjoinv (":", (gchar **)dirs); + + g_assert_cmpstr (s, ==, xdg); + + g_free (s); +#else + g_test_skip ("User special dirs are not defined using environment variables on non-Unix systems"); +#endif +} + +int +main (int argc, + char *argv[]) +{ + /* Note: We do *not* use G_TEST_OPTION_ISOLATE_DIRS here, as we want to test + * the calculation of the XDG dirs from a real environment. */ + g_test_init (&argc, &argv, NULL); + + /* You almost certainly want to add a test to glib/tests/utils.c instead of here. */ + g_test_add_func ("/utils/xdgdirs", test_xdg_dirs); + + return g_test_run (); +} diff --git a/glib/tests/utils.c b/glib/tests/utils.c index 1e7ec417e..97148b8a7 100644 --- a/glib/tests/utils.c +++ b/glib/tests/utils.c @@ -817,74 +817,6 @@ test_hostname (void) g_assert_true (g_utf8_validate (name, -1, NULL)); } -#ifdef G_OS_UNIX -static void -test_xdg_dirs (void) -{ - gchar *xdg; - const gchar *dir; - const gchar * const *dirs; - gchar *s; - - xdg = g_strdup (g_getenv ("XDG_CONFIG_HOME")); - if (!xdg) - xdg = g_build_filename (g_get_home_dir (), ".config", NULL); - - dir = g_get_user_config_dir (); - - g_assert_cmpstr (dir, ==, xdg); - g_free (xdg); - - xdg = g_strdup (g_getenv ("XDG_DATA_HOME")); - if (!xdg) - xdg = g_build_filename (g_get_home_dir (), ".local", "share", NULL); - - dir = g_get_user_data_dir (); - - g_assert_cmpstr (dir, ==, xdg); - g_free (xdg); - - xdg = g_strdup (g_getenv ("XDG_CACHE_HOME")); - if (!xdg) - xdg = g_build_filename (g_get_home_dir (), ".cache", NULL); - - dir = g_get_user_cache_dir (); - - g_assert_cmpstr (dir, ==, xdg); - g_free (xdg); - - xdg = g_strdup (g_getenv ("XDG_STATE_HOME")); - if (!xdg) - xdg = g_build_filename (g_get_home_dir (), ".local/state", NULL); - - dir = g_get_user_state_dir (); - - g_assert_cmpstr (dir, ==, xdg); - g_free (xdg); - - xdg = g_strdup (g_getenv ("XDG_RUNTIME_DIR")); - if (!xdg) - xdg = g_strdup (g_get_user_cache_dir ()); - - dir = g_get_user_runtime_dir (); - - g_assert_cmpstr (dir, ==, xdg); - g_free (xdg); - - xdg = (gchar *)g_getenv ("XDG_CONFIG_DIRS"); - if (!xdg) - xdg = "/etc/xdg"; - - dirs = g_get_system_config_dirs (); - - s = g_strjoinv (":", (gchar **)dirs); - - g_assert_cmpstr (s, ==, xdg); - - g_free (s); -} -#endif - static void test_special_dir (void) { @@ -1353,7 +1285,7 @@ main (int argc, */ g_set_prgname (argv[0]); - g_test_init (&argc, &argv, NULL); + g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); g_test_add_func ("/utils/language-names", test_language_names); g_test_add_func ("/utils/locale-variants", test_locale_variants); @@ -1374,9 +1306,6 @@ main (int argc, g_test_add_func ("/utils/username", test_username); g_test_add_func ("/utils/realname", test_realname); g_test_add_func ("/utils/hostname", test_hostname); -#ifdef G_OS_UNIX - g_test_add_func ("/utils/xdgdirs", test_xdg_dirs); -#endif g_test_add_func ("/utils/specialdir", test_special_dir); g_test_add_func ("/utils/specialdir/desktop", test_desktop_special_dir); g_test_add_func ("/utils/os-info", test_os_info);