tests all bundled modules are importable just add "PEERDIR(library/python/import_test)" to your CMakeLists.txt and "from import_test import test_imports" to your python test source file.
(no_check=(), extra=(), skip_func=None, py_main=None)
| 24 | |
| 25 | |
| 26 | def check_imports(no_check=(), extra=(), skip_func=None, py_main=None): |
| 27 | """ |
| 28 | tests all bundled modules are importable |
| 29 | just add |
| 30 | "PEERDIR(library/python/import_test)" to your CMakeLists.txt and |
| 31 | "from import_test import test_imports" to your python test source file. |
| 32 | """ |
| 33 | |
| 34 | if not isinstance(b'', str): |
| 35 | def str_(s): |
| 36 | return s.decode('UTF-8') |
| 37 | else: |
| 38 | def str_(s): |
| 39 | return s |
| 40 | |
| 41 | exceptions = list(no_check) |
| 42 | for key, _ in __res.iter_keys(b'py/no_check_imports/'): |
| 43 | exceptions += str_(__res.find(key)).split() |
| 44 | if exceptions: |
| 45 | exceptions.sort() |
| 46 | print('NO_CHECK_IMPORTS', ' '.join(exceptions)) |
| 47 | |
| 48 | # all test modules get imported when tests are run |
| 49 | exceptions.append('__tests__.*') |
| 50 | |
| 51 | patterns = [re.escape(s).replace(r'\*', r'.*') for s in exceptions] |
| 52 | rx = re.compile('^({})$'.format('|'.join(patterns))) |
| 53 | |
| 54 | failed = [] |
| 55 | import_times = {} |
| 56 | |
| 57 | def norm(s): |
| 58 | return s[:-9] if s.endswith('.__init__') else s |
| 59 | |
| 60 | modules = sys.extra_modules | set(extra) |
| 61 | modules = sorted(modules, key=norm) |
| 62 | if py_main: |
| 63 | modules = [py_main] + modules |
| 64 | |
| 65 | for module in modules: |
| 66 | if module not in extra and (rx.search(module) or skip_func and skip_func(module)): |
| 67 | print('SKIP', module) |
| 68 | continue |
| 69 | |
| 70 | name = module.rsplit('.', 1)[-1] |
| 71 | if name == '__main__' and 'if __name__ ==' not in importer.get_source(module): |
| 72 | print('SKIP', module, '''without "if __name__ == '__main__'" check''') |
| 73 | continue |
| 74 | |
| 75 | def print_backtrace_marked(e): |
| 76 | tb_exc = traceback.format_exception(*e) |
| 77 | for item in tb_exc: |
| 78 | for line in item.splitlines(): |
| 79 | print('FAIL:', line, file=sys.stderr) |
| 80 | |
| 81 | try: |
| 82 | print('TRY', module) |
| 83 | # XXX waiting for py3 to use print(..., flush=True) |
no test coverage detected