[tracker/tracker-1.0] libtracker-data: Fixed unit tests so files are not left around failing distcheck



commit 5e688434b108e600037c17937a647b5f72b53a99
Author: Martyn Russell <martyn lanedo com>
Date:   Wed Aug 13 11:26:16 2014 +0100

    libtracker-data: Fixed unit tests so files are not left around failing distcheck

 tests/libtracker-data/tracker-ontology-test.c     |   77 ++++++++++++++-------
 tests/libtracker-data/tracker-sparql-blank-test.c |   51 +++++++++-----
 tests/libtracker-data/tracker-sparql-test.c       |   45 ++++++++----
 3 files changed, 115 insertions(+), 58 deletions(-)
---
diff --git a/tests/libtracker-data/tracker-ontology-test.c b/tests/libtracker-data/tracker-ontology-test.c
index 614692e..307ccf6 100644
--- a/tests/libtracker-data/tracker-ontology-test.c
+++ b/tests/libtracker-data/tracker-ontology-test.c
@@ -32,12 +32,14 @@
 #include <libtracker-data/tracker-data.h>
 #include <libtracker-data/tracker-sparql-query.h>
 
+static gchar *tests_data_dir = NULL;
+static gchar *xdg_location = NULL;
+
 typedef struct _TestInfo TestInfo;
 
 struct _TestInfo {
        const gchar *test_name;
        const gchar *data;
-       gchar *xdg_location;
 };
 
 typedef struct _ChangeInfo ChangeInfo;
@@ -49,6 +51,11 @@ struct _ChangeInfo {
        const gchar *ptr;
 };
 
+const TestInfo all_other_tests[] = {
+       { "init", NULL },
+       { NULL }
+};
+
 const TestInfo nie_tests[] = {
        { "nie/filter-subject-1", "nie/data-1" },
        { "nie/filter-characterset-1", "nie/data-1" },
@@ -61,7 +68,7 @@ const TestInfo nie_tests[] = {
        { "nie/filter-legal-1", "nie/data-1" },
        { "nie/filter-title-1", "nie/data-1" },
        { "nie/filter-version-1", "nie/data-1" },
-       { NULL }
+       { NULL, NULL }
 };
 
 const TestInfo nmo_tests[] = {
@@ -79,7 +86,7 @@ const TestInfo nmo_tests[] = {
        { "nmo/filter-isrecent-1", "nmo/data-1" },
        { "nmo/filter-messageid-1", "nmo/data-1" },
        { "nmo/filter-messagesubject-1", "nmo/data-1" },
-       { NULL }
+       { NULL, NULL }
 };
 
 static void
@@ -171,7 +178,8 @@ query_helper (const gchar *query_filename, const gchar *results_filename)
 }
 
 static void
-test_ontology_init (void)
+test_ontology_init (TestInfo      *test_info,
+                    gconstpointer  context)
 {
        GError *error = NULL;
 
@@ -225,7 +233,6 @@ test_query (TestInfo      *test_info,
        gchar *prefix, *data_prefix, *test_prefix;
 
        prefix = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", NULL);
-
        data_prefix = g_build_filename (prefix, test_info->data, NULL);
        test_prefix = g_build_filename (prefix, test_info->test_name, NULL);
        g_free (prefix);
@@ -271,21 +278,24 @@ static inline void
 setup (TestInfo *info,
        gint      i)
 {
-       gchar *basename;
-       gchar *current_dir;
-
-       basename = g_strdup_printf ("%d-%d", i, g_test_rand_int_range (0, G_MAXINT));
-       current_dir = g_get_current_dir ();
-       info->xdg_location = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", basename, NULL);
-       g_free (current_dir);
-       g_free (basename);
-
-       g_setenv ("XDG_DATA_HOME", info->xdg_location, TRUE);
-       g_setenv ("XDG_CACHE_HOME", info->xdg_location, TRUE);
-       g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
+       /* Sadly, we can't use ONE location per test because GLib
+        * caches XDG env vars, so g_get_*dir() will not change if we
+        * update the environment, this sucks majorly.
+        */
+       if (!xdg_location) {
+               gchar *basename;
+
+               /* NOTE: g_test_build_filename() doesn't work env vars G_TEST_* are not defined?? */
+               basename = g_strdup_printf ("%d", g_test_rand_int_range (0, G_MAXINT));
+               xdg_location = g_build_path (G_DIR_SEPARATOR_S, tests_data_dir, basename, NULL);
+               g_free (basename);
+
+               g_assert_true (g_setenv ("XDG_DATA_HOME", xdg_location, TRUE));
+               g_assert_true (g_setenv ("XDG_CACHE_HOME", xdg_location, TRUE));
+               g_assert_true (g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE));
+       }
 }
 
-
 static void
 setup_nie (TestInfo      *info,
            gconstpointer  context)
@@ -307,24 +317,36 @@ setup_nmo (TestInfo      *info,
 }
 
 static void
+setup_all_others (TestInfo      *info,
+                  gconstpointer  context)
+{
+       gint i = GPOINTER_TO_INT (context);
+
+       *info = all_other_tests[i];
+       setup (info, i);
+}
+
+static void
 teardown (TestInfo      *info,
           gconstpointer  context)
 {
        gchar *cleanup_command;
 
        /* clean up */
-       g_print ("Removing temporary data (%s)\n", info->xdg_location);
+       g_print ("Removing temporary data (%s)\n", xdg_location);
 
-       cleanup_command = g_strdup_printf ("rm -Rf %s/", info->xdg_location);
+       cleanup_command = g_strdup_printf ("rm -Rf %s/", xdg_location);
        g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
        g_free (cleanup_command);
 
-       g_free (info->xdg_location);
+       g_free (xdg_location);
+       xdg_location = NULL;
 }
 
 int
 main (int argc, char **argv)
 {
+       gchar *current_dir;
        gint result;
        gint i;
 
@@ -332,16 +354,18 @@ main (int argc, char **argv)
         * that the expected order in the test results is always the same! */
        setlocale (LC_COLLATE, "en_US.utf8");
 
+       current_dir = g_get_current_dir ();
+       tests_data_dir = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", NULL);
+       g_free (current_dir);
+
        g_test_init (&argc, &argv, NULL);
 
        /* add test cases */
-       g_test_add_func ("/libtracker-data/ontology-init", test_ontology_init);
+       g_test_add ("/libtracker-data/ontology-init", TestInfo, GINT_TO_POINTER(0), setup_all_others, 
test_ontology_init, teardown);
 
        for (i = 0; nie_tests[i].test_name; i++) {
                gchar *testpath;
 
-               g_message ("%d", i);
-
                testpath = g_strconcat ("/libtracker-data/nie/", nie_tests[i].test_name, NULL);
                g_test_add (testpath, TestInfo, GINT_TO_POINTER(i), setup_nie, test_query, teardown);
                g_free (testpath);
@@ -350,8 +374,6 @@ main (int argc, char **argv)
        for (i = 0; nmo_tests[i].test_name; i++) {
                gchar *testpath;
 
-               g_message ("%d", i);
-
                testpath = g_strconcat ("/libtracker-data/nmo/", nmo_tests[i].test_name, NULL);
                g_test_add (testpath, TestInfo, GINT_TO_POINTER(i), setup_nmo, test_query, teardown);
                g_free (testpath);
@@ -360,5 +382,8 @@ main (int argc, char **argv)
        /* run tests */
        result = g_test_run ();
 
+       g_remove (tests_data_dir);
+       g_free (tests_data_dir);
+
        return result;
 }
diff --git a/tests/libtracker-data/tracker-sparql-blank-test.c 
b/tests/libtracker-data/tracker-sparql-blank-test.c
index f056ae7..d3b795c 100644
--- a/tests/libtracker-data/tracker-sparql-blank-test.c
+++ b/tests/libtracker-data/tracker-sparql-blank-test.c
@@ -23,6 +23,7 @@
 #include <locale.h>
 
 #include <glib.h>
+#include <glib/gstdio.h>
 #include <gio/gio.h>
 
 #include <libtracker-data/tracker-data-manager.h>
@@ -31,8 +32,11 @@
 #include <libtracker-data/tracker-data.h>
 #include <libtracker-data/tracker-sparql-query.h>
 
+static gchar *tests_data_dir = NULL;
+static gchar *xdg_location = NULL;
+
 typedef struct {
-       gchar *xdg_location;
+       void *user_data;
 } TestInfo;
 
 static void
@@ -129,20 +133,22 @@ static void
 setup (TestInfo      *info,
        gconstpointer  context)
 {
-       gchar *current_dir, *basename;
-       gint i;
-
-       i = GPOINTER_TO_INT (context);
-
-       basename = g_strdup_printf ("%d-%d", i, g_test_rand_int_range (0, G_MAXINT));
-       current_dir = g_get_current_dir ();
-       info->xdg_location = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", basename, NULL);
-       g_free (current_dir);
-       g_free (basename);
-
-       g_setenv ("XDG_DATA_HOME", info->xdg_location, TRUE);
-       g_setenv ("XDG_CACHE_HOME", info->xdg_location, TRUE);
-       g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
+       /* Sadly, we can't use ONE location per test because GLib
+        * caches XDG env vars, so g_get_*dir() will not change if we
+        * update the environment, this sucks majorly.
+        */
+       if (!xdg_location) {
+               gchar *basename;
+
+               /* NOTE: g_test_build_filename() doesn't work env vars G_TEST_* are not defined?? */
+               basename = g_strdup_printf ("%d", g_test_rand_int_range (0, G_MAXINT));
+               xdg_location = g_build_path (G_DIR_SEPARATOR_S, tests_data_dir, basename, NULL);
+               g_free (basename);
+
+               g_assert_true (g_setenv ("XDG_DATA_HOME", xdg_location, TRUE));
+               g_assert_true (g_setenv ("XDG_CACHE_HOME", xdg_location, TRUE));
+               g_assert_true (g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE));
+       }
 }
 
 static void
@@ -152,27 +158,36 @@ teardown (TestInfo      *info,
        gchar *cleanup_command;
 
        /* clean up */
-       g_print ("Removing temporary data (%s)\n", info->xdg_location);
+       g_print ("Removing temporary data (%s)\n", xdg_location);
 
-       cleanup_command = g_strdup_printf ("rm -Rf %s/", info->xdg_location);
+       cleanup_command = g_strdup_printf ("rm -Rf %s/", xdg_location);
        g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
        g_free (cleanup_command);
 
-       g_free (info->xdg_location);
+       g_free (xdg_location);
+       xdg_location = NULL;
 }
 
 int
 main (int argc, char **argv)
 {
+       gchar *current_dir;
        gint result;
 
        setlocale (LC_COLLATE, "en_US.utf8");
 
+       current_dir = g_get_current_dir ();
+       tests_data_dir = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", NULL);
+       g_free (current_dir);
+
        g_test_init (&argc, &argv, NULL);
        g_test_add ("/libtracker-data/sparql-blank", TestInfo, GINT_TO_POINTER(0), setup, test_blank, 
teardown);
 
        /* run tests */
        result = g_test_run ();
 
+       g_remove (tests_data_dir);
+       g_free (tests_data_dir);
+
        return result;
 }
diff --git a/tests/libtracker-data/tracker-sparql-test.c b/tests/libtracker-data/tracker-sparql-test.c
index 3674e0e..939bb31 100644
--- a/tests/libtracker-data/tracker-sparql-test.c
+++ b/tests/libtracker-data/tracker-sparql-test.c
@@ -33,6 +33,9 @@
 #include <libtracker-data/tracker-data.h>
 #include <libtracker-data/tracker-sparql-query.h>
 
+static gchar *tests_data_dir = NULL;
+static gchar *xdg_location = NULL;
+
 typedef struct _TestInfo TestInfo;
 
 struct _TestInfo {
@@ -40,7 +43,6 @@ struct _TestInfo {
        const gchar *data;
        gboolean expect_query_error;
        gboolean expect_update_error;
-       gchar *xdg_location;
 };
 
 const TestInfo tests[] = {
@@ -319,21 +321,27 @@ static void
 setup (TestInfo      *info,
        gconstpointer  context)
 {
-       gchar *current_dir, *basename;
        gint i;
 
        i = GPOINTER_TO_INT (context);
        *info = tests[i];
 
-       basename = g_strdup_printf ("%d-%d", i, g_test_rand_int_range (0, G_MAXINT));
-       current_dir = g_get_current_dir ();
-       info->xdg_location = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", basename, NULL);
-       g_free (current_dir);
-       g_free (basename);
-
-       g_setenv ("XDG_DATA_HOME", info->xdg_location, TRUE);
-       g_setenv ("XDG_CACHE_HOME", info->xdg_location, TRUE);
-       g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
+       /* Sadly, we can't use ONE location per test because GLib
+        * caches XDG env vars, so g_get_*dir() will not change if we
+        * update the environment, this sucks majorly.
+        */
+       if (!xdg_location) {
+               gchar *basename;
+
+               /* NOTE: g_test_build_filename() doesn't work env vars G_TEST_* are not defined?? */
+               basename = g_strdup_printf ("%d", g_test_rand_int_range (0, G_MAXINT));
+               xdg_location = g_build_path (G_DIR_SEPARATOR_S, tests_data_dir, basename, NULL);
+               g_free (basename);
+
+               g_assert_true (g_setenv ("XDG_DATA_HOME", xdg_location, TRUE));
+               g_assert_true (g_setenv ("XDG_CACHE_HOME", xdg_location, TRUE));
+               g_assert_true (g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE));
+       }
 }
 
 static void
@@ -343,23 +351,29 @@ teardown (TestInfo      *info,
        gchar *cleanup_command;
 
        /* clean up */
-       g_print ("Removing temporary data (%s)\n", info->xdg_location);
+       g_print ("Removing temporary data (%s)\n", xdg_location);
 
-       cleanup_command = g_strdup_printf ("rm -Rf %s/", info->xdg_location);
+       cleanup_command = g_strdup_printf ("rm -Rf %s/", xdg_location);
        g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
        g_free (cleanup_command);
 
-       g_free (info->xdg_location);
+       g_free (xdg_location);
+       xdg_location = NULL;
 }
 
 int
 main (int argc, char **argv)
 {
+       gchar *current_dir;
        gint result;
        gint i;
 
        setlocale (LC_COLLATE, "en_US.utf8");
 
+       current_dir = g_get_current_dir ();
+       tests_data_dir = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", NULL);
+       g_free (current_dir);
+
        g_test_init (&argc, &argv, NULL);
 
        /* add test cases */
@@ -385,5 +399,8 @@ main (int argc, char **argv)
        /* run tests */
        result = g_test_run ();
 
+       g_remove (tests_data_dir);
+       g_free (tests_data_dir);
+
        return result;
 }


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