diff --git a/src/calibre/db/tests/main.py b/src/calibre/db/tests/main.py index 85f358a383..e3afe467b1 100644 --- a/src/calibre/db/tests/main.py +++ b/src/calibre/db/tests/main.py @@ -6,13 +6,11 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import os -from calibre.utils.run_tests import find_tests_in_dir, run_tests +from calibre.utils.run_tests import find_tests_in_package, run_tests def find_tests(): - base = os.path.dirname(os.path.abspath(__file__)) - return find_tests_in_dir(base) + return find_tests_in_package('calibre.db.tests') if __name__ == '__main__': diff --git a/src/calibre/ebooks/oeb/polish/tests/main.py b/src/calibre/ebooks/oeb/polish/tests/main.py index 33aa02bc8d..f8a78fb2b8 100644 --- a/src/calibre/ebooks/oeb/polish/tests/main.py +++ b/src/calibre/ebooks/oeb/polish/tests/main.py @@ -5,13 +5,11 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' -import os -from calibre.utils.run_tests import find_tests_in_dir, run_tests +from calibre.utils.run_tests import find_tests_in_package, run_tests def find_tests(): - base = os.path.dirname(os.path.abspath(__file__)) - return find_tests_in_dir(base) + return find_tests_in_package('calibre.ebooks.oeb.polish.tests') if __name__ == '__main__': diff --git a/src/calibre/srv/tests/main.py b/src/calibre/srv/tests/main.py index 4da0b0a6e3..63d3b14e5f 100644 --- a/src/calibre/srv/tests/main.py +++ b/src/calibre/srv/tests/main.py @@ -5,13 +5,11 @@ __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' -import os -from calibre.utils.run_tests import find_tests_in_dir, run_tests +from calibre.utils.run_tests import find_tests_in_package, run_tests def find_tests(): - base = os.path.dirname(os.path.abspath(__file__)) - return find_tests_in_dir(base) + return find_tests_in_package('calibre.srv.tests') if __name__ == '__main__': diff --git a/src/calibre/utils/run_tests.py b/src/calibre/utils/run_tests.py index 676667152e..c6c36c4253 100644 --- a/src/calibre/utils/run_tests.py +++ b/src/calibre/utils/run_tests.py @@ -3,7 +3,7 @@ # License: GPLv3 Copyright: 2016, Kovid Goyal -import unittest, functools, os, importlib, zipfile +import unittest, functools, importlib from calibre.utils.monotonic import monotonic @@ -53,20 +53,9 @@ class TestResult(unittest.TextTestResult): self.stream.writeln('\nSlowest tests: %s' % ' '.join(slowest)) -def find_tests_in_dir(path, excludes=('main.py',)): - if not os.path.exists(path) and '.zip' in path: - idx = path.rfind('.zip') - zf = path[:idx+4] - prefix = os.path.relpath(path, zf).replace(os.sep, '/') - package = prefix.replace('/', '.') - with zipfile.ZipFile(zf) as f: - namelist = f.namelist() - items = [i for i in namelist if i.startswith(prefix) and i.count('/') == prefix.count('/') + 1] - else: - d = os.path.dirname - base = d(d(d(os.path.abspath(__file__)))) - package = os.path.relpath(path, base).replace(os.sep, '/').replace('/', '.') - items = os.listdir(path) +def find_tests_in_package(package, excludes=('main.py',)): + loader = importlib.import_module(package).__spec__.loader + items = list(loader.contents()) suits = [] for x in items: if x.endswith('.py') and x not in excludes: diff --git a/src/tinycss/tests/main.py b/src/tinycss/tests/main.py index 3bef849f33..364172080b 100644 --- a/src/tinycss/tests/main.py +++ b/src/tinycss/tests/main.py @@ -5,12 +5,13 @@ __license__ = 'GPL v3' __copyright__ = '2014, Kovid Goyal ' -import unittest, os, argparse +import unittest, argparse + def find_tests(): - from calibre.utils.run_tests import find_tests_in_dir - base = os.path.dirname(os.path.abspath(__file__)) - return find_tests_in_dir(base) + from calibre.utils.run_tests import find_tests_in_package + return find_tests_in_package('tinycss.tests') + def run_tests(find_tests=find_tests, for_build=False): if not for_build: @@ -36,7 +37,7 @@ def run_tests(find_tests=find_tests, for_build=False): except StopIteration: pass if ans is None: - print ('No test named %s found' % args.name) + print('No test named %s found' % args.name) raise SystemExit(1) tests = ans else: @@ -50,7 +51,6 @@ def run_tests(find_tests=find_tests, for_build=False): if for_build and result.errors or result.failures: raise SystemExit(1) + if __name__ == '__main__': run_tests() - -