tests: Move various test paths to g_get_tmp_dir()

Rather than creating files in the current directory. This is a bit
neater, and avoids races between parallel invocations of the unit tests
if the file names aren’t guaranteed to be unique (e.g. by using
`g_mkstemp()`).

Add `G_TEST_OPTION_ISOLATE_DIRS` too, to make sure we use a unique
subdirectory of `g_get_tmp_dir()`. This means that paths like
`g_get_tmp_dir() / some-file` are guaranteed to be race-free even if the
filename is not unique, because the test tmp dir now is.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall 2025-03-19 16:42:19 +00:00
parent 4d2b873211
commit e7b820d66c
No known key found for this signature in database
GPG Key ID: C5C42CFB268637CA
6 changed files with 36 additions and 30 deletions

View File

@ -96,7 +96,8 @@ test_filesystem_readonly (gconstpointer with_mount_monitor)
GFile *mounted_file;
GUnixMountMonitor *mount_monitor = NULL;
gchar *bindfs, *fusermount;
gchar *curdir, *dir_to_mount, *dir_mountpoint;
const char *curdir;
char *dir_to_mount = NULL, *dir_mountpoint = NULL;
gchar *file_in_mount, *file_in_mountpoint;
GError *error = NULL;
@ -127,7 +128,7 @@ test_filesystem_readonly (gconstpointer with_mount_monitor)
return;
}
curdir = g_get_current_dir ();
curdir = g_get_tmp_dir ();
dir_to_mount = g_build_filename (curdir, "dir_bindfs_to_mount", NULL);
file_in_mount = g_build_filename (dir_to_mount, "example.txt", NULL);
dir_mountpoint = g_build_filename (curdir, "dir_bindfs_mountpoint", NULL);
@ -141,7 +142,6 @@ test_filesystem_readonly (gconstpointer with_mount_monitor)
g_free (dir_mountpoint);
g_free (file_in_mount);
g_free (dir_to_mount);
g_free (curdir);
g_free (fusermount);
g_free (bindfs);
return;
@ -164,7 +164,6 @@ test_filesystem_readonly (gconstpointer with_mount_monitor)
g_free (dir_mountpoint);
g_free (file_in_mount);
g_free (dir_to_mount);
g_free (curdir);
g_free (fusermount);
g_free (bindfs);
@ -197,7 +196,6 @@ test_filesystem_readonly (gconstpointer with_mount_monitor)
g_free (dir_mountpoint);
g_free (file_in_mount);
g_free (dir_to_mount);
g_free (curdir);
g_free (fusermount);
g_free (bindfs);
@ -241,7 +239,6 @@ test_filesystem_readonly (gconstpointer with_mount_monitor)
g_free (bindfs);
g_free (fusermount);
g_free (curdir);
g_free (dir_to_mount);
g_free (dir_mountpoint);
g_free (file_in_mount);
@ -254,7 +251,7 @@ main (int argc, char *argv[])
/* To avoid unnecessary D-Bus calls, see http://goo.gl/ir56j2 */
g_setenv ("GIO_USE_VFS", "local", FALSE);
g_test_init (&argc, &argv, NULL);
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=787731");

View File

@ -3289,7 +3289,7 @@ main (int argc, char *argv[])
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
if (!g_test_subprocess ())
{

View File

@ -32,8 +32,6 @@
#include <string.h>
#include <sys/stat.h>
#define DEFAULT_TEST_DIR "testdir_live-g-file"
#define PATTERN_FILE_SIZE 0x10000
#define TEST_HANDLE_SPECIAL TRUE
@ -1342,9 +1340,10 @@ int
main (int argc, char *argv[])
{
static gboolean only_create_struct;
const char *target_path;
char *target_path = NULL;
GError *error;
GOptionContext *context;
int retval;
static GOptionEntry cmd_entries[] = {
{"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
@ -1365,7 +1364,7 @@ main (int argc, char *argv[])
posix_compat = FALSE;
/* strip all gtester-specific args */
g_test_init (&argc, &argv, NULL);
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
/* no extra parameters specified, assume we're executed from glib test suite */
if (argc < 2)
@ -1374,7 +1373,7 @@ main (int argc, char *argv[])
verbose = TRUE;
write_test = TRUE;
only_create_struct = FALSE;
target_path = DEFAULT_TEST_DIR;
target_path = g_build_filename (g_get_tmp_dir (), "testdir_live-g-file", NULL);
#ifdef G_PLATFORM_WIN32
posix_compat = FALSE;
#else
@ -1394,7 +1393,7 @@ main (int argc, char *argv[])
/* remaining arg should is the target path; we don't care of the extra args here */
if (argc >= 2)
target_path = strdup (argv[1]);
target_path = g_strdup (argv[1]);
if (! target_path)
{
@ -1471,6 +1470,9 @@ main (int argc, char *argv[])
g_test_add_data_func ("/live-g-file/final_clean", target_path,
prep_clean_structure);
return g_test_run ();
retval = g_test_run ();
g_free (target_path);
return retval;
}

View File

@ -153,6 +153,7 @@ test_corner_cases (void)
gchar *message, **messages;
GError *error = NULL;
GBookmarkFile *bookmark;
char *path = NULL;
bookmark = g_bookmark_file_new ();
@ -669,8 +670,10 @@ test_corner_cases (void)
g_clear_error (&error);
/* g_bookmark_file_to_file() */
g_assert_true (g_bookmark_file_to_file (bookmark, "a", &error));
path = g_build_filename (g_get_tmp_dir (), "corner-case-bookmark-file.xbel", NULL);
g_assert_true (g_bookmark_file_to_file (bookmark, path, &error));
g_assert_no_error (error);
g_free (path);
/* g_bookmark_file_remove_group() */
g_assert_false (g_bookmark_file_remove_group (bookmark, "a", NULL, NULL));

View File

@ -664,7 +664,7 @@ test_build_filenamev (void)
static void
test_mkdir_with_parents_1 (const gchar *base)
{
char *p0 = g_build_filename (base, "fum", NULL);
char *p0 = g_build_filename (g_get_tmp_dir (), base, "fum", NULL);
char *p1 = g_build_filename (p0, "tem", NULL);
char *p2 = g_build_filename (p1, "zap", NULL);
FILE *f;
@ -817,6 +817,7 @@ test_mkdir_with_parents (void)
#ifndef G_OS_WIN32
gboolean can_override_dac = check_cap_dac_override (NULL);
#endif
g_test_message ("Checking g_mkdir_with_parents() in subdir ./hum/");
test_mkdir_with_parents_1 ("hum");
g_remove ("hum");
@ -828,9 +829,6 @@ test_mkdir_with_parents (void)
g_remove ("hii");
cwd = g_get_current_dir ();
g_test_message ("Checking g_mkdir_with_parents() in cwd: %s", cwd);
test_mkdir_with_parents_1 (cwd);
new_path = g_build_filename (cwd, "new", NULL);
g_assert_cmpint (g_mkdir_with_parents (new_path, 0), ==, 0);
g_assert_cmpint (g_rmdir (new_path), ==, 0);
@ -1445,7 +1443,7 @@ test_get_contents (void)
gchar *contents;
GError *error = NULL;
const gchar *text = "abcdefghijklmnopqrstuvwxyz";
const gchar *filename = "file-test-get-contents";
char *filename = g_build_filename (g_get_tmp_dir (), "file-test-get-contents", NULL);
gsize bytes_written;
f = g_fopen (filename, "w");
@ -1474,6 +1472,7 @@ test_get_contents (void)
g_free (contents);
g_remove (filename);
g_free (filename);
}
static gboolean
@ -2196,7 +2195,7 @@ test_stdio_wrappers (void)
static void
test_fopen_modes (void)
{
char *path = g_build_filename ("temp-fopen", NULL);
char *path = g_build_filename (g_get_tmp_dir (), "temp-fopen", NULL);
gsize i;
const gchar *modes[] =
{

View File

@ -39,8 +39,10 @@ test_small_writes (void)
guint bytes_remaining;
gchar tmp;
GError *local_error = NULL;
char *path = NULL;
io = g_io_channel_new_file ("iochannel-test-outfile", "w", &local_error);
path = g_build_filename (g_get_tmp_dir (), "iochannel-test-outfile", NULL);
io = g_io_channel_new_file (path, "w", &local_error);
g_assert_no_error (local_error);
g_io_channel_set_encoding (io, NULL, NULL);
@ -61,7 +63,8 @@ test_small_writes (void)
g_assert_cmpint (status, ==, G_IO_STATUS_NORMAL);
g_io_channel_unref (io);
g_remove ("iochannel-test-outfile");
g_remove (path);
g_free (path);
}
static void
@ -70,7 +73,7 @@ test_read_write (void)
GIOChannel *gio_r, *gio_w ;
GError *local_error = NULL;
GString *buffer;
char *filename;
char *in_path = NULL, *out_path = NULL;
gint rlength = 0;
glong wlength = 0;
gsize length_out;
@ -78,14 +81,15 @@ test_read_write (void)
GIOStatus status;
const gsize buffer_size_bytes = 1024;
filename = g_test_build_filename (G_TEST_DIST, "iochannel-test-infile", NULL);
in_path = g_test_build_filename (G_TEST_DIST, "iochannel-test-infile", NULL);
setbuf (stdout, NULL); /* For debugging */
gio_r = g_io_channel_new_file (filename, "r", &local_error);
gio_r = g_io_channel_new_file (in_path, "r", &local_error);
g_assert_no_error (local_error);
gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &local_error);
out_path = g_build_filename (g_get_tmp_dir (), "iochannel-test-outfile", NULL);
gio_w = g_io_channel_new_file (out_path, "w", &local_error);
g_assert_no_error (local_error);
g_io_channel_set_encoding (gio_r, encoding, &local_error);
@ -164,7 +168,8 @@ test_read_write (void)
test_small_writes ();
g_free (filename);
g_free (in_path);
g_free (out_path);
g_string_free (buffer, TRUE);
}
@ -227,7 +232,7 @@ int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
g_test_add_func ("/io-channel/read-write", test_read_write);
g_test_add_func ("/io-channel/read-line/embedded-nuls", test_read_line_embedded_nuls);