gtestutils: Use $G_TEST_TMPDIR as temporary directory when defined

During tests in which we are isolating directories, we may still create
temporary files in the global temporary directory without cleaning them
because the value returned by g_get_tmp_dir() is cached when we isolate
the tests directory to the global TMPDIR.

To ensure that we're always isolating the temporary directories, let's
unset the cached temporary directory once we've defined $G_TEST_TMPDIR
so that the returned value of g_get_tmpdir() can be recomputed using the
test isolated temporary directory.
This commit is contained in:
Marco Trevisan (Treviño)
2022-09-13 03:04:54 +02:00
parent f520066563
commit ca0f04d1c6
4 changed files with 120 additions and 0 deletions

View File

@@ -1205,6 +1205,7 @@ test_dir_make_tmp (void)
name = g_dir_make_tmp ("testXXXXXXtest", &error);
g_assert_no_error (error);
g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
g_assert_true (g_str_has_prefix (name, g_getenv ("G_TEST_TMPDIR")));
ret = g_rmdir (name);
g_assert_cmpint (ret, ==, 0);
g_free (name);
@@ -1212,6 +1213,7 @@ test_dir_make_tmp (void)
name = g_dir_make_tmp (NULL, &error);
g_assert_no_error (error);
g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
g_assert_true (g_str_has_prefix (name, g_getenv ("G_TEST_TMPDIR")));
ret = g_rmdir (name);
g_assert_cmpint (ret, ==, 0);
g_free (name);
@@ -1238,6 +1240,7 @@ test_file_open_tmp (void)
g_assert_cmpint (fd, !=, -1);
g_assert_no_error (error);
g_assert_nonnull (name);
g_assert_true (g_str_has_prefix (name, g_getenv ("G_TEST_TMPDIR")));
unlink (name);
g_free (name);
close (fd);
@@ -1246,6 +1249,7 @@ test_file_open_tmp (void)
g_assert_cmpint (fd, !=, -1);
g_assert_no_error (error);
g_assert_nonnull (name);
g_assert_true (g_str_has_prefix (name, g_getenv ("G_TEST_TMPDIR")));
g_unlink (name);
g_free (name);
close (fd);