gio/tests/portal-support: Fix snap test ordering race

When the gnome test runner executes the tests, the test appear to execute in disk
order. This means it sometimes works and sometimes we see breakage in portal-support-snap
and portal-support-snap-classic.

The issue is that some tests create config files but some don't. If they run
in the wrong order, tests see config files they shouldn't and break.

Fix this by deleting the files after each test run, properly cleaning up after
themselves. The cleanup code is based upon gtestutils.c:rm_rf().

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Richard Purdie 2023-05-30 11:52:38 +01:00
parent 49971ee217
commit c63cf19d9a
4 changed files with 35 additions and 0 deletions

View File

@ -66,6 +66,9 @@ tests_teardown (SetupData *setup_data,
else else
g_unsetenv ("SNAP"); g_unsetenv ("SNAP");
cleanup_snapfiles (setup_data->snap_path);
cleanup_snapfiles (setup_data->bin_path);
g_clear_pointer (&setup_data->old_path, g_free); g_clear_pointer (&setup_data->old_path, g_free);
g_clear_pointer (&setup_data->old_snap, g_free); g_clear_pointer (&setup_data->old_snap, g_free);
} }

View File

@ -67,6 +67,9 @@ tests_teardown (SetupData *setup_data,
else else
g_unsetenv ("SNAP"); g_unsetenv ("SNAP");
cleanup_snapfiles (setup_data->snap_path);
cleanup_snapfiles (setup_data->bin_path);
g_clear_pointer (&setup_data->old_path, g_free); g_clear_pointer (&setup_data->old_path, g_free);
g_clear_pointer (&setup_data->old_snap, g_free); g_clear_pointer (&setup_data->old_snap, g_free);
} }

View File

@ -26,6 +26,33 @@
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
void
cleanup_snapfiles (const gchar *path)
{
GDir *dir = NULL;
const gchar *entry;
dir = g_dir_open (path, 0, NULL);
if (dir == NULL)
{
/* Assume its a file. Ignore failure. */
(void) g_remove (path);
return;
}
while ((entry = g_dir_read_name (dir)) != NULL)
{
gchar *sub_path = g_build_filename (path, entry, NULL);
cleanup_snapfiles (sub_path);
g_free (sub_path);
}
g_dir_close (dir);
g_rmdir (path);
}
void void
create_fake_snapctl (const char *path, create_fake_snapctl (const char *path,
const char *supported_op) const char *supported_op)

View File

@ -23,6 +23,8 @@
#include <glib.h> #include <glib.h>
void cleanup_snapfiles (const gchar *path);
void create_fake_snap_yaml (const char *snap_path, void create_fake_snap_yaml (const char *snap_path,
gboolean is_classic); gboolean is_classic);