| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
# Copyright 2016-2021 The Meson development team |
| 4 |
|
| 5 |
# Work around some pathlib bugs... |
| 6 |
from mesonbuild import _pathlib |
| 7 |
import sys |
| 8 |
sys.modules['pathlib'] = _pathlib |
| 9 |
|
| 10 |
import time |
| 11 |
import subprocess |
| 12 |
import os |
| 13 |
import unittest |
| 14 |
import importlib |
| 15 |
import typing as T |
| 16 |
|
| 17 |
import mesonbuild.coredata |
| 18 |
from mesonbuild.mesonlib import python_command, setup_vsenv |
| 19 |
|
| 20 |
|
| 21 |
def convert_args(argv): |
| 22 |
# If we got passed a list of tests, pass it on |
| 23 |
pytest_args = ['-v'] if '-v' in argv else [] |
| 24 |
test_list = [] |
| 25 |
for arg in argv: |
| 26 |
if arg.startswith('-'): |
| 27 |
if arg in ('-f', '--failfast'): |
| 28 |
arg = '--exitfirst' |
| 29 |
pytest_args.append(arg) |
| 30 |
continue |
| 31 |
# ClassName.test_name => 'ClassName and test_name' |
| 32 |
if '.' in arg: |
| 33 |
arg = ' and '.join(arg.split('.')) |
| 34 |
test_list.append(arg) |
| 35 |
if test_list: |
| 36 |
pytest_args += ['-k', ' or '.join(test_list)] |
| 37 |
return pytest_args |
| 38 |
|
| 39 |
def running_single_tests(argv, cases): |
| 40 |
''' |
| 41 |
Check whether we only got arguments for running individual tests, not |
| 42 |
entire testcases, and not all testcases (no test args). |
| 43 |
''' |
| 44 |
got_test_arg = False |
| 45 |
for arg in argv: |
| 46 |
if arg.startswith('-'): |
| 47 |
continue |
| 48 |
for case in cases: |
| 49 |
if not arg.startswith(case): |
| 50 |
continue |
| 51 |
if '.' not in arg: |
| 52 |
# Got a testcase, done |
| 53 |
return False |
| 54 |
got_test_arg = True |
| 55 |
return got_test_arg |
| 56 |
|
| 57 |
def setup_backend(): |
| 58 |
filtered = [] |
| 59 |
be = 'ninja' |
| 60 |
for a in sys.argv: |
| 61 |
if a.startswith('--backend'): |
| 62 |
be = a.split('=', 1)[1] |
| 63 |
else: |
| 64 |
filtered.append(a) |
| 65 |
# Since we invoke the tests via unittest or xtest test runner |
| 66 |
# we need to pass the backend to use to the spawned process via |
| 67 |
# this side channel. Yes it sucks, but at least is is fully |
| 68 |
# internal to this file. |
| 69 |
os.environ['MESON_UNIT_TEST_BACKEND'] = be |
| 70 |
sys.argv = filtered |
| 71 |
|
| 72 |
def import_test_cases(suite: unittest.TestSuite) -> T.Set[str]: |
| 73 |
''' |
| 74 |
Imports all test classes into the current module and returns their names |
| 75 |
''' |
| 76 |
classes = set() |
| 77 |
for test in suite: |
| 78 |
if isinstance(test, unittest.TestSuite): |
| 79 |
classes.update(import_test_cases(test)) |
| 80 |
elif isinstance(test, unittest.TestCase): |
| 81 |
mod = importlib.import_module(test.__module__) |
| 82 |
class_name = test.__class__.__name__ |
| 83 |
test_class = getattr(mod, class_name) |
| 84 |
classes.add(class_name) |
| 85 |
setattr(sys.modules[__name__], class_name, test_class) |
| 86 |
return classes |
| 87 |
|
| 88 |
def discover_test_cases() -> T.Set[str]: |
| 89 |
current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 90 |
loader = unittest.TestLoader() |
| 91 |
suite = loader.discover(os.path.join(current_dir, 'unittests'), '*tests.py', current_dir) |
| 92 |
if loader.errors: |
| 93 |
raise SystemExit(loader.errors) |
| 94 |
return import_test_cases(suite) |
| 95 |
|
| 96 |
def main(): |
| 97 |
cases = sorted(discover_test_cases()) |
| 98 |
setup_backend() |
| 99 |
|
| 100 |
try: |
| 101 |
import pytest # noqa: F401 |
| 102 |
pytest_args = [] |
| 103 |
try: |
| 104 |
# Need pytest-xdist for `-n` arg |
| 105 |
import xdist # noqa: F401 |
| 106 |
# Don't use pytest-xdist when running single unit tests since it wastes |
| 107 |
# time spawning a lot of processes to distribute tests to in that case. |
| 108 |
if not running_single_tests(sys.argv, cases): |
| 109 |
pytest_args += ['-n', 'auto'] |
| 110 |
except ImportError: |
| 111 |
print('pytest-xdist not found, tests will not be distributed across CPU cores') |
| 112 |
# Let there be colors! |
| 113 |
if 'CI' in os.environ: |
| 114 |
pytest_args += ['--color=yes'] |
| 115 |
pytest_args += ['unittests'] |
| 116 |
pytest_args += convert_args(sys.argv[1:]) |
| 117 |
# Always disable pytest-cov because we use a custom setup |
| 118 |
try: |
| 119 |
import pytest_cov # noqa: F401 |
| 120 |
print('Disabling pytest-cov') |
| 121 |
pytest_args += ['-p' 'no:cov'] |
| 122 |
except ImportError: |
| 123 |
pass |
| 124 |
return subprocess.run(python_command + ['-m', 'pytest'] + pytest_args).returncode |
| 125 |
except ImportError: |
| 126 |
print('pytest not found, using unittest instead') |
| 127 |
# Fallback to plain unittest. |
| 128 |
return unittest.main(defaultTest=cases, buffer=True) |
| 129 |
|
| 130 |
if __name__ == '__main__': |
| 131 |
setup_vsenv() |
| 132 |
print('Meson build system', mesonbuild.coredata.version, 'Unit Tests') |
| 133 |
start = time.monotonic() |
| 134 |
try: |
| 135 |
raise SystemExit(main()) |
| 136 |
finally: |
| 137 |
print('Total time: {:.3f} seconds'.format(time.monotonic() - start)) |