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 <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall 2023-11-23 12:09:54 +00:00
parent 6a6b36bbc7
commit f0032094d2

View File

@ -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