diff options
author | Carlos Garnacho <mrgarnacho@gmail.com> | 2020-03-04 21:21:34 +0000 |
---|---|---|
committer | Carlos Garnacho <mrgarnacho@gmail.com> | 2020-03-04 21:21:34 +0000 |
commit | 925082b0fb283017e8130f94bd9493724f25fe09 (patch) | |
tree | 025ac423b89fef282f431d8eeace6c67678bc3c3 | |
parent | 5b720cd2dd7cc32a845e71ba3385b163a59fe80f (diff) | |
parent | e40c904a7b34513d280c92c5c5138eb5b56e6800 (diff) | |
download | tracker-925082b0fb283017e8130f94bd9493724f25fe09.tar.gz |
Merge branch 'sam/cli-uninstalled-fixes' into 'master'
Add initial tests for the CLI
See merge request GNOME/tracker!187
-rw-r--r-- | src/tracker/make-uninstalled-subcommand-links.sh | 22 | ||||
-rw-r--r-- | src/tracker/meson.build | 3 | ||||
-rw-r--r-- | src/tracker/tracker-main.c | 45 | ||||
-rw-r--r-- | tests/functional-tests/cli.py | 57 | ||||
-rw-r--r-- | tests/functional-tests/configuration.json.in | 4 | ||||
-rw-r--r-- | tests/functional-tests/configuration.py | 8 | ||||
-rw-r--r-- | tests/functional-tests/fixtures.py | 47 | ||||
-rw-r--r-- | tests/functional-tests/meson.build | 3 |
8 files changed, 172 insertions, 17 deletions
diff --git a/src/tracker/make-uninstalled-subcommand-links.sh b/src/tracker/make-uninstalled-subcommand-links.sh new file mode 100644 index 000000000..bce8d1124 --- /dev/null +++ b/src/tracker/make-uninstalled-subcommand-links.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -e + +cli_dir=$MESON_BUILD_ROOT/$MESON_SUBDIR +subcommands_dir=$MESON_BUILD_ROOT/$MESON_SUBDIR/subcommands + +mkdir -p $subcommands_dir + +for l in `find $subcommands_dir -type l` +do +# Delete all previous links to our own binary +if [ `readlink $l` = "$cli_dir/tracker" ] +then + rm $l +fi +done + +for subcommand in $@ +do + ln -s $cli_dir/tracker $subcommands_dir/$subcommand +done diff --git a/src/tracker/meson.build b/src/tracker/meson.build index 650d5155d..f02f1986a 100644 --- a/src/tracker/meson.build +++ b/src/tracker/meson.build @@ -35,3 +35,6 @@ if get_option('bash_completion') sources: 'bash-completion/tracker', install_dir: bash_completion_dir) endif + +run_command('make-uninstalled-subcommand-links.sh', modules, + check: true) diff --git a/src/tracker/tracker-main.c b/src/tracker/tracker-main.c index 5b8866d3a..82fe1523e 100644 --- a/src/tracker/tracker-main.c +++ b/src/tracker/tracker-main.c @@ -161,6 +161,13 @@ print_usage_list_cmds (void) GFileEnumerator *enumerator; GFileInfo *info; GFile *dir; + GError *error = NULL; + const gchar *subcommands_dir; + + subcommands_dir = g_getenv ("TRACKER_CLI_SUBCOMMANDS_DIR"); + if (!subcommands_dir) { + subcommands_dir = LIBEXECDIR "/tracker/"; + } for (i = 0; i < G_N_ELEMENTS(commands); i++) { if (longest < strlen (commands[i].cmd)) @@ -175,27 +182,31 @@ print_usage_list_cmds (void) puts (_(commands[i].help)); } - dir = g_file_new_for_path (LIBEXECDIR "/tracker/"); + dir = g_file_new_for_path (subcommands_dir); enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, - NULL, NULL); + NULL, &error); g_object_unref (dir); - while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) { - /* Filter builtin commands */ - if (g_file_info_get_is_symlink (info) && - g_strcmp0 (g_file_info_get_symlink_target (info), BINDIR "/tracker") == 0) - continue; + if (enumerator) { + while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) { + /* Filter builtin commands */ + if (g_file_info_get_is_symlink (info) && + g_strcmp0 (g_file_info_get_symlink_target (info), BINDIR "/tracker") == 0) + continue; - extra_commands = g_list_prepend (extra_commands, - g_strdup (g_file_info_get_name (info))); - g_object_unref (info); - } + extra_commands = g_list_prepend (extra_commands, + g_strdup (g_file_info_get_name (info))); + g_object_unref (info); + } - g_object_unref (enumerator); + g_object_unref (enumerator); + } else { + g_warning ("Failed to list extra commands: %s", error->message); + } if (extra_commands) { extra_commands = g_list_sort (extra_commands, (GCompareFunc) g_strcmp0); @@ -224,6 +235,7 @@ main (int argc, char *argv[]) { gboolean basename_is_bin = FALSE; gchar *command_basename; + const gchar *subcommands_dir; setlocale (LC_ALL, ""); @@ -235,8 +247,13 @@ main (int argc, char *argv[]) basename_is_bin = g_strcmp0 (command_basename, "tracker") == 0; g_free (command_basename); + subcommands_dir = g_getenv ("TRACKER_CLI_SUBCOMMANDS_DIR"); + if (!subcommands_dir) { + subcommands_dir = LIBEXECDIR "/tracker/"; + } + if (g_path_is_absolute (argv[0]) && - g_str_has_prefix (argv[0], LIBEXECDIR "/tracker/")) { + g_str_has_prefix (argv[0], subcommands_dir)) { /* This is a subcommand call */ handle_command (argc, (const gchar **) argv); exit (EXIT_FAILURE); @@ -255,7 +272,7 @@ main (int argc, char *argv[]) subcommand = "help"; } - path = g_build_filename (LIBEXECDIR, "tracker", subcommand, NULL); + path = g_build_filename (subcommands_dir, subcommand, NULL); if (g_file_test (path, G_FILE_TEST_EXISTS)) { /* Manipulate argv in place, in order to launch subcommand */ 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') |