tests: Add temporary working directory for appmonitor test

Rather than creating a temporary directory in the current directory
(typically the builddir), then never deleting it; create one in the
system /tmp directory, and clean it up properly afterwards.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=785260
This commit is contained in:
Philip Withnall 2017-08-18 09:53:23 +01:00
parent 3ce00b29ec
commit 0f5b523fac

View File

@ -1,6 +1,41 @@
#include <gio/gio.h> #include <gio/gio.h>
#include <gstdio.h> #include <gstdio.h>
typedef struct
{
gchar *data_dir;
gchar *applications_dir;
} Fixture;
static void
setup (Fixture *fixture,
gconstpointer user_data)
{
GError *error = NULL;
fixture->data_dir = g_dir_make_tmp ("gio-test-app-monitor_XXXXXX", &error);
g_assert_no_error (error);
fixture->applications_dir = g_build_filename (fixture->data_dir, "applications", NULL);
g_assert_cmpint (g_mkdir (fixture->applications_dir, 0755), ==, 0);
g_setenv ("XDG_DATA_DIRS", fixture->data_dir, TRUE);
g_setenv ("XDG_DATA_HOME", fixture->data_dir, TRUE);
g_test_message ("Using data directory: %s", fixture->data_dir);
}
static void
teardown (Fixture *fixture,
gconstpointer user_data)
{
g_assert_cmpint (g_rmdir (fixture->applications_dir), ==, 0);
g_clear_pointer (&fixture->applications_dir, g_free);
g_assert_cmpint (g_rmdir (fixture->data_dir), ==, 0);
g_clear_pointer (&fixture->data_dir, g_free);
}
static gboolean static gboolean
create_app (gpointer data) create_app (gpointer data)
{ {
@ -48,16 +83,14 @@ quit_loop (gpointer data)
} }
static void static void
test_app_monitor (void) test_app_monitor (Fixture *fixture,
gconstpointer user_data)
{ {
gchar *path, *app_path; gchar *app_path;
GAppInfoMonitor *monitor; GAppInfoMonitor *monitor;
GMainLoop *loop; GMainLoop *loop;
path = g_build_filename (g_get_user_data_dir (), "applications", NULL); app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL);
g_mkdir (path, 0755);
app_path = g_build_filename (path, "app.desktop", NULL);
/* FIXME: this shouldn't be required */ /* FIXME: this shouldn't be required */
g_list_free_full (g_app_info_get_all (), g_object_unref); g_list_free_full (g_app_info_get_all (), g_object_unref);
@ -90,23 +123,15 @@ test_app_monitor (void)
g_object_unref (monitor); g_object_unref (monitor);
g_free (path);
g_free (app_path); g_free (app_path);
} }
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
gchar *path = NULL;
path = g_mkdtemp (g_strdup ("app_monitor_XXXXXX"));
g_setenv ("XDG_DATA_DIRS", path, TRUE);
g_setenv ("XDG_DATA_HOME", path, TRUE);
g_free (path);
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/monitor/app", test_app_monitor); g_test_add ("/monitor/app", Fixture, NULL, setup, test_app_monitor, teardown);
return g_test_run (); return g_test_run ();
} }