[5aca6f08b6780157a0da46aaaef9d86d2fe55919395b0e2aba71e52a377db90d/sam/cli-search-test: 32/32] tests: Add cli test for `tracker search`



commit f1a969457129f2e9615084f5ce8daba0634062e2
Author: Sam Thursfield <sam afuera me uk>
Date:   Sun Jun 21 17:32:32 2020 +0200

    tests: Add cli test for `tracker search`
    
    This succeeds, but it currently has to work around a bug by
    passing `--all`.

 meson.build                                        |  3 +-
 tests/functional-tests/cli.py                      | 52 ++++++++++++++++++++
 tests/functional-tests/configuration.json.in       |  2 +
 tests/functional-tests/configuration.py            |  8 +++
 tests/functional-tests/fixtures.py                 | 57 +++++++++++++++++++++-
 tests/functional-tests/meson.build                 |  3 ++
 .../test-cli-data/text/Document 1.txt              |  5 ++
 .../test-cli-data/text/Document 2.txt              |  7 +++
 8 files changed, 135 insertions(+), 2 deletions(-)
---
diff --git a/meson.build b/meson.build
index 6f4f9c439..60ab48ba1 100644
--- a/meson.build
+++ b/meson.build
@@ -402,6 +402,7 @@ tracker_writeback_modules_dir = join_paths(get_option('prefix'), get_option('lib
 tracker_extract_rules_dir = join_paths(get_option('prefix'), get_option('datadir'), tracker_versioned_name, 
'extract-rules')
 tracker_miner_services_dir = join_paths(get_option('prefix'), get_option('datadir'), tracker_versioned_name, 
'miners')
 
+tracker_uninstalled_cli_subcommands_dir = meson.current_build_dir() / 'src' / 'tracker' / 'subcommands'
 tracker_uninstalled_extract_rules_dir = join_paths(meson.current_build_dir(), 'src', 'tracker-extract', 
'uninstalled-rules')
 tracker_uninstalled_writeback_modules_dir = join_paths(meson.current_build_dir(), 'src', 'tracker-writeback')
 uninstalled_tracker_extract_path = join_paths(meson.current_build_dir(), 'src', 'tracker-extract', 
'tracker-extract-3')
@@ -431,7 +432,7 @@ meson.add_install_script('meson_integration_commands.sh', glib_compile_schemas.p
 run_uninstalled_conf = configuration_data()
 run_uninstalled_conf.set('tracker_sparql_uninstalled_dir', tracker_sparql_uninstalled_dir)
 run_uninstalled_conf.set('tracker_uninstalled_cli_dir', tracker_uninstalled_cli_dir)
-run_uninstalled_conf.set('tracker_uninstalled_cli_subcommands_dir', meson.current_build_dir() / 'src' / 
'tracker' / 'subcommands')
+run_uninstalled_conf.set('tracker_uninstalled_cli_subcommands_dir', tracker_uninstalled_cli_subcommands_dir)
 run_uninstalled_conf.set('tracker_uninstalled_domain_rule', meson.current_build_dir() / 'tests' / 
'functional-tests' / 'test-domain.rule')
 run_uninstalled_conf.set('tracker_uninstalled_extract_rules_dir', tracker_uninstalled_extract_rules_dir)
 run_uninstalled_conf.set('tracker_uninstalled_extractors_dir', meson.current_build_dir() / 'src' / 
'tracker-extract')
diff --git a/tests/functional-tests/cli.py b/tests/functional-tests/cli.py
new file mode 100644
index 000000000..72d9a02d9
--- /dev/null
+++ b/tests/functional-tests/cli.py
@@ -0,0 +1,52 @@
+# Copyright (C) 2020, Sam Thursfield <sam afuera me uk>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+"""
+Test `tracker` commandline tool
+"""
+
+import pathlib
+
+import configuration
+import fixtures
+
+class TestCli(fixtures.TrackerCommandLineTestCase):
+    def test_search(self):
+        datadir = pathlib.Path(__file__).parent.joinpath('test-cli-data')
+
+        # FIXME: synchronous `tracker index` isn't ready yet; 
+        # see https://gitlab.gnome.org/GNOME/tracker/-/issues/188
+        # in the meantime we manually wait for it to finish.
+
+        file1 = datadir.joinpath('text/Document 1.txt')
+        file2 = datadir.joinpath('text/Document 2.txt')
+
+        with self.await_document_inserted(file1):
+            with self.await_document_inserted(file2):
+                output = self.run_cli(
+                    ['tracker3', 'index', '--file', str(datadir)])
+
+        # FIXME: the --all should NOT be needed.
+        # See: https://gitlab.gnome.org/GNOME/tracker-miners/-/issues/116
+        output = self.run_cli(
+            ['tracker3', 'search', '--all', 'banana'])
+        self.assertIn(file1.as_uri(), output)
+        self.assertNotIn(file2.as_uri(), output)
+
+
+if __name__ == '__main__':
+    fixtures.tracker_test_main()
diff --git a/tests/functional-tests/configuration.json.in b/tests/functional-tests/configuration.json.in
index 7ef895fe7..6a3187cfb 100644
--- a/tests/functional-tests/configuration.json.in
+++ b/tests/functional-tests/configuration.json.in
@@ -1,4 +1,6 @@
 {
+    "TEST_CLI_DIR": "@TEST_CLI_DIR@",
+    "TEST_CLI_SUBCOMMANDS_DIR": "@TEST_CLI_SUBCOMMANDS_DIR@",
     "TEST_DBUS_DAEMON_CONFIG_FILE": "@TEST_DBUS_DAEMON_CONFIG_FILE@",
     "TEST_DCONF_PROFILE": "@TEST_DCONF_PROFILE@",
     "TEST_DOMAIN_ONTOLOGY_RULE": "@TEST_DOMAIN_ONTOLOGY_RULE@",
diff --git a/tests/functional-tests/configuration.py b/tests/functional-tests/configuration.py
index 22ba09db2..8ec7e13b5 100644
--- a/tests/functional-tests/configuration.py
+++ b/tests/functional-tests/configuration.py
@@ -59,6 +59,14 @@ def test_environment(tmpdir):
     }
 
 
+def cli_dir():
+    return config['TEST_CLI_DIR']
+
+
+def cli_subcommands_dir():
+    return config['TEST_CLI_SUBCOMMANDS_DIR']
+
+
 # This path is used for test data for tests which expect filesystem monitoring
 # to work. For this reason we must avoid it being on a tmpfs filesystem. Note
 # that this MUST NOT be a hidden directory, as Tracker is hardcoded to ignore
diff --git a/tests/functional-tests/fixtures.py b/tests/functional-tests/fixtures.py
index f6efe7adb..119c3534e 100644
--- a/tests/functional-tests/fixtures.py
+++ b/tests/functional-tests/fixtures.py
@@ -29,6 +29,7 @@ gi.require_version('Gio', '2.0')
 from gi.repository import GLib, Gio
 from gi.repository import Tracker
 
+import contextlib
 import errno
 import json
 import logging
@@ -37,6 +38,7 @@ import pathlib
 import shutil
 import subprocess
 import sys
+import tempfile
 import time
 import unittest as ut
 
@@ -139,7 +141,10 @@ class TrackerMinerTest(ut.TestCase):
 
     def await_document_inserted(self, path, content=None):
         """Wraps await_insert() context manager."""
-        url = self.uri(path)
+        if isinstance(path, pathlib.Path):
+            url = path.as_uri()
+        else:
+            url = self.uri(path)
 
         expected = [
             'a nfo:Document',
@@ -456,3 +461,53 @@ class TrackerWritebackTest (TrackerMinerTest):
         raise Exception(
             "Timeout waiting for %s to be updated (mtime has not changed)" %
             filename)
+
+
+class CliError(Exception):
+    pass
+
+
+class TrackerCommandLineTestCase(TrackerMinerTest):
+    def setUp(self):
+        super(TrackerCommandLineTestCase, self).setUp()
+
+        extra_env = cfg.test_environment(self.workdir)
+        extra_env['LANG'] = 'en_GB.utf8'
+
+        self.env = os.environ.copy()
+        self.env.update(extra_env)
+
+        path = self.env.get('PATH', []).split(':')
+        self.env['PATH'] = ':'.join([cfg.cli_dir()] + path)
+        self.env['TRACKER_CLI_SUBCOMMANDS_DIR'] = cfg.cli_subcommands_dir()
+
+    @contextlib.contextmanager
+    def tmpdir(self):
+        try:
+            dirpath = tempfile.mkdtemp()
+            yield pathlib.Path(dirpath)
+        finally:
+            shutil.rmtree(dirpath, ignore_errors=True)
+
+    def data_path(self, filename):
+        test_data = pathlib.Path(__file__).parent.joinpath('test-data')
+        return test_data.joinpath(filename)
+
+    def run_cli(self, command):
+        command = [str(c) for c in command]
+        log.info("Running: %s", ' '.join(command))
+        result = subprocess.run(command, stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE, env=self.env)
+
+        if len(result.stdout) > 0:
+            log.debug("stdout: %s", result.stdout)
+        if len(result.stderr) > 0:
+            log.debug("stderr: %s", result.stderr)
+
+        if result.returncode != 0:
+            raise CliError('\n'.join([
+                "CLI command failed.",
+                "Command: %s" % ' '.join(command),
+                "Error: %s" % result.stderr.decode('utf-8')]))
+
+        return result.stdout.decode('utf-8')
diff --git a/tests/functional-tests/meson.build b/tests/functional-tests/meson.build
index 9efe8752e..7f1f5a7b7 100644
--- a/tests/functional-tests/meson.build
+++ b/tests/functional-tests/meson.build
@@ -7,6 +7,8 @@ config_json_full_path = meson.current_build_dir() / 'configuration.json'
 dconf_profile_full_path = meson.current_source_dir() / 'trackertest'
 tracker_extractors_dir = meson.current_build_dir() / '..' / '..' / 'src' / 'tracker-extract'
 
+testconf.set('TEST_CLI_DIR', tracker_uninstalled_cli_dir)
+testconf.set('TEST_CLI_SUBCOMMANDS_DIR', tracker_uninstalled_cli_subcommands_dir)
 testconf.set('TEST_DBUS_DAEMON_CONFIG_FILE', build_root / 'tests' / 'test-bus.conf')
 testconf.set('TEST_DCONF_PROFILE', dconf_profile_full_path)
 testconf.set('TEST_DOMAIN_ONTOLOGY_RULE', meson.current_build_dir() / 'test-domain.rule')
@@ -115,6 +117,7 @@ functional_tests = [
   'fts-file-operations',
   'fts-stopwords',
   'extractor-decorator',
+  'cli',
 ]
 
 if libcue.found()
diff --git a/tests/functional-tests/test-cli-data/text/Document 1.txt 
b/tests/functional-tests/test-cli-data/text/Document 1.txt
new file mode 100644
index 000000000..699c00d6d
--- /dev/null
+++ b/tests/functional-tests/test-cli-data/text/Document 1.txt  
@@ -0,0 +1,5 @@
+This is Document 1.
+
+Word of the day: banana
+
+Sentence of the day: Please may I have a banana?
diff --git a/tests/functional-tests/test-cli-data/text/Document 2.txt 
b/tests/functional-tests/test-cli-data/text/Document 2.txt
new file mode 100644
index 000000000..42c4b96c2
--- /dev/null
+++ b/tests/functional-tests/test-cli-data/text/Document 2.txt  
@@ -0,0 +1,7 @@
+This is Document 2.
+
+Word of the day: platypus
+
+Sentence of the day: Did you know that a male platypus is poisonous?
+
+(Reference: https://web.archive.org/web/20110621170907/http://rainforest-australia.com/platypus_poison.htm)


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