[tracker/wip/carlosg/domain-ontologies: 24/116] libtracker-data: Require location directories in all internals



commit 583da483667994b50452a297489b850a5f15907e
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jun 4 01:22:33 2017 +0200

    libtracker-data: Require location directories in all internals
    
    cache/journal/ontology dirs are now mandatory in
    tracker_data_manager_init(), all the upper layers must know what
    database and ontology they are using.

 src/libtracker-data/libtracker-data.vapi           |    2 +-
 src/libtracker-data/tracker-data-backup.c          |    8 ++
 src/libtracker-data/tracker-data-manager.c         |   34 ++-----
 src/libtracker-direct/Makefile.am                  |    1 +
 src/libtracker-direct/tracker-direct.vala          |    8 ++
 src/tracker-store/tracker-main.vala                |   12 ++-
 src/tracker/Makefile.am                            |    1 +
 src/tracker/tracker-reset.c                        |   12 +++-
 src/tracker/tracker-sql.c                          |   17 ++++-
 tests/libtracker-data/tracker-backup-test.c        |   61 +++++--------
 .../libtracker-data/tracker-ontology-change-test.c |   21 ++---
 tests/libtracker-data/tracker-ontology-test.c      |   93 ++++++++------------
 tests/libtracker-data/tracker-sparql-blank-test.c  |   38 ++++-----
 tests/libtracker-data/tracker-sparql-test.c        |   43 ++++------
 tests/libtracker-fts/tracker-fts-test.c            |   19 +++--
 15 files changed, 175 insertions(+), 195 deletions(-)
---
diff --git a/src/libtracker-data/libtracker-data.vapi b/src/libtracker-data/libtracker-data.vapi
index b6337ab..a7fe354 100644
--- a/src/libtracker-data/libtracker-data.vapi
+++ b/src/libtracker-data/libtracker-data.vapi
@@ -230,7 +230,7 @@ namespace Tracker {
 
        [CCode (cheader_filename = "libtracker-data/tracker-data-manager.h")]
        namespace Data.Manager {
-               public bool init (DBManagerFlags flags, GLib.File? cache_location, GLib.File? data_location, 
GLib.File? ontology_location, out bool first_time, bool journal_check, bool restoring_backup, uint 
select_cache_size, uint update_cache_size, BusyCallback? busy_callback, string? busy_status) throws 
DBInterfaceError, DBJournalError;
+               public bool init (DBManagerFlags flags, GLib.File cache_location, GLib.File data_location, 
GLib.File ontology_location, out bool first_time, bool journal_check, bool restoring_backup, uint 
select_cache_size, uint update_cache_size, BusyCallback? busy_callback, string? busy_status) throws 
DBInterfaceError, DBJournalError;
                public void shutdown ();
        }
 
diff --git a/src/libtracker-data/tracker-data-backup.c b/src/libtracker-data/tracker-data-backup.c
index 999d1b1..d7b3aa9 100644
--- a/src/libtracker-data/tracker-data-backup.c
+++ b/src/libtracker-data/tracker-data-backup.c
@@ -556,6 +556,14 @@ tracker_data_backup_restore (GFile                *journal,
        BackupSaveInfo *info;
        GError *internal_error = NULL;
 
+       if (!cache_location || !data_location || !ontology_location) {
+               g_set_error (error,
+                            TRACKER_DATA_ONTOLOGY_ERROR,
+                            TRACKER_DATA_UNSUPPORTED_LOCATION,
+                            "All data storage and ontology locations must be provided");
+               return;
+       }
+
        info = g_new0 (BackupSaveInfo, 1);
 #ifndef DISABLE_JOURNAL
        info->destination = g_file_new_for_path (tracker_db_journal_get_filename ());
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 7891e63..a8c4f52 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -3752,37 +3752,21 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
        gboolean read_journal;
 #endif
 
-       if ((cache_location && !g_file_is_native (cache_location)) ||
-           (data_location && !g_file_is_native (data_location))) {
+       if (!cache_location || !data_location || !ontology_location) {
                g_set_error (error,
                             TRACKER_DATA_ONTOLOGY_ERROR,
                             TRACKER_DATA_UNSUPPORTED_LOCATION,
-                            "Cache and data locations must be local");
+                            "All data storage and ontology locations must be provided");
                return FALSE;
        }
 
-       if (!cache_location) {
-               gchar *dir = g_build_filename (g_get_user_cache_dir (),
-                                              "tracker",
-                                              NULL);
-               cache_location = g_file_new_for_path (dir);
-               g_free (dir);
-       }
-
-       if (!data_location) {
-               gchar *dir = g_build_filename (g_get_user_data_dir (),
-                                              "tracker",
-                                              "data",
-                                              NULL);
-               data_location = g_file_new_for_path (dir);
-               g_free (dir);
-       }
-
-       if (!ontology_location) {
-               gchar *dir = g_build_filename (SHAREDIR, "tracker",
-                                              "ontologies", NULL);
-               ontology_location = g_file_new_for_path (dir);
-               g_free (dir);
+       if (!g_file_is_native (cache_location) ||
+           !g_file_is_native (data_location)) {
+               g_set_error (error,
+                            TRACKER_DATA_ONTOLOGY_ERROR,
+                            TRACKER_DATA_UNSUPPORTED_LOCATION,
+                            "Cache and data locations must be local");
+               return FALSE;
        }
 
        read_only = (flags & TRACKER_DB_MANAGER_READONLY) ? TRUE : FALSE;
diff --git a/src/libtracker-direct/Makefile.am b/src/libtracker-direct/Makefile.am
index 282ae5b..5e9a42f 100644
--- a/src/libtracker-direct/Makefile.am
+++ b/src/libtracker-direct/Makefile.am
@@ -15,6 +15,7 @@ AM_CPPFLAGS =                                          \
        $(BUILD_VALACFLAGS)                            \
        -I$(top_srcdir)/src                            \
        -I$(top_builddir)/src                          \
+       -DSHAREDIR=\""$(datadir)"\"                    \
        $(LIBTRACKER_DIRECT_CFLAGS)
 
 libtracker_direct_la_SOURCES =                         \
diff --git a/src/libtracker-direct/tracker-direct.vala b/src/libtracker-direct/tracker-direct.vala
index aac1fe5..5c20e90 100644
--- a/src/libtracker-direct/tracker-direct.vala
+++ b/src/libtracker-direct/tracker-direct.vala
@@ -37,6 +37,9 @@ public class Tracker.Direct.Connection : Tracker.Sparql.Connection, AsyncInitabl
 
        private AsyncQueue<Task> update_queue;
 
+       [CCode (cname = "SHAREDIR")]
+       extern const string SHAREDIR;
+
        enum TaskType {
                QUERY,
                UPDATE,
@@ -196,6 +199,11 @@ public class Tracker.Direct.Connection : Tracker.Sparql.Connection, AsyncInitabl
                ontology_loc = ontology;
                flags = connection_flags;
 
+               if (journal_loc == null)
+                       journal_loc = database_loc;
+               if (ontology_loc == null)
+                       ontology_loc = File.new_for_path (Path.build_filename (SHAREDIR, "tracker", 
"ontologies"));
+
                update_queue = new AsyncQueue<Task> ();
        }
 
diff --git a/src/tracker-store/tracker-main.vala b/src/tracker-store/tracker-main.vala
index e385da4..2bfcefa 100644
--- a/src/tracker-store/tracker-main.vala
+++ b/src/tracker-store/tracker-main.vala
@@ -345,9 +345,15 @@ License which can be viewed at:
                bool is_first_time_index;
 
                try {
-                       File final_cache_location = cache_location != null ? File.new_for_path 
(cache_location.replace ("%HOME%", Environment.get_home_dir()).replace("%SHAREDIR%", SHAREDIR)) : null;
-                       File final_data_location = data_location != null ? File.new_for_path 
(data_location.replace ("%HOME%", Environment.get_home_dir()).replace("%SHAREDIR%", SHAREDIR)) : null;
-                       File final_ontology_location = ontology_location != null ? File.new_for_path 
(ontology_location.replace ("%HOME%", Environment.get_home_dir()).replace("%SHAREDIR%", SHAREDIR)) : null;
+                       File final_cache_location = cache_location != null ?
+                               File.new_for_path (cache_location.replace ("%HOME%", 
Environment.get_home_dir()).replace("%SHAREDIR%", SHAREDIR)) :
+                               File.new_for_path (Path.build_filename (Environment.get_user_cache_dir (), 
"tracker"));
+                       File final_data_location = data_location != null ?
+                               File.new_for_path (data_location.replace ("%HOME%", 
Environment.get_home_dir()).replace("%SHAREDIR%", SHAREDIR)) :
+                               File.new_for_path (Path.build_filename (Environment.get_user_data_dir (), 
"tracker", "data"));
+                       File final_ontology_location = ontology_location != null ?
+                               File.new_for_path (ontology_location.replace ("%HOME%", 
Environment.get_home_dir()).replace("%SHAREDIR%", SHAREDIR)) :
+                               File.new_for_path (Path.build_filename (SHAREDIR, "tracker", "ontologies"));
 
                        Tracker.Data.Manager.init (flags,
                                                   final_cache_location,
diff --git a/src/tracker/Makefile.am b/src/tracker/Makefile.am
index 66210b0..34eadd8 100644
--- a/src/tracker/Makefile.am
+++ b/src/tracker/Makefile.am
@@ -3,6 +3,7 @@ AM_CPPFLAGS =                                          \
        -DLIBEXECDIR=\""$(libexecdir)"\"               \
        -DLOCALEDIR=\""$(localedir)"\"                 \
        -DMANDIR=\""$(mandir)"\" \
+       -DSHAREDIR=\""$(datadir)"\" \
        -DTRACKER_EXTRACTOR_RULES_DIR=\""$(TRACKER_EXTRACT_RULES_DIR)"\" \
        -I$(top_srcdir)/src                            \
        -I$(top_builddir)/src                          \
diff --git a/src/tracker/tracker-reset.c b/src/tracker/tracker-reset.c
index 3eb9de7..2431046 100644
--- a/src/tracker/tracker-reset.c
+++ b/src/tracker/tracker-reset.c
@@ -259,6 +259,8 @@ reset_run (void)
 
        if (hard_reset || soft_reset) {
                guint log_handler_id;
+               GFile *cache_location, *data_location;
+               gchar *dir;
 #ifndef DISABLE_JOURNAL
                gchar *rotate_to;
                TrackerDBConfig *db_config;
@@ -266,6 +268,14 @@ reset_run (void)
                gint chunk_size_mb;
 #endif /* DISABLE_JOURNAL */
 
+               dir = g_build_filename (g_get_user_cache_dir (), "tracker", NULL);
+               cache_location = g_file_new_for_path (dir);
+               g_free (dir);
+
+               dir = g_build_filename (g_get_user_data_dir (), "tracker", "data", NULL);
+               data_location = g_file_new_for_path (dir);
+               g_free (dir);
+
                /* Set log handler for library messages */
                log_handler_id = g_log_set_handler (NULL,
                                                    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL,
@@ -292,7 +302,7 @@ reset_run (void)
 
                /* Clean up (select_cache_size and update_cache_size don't matter here) */
                if (!tracker_db_manager_init (TRACKER_DB_MANAGER_REMOVE_ALL,
-                                             NULL, NULL, /* cache_location, data_location */
+                                             cache_location, data_location,
                                              NULL,
                                              FALSE,
                                              FALSE,
diff --git a/src/tracker/tracker-sql.c b/src/tracker/tracker-sql.c
index de0301e..05f6c83 100644
--- a/src/tracker/tracker-sql.c
+++ b/src/tracker/tracker-sql.c
@@ -96,8 +96,23 @@ sql_by_query (void)
        GError *error = NULL;
        gboolean first_time = FALSE;
        gint n_rows = 0;
+       GFile *cache_location, *data_location, *ontology_location;
+       gchar *dir;
 
-       if (!tracker_data_manager_init (0, NULL, NULL, NULL,
+       dir = g_build_filename (g_get_user_cache_dir (), "tracker", NULL);
+       cache_location = g_file_new_for_path (dir);
+       g_free (dir);
+
+       dir = g_build_filename (g_get_user_data_dir (), "tracker", "data", NULL);
+       data_location = g_file_new_for_path (dir);
+       g_free (dir);
+
+       dir = g_build_filename (SHAREDIR, "tracker", "ontologies", NULL);
+       ontology_location = g_file_new_for_path (dir);
+       g_free (dir);
+
+       if (!tracker_data_manager_init (0, cache_location,
+                                       data_location, ontology_location,
                                        &first_time,
                                        FALSE,
                                        FALSE,
diff --git a/tests/libtracker-data/tracker-backup-test.c b/tests/libtracker-data/tracker-backup-test.c
index ceaeb0b..07df28f 100644
--- a/tests/libtracker-data/tracker-backup-test.c
+++ b/tests/libtracker-data/tracker-backup-test.c
@@ -29,7 +29,6 @@
 #include <libtracker-data/tracker-data.h>
 
 static gchar *tests_data_dir = NULL;
-static gchar *xdg_location = NULL;
 static gint backup_calls = 0;
 static GMainLoop *loop = NULL;
 
@@ -38,6 +37,7 @@ typedef struct _TestInfo TestInfo;
 struct _TestInfo {
        const gchar *test_name;
        gboolean use_journal;
+       gchar *data_location;
 };
 
 const TestInfo tests[] = {
@@ -101,14 +101,16 @@ check_content_in_db (gint expected_instances,
  * Run again the queries
  */
 static void
-test_backup_and_restore_helper (gboolean journal)
+test_backup_and_restore_helper (const gchar *db_location,
+                                gboolean     journal)
 {
-       gchar  *data_prefix, *data_filename, *backup_location, *backup_filename, *db_location, *meta_db, 
*ontologies;
+       gchar  *data_prefix, *data_filename, *backup_location, *backup_filename, *meta_db, *ontologies;
        GError *error = NULL;
        GFile  *backup_file;
-       GFile  *test_schemas;
+       GFile  *data_location, *test_schemas;
+
+       data_location = g_file_new_for_path (db_location);
 
-       db_location = g_build_path (G_DIR_SEPARATOR_S, xdg_location, "tracker", NULL);
        data_prefix = g_build_path (G_DIR_SEPARATOR_S, 
                                    TOP_SRCDIR, "tests", "libtracker-data", "backup", "backup",
                                    NULL);
@@ -121,7 +123,7 @@ test_backup_and_restore_helper (gboolean journal)
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 
        tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
-                                  NULL, NULL, test_schemas,
+                                  data_location, data_location, test_schemas,
                                   NULL, FALSE, FALSE,
                                   100, 100, NULL, NULL, NULL, &error);
 
@@ -187,7 +189,7 @@ test_backup_and_restore_helper (gboolean journal)
 #endif /* DISABLE_JOURNAL */
 
        tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
-                                  NULL, NULL, test_schemas,
+                                  data_location, data_location, test_schemas,
                                   NULL, FALSE, FALSE,
                                   100, 100, NULL, NULL, NULL, &error);
 
@@ -195,7 +197,7 @@ test_backup_and_restore_helper (gboolean journal)
 
        check_content_in_db (0, 0);
 
-       tracker_data_backup_restore (backup_file, NULL, NULL, test_schemas, NULL, NULL, &error);
+       tracker_data_backup_restore (backup_file, data_location, data_location, test_schemas, NULL, NULL, 
&error);
        g_assert_no_error (error);
        check_content_in_db (3, 1);
 
@@ -211,12 +213,7 @@ static void
 test_backup_and_restore (TestInfo      *info,
                          gconstpointer  context)
 {
-       gint index;
-
-       index = GPOINTER_TO_INT (context);
-       *info = tests[index];
-
-       test_backup_and_restore_helper (info->use_journal);
+       test_backup_and_restore_helper (info->data_location, info->use_journal);
        backup_calls = 0;
 }
 
@@ -224,26 +221,15 @@ static void
 setup (TestInfo      *info,
        gconstpointer  context)
 {
-       gint i;
+       const TestInfo *test = context;
+       gchar *basename;
 
-       i = GPOINTER_TO_INT (context);
-       *info = tests[i];
+       *info = *test;
 
-       /* 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));
-       }
+       /* 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));
+       info->data_location = g_build_path (G_DIR_SEPARATOR_S, tests_data_dir, basename, NULL);
+       g_free (basename);
 }
 
 static void
@@ -253,14 +239,13 @@ teardown (TestInfo      *info,
        gchar *cleanup_command;
 
        /* clean up */
-       g_print ("Removing temporary data (%s)\n", xdg_location);
+       g_print ("Removing temporary data (%s)\n", info->data_location);
 
-       cleanup_command = g_strdup_printf ("rm -Rf %s/", xdg_location);
+       cleanup_command = g_strdup_printf ("rm -Rf %s/", info->data_location);
        g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
        g_free (cleanup_command);
 
-       g_free (xdg_location);
-       xdg_location = NULL;
+       g_free (info->data_location);
 }
 
 int
@@ -277,8 +262,8 @@ main (int argc, char **argv)
 
        g_test_init (&argc, &argv, NULL);
 
-       g_test_add ("/libtracker-data/backup/journal_then_save_and_restore", TestInfo, GINT_TO_POINTER(0), 
setup, test_backup_and_restore, teardown);
-       g_test_add ("/libtracker-data/backup/save_and_restore", TestInfo, GINT_TO_POINTER(1), setup, 
test_backup_and_restore, teardown);
+       g_test_add ("/libtracker-data/backup/journal_then_save_and_restore", TestInfo, &tests[0], setup, 
test_backup_and_restore, teardown);
+       g_test_add ("/libtracker-data/backup/save_and_restore", TestInfo, &tests[1], setup, 
test_backup_and_restore, teardown);
 
        result = g_test_run ();
 
diff --git a/tests/libtracker-data/tracker-ontology-change-test.c 
b/tests/libtracker-data/tracker-ontology-change-test.c
index e5c000a..b390440 100644
--- a/tests/libtracker-data/tracker-ontology-change-test.c
+++ b/tests/libtracker-data/tracker-ontology-change-test.c
@@ -183,10 +183,10 @@ test_ontology_change (void)
        gchar *ontology_file;
        GFile *file2;
        gchar *prefix, *build_prefix, *ontologies;
-       gchar *ontology_dir;
+       gchar *data_dir, *ontology_dir;
        guint i;
        GError *error = NULL;
-       GFile *test_schemas;
+       GFile *data_location, *test_schemas;
 
        delete_db (TRUE);
 
@@ -205,6 +205,10 @@ test_ontology_change (void)
        test_schemas = g_file_new_for_path (ontology_dir);
        g_free (ontology_dir);
 
+       data_dir = g_build_filename (g_get_current_dir (), "test-cache", NULL);
+       data_location = g_file_new_for_path (data_dir);
+       g_free (data_dir);
+
        for (i = 0; changes[i].ontology; i++) {
                GFile *file1;
                gchar *queries = NULL;
@@ -225,7 +229,7 @@ test_ontology_change (void)
                g_assert_no_error (error);
                g_chmod (ontology_file, 0666);
 
-               tracker_data_manager_init (0, NULL, NULL, test_schemas,
+               tracker_data_manager_init (0, data_location, data_location, test_schemas,
                                           NULL, FALSE, FALSE,
                                           100, 100, NULL, NULL, NULL, &error);
 
@@ -269,7 +273,7 @@ test_ontology_change (void)
 
        delete_db (FALSE);
 
-       tracker_data_manager_init (0, NULL, NULL, test_schemas,
+       tracker_data_manager_init (0, data_location, data_location, test_schemas,
                                   NULL, TRUE, FALSE,
                                   100, 100, NULL, NULL, NULL, &error);
 
@@ -297,6 +301,7 @@ test_ontology_change (void)
 
        g_object_unref (file2);
        g_object_unref (test_schemas);
+       g_object_unref (data_location);
        g_free (ontologies);
        g_free (build_prefix);
        g_free (prefix);
@@ -306,15 +311,9 @@ int
 main (int argc, char **argv)
 {
        gint result;
-       gchar *data_dir;
 
        g_test_init (&argc, &argv, NULL);
 
-       data_dir = g_build_filename (g_get_current_dir (), "test-cache", NULL);
-
-       g_setenv ("XDG_DATA_HOME", data_dir, TRUE);
-       g_setenv ("XDG_CACHE_HOME", data_dir, TRUE);
-
        /* add test cases */
 
        g_test_add_func ("/libtracker-data/ontology-change", test_ontology_change);
@@ -329,7 +328,5 @@ main (int argc, char **argv)
        g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
        g_spawn_command_line_sync ("rm -R test-cache/", NULL, NULL, NULL, NULL);
 
-       g_free (data_dir);
-
        return result;
 }
diff --git a/tests/libtracker-data/tracker-ontology-test.c b/tests/libtracker-data/tracker-ontology-test.c
index 32a90fb..e050285 100644
--- a/tests/libtracker-data/tracker-ontology-test.c
+++ b/tests/libtracker-data/tracker-ontology-test.c
@@ -33,13 +33,13 @@
 #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 *data_location;
 };
 
 typedef struct _ChangeInfo ChangeInfo;
@@ -182,12 +182,15 @@ test_ontology_init (TestInfo      *test_info,
                     gconstpointer  context)
 {
        GError *error = NULL;
+       GFile *data_location;
+
+       data_location = g_file_new_for_path (test_info->data_location);
 
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 
        /* first-time initialization */
        tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
-                                  NULL, NULL, NULL,
+                                  data_location, data_location, data_location,
                                   NULL,
                                   FALSE,
                                   FALSE,
@@ -206,7 +209,7 @@ test_ontology_init (TestInfo      *test_info,
 
        /* initialization from existing database */
        tracker_data_manager_init (0,
-                                  NULL, NULL, NULL,
+                                  data_location, data_location, data_location,
                                   NULL,
                                   FALSE,
                                   FALSE,
@@ -220,6 +223,8 @@ test_ontology_init (TestInfo      *test_info,
        g_assert_no_error (error);
 
        tracker_data_manager_shutdown ();
+
+       g_object_unref (data_location);
 }
 
 static void
@@ -230,19 +235,25 @@ test_query (TestInfo      *test_info,
        gchar *data_filename;
        gchar *query_filename;
        gchar *results_filename;
-       gchar *prefix, *data_prefix, *test_prefix;
-       GFile *file;
+       gchar *prefix, *data_prefix, *test_prefix, *ontology_path;
+       GFile *file, *data_location, *ontology_location;
+
+       data_location = g_file_new_for_path (test_info->data_location);
 
        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);
 
+       ontology_path = g_build_filename (TOP_SRCDIR, "src", "ontologies", NULL);
+       ontology_location = g_file_new_for_path (ontology_path);
+       g_free (ontology_path);
+
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 
        /* initialization */
        tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
-                                  NULL, NULL, NULL, /* loc, domain and ontology_name */
+                                  data_location, data_location, ontology_location,
                                   NULL,
                                   FALSE,
                                   FALSE,
@@ -274,58 +285,25 @@ test_query (TestInfo      *test_info,
        g_free (query_filename);
        g_free (results_filename);
 
+       g_object_unref (ontology_location);
+       g_object_unref (data_location);
+
        tracker_data_manager_shutdown ();
 }
 
 static inline void
-setup (TestInfo *info,
-       gint      i)
-{
-       /* 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));
-       }
-}
-
-static void
-setup_nie (TestInfo      *info,
-           gconstpointer  context)
+setup (TestInfo      *info,
+       gconstpointer  context)
 {
-       gint i = GPOINTER_TO_INT (context);
+       const TestInfo *test = context;
+       gchar *basename;
 
-       *info = nie_tests[i];
-       setup (info, i);
-}
-
-static void
-setup_nmo (TestInfo      *info,
-           gconstpointer  context)
-{
-       gint i = GPOINTER_TO_INT (context);
-
-       *info = nmo_tests[i];
-       setup (info, i);
-}
-
-static void
-setup_all_others (TestInfo      *info,
-                  gconstpointer  context)
-{
-       gint i = GPOINTER_TO_INT (context);
+       *info = *test;
 
-       *info = all_other_tests[i];
-       setup (info, i);
+       /* 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));
+       info->data_location = g_build_path (G_DIR_SEPARATOR_S, tests_data_dir, basename, NULL);
+       g_free (basename);
 }
 
 static void
@@ -335,14 +313,13 @@ teardown (TestInfo      *info,
        gchar *cleanup_command;
 
        /* clean up */
-       g_print ("Removing temporary data (%s)\n", xdg_location);
+       g_print ("Removing temporary data (%s)\n", info->data_location);
 
-       cleanup_command = g_strdup_printf ("rm -Rf %s/", xdg_location);
+       cleanup_command = g_strdup_printf ("rm -Rf %s/", info->data_location);
        g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
        g_free (cleanup_command);
 
-       g_free (xdg_location);
-       xdg_location = NULL;
+       g_free (info->data_location);
 }
 
 int
@@ -363,13 +340,13 @@ main (int argc, char **argv)
        g_test_init (&argc, &argv, NULL);
 
        /* add test cases */
-       g_test_add ("/libtracker-data/ontology-init", TestInfo, GINT_TO_POINTER(0), setup_all_others, 
test_ontology_init, teardown);
+       g_test_add ("/libtracker-data/ontology-init", TestInfo, &all_other_tests[0], setup, 
test_ontology_init, teardown);
 
        for (i = 0; nie_tests[i].test_name; i++) {
                gchar *testpath;
 
                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_test_add (testpath, TestInfo, &nie_tests[i], setup, test_query, teardown);
                g_free (testpath);
        }
 
@@ -377,7 +354,7 @@ main (int argc, char **argv)
                gchar *testpath;
 
                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_test_add (testpath, TestInfo, &nmo_tests[i], setup, test_query, teardown);
                g_free (testpath);
        }
 
diff --git a/tests/libtracker-data/tracker-sparql-blank-test.c 
b/tests/libtracker-data/tracker-sparql-blank-test.c
index 8b4b239..f0f3a1e 100644
--- a/tests/libtracker-data/tracker-sparql-blank-test.c
+++ b/tests/libtracker-data/tracker-sparql-blank-test.c
@@ -33,10 +33,10 @@
 #include <libtracker-data/tracker-sparql-query.h>
 
 static gchar *tests_data_dir = NULL;
-static gchar *xdg_location = NULL;
 
 typedef struct {
        void *user_data;
+       gchar *data_location;
 } TestInfo;
 
 static void
@@ -49,14 +49,17 @@ test_blank (TestInfo      *info,
        GVariant *rows;
        guint len = 0;
        gchar *solutions[3][3];
+       GFile *data_location;
 
        error = NULL;
 
+       data_location = g_file_new_for_path (info->data_location);
+
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 
        /* initialization */
        tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
-                                  NULL, NULL, NULL, /* loc, domain and ontology_name */
+                                  data_location, data_location, data_location, /* loc, domain and 
ontology_name */
                                   NULL,
                                   FALSE,
                                   FALSE,
@@ -125,6 +128,7 @@ test_blank (TestInfo      *info,
        g_free (solutions[2][1]);
 
        g_variant_unref (updates);
+       g_object_unref (data_location);
 
        tracker_data_manager_shutdown ();
 }
@@ -133,21 +137,12 @@ static void
 setup (TestInfo      *info,
        gconstpointer  context)
 {
-       /* 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));
-       }
+       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));
+       info->data_location = g_build_path (G_DIR_SEPARATOR_S, tests_data_dir, basename, NULL);
+       g_free (basename);
 }
 
 static void
@@ -157,14 +152,13 @@ teardown (TestInfo      *info,
        gchar *cleanup_command;
 
        /* clean up */
-       g_print ("Removing temporary data (%s)\n", xdg_location);
+       g_print ("Removing temporary data (%s)\n", info->data_location);
 
-       cleanup_command = g_strdup_printf ("rm -Rf %s/", xdg_location);
+       cleanup_command = g_strdup_printf ("rm -Rf %s/", info->data_location);
        g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
        g_free (cleanup_command);
 
-       g_free (xdg_location);
-       xdg_location = NULL;
+       g_free (info->data_location);
 }
 
 int
@@ -180,7 +174,7 @@ main (int argc, char **argv)
        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);
+       g_test_add ("/libtracker-data/sparql-blank", TestInfo, NULL, setup, test_blank, teardown);
 
        /* run tests */
        result = g_test_run ();
diff --git a/tests/libtracker-data/tracker-sparql-test.c b/tests/libtracker-data/tracker-sparql-test.c
index 56fcad9..bf6ff3d 100644
--- a/tests/libtracker-data/tracker-sparql-test.c
+++ b/tests/libtracker-data/tracker-sparql-test.c
@@ -34,7 +34,6 @@
 #include <libtracker-data/tracker-sparql-query.h>
 
 static gchar *tests_data_dir = NULL;
-static gchar *xdg_location = NULL;
 
 typedef struct _TestInfo TestInfo;
 
@@ -43,6 +42,7 @@ struct _TestInfo {
        const gchar *data;
        gboolean expect_query_error;
        gboolean expect_update_error;
+       gchar *data_location;
 };
 
 const TestInfo tests[] = {
@@ -255,7 +255,7 @@ test_sparql_query (TestInfo      *test_info,
        gchar *query, *query_filename;
        gchar *results_filename;
        gchar *prefix, *data_prefix, *test_prefix;
-       GFile *file, *test_schemas;
+       GFile *file, *test_schemas, *data_location;
 
        /* initialization */
        prefix = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", NULL);
@@ -267,10 +267,12 @@ test_sparql_query (TestInfo      *test_info,
        test_schemas = g_file_get_parent (file);
        g_object_unref (file);
 
+       data_location = g_file_new_for_path (test_info->data_location);
+
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 
        tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
-                                  NULL, NULL, test_schemas, /* loc, domain and ontology_name */
+                                  data_location, data_location, test_schemas, /* loc, domain and 
ontology_name */
                                   NULL, FALSE, FALSE,
                                   100, 100, NULL, NULL, NULL, &error);
 
@@ -345,6 +347,7 @@ test_sparql_query (TestInfo      *test_info,
        g_free (query);
        g_free (results_filename);
        g_object_unref (test_schemas);
+       g_object_unref (data_location);
 
        tracker_data_manager_shutdown ();
 }
@@ -353,26 +356,15 @@ static void
 setup (TestInfo      *info,
        gconstpointer  context)
 {
-       gint i;
-
-       i = GPOINTER_TO_INT (context);
-       *info = tests[i];
+       const TestInfo *test = context;
+       gchar *basename;
 
-       /* 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;
+       *info = *test;
 
-               /* 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));
-       }
+       /* 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));
+       info->data_location = g_build_path (G_DIR_SEPARATOR_S, tests_data_dir, basename, NULL);
+       g_free (basename);
 }
 
 static void
@@ -382,14 +374,13 @@ teardown (TestInfo      *info,
        gchar *cleanup_command;
 
        /* clean up */
-       g_print ("Removing temporary data (%s)\n", xdg_location);
+       g_print ("Removing temporary data (%s)\n", info->data_location);
 
-       cleanup_command = g_strdup_printf ("rm -Rf %s/", xdg_location);
+       cleanup_command = g_strdup_printf ("rm -Rf %s/", info->data_location);
        g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
        g_free (cleanup_command);
 
-       g_free (xdg_location);
-       xdg_location = NULL;
+       g_free (info->data_location);
 }
 
 int
@@ -423,7 +414,7 @@ main (int argc, char **argv)
 #endif
 
                testpath = g_strconcat ("/libtracker-data/sparql/", tests[i].test_name, NULL);
-               g_test_add (testpath, TestInfo, GINT_TO_POINTER(i), setup, test_sparql_query, teardown);
+               g_test_add (testpath, TestInfo, &tests[i], setup, test_sparql_query, teardown);
                g_free (testpath);
        }
 
diff --git a/tests/libtracker-fts/tracker-fts-test.c b/tests/libtracker-fts/tracker-fts-test.c
index 328c295..729ddff 100644
--- a/tests/libtracker-fts/tracker-fts-test.c
+++ b/tests/libtracker-fts/tracker-fts-test.c
@@ -43,6 +43,8 @@ const TestInfo tests[] = {
        { NULL }
 };
 
+static gchar *datadir = NULL;
+
 static void
 test_sparql_query (gconstpointer test_data)
 {
@@ -54,7 +56,7 @@ test_sparql_query (gconstpointer test_data)
        gchar *query, *query_filename;
        gchar *results, *results_filename;
        gchar *prefix, *test_prefix;
-       GFile *ontology;
+       GFile *ontology, *data_location;
        gint i;
 
        error = NULL;
@@ -66,9 +68,11 @@ test_sparql_query (gconstpointer test_data)
        ontology = g_file_new_for_path (prefix);
        g_free (prefix);
 
+       data_location = g_file_new_for_path (datadir);
+
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
        tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
-                                  NULL, NULL, ontology,
+                                  data_location, data_location, ontology,
                                   NULL, FALSE, FALSE,
                                   100, 100, NULL, NULL, NULL, &error);
 
@@ -163,6 +167,7 @@ test_sparql_query (gconstpointer test_data)
        }
 
        g_free (test_prefix);
+       g_object_unref (data_location);
 
        tracker_data_manager_shutdown ();
 }
@@ -172,19 +177,16 @@ main (int argc, char **argv)
 {
        gint result;
        gint i;
-       gchar *current_dir;
-       gchar *path;
+       gchar *current_dir, *path;
 
        g_test_init (&argc, &argv, NULL);
 
        current_dir = g_get_current_dir ();
+       datadir = g_build_filename (current_dir, "tracker", NULL);
+       g_free (current_dir);
 
-       g_setenv ("XDG_DATA_HOME", current_dir, TRUE);
-       g_setenv ("XDG_CACHE_HOME", current_dir, TRUE);
        g_setenv ("TRACKER_FTS_STOP_WORDS", "0", TRUE);
 
-       g_free (current_dir);
-
        /* add test cases */
        for (i = 0; tests[i].test_name; i++) {
                gchar *testpath;
@@ -204,6 +206,7 @@ main (int argc, char **argv)
        path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-fts", "dconf", "user", NULL);
        g_unlink (path);
        g_free (path);
+       g_free (datadir);
 
        return result;
 }


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