From f0032094d24548130ff691fd82c0227694f7de66 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 23 Nov 2023 12:09:54 +0000 Subject: [PATCH] tests: Fix use of TMPDIR in utils test All GLib tests normally respect `TMPDIR` for writing their temporary files to. The utils test, however, contained a hack which overwrote `TMPDIR` so that it could test the behaviour of `g_get_tmp_dir()` for regressions. Unfortunately, that hack affected the whole `utils` test suite, not just the one regression test. Use the new `g_test_trap_subprocess_with_envp()` API to allow the regression test to be run as a subprocess with its environment modified, which allows us to remove the hack affecting the rest of the test suite. Spotted in https://gitlab.gnome.org/GNOME/glib/-/issues/3179#note_1925161. Signed-off-by: Philip Withnall --- glib/tests/utils.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/glib/tests/utils.c b/glib/tests/utils.c index 3abef9312..e71d9a6ef 100644 --- a/glib/tests/utils.c +++ b/glib/tests/utils.c @@ -211,8 +211,27 @@ test_prgname_thread_safety (void) static void test_tmpdir (void) { + char **envp = NULL; + g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=627969"); - g_assert_cmpstr (g_get_tmp_dir (), !=, ""); + g_test_summary ("Test that g_get_tmp_dir() returns a correct default if TMPDIR is set to the empty string"); + + if (g_test_subprocess ()) + { + g_assert_cmpstr (g_get_tmp_dir (), !=, ""); + return; + } + + envp = g_get_environ (); + + envp = g_environ_setenv (g_steal_pointer (&envp), "TMPDIR", "", TRUE); + envp = g_environ_unsetenv (g_steal_pointer (&envp), "TMP"); + envp = g_environ_unsetenv (g_steal_pointer (&envp), "TEMP"); + + g_test_trap_subprocess_with_envp (NULL, (const gchar * const *) envp, + 0, G_TEST_SUBPROCESS_DEFAULT); + g_test_trap_assert_passed (); + g_strfreev (envp); } #if defined(__GNUC__) && (__GNUC__ >= 4) @@ -1309,11 +1328,6 @@ main (int argc, { argv0 = argv[0]; - /* for tmpdir test, need to do this early before g_get_any_init */ - g_setenv ("TMPDIR", "", TRUE); - g_unsetenv ("TMP"); - g_unsetenv ("TEMP"); - /* g_test_init() only calls g_set_prgname() if g_get_prgname() * returns %NULL, but g_get_prgname() on Windows never returns NULL. * So we need to do this by hand to make test_appname() work on