tests: Only use g_mkstemp() in /tmp rather than current directory

It’s not entirely clear from the documentation, but `g_mkstemp()` (and
`g_mkdtemp()`) operate in the current directory, rather than the system
temporary directory.

This meant these tests were all writing files to the build directory.
This is messy, though thankfully not a correctness issue or a race
because `g_mkstemp()` guarantees to return a unique file for each
caller.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall
2025-03-19 16:32:46 +00:00
parent 581a0b1285
commit 4345eb2756
3 changed files with 57 additions and 35 deletions

View File

@@ -2843,7 +2843,7 @@ test_measure_async (void)
static void
test_load_bytes (void)
{
gchar filename[] = "g_file_load_bytes_XXXXXX";
char *filename = NULL;
GError *error = NULL;
GBytes *bytes;
GFile *file;
@@ -2851,6 +2851,7 @@ test_load_bytes (void)
int fd;
int ret;
filename = g_build_filename (g_get_tmp_dir (), "g_file_load_bytes_XXXXXX", NULL);
fd = g_mkstemp (filename);
g_assert_cmpint (fd, !=, -1);
len = strlen ("test_load_bytes");
@@ -2870,6 +2871,7 @@ test_load_bytes (void)
g_bytes_unref (bytes);
g_object_unref (file);
g_free (filename);
}
typedef struct
@@ -2899,12 +2901,13 @@ static void
test_load_bytes_async (void)
{
LoadBytesAsyncData data = { 0 };
gchar filename[] = "g_file_load_bytes_XXXXXX";
char *filename = NULL;
GError *error = NULL;
int len;
int fd;
int ret;
filename = g_build_filename (g_get_tmp_dir (), "g_file_load_bytes_XXXXXX", NULL);
fd = g_mkstemp (filename);
g_assert_cmpint (fd, !=, -1);
len = strlen ("test_load_bytes_async");
@@ -2926,6 +2929,7 @@ test_load_bytes_async (void)
g_object_unref (data.file);
g_bytes_unref (data.bytes);
g_main_loop_unref (data.main_loop);
g_free (filename);
}
#if GLIB_SIZEOF_SIZE_T > 4
@@ -2987,15 +2991,19 @@ check_testfile_4gb_contents (const char *data,
static void
test_load_contents_4gb (void)
{
char filename[] = "g_file_load_contents_4gb_XXXXXX";
char *filename = NULL;
GError *error = NULL;
gboolean result;
char *data;
gsize len;
GFile *file;
filename = g_build_filename (g_get_tmp_dir (), "g_file_load_contents_4gb_XXXXXX", NULL);
if (!create_testfile_4gb_or_skip (filename))
return;
{
g_free (filename);
return;
}
file = g_file_new_for_path (filename);
result = g_file_load_contents (file, NULL, &data, &len, NULL, &error);
@@ -3008,6 +3016,7 @@ test_load_contents_4gb (void)
g_free (data);
g_object_unref (file);
g_free (filename);
}
static void
@@ -3026,7 +3035,7 @@ load_contents_4gb_cb (GObject *object,
static void
test_load_contents_4gb_async (void)
{
char filename[] = "g_file_load_contents_4gb_async_XXXXXX";
char *filename = NULL;
GFile *file;
GAsyncResult *async_result = NULL;
GError *error = NULL;
@@ -3034,8 +3043,12 @@ test_load_contents_4gb_async (void)
gsize len;
gboolean ret;
filename = g_build_filename (g_get_tmp_dir (), "g_file_load_contents_4gb_async_XXXXXX", NULL);
if (!create_testfile_4gb_or_skip (filename))
return;
{
g_free (filename);
return;
}
file = g_file_new_for_path (filename);
g_file_load_contents_async (file, NULL, load_contents_4gb_cb, &async_result);
@@ -3054,18 +3067,23 @@ test_load_contents_4gb_async (void)
g_free (data);
g_object_unref (async_result);
g_object_unref (file);
g_free (filename);
}
static void
test_load_bytes_4gb (void)
{
char filename[] = "g_file_load_bytes_4gb_XXXXXX";
char *filename = NULL;
GError *error = NULL;
GBytes *bytes;
GFile *file;
filename = g_build_filename (g_get_tmp_dir (), "g_file_load_bytes_4gb_XXXXXX", NULL);
if (!create_testfile_4gb_or_skip (filename))
return;
{
g_free (filename);
return;
}
file = g_file_new_for_path (filename);
bytes = g_file_load_bytes (file, NULL, NULL, &error);
@@ -3078,6 +3096,7 @@ test_load_bytes_4gb (void)
g_bytes_unref (bytes);
g_object_unref (file);
g_free (filename);
}
static void

View File

@@ -1342,7 +1342,7 @@ test_mkstemp (void)
g_free (name);
/* Test normal case */
name = g_strdup ("testXXXXXXtest"),
name = g_build_filename (g_get_tmp_dir (), "testXXXXXXtest", NULL),
fd = g_mkstemp (name);
g_assert_cmpint (fd, !=, -1);
g_assert_null (strstr (name, "XXXXXX"));
@@ -1358,8 +1358,8 @@ test_mkstemp (void)
strcpy (template, "foobarXXX");
g_assert_cmpint (g_mkstemp (template), ==, -1);
strcpy (template, "fooXXXXXX");
fd = g_mkstemp (template);
name = g_build_filename (g_get_tmp_dir (), "fooXXXXXX", NULL);
fd = g_mkstemp (name);
g_assert_cmpint (fd, !=, -1);
result = write (fd, hello, hellolen);
g_assert_cmpint (result, !=, -1);
@@ -1374,15 +1374,16 @@ test_mkstemp (void)
g_assert_cmpstr (chars, ==, hello);
close (fd);
remove (template);
remove (name);
g_free (name);
/* Check that is does not work for "fooXXXXXX.pdf" */
strcpy (template, "fooXXXXXX.pdf");
fd = g_mkstemp (template);
/* Check that it works for "fooXXXXXX.pdf" */
name = g_build_filename (g_get_tmp_dir (), "fooXXXXXX.pdf", NULL);
fd = g_mkstemp (name);
g_assert_cmpint (fd, !=, -1);
close (fd);
remove (template);
remove (name);
g_free (name);
}
static void
@@ -1390,10 +1391,10 @@ test_mkdtemp (void)
{
gint fd;
gchar *ret;
gchar *name;
gchar *name, *name2;
char template[32];
name = g_strdup ("testXXXXXXtest"),
name = g_build_filename (g_get_tmp_dir (), "testXXXXXXtest", NULL),
ret = g_mkdtemp (name);
g_assert_true (ret == name);
g_assert_null (strstr (name, "XXXXXX"));
@@ -1411,27 +1412,29 @@ test_mkdtemp (void)
strcpy (template, "foodir");
g_assert_null (g_mkdtemp (template));
strcpy (template, "fooXXXXXX");
ret = g_mkdtemp (template);
name = g_build_filename (g_get_tmp_dir (), "fooXXXXXX", NULL);
ret = g_mkdtemp (name);
g_assert_nonnull (ret);
g_assert_true (ret == template);
g_assert_false (g_file_test (template, G_FILE_TEST_IS_REGULAR));
g_assert_true (g_file_test (template, G_FILE_TEST_IS_DIR));
g_assert_true (ret == name);
g_assert_false (g_file_test (name, G_FILE_TEST_IS_REGULAR));
g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
strcat (template, "/abc");
fd = g_open (template, O_WRONLY | O_CREAT, 0600);
name2 = g_build_filename (name, "abc", NULL);
fd = g_open (name2, O_WRONLY | O_CREAT, 0600);
g_assert_cmpint (fd, !=, -1);
close (fd);
g_assert_true (g_file_test (template, G_FILE_TEST_IS_REGULAR));
g_assert_cmpint (g_unlink (template), !=, -1);
g_assert_true (g_file_test (name2, G_FILE_TEST_IS_REGULAR));
g_assert_cmpint (g_unlink (name2), !=, -1);
g_free (name2);
template[9] = '\0';
g_assert_cmpint (g_rmdir (template), !=, -1);
g_assert_cmpint (g_rmdir (name), !=, -1);
g_free (name);
strcpy (template, "fooXXXXXX.dir");
g_assert_nonnull (g_mkdtemp (template));
g_assert_true (g_file_test (template, G_FILE_TEST_IS_DIR));
g_rmdir (template);
name = g_build_filename (g_get_tmp_dir (), "fooXXXXXX.dir", NULL);
g_assert_nonnull (g_mkdtemp (name));
g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
g_rmdir (name);
g_free (name);
}
static void

View File

@@ -1592,7 +1592,7 @@ test_save (void)
ok = g_key_file_load_from_data (kf, data, strlen (data), 0, NULL);
g_assert_true (ok);
file = g_strdup ("key_file_XXXXXX");
file = g_build_filename (g_get_tmp_dir (), "key_file_XXXXXX", NULL);
fd = g_mkstemp (file);
g_assert_cmpint (fd, !=, -1);
ok = g_close (fd, &error);