From 538f0a718640e09ba2fbedf3f0631ecd182952f7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 21 Jun 2016 23:12:06 +0530 Subject: [PATCH] A single entry point to run all tests --- setup/commands.py | 5 +- setup/test.py | 67 +++++++++++++++++++++++ src/calibre/ebooks/css_transform_rules.py | 4 +- src/calibre/ebooks/docx/fields.py | 4 +- src/calibre/ebooks/docx/writer/utils.py | 4 +- src/calibre/ebooks/epub/cfi/tests.py | 6 +- src/calibre/ebooks/metadata/opf3_test.py | 6 +- src/calibre/ebooks/oeb/normalize_css.py | 4 +- src/calibre/utils/icu_test.py | 6 +- src/calibre/utils/matcher.py | 5 +- src/calibre/utils/smartypants.py | 4 +- src/tinycss/tests/main.py | 10 +++- 12 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 setup/test.py diff --git a/setup/commands.py b/setup/commands.py index 53e5be44ed..737781752c 100644 --- a/setup/commands.py +++ b/setup/commands.py @@ -12,7 +12,7 @@ __all__ = [ 'gui', 'develop', 'install', 'kakasi', 'coffee', 'rapydscript', 'cacerts', 'resources', - 'check', + 'check', 'test', 'sdist', 'bootstrap', 'manual', 'tag_release', 'pypi_register', 'pypi_upload', 'upload_to_server', @@ -50,6 +50,9 @@ gui = GUI() from setup.check import Check check = Check() +from setup.test import Test +test = Test() + from setup.resources import Resources, Kakasi, Coffee, CACerts, RapydScript resources = Resources() kakasi = Kakasi() diff --git a/setup/test.py b/setup/test.py new file mode 100644 index 0000000000..47bf57c297 --- /dev/null +++ b/setup/test.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2016, Kovid Goyal + +from __future__ import (unicode_literals, division, absolute_import, + print_function) +import unittest + +from setup import Command + +TEST_MODULES = frozenset('srv db polish selectors opf css docx cfi matcher icu smartypants'.split()) + +def find_tests(which_tests=None): + ans = [] + a = ans.append + if not which_tests or 'srv' in which_tests: + from calibre.srv.tests.main import find_tests + a(find_tests()) + if not which_tests or 'db' in which_tests: + from calibre.db.tests.main import find_tests + a(find_tests()) + if not which_tests or 'polish' in which_tests: + from calibre.ebooks.oeb.polish.tests.main import find_tests + a(find_tests()) + if not which_tests or 'selectors' in which_tests: + from css_selectors.tests import find_tests + a(find_tests()) + if not which_tests or 'opf' in which_tests: + from calibre.ebooks.metadata.opf2 import suite + a(suite()) + from calibre.ebooks.metadata.opf3_test import suite + a(suite()) + if not which_tests or 'css' in which_tests: + from tinycss.tests.main import find_tests + a(find_tests()) + from calibre.ebooks.oeb.normalize_css import test_normalization + a(test_normalization(return_tests=True)) + from calibre.ebooks.css_transform_rules import test + a(test(return_tests=True)) + if not which_tests or 'docx' in which_tests: + from calibre.ebooks.docx.fields import test_parse_fields + a(test_parse_fields(return_tests=True)) + from calibre.ebooks.docx.writer.utils import test_convert_color + a(test_convert_color(return_tests=True)) + if not which_tests or 'cfi' in which_tests: + from calibre.ebooks.epub.cfi.tests import find_tests + a(find_tests()) + if not which_tests or 'matcher' in which_tests: + from calibre.utils.matcher import test + a(test(return_tests=True)) + if not which_tests or 'icu' in which_tests: + from calibre.utils.icu_test import find_tests + a(find_tests()) + if not which_tests or 'smartypants' in which_tests: + from calibre.utils.smartypants import run_tests + a(run_tests(return_tests=True)) + + tests = unittest.TestSuite(ans) + return tests + +class Test(Command): + + def run(self, opts): + from calibre.gui2 import ensure_app, load_builtin_fonts + ensure_app(), load_builtin_fonts() + r = unittest.TextTestRunner + r(verbosity=2).run(find_tests()) diff --git a/src/calibre/ebooks/css_transform_rules.py b/src/calibre/ebooks/css_transform_rules.py index 103f458099..34f9c4f8b4 100644 --- a/src/calibre/ebooks/css_transform_rules.py +++ b/src/calibre/ebooks/css_transform_rules.py @@ -349,7 +349,7 @@ def import_rules(raw_data): if current_rule: yield sanitize(current_rule) -def test(): # {{{ +def test(return_tests=False): # {{{ import unittest def apply_rule(style, **rule): @@ -430,6 +430,8 @@ def test(): # {{{ self.ae(rule, next(import_rules(export_rules([rule])))) tests = unittest.defaultTestLoader.loadTestsFromTestCase(TestTransforms) + if return_tests: + return tests unittest.TextTestRunner(verbosity=4).run(tests) if __name__ == '__main__': diff --git a/src/calibre/ebooks/docx/fields.py b/src/calibre/ebooks/docx/fields.py index 8bc88815cb..9c774a0791 100644 --- a/src/calibre/ebooks/docx/fields.py +++ b/src/calibre/ebooks/docx/fields.py @@ -224,7 +224,7 @@ class Fields(object): for idx, blocks in self.index_fields: polish_index_markup(idx, [rmap[b] for b in blocks]) -def test_parse_fields(): +def test_parse_fields(return_tests=False): import unittest class TestParseFields(unittest.TestCase): @@ -249,6 +249,8 @@ def test_parse_fields(): ae(r'\b \c 1', {'bookmark':None, 'columns-per-page': '1'}) suite = unittest.TestLoader().loadTestsFromTestCase(TestParseFields) + if return_tests: + return suite unittest.TextTestRunner(verbosity=4).run(suite) if __name__ == '__main__': diff --git a/src/calibre/ebooks/docx/writer/utils.py b/src/calibre/ebooks/docx/writer/utils.py index ed8bb1a8f4..8b6b50cfb6 100644 --- a/src/calibre/ebooks/docx/writer/utils.py +++ b/src/calibre/ebooks/docx/writer/utils.py @@ -27,7 +27,7 @@ def convert_color(value): return return '%02X%02X%02X' % (int(val.red * 255), int(val.green * 255), int(val.blue * 255)) -def test_convert_color(): +def test_convert_color(return_tests=False): import unittest class TestColors(unittest.TestCase): @@ -48,5 +48,7 @@ def test_convert_color(): ae('FFFFFF', cc('rgb(255, 255, 255)')) ae('FF0000', cc('rgba(255, 0, 0, 23)')) tests = unittest.defaultTestLoader.loadTestsFromTestCase(TestColors) + if return_tests: + return tests unittest.TextTestRunner(verbosity=4).run(tests) # }}} diff --git a/src/calibre/ebooks/epub/cfi/tests.py b/src/calibre/ebooks/epub/cfi/tests.py index e077640021..2e3bce5caf 100644 --- a/src/calibre/ebooks/epub/cfi/tests.py +++ b/src/calibre/ebooks/epub/cfi/tests.py @@ -90,6 +90,8 @@ class Tests(unittest.TestCase): ]: self.assertEqual(p.parse_path(raw), (path, leftover)) +def find_tests(): + return unittest.TestLoader().loadTestsFromTestCase(Tests) + if __name__ == '__main__': - suite = unittest.TestLoader().loadTestsFromTestCase(Tests) - unittest.TextTestRunner(verbosity=2).run(suite) + unittest.TextTestRunner(verbosity=2).run(find_tests()) diff --git a/src/calibre/ebooks/metadata/opf3_test.py b/src/calibre/ebooks/metadata/opf3_test.py index 46426ae848..d96ce73b03 100644 --- a/src/calibre/ebooks/metadata/opf3_test.py +++ b/src/calibre/ebooks/metadata/opf3_test.py @@ -174,11 +174,13 @@ class TestOPF3(unittest.TestCase): # Run tests {{{ +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(TestOPF3) + class TestRunner(unittest.main): def createTests(self): - tl = unittest.TestLoader() - self.test = tl.loadTestsFromTestCase(TestOPF3) + self.test = suite() def run(verbosity=4): TestRunner(verbosity=verbosity, exit=False) diff --git a/src/calibre/ebooks/oeb/normalize_css.py b/src/calibre/ebooks/oeb/normalize_css.py index ce919ac12b..b00e54ca23 100644 --- a/src/calibre/ebooks/oeb/normalize_css.py +++ b/src/calibre/ebooks/oeb/normalize_css.py @@ -284,7 +284,7 @@ def condense_sheet(sheet): if rule.type == rule.STYLE_RULE: condense_rule(rule.style) -def test_normalization(): # {{{ +def test_normalization(return_tests=False): # {{{ import unittest from cssutils import parseStyle from itertools import product @@ -442,6 +442,8 @@ def test_normalization(): # {{{ self.assertEqual(style.getProperty('border-left').value, vals.replace('red', 'green')) tests = unittest.defaultTestLoader.loadTestsFromTestCase(TestNormalization) + if return_tests: + return tests unittest.TextTestRunner(verbosity=4).run(tests) # }}} diff --git a/src/calibre/utils/icu_test.py b/src/calibre/utils/icu_test.py index 5fe0b9536e..51fe8275df 100644 --- a/src/calibre/utils/icu_test.py +++ b/src/calibre/utils/icu_test.py @@ -197,11 +197,13 @@ class TestICU(unittest.TestCase): fpos = index_of(needle, haystack) self.ae(pos, fpos, 'Failed to find index of %r in %r (%d != %d)' % (needle, haystack, pos, fpos)) +def find_tests(): + return unittest.defaultTestLoader.loadTestsFromTestCase(TestICU) + class TestRunner(unittest.main): def createTests(self): - tl = unittest.TestLoader() - self.test = tl.loadTestsFromTestCase(TestICU) + self.test = find_tests() def run(verbosity=4): TestRunner(verbosity=verbosity, exit=False) diff --git a/src/calibre/utils/matcher.py b/src/calibre/utils/matcher.py index 04e9d08ee7..0d855de089 100644 --- a/src/calibre/utils/matcher.py +++ b/src/calibre/utils/matcher.py @@ -220,7 +220,7 @@ class CScorer(object): for score, pos in izip(scores, positions): yield score, pos -def test(): +def test(return_tests=False): import unittest class Test(unittest.TestCase): @@ -251,6 +251,9 @@ def test(): positions = next(m(raw).itervalues()) self.assertEqual(positions, (0, 1, (2 if sys.maxunicode >= 0x10ffff else 3))) + if return_tests: + return unittest.TestLoader().loadTestsFromTestCase(Test) + class TestRunner(unittest.main): def createTests(self): diff --git a/src/calibre/utils/smartypants.py b/src/calibre/utils/smartypants.py index a7e4038bdd..e9de316290 100644 --- a/src/calibre/utils/smartypants.py +++ b/src/calibre/utils/smartypants.py @@ -887,7 +887,7 @@ def _tokenize(str): return tokens -def run_tests(): +def run_tests(return_tests=False): import unittest sp = smartyPants @@ -929,6 +929,8 @@ def run_tests(): self.assertEqual(sp('''"Isn't this fun?"'''), '''“Isn’t this fun?”''') tests = unittest.defaultTestLoader.loadTestsFromTestCase(TestSmartypantsAllAttributes) + if return_tests: + return tests unittest.TextTestRunner(verbosity=4).run(tests) diff --git a/src/tinycss/tests/main.py b/src/tinycss/tests/main.py index cc2f45a9d4..cd2fa3b4dc 100644 --- a/src/tinycss/tests/main.py +++ b/src/tinycss/tests/main.py @@ -6,10 +6,16 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2014, Kovid Goyal ' -import unittest, os, argparse +import unittest, os, argparse, importlib def find_tests(): - return unittest.defaultTestLoader.discover(os.path.dirname(os.path.abspath(__file__)), pattern='*.py') + base = os.path.dirname(os.path.abspath(__file__)) + suits = [] + for x in os.listdir(base): + if x.endswith('.py') and x != 'main.py': + m = importlib.import_module('tinycss.tests.' + x.partition('.')[0]) + suits.append(unittest.defaultTestLoader.loadTestsFromModule(m)) + return unittest.TestSuite(suits) def run_tests(find_tests=find_tests, for_build=False): if not for_build: