1#!/usr/bin/env python3
2# -.- coding: utf-8 -.-
3
4# Zeitgeist
5#
6# Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
7# Copyright © 2009-2010 Markus Korn <thekorn@gmx.de>
8# Copyright © 2011-2012 Collabora Ltd.
9#             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License as published by
13# the Free Software Foundation, either version 2.1 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU Lesser General Public License for more details.
20#
21# You should have received a copy of the GNU Lesser General Public License
22# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24import os
25import unittest
26import logging
27import sys
28
29from optparse import OptionParser
30
31if not os.path.isfile("NEWS"):
32    print("*** Please run from root directory.", file=sys.stderr)
33    raise SystemExit
34
35# Load the updated Zeitgeist Python module
36sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../.."))
37
38if not os.path.isdir("zeitgeist"):
39	print("*** If you get unexpected failures, " \
40		"you may want to run: `ln -s python zeitgeist`", file=sys.stderr)
41
42from testutils import RemoteTestCase
43
44# Find the test/ directory
45TESTDIR = os.path.dirname(os.path.abspath(__file__))
46
47def iter_tests(suite):
48	for test in suite:
49		if isinstance(test, unittest.TestSuite):
50			for t in iter_tests(test):
51				yield t
52		else:
53			yield test
54
55def get_test_name(test):
56	return ".".join((test.__class__.__module__,
57		test.__class__.__name__, test._testMethodName))
58
59def load_tests(module, pattern):
60	suite = unittest.defaultTestLoader.loadTestsFromModule(module)
61	for test in iter_tests(suite):
62		name = get_test_name(test)
63		if pattern is not None:
64			for p in pattern:
65				if name.startswith(p):
66					yield test
67					break
68		else:
69			yield test
70
71def compile_suite(pattern=None):
72	# Create a test suite to run all tests
73	suite = unittest.TestSuite()
74
75	# Add all of the tests from each file that ends with "-test.py"
76	for fname in os.listdir(TESTDIR):
77		if fname.endswith("-test.py"):
78			fname = os.path.basename(fname)[:-3] # filename without the ".py"
79			module = __import__(fname)
80			tests = list(load_tests(module, pattern))
81			suite.addTests(tests)
82	return suite
83
84if __name__ == "__main__":
85	parser = OptionParser()
86	parser.add_option("-v", action="count", dest="verbosity")
87	(options, args) = parser.parse_args()
88
89	if options.verbosity:
90		# do more fine grained stuff later
91		# redirect all debugging output to stderr
92		logging.basicConfig(stream=sys.stderr)
93		sys.argv.append('--verbose-subprocess')
94	else:
95		logging.basicConfig(filename="/dev/null")
96
97	from testutils import DBusPrivateMessageBus
98	bus = DBusPrivateMessageBus()
99	err = bus.run(ignore_errors=True)
100	if err:
101		print("*** Failed to setup private bus, error was: %s" %err, file=sys.stderr)
102		raise SystemExit
103	else:
104		print("*** Testsuite is running using a private dbus bus", file=sys.stderr)
105		config = bus.dbus_config.copy()
106		config.update({"DISPLAY": bus.DISPLAY, "pid.Xvfb": bus.display.pid})
107		print("*** Configuration: %s" %config, file=sys.stderr)
108	try:
109		os.environ["ZEITGEIST_DEFAULT_EXTENSIONS"] = \
110			"_zeitgeist.engine.extensions.blacklist.Blacklist," \
111			"_zeitgeist.engine.extensions.datasource_registry.DataSourceRegistry"
112		suite = compile_suite(args or None)
113		# Run all of the tests
114		unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)
115	finally:
116		bus.quit(ignore_errors=True)
117
118# vim:noexpandtab:ts=4:sw=4
119