Commit f1a96945 authored by Sam Thursfield's avatar Sam Thursfield

tests: Add cli test for `tracker search`

This succeeds, but it currently has to work around a bug by
passing `--all`.
parent 5b2dffcb
Pipeline #194854 passed with stage
in 2 minutes and 23 seconds
......@@ -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')
......
# 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()
{
"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@",
......
......@@ -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
......
......@@ -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')
......@@ -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()
......
This is Document 1.
Word of the day: banana
Sentence of the day: Please may I have a banana?
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment