[glib] tests: Add temporary working directory for appmonitor test



commit 0f5b523fac8b2d9b9305419937e51b6c87e765be
Author: Philip Withnall <withnall endlessm com>
Date:   Fri Aug 18 09:53:23 2017 +0100

    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

 gio/tests/appmonitor.c |   55 ++++++++++++++++++++++++++++++++++-------------
 1 files changed, 40 insertions(+), 15 deletions(-)
---
diff --git a/gio/tests/appmonitor.c b/gio/tests/appmonitor.c
index be46bba..30ea108 100644
--- a/gio/tests/appmonitor.c
+++ b/gio/tests/appmonitor.c
@@ -1,6 +1,41 @@
 #include <gio/gio.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
 create_app (gpointer data)
 {
@@ -48,16 +83,14 @@ quit_loop (gpointer data)
 }
 
 static void
-test_app_monitor (void)
+test_app_monitor (Fixture       *fixture,
+                  gconstpointer  user_data)
 {
-  gchar *path, *app_path;
+  gchar *app_path;
   GAppInfoMonitor *monitor;
   GMainLoop *loop;
 
-  path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
-  g_mkdir (path, 0755);
-
-  app_path = g_build_filename (path, "app.desktop", NULL);
+  app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL);
 
   /* FIXME: this shouldn't be required */
   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_free (path);
   g_free (app_path);
 }
 
 int
 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_add_func ("/monitor/app", test_app_monitor);
+  g_test_add ("/monitor/app", Fixture, NULL, setup, test_app_monitor, teardown);
 
   return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]