[tracker/sam/cli-uninstalled-fixes: 2/2] Add initial tests for `tracker` CLI



commit 43f66fa4e1e17e5096867613f7dcd36532feeb3e
Author: Sam Thursfield <sam afuera me uk>
Date:   Tue Feb 25 16:54:27 2020 +0100

    Add initial tests for `tracker` CLI
    
    This gives us some basic coverage of the commandline interface.

 tests/functional-tests/cli.py                | 57 ++++++++++++++++++++++++++++
 tests/functional-tests/configuration.json.in |  4 +-
 tests/functional-tests/configuration.py      |  8 ++++
 tests/functional-tests/fixtures.py           | 47 ++++++++++++++++++++++-
 tests/functional-tests/meson.build           |  3 ++
 5 files changed, 116 insertions(+), 3 deletions(-)
---
diff --git a/tests/functional-tests/cli.py b/tests/functional-tests/cli.py
new file mode 100644
index 000000000..63d330384
--- /dev/null
+++ b/tests/functional-tests/cli.py
@@ -0,0 +1,57 @@
+# 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 unittest
+
+import configuration
+import fixtures
+
+
+class TestCli(fixtures.TrackerCommandLineTestCase):
+    def test_version(self):
+        """Check we're testing the correct version of the CLI"""
+        output = self.run_cli(
+            ['tracker', '--version'])
+
+        version_line = output.splitlines()[0]
+        expected_version_line = 'Tracker %s' % configuration.tracker_version()
+        self.assertEqual(version_line, expected_version_line)
+
+    def test_create_local_database(self):
+        """Create a database using `tracker endpoint` for local testing"""
+
+        with self.tmpdir() as tmpdir:
+            ontology_path = configuration.ontologies_dir()
+
+            # Create the database
+            self.run_cli(
+                ['tracker', 'endpoint', '--database', tmpdir,
+                 '--ontology-path', ontology_path])
+
+            # Sanity check that it works.
+            self.run_cli(
+                ['tracker', 'sparql', '--database', tmpdir,
+                 '--query', 'ASK { ?u a rdfs:Resource }'])
+
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2)
diff --git a/tests/functional-tests/configuration.json.in b/tests/functional-tests/configuration.json.in
index 0b2cd5f9d..6ec1d72e1 100644
--- a/tests/functional-tests/configuration.json.in
+++ b/tests/functional-tests/configuration.json.in
@@ -1,3 +1,5 @@
 {
-    "TEST_ONTOLOGIES_DIR": "@TEST_ONTOLOGIES_DIR@"
+    "TEST_CLI_DIR": "@TEST_CLI_DIR@",
+    "TEST_ONTOLOGIES_DIR": "@TEST_ONTOLOGIES_DIR@",
+    "TRACKER_VERSION": "@TRACKER_VERSION@"
 }
diff --git a/tests/functional-tests/configuration.py b/tests/functional-tests/configuration.py
index 8146ceef4..60db8cade 100644
--- a/tests/functional-tests/configuration.py
+++ b/tests/functional-tests/configuration.py
@@ -37,10 +37,18 @@ with open(os.environ['TRACKER_FUNCTIONAL_TEST_CONFIG']) as f:
     config = json.load(f)
 
 
+def cli_dir():
+    return config['TEST_CLI_DIR']
+
+
 def ontologies_dir():
     return config['TEST_ONTOLOGIES_DIR']
 
 
+def tracker_version():
+    return config['TRACKER_VERSION']
+
+
 def get_environment_boolean(variable):
     '''Parse a yes/no boolean passed through the environment.'''
 
diff --git a/tests/functional-tests/fixtures.py b/tests/functional-tests/fixtures.py
index e90b4583a..46a392d61 100644
--- a/tests/functional-tests/fixtures.py
+++ b/tests/functional-tests/fixtures.py
@@ -27,12 +27,14 @@ gi.require_version('Tracker', '3.0')
 from gi.repository import Gio, GLib
 from gi.repository import Tracker
 
+import contextlib
 import logging
 import os
+import pathlib
 import multiprocessing
 import shutil
+import subprocess
 import tempfile
-import time
 import unittest as ut
 
 import trackertestutils.helpers
@@ -58,7 +60,7 @@ class TrackerSparqlDirectTest(ut.TestCase):
                 None)
 
             self.tracker = trackertestutils.helpers.StoreHelper(self.conn)
-        except Exception as e:
+        except Exception:
             shutil.rmtree(self.tmpdir, ignore_errors=True)
             raise
 
@@ -122,3 +124,44 @@ class TrackerSparqlBusTest (ut.TestCase):
         self.conn.close()
         self.process.terminate()
         shutil.rmtree(self.tmpdir, ignore_errors=True)
+
+
+
+class CliError(Exception):
+    pass
+
+
+class TrackerCommandLineTestCase(ut.TestCase):
+    def setUp(self):
+        self.env = os.environ.copy()
+
+        path = self.env.get('PATH', []).split(':')
+        self.env['PATH'] = ':'.join([cfg.cli_dir()] + path)
+        self.env['TRACKER_CLI_SUBCOMMANDS_DIR'] = os.path.join(cfg.cli_dir(), 'subcommands')
+
+    @contextlib.contextmanager
+    def tmpdir(self):
+        try:
+            dirpath = tempfile.mkdtemp()
+            yield pathlib.Path(dirpath)
+        finally:
+            shutil.rmtree(dirpath, ignore_errors=True)
+
+    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 eb5a5c75f..d5b90dca3 100644
--- a/tests/functional-tests/meson.build
+++ b/tests/functional-tests/meson.build
@@ -4,7 +4,9 @@ testconf = configuration_data()
 
 config_json_full_path = join_paths(meson.current_build_dir(), 'configuration.json')
 
+testconf.set('TEST_CLI_DIR', tracker_uninstalled_cli_dir)
 testconf.set('TEST_ONTOLOGIES_DIR', tracker_uninstalled_nepomuk_ontologies_dir)
+testconf.set('TRACKER_VERSION', meson.project_version())
 
 config_json = configure_file(
   input: 'configuration.json.in',
@@ -24,6 +26,7 @@ functional_tests = [
   'notifier',
   'collation',
   'ontology-changes',
+  'cli',
 ]
 
 if get_option('fts')


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