[tracker-miners/sam/blocklist-dds: 1/3] Add a test for tracker-extract module manager.



commit e88965aa346d84ddcfe630647aace2ce40f9f454
Author: Sam Thursfield <sam afuera me uk>
Date:   Sun Nov 3 17:28:34 2019 +0100

    Add a test for tracker-extract module manager.
    
    The module manager decides which module should be used for a given file
    based on its MIME type. This is an important codepath which we should
    have automated tests for.

 src/libtracker-extract/tracker-module-manager.c    | 33 ++++++++-
 src/libtracker-extract/tracker-module-manager.h    |  2 +
 tests/libtracker-extract/meson.build               |  1 +
 .../test-extract-rules/90-audio-generic.rule       |  4 ++
 .../test-extract-rules/90-image-generic.rule       |  4 ++
 .../tracker-module-manager-test.c                  | 84 ++++++++++++++++++++++
 6 files changed, 125 insertions(+), 3 deletions(-)
---
diff --git a/src/libtracker-extract/tracker-module-manager.c b/src/libtracker-extract/tracker-module-manager.c
index 1ab9b122b..426cfb385 100644
--- a/src/libtracker-extract/tracker-module-manager.c
+++ b/src/libtracker-extract/tracker-module-manager.c
@@ -28,6 +28,7 @@
 #define SHUTDOWN_FUNCTION  "tracker_extract_module_shutdown"
 
 typedef struct {
+       const gchar *rule_path;
        const gchar *module_path; /* intern string */
        GList *patterns;
        GStrv fallback_rdf_types;
@@ -65,8 +66,9 @@ dummy_extract_func (TrackerExtractInfo *info)
 }
 
 static gboolean
-load_extractor_rule (GKeyFile  *key_file,
-                     GError   **error)
+load_extractor_rule (GKeyFile    *key_file,
+                     const gchar *rule_path,
+                     GError     **error)
 {
        GError *local_error = NULL;
        gchar *module_path, **mimetypes;
@@ -114,6 +116,8 @@ load_extractor_rule (GKeyFile  *key_file,
                return FALSE;
        }
 
+       rule.rule_path = g_strdup (rule_path);
+
        rule.fallback_rdf_types = g_key_file_get_string_list (key_file, "ExtractorRule", "FallbackRdfTypes", 
NULL, NULL);
 
        /* Construct the rule */
@@ -189,7 +193,7 @@ tracker_extract_module_manager_init (void)
                key_file = g_key_file_new ();
 
                if (!g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error) ||
-                   !load_extractor_rule (key_file, &error)) {
+                   !load_extractor_rule (key_file, path, &error)) {
                        g_warning ("  Could not load extractor rule file '%s': %s", name, error->message);
                        g_clear_error (&error);
                } else {
@@ -262,6 +266,29 @@ lookup_rules (const gchar *mimetype)
        return mimetype_rules;
 }
 
+/**
+ * tracker_extract_module_manager_get_matching_rules:
+ * @mimetype: a MIME type string
+ *
+ * Returns: (transfer none): a list of extract .rule files that support the given type.
+ **/
+GList *
+tracker_extract_module_manager_get_matching_rules (const gchar *mimetype)
+{
+       GList *rule_list, *l;
+       GList *rule_path_list = NULL;
+
+       rule_list = lookup_rules (mimetype);
+
+       for (l = rule_list; l; l = l->next) {
+               RuleInfo *info = l->data;
+
+               rule_path_list = g_list_prepend (rule_path_list, (char *)(info->rule_path));
+       }
+
+       return g_list_reverse (rule_path_list);
+}
+
 GStrv
 tracker_extract_module_manager_get_fallback_rdf_types (const gchar *mimetype)
 {
diff --git a/src/libtracker-extract/tracker-module-manager.h b/src/libtracker-extract/tracker-module-manager.h
index 0c0667156..647605a43 100644
--- a/src/libtracker-extract/tracker-module-manager.h
+++ b/src/libtracker-extract/tracker-module-manager.h
@@ -47,6 +47,8 @@ GStrv tracker_extract_module_manager_get_rdf_types (void);
 TrackerMimetypeInfo * tracker_extract_module_manager_get_mimetype_handlers  (const gchar *mimetype);
 GStrv                 tracker_extract_module_manager_get_fallback_rdf_types (const gchar *mimetype);
 
+GList *tracker_extract_module_manager_get_matching_rules (const gchar *mimetype);
+
 GModule * tracker_mimetype_info_get_module (TrackerMimetypeInfo          *info,
                                             TrackerExtractMetadataFunc   *extract_func);
 gboolean  tracker_mimetype_info_iter_next  (TrackerMimetypeInfo          *info);
diff --git a/tests/libtracker-extract/meson.build b/tests/libtracker-extract/meson.build
index ba1b02f70..196f332ea 100644
--- a/tests/libtracker-extract/meson.build
+++ b/tests/libtracker-extract/meson.build
@@ -1,5 +1,6 @@
 libtracker_extract_tests = [
     'extract-info',
+    'module-manager',
     'guarantee',
     'utils',
     'xmp',
diff --git a/tests/libtracker-extract/test-extract-rules/90-audio-generic.rule 
b/tests/libtracker-extract/test-extract-rules/90-audio-generic.rule
new file mode 100644
index 000000000..941151677
--- /dev/null
+++ b/tests/libtracker-extract/test-extract-rules/90-audio-generic.rule
@@ -0,0 +1,4 @@
+[ExtractorRule]
+ModulePath=TEST_MODULE_AUDIO
+MimeTypes=audio/*;
+FallbackRdfTypes=nfo:Audio;
diff --git a/tests/libtracker-extract/test-extract-rules/90-image-generic.rule 
b/tests/libtracker-extract/test-extract-rules/90-image-generic.rule
new file mode 100644
index 000000000..642c3e0da
--- /dev/null
+++ b/tests/libtracker-extract/test-extract-rules/90-image-generic.rule
@@ -0,0 +1,4 @@
+[ExtractorRule]
+ModulePath=libextract-gstreamer.so
+MimeTypes=image/*;
+FallbackRdfTypes=nfo:Image;nmm:Photo;
diff --git a/tests/libtracker-extract/tracker-module-manager-test.c 
b/tests/libtracker-extract/tracker-module-manager-test.c
new file mode 100644
index 000000000..ffd2a39b2
--- /dev/null
+++ b/tests/libtracker-extract/tracker-module-manager-test.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2019, Sam Thursfield <sam afuera me uk>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "config-miners.h"
+
+#include <glib.h>
+
+#include <libtracker-miners-common/tracker-common.h>
+#include <libtracker-extract/tracker-module-manager.h>
+
+#define assert_path_basename(path, cmp, expected_value) {    \
+       g_autofree gchar *basename = g_path_get_basename (path); \
+       g_assert_cmpstr (basename, cmp, expected_value);         \
+}
+
+static gchar *
+get_test_rules_dir (void)
+{
+       return g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-extract", 
"test-extract-rules", NULL);
+}
+
+static void
+init_module_manager (void) {
+       gboolean success;
+       g_autofree gchar *test_rules_dir = NULL;
+
+       test_rules_dir = get_test_rules_dir ();
+       g_setenv ("TRACKER_EXTRACTOR_RULES_DIR", test_rules_dir, TRUE);
+
+       success = tracker_extract_module_manager_init ();
+       g_assert (success);
+}
+
+static void
+test_extract_rules (void)
+{
+       GList *l;
+
+       // The audio/* rule should match this, but the image/* rule should not.
+       l = tracker_extract_module_manager_get_matching_rules("audio/mpeg");
+
+       g_assert_cmpint (g_list_length (l), ==, 1);
+       assert_path_basename (l->data, ==, "90-audio-generic.rule");
+
+       // The image/* rule should match this, but the audio/* rule should not.
+       l = tracker_extract_module_manager_get_matching_rules("image/png");
+
+       g_assert_cmpint (g_list_length (l), ==, 1);
+       assert_path_basename (l->data, ==, "90-image-generic.rule");
+
+       // No rule should match this.
+       l = tracker_extract_module_manager_get_matching_rules("text/generic");
+       g_assert_cmpint (g_list_length (l), ==, 0);
+}
+
+int
+main (int argc, char **argv)
+{
+       gchar *used_filename;
+
+       g_test_init (&argc, &argv, NULL);
+       tracker_log_init (3, &used_filename);
+       init_module_manager ();
+
+       g_test_add_func ("/libtracker-extract/module-manager/extract-rules",
+                        test_extract_rules);
+       return g_test_run ();
+}


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