| 1 def warning(object, *message): |
1 from warnings import warn |
| |
2 |
| |
3 def report_element(bad_object, type, error_name, args): |
| 2 return { |
4 return { |
| 3 'type': 'warning', |
5 'type': type, |
| 4 'object': object, |
6 'object': bad_object, |
| 5 'message': str.format(*message), |
7 'name': error_name, |
| |
8 'args': args, |
| 6 } |
9 } |
| 7 |
10 |
| 8 def error(object, *message): |
11 def warning(bad_object, error_name, *args): |
| 9 return { |
12 return report_element(bad_object, 'warning', error_name, args) |
| 10 'type': 'error', |
13 |
| 11 'object': object, |
14 def error(bad_object, error_name, *args): |
| 12 'message': str.format(*message), |
15 return report_element(bad_object, 'error', error_name, args) |
| 13 } |
16 |
| |
17 def test_discovery(): |
| |
18 ''' |
| |
19 Finds all test modules and yields their names. |
| |
20 ''' |
| |
21 from pkgutil import walk_packages |
| |
22 import tests |
| |
23 yield from sorted( |
| |
24 'tests.' + result.name |
| |
25 for result in walk_packages(tests.__path__) |
| |
26 ) |
| |
27 |
| |
28 def do_manifest_integrity_checks(test_suite, module): |
| |
29 ''' |
| |
30 Runs integrity checks on a given module's manifest. |
| |
31 ''' |
| |
32 def check_for_extra_keys(): |
| |
33 extra_keys = module.manifest.keys() - test_suite.keys() |
| |
34 if extra_keys: |
| |
35 warn(str.format( |
| |
36 '{}: extra keys in manifest: {}', |
| |
37 module.__name__, |
| |
38 ', '.join(map(str, extra_keys)) |
| |
39 )) |
| |
40 def check_for_manifest_duplicates(): |
| |
41 for key in test_suite.keys(): |
| |
42 duplicates = module.manifest[key].keys() & test_suite[key].keys() |
| |
43 if duplicates: |
| |
44 warn(str.format( |
| |
45 '{}: redefined {} in manifests: {}', |
| |
46 module.__name__, |
| |
47 key, |
| |
48 duplicates, |
| |
49 )) |
| |
50 check_for_extra_keys() |
| |
51 check_for_manifest_duplicates() |
| |
52 |
| |
53 def load_tests(): |
| |
54 ''' |
| |
55 Imports test modules and combines their manifests into a test suite. |
| |
56 ''' |
| |
57 test_suite = {'tests': {}, 'messages': {}} |
| |
58 for module_name in test_discovery(): |
| |
59 from importlib import import_module |
| |
60 module = import_module(module_name) |
| |
61 if hasattr(module, 'manifest'): |
| |
62 do_manifest_integrity_checks(test_suite, module) |
| |
63 # Merge the data from the manifest |
| |
64 for key in module.manifest.keys() & test_suite.keys(): |
| |
65 test_suite[key].update(module.manifest[key]) |
| |
66 else: |
| |
67 warn(str.format('Module {} does not have a manifest', module_name)) |
| |
68 return test_suite |
| |
69 |
| |
70 if __name__ == '__main__': |
| |
71 from pprint import pprint |
| |
72 pprint(load_tests()) |