| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
|
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
|
| 9 |
from setup import Command, is_ci, ismacos, iswindows |
| 10 |
|
| 11 |
TEST_MODULES = frozenset('ai fork scraper srv db polish opf css docx cfi matcher icu smartypants build misc dbcli ebooks podofo'.split()) |
| 12 |
|
| 13 |
|
| 14 |
class BaseTest(Command): |
| 15 |
def run(self, opts): |
| 16 |
if opts.under_sanitize and 'CALIBRE_EXECED_UNDER_SANITIZE' not in os.environ: |
| 17 |
if 'libasan' not in os.environ.get('LD_PRELOAD', ''): |
| 18 |
os.environ['LD_PRELOAD'] = os.path.abspath(subprocess.check_output('gcc -print-file-name=libasan.so'.split()).decode('utf-8').strip()) |
| 19 |
os.environ['CALIBRE_EXECED_UNDER_SANITIZE'] = '1' |
| 20 |
os.environ['ASAN_OPTIONS'] = 'detect_leaks=0' |
| 21 |
os.environ['PYCRYPTODOME_DISABLE_DEEPBIND'] = '1' # https://github.com/Legrandin/pycryptodome/issues/558 |
| 22 |
self.info(f'Re-execing with LD_PRELOAD={os.environ["LD_PRELOAD"]}') |
| 23 |
sys.stdout.flush() |
| 24 |
os.execl('setup.py', *sys.argv) |
| 25 |
|
| 26 |
def add_options(self, parser): |
| 27 |
parser.add_option('--under-sanitize', default=False, action='store_true', help='Run the test suite with the sanitizer preloaded') |
| 28 |
|
| 29 |
|
| 30 |
class Test(BaseTest): |
| 31 |
description = 'Run the calibre test suite.' |
| 32 |
usage_help = 'To run specific tests, specify their names as command line arguments or use --test-name' |
| 33 |
|
| 34 |
def add_options(self, parser): |
| 35 |
super().add_options(parser) |
| 36 |
parser.add_option('--test-verbosity', type=int, default=4, help='Test verbosity (0-4)') |
| 37 |
parser.add_option( |
| 38 |
'--test-module', |
| 39 |
'--test-group', |
| 40 |
default=[], |
| 41 |
action='append', |
| 42 |
type='choice', |
| 43 |
choices=sorted(map(str, TEST_MODULES)), |
| 44 |
help='The test module to run (can be specified more than once for multiple modules). Choices: {}'.format(', '.join(sorted(TEST_MODULES))), |
| 45 |
) |
| 46 |
parser.add_option( |
| 47 |
'--test-name', |
| 48 |
'-n', |
| 49 |
default=[], |
| 50 |
action='append', |
| 51 |
help='The name of an individual test to run. Can be specified more than once for multiple tests. The name of the' |
| 52 |
' test is the name of the test function without the leading test_. For example, the function test_something()' |
| 53 |
' can be run by specifying the name "something".', |
| 54 |
) |
| 55 |
parser.add_option( |
| 56 |
'--exclude-test-module', |
| 57 |
default=[], |
| 58 |
action='append', |
| 59 |
type='choice', |
| 60 |
choices=sorted(map(str, TEST_MODULES)), |
| 61 |
help='A test module to be excluded from the test run (can be specified more than once for multiple modules). Choices: {}'.format( |
| 62 |
', '.join(sorted(TEST_MODULES)) |
| 63 |
), |
| 64 |
) |
| 65 |
parser.add_option( |
| 66 |
'--exclude-test-name', |
| 67 |
default=[], |
| 68 |
action='append', |
| 69 |
help='The name of an individual test to be excluded from the test run. Can be specified more than once for multiple tests.', |
| 70 |
) |
| 71 |
parser.add_option( |
| 72 |
'--worker', |
| 73 |
default=None, |
| 74 |
help='Python code to eval inside a worker process (used internally by the parallel test runner).', |
| 75 |
) |
| 76 |
|
| 77 |
def run(self, opts): |
| 78 |
super().run(opts) |
| 79 |
|
| 80 |
if is_ci and (SW := os.environ.get('SW')): |
| 81 |
if ismacos: |
| 82 |
import ctypes |
| 83 |
|
| 84 |
sys.libxml2_dylib = ctypes.CDLL(os.path.join(SW, 'lib', 'libxml2.dylib')) |
| 85 |
sys.libxslt_dylib = ctypes.CDLL(os.path.join(SW, 'lib', 'libxslt.dylib')) |
| 86 |
sys.libexslt_dylib = ctypes.CDLL(os.path.join(SW, 'lib', 'libexslt.dylib')) |
| 87 |
print(sys.libxml2_dylib, sys.libxslt_dylib, sys.libexslt_dylib, file=sys.stderr, flush=True) |
| 88 |
elif iswindows: |
| 89 |
ffmpeg_dll_dir = os.path.join(SW, 'ffmpeg', 'bin') |
| 90 |
os.add_dll_directory(ffmpeg_dll_dir) # type: ignore |
| 91 |
|
| 92 |
from calibre.utils.run_tests import filter_tests_by_name, find_tests, remove_tests_by_name, run_cli |
| 93 |
|
| 94 |
if opts.worker is not None: |
| 95 |
eval(compile(opts.worker, '<worker>', 'exec')) |
| 96 |
raise SystemExit(0) |
| 97 |
|
| 98 |
tests = find_tests(which_tests=frozenset(opts.test_module), exclude_tests=frozenset(opts.exclude_test_module)) |
| 99 |
only = [] |
| 100 |
if opts.test_name: |
| 101 |
only += list(opts.test_name) |
| 102 |
if opts.cli_args: |
| 103 |
only += list(opts.cli_args) |
| 104 |
if only: |
| 105 |
tests = filter_tests_by_name(tests, *only) |
| 106 |
if opts.exclude_test_name: |
| 107 |
tests = remove_tests_by_name(tests, *opts.exclude_test_name) |
| 108 |
worker_cmd = [sys.executable, 'setup.py', 'test', '--worker'] |
| 109 |
run_cli(tests, verbosity=opts.test_verbosity, buffer=not opts.test_name, worker_cmd=worker_cmd) |
| 110 |
if is_ci: |
| 111 |
print('run_cli returned', flush=True) |
| 112 |
raise SystemExit(0) |
| 113 |
|
| 114 |
|
| 115 |
class TestRS(BaseTest): |
| 116 |
description = 'Run tests for RapydScript code' |
| 117 |
|
| 118 |
def add_options(self, parser): |
| 119 |
super().add_options(parser) |
| 120 |
|
| 121 |
def run(self, opts): |
| 122 |
super().run(opts) |
| 123 |
from calibre.utils.rapydscript import run_rapydscript_tests |
| 124 |
|
| 125 |
run_rapydscript_tests() |