Wed, 31 Jan 2018 14:36:36 +0200
add LICENSE
16 | 1 | from warnings import warn |
2 | ||
3 | def report_element(bad_object, type, error_name, args): | |
12 | 4 | return { |
16 | 5 | 'type': type, |
6 | 'object': bad_object, | |
7 | 'name': error_name, | |
8 | 'args': args, | |
13 | 9 | } |
10 | ||
17 | 11 | def warning(bad_object, error_name, **args): |
16 | 12 | return report_element(bad_object, 'warning', error_name, args) |
13 | ||
17 | 14 | def error(bad_object, error_name, **args): |
16 | 15 | return report_element(bad_object, 'error', error_name, args) |
16 | ||
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
17 | def notice(bad_object, error_name, **args): |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
18 | return report_element(bad_object, 'notice', error_name, args) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
19 | |
16 | 20 | def test_discovery(): |
21 | ''' | |
22 | Finds all test modules and yields their names. | |
23 | ''' | |
24 | from pkgutil import walk_packages | |
25 | import tests | |
26 | yield from sorted( | |
27 | 'tests.' + result.name | |
28 | for result in walk_packages(tests.__path__) | |
29 | ) | |
30 | ||
31 | def do_manifest_integrity_checks(test_suite, module): | |
32 | ''' | |
33 | Runs integrity checks on a given module's manifest. | |
34 | ''' | |
35 | def check_for_extra_keys(): | |
36 | extra_keys = module.manifest.keys() - test_suite.keys() | |
37 | if extra_keys: | |
38 | warn(str.format( | |
39 | '{}: extra keys in manifest: {}', | |
40 | module.__name__, | |
41 | ', '.join(map(str, extra_keys)) | |
42 | )) | |
43 | def check_for_manifest_duplicates(): | |
44 | for key in test_suite.keys(): | |
45 | duplicates = module.manifest[key].keys() & test_suite[key].keys() | |
46 | if duplicates: | |
47 | warn(str.format( | |
48 | '{}: redefined {} in manifests: {}', | |
49 | module.__name__, | |
50 | key, | |
51 | duplicates, | |
52 | )) | |
53 | check_for_extra_keys() | |
54 | check_for_manifest_duplicates() | |
55 | ||
56 | def load_tests(): | |
57 | ''' | |
58 | Imports test modules and combines their manifests into a test suite. | |
59 | ''' | |
60 | test_suite = {'tests': {}, 'messages': {}} | |
61 | for module_name in test_discovery(): | |
62 | from importlib import import_module | |
63 | module = import_module(module_name) | |
64 | if hasattr(module, 'manifest'): | |
65 | do_manifest_integrity_checks(test_suite, module) | |
66 | # Merge the data from the manifest | |
67 | for key in module.manifest.keys() & test_suite.keys(): | |
68 | test_suite[key].update(module.manifest[key]) | |
69 | else: | |
70 | warn(str.format('Module {} does not have a manifest', module_name)) | |
71 | return test_suite | |
72 | ||
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
73 | def problem_key(problem): |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
74 | problem_hierarchy = ['error', 'warning', 'notice'] |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
75 | return (problem_hierarchy.index(problem['type']), problem['line-number']) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
76 | |
17 | 77 | def check_model(model, test_suite = None): |
78 | if not test_suite: | |
79 | test_suite = load_tests() | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
80 | problems = [] |
17 | 81 | line_numbers = { |
82 | element: (i, i + 1 + model.body_offset) | |
83 | for i, element in enumerate(model.body) | |
84 | } | |
85 | for test_name, test_function in test_suite['tests'].items(): | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
86 | for problem in test_function(model): |
17 | 87 | problem['body-index'], problem['line-number'] \ |
88 | = line_numbers[problem['object']] | |
89 | del problem['object'] | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
90 | problems.append(problem) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
91 | return { |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
92 | 'passed': not any( |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
93 | problem['type'] == 'error' |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
94 | for problem in problems |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
95 | ), |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
96 | 'problems': sorted(problems, key = problem_key), |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
97 | } |
17 | 98 | |
29 | 99 | def problem_text(problem, test_suite): |
100 | message = test_suite['messages'][problem['name']] | |
101 | if callable(message): | |
102 | message = message(**problem['args']) | |
103 | return message | |
104 | ||
105 | def format_report_html(report, model, test_suite): | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
106 | messages = [] |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
107 | for problem in report['problems']: |
29 | 108 | ldraw_code = model.body[problem['body-index']].textual_representation() |
109 | message = str.format( | |
110 | '<li class="{problem_type}">{model_name}:{line_number}:' | |
111 | '{problem_type}: {message}<br />{ldraw_code}</li>', | |
112 | model_name = model.name, | |
113 | line_number = problem['line-number'], | |
114 | problem_type = problem['type'], | |
115 | message = problem_text(problem, test_suite), | |
116 | ldraw_code = ldraw_code, | |
117 | ) | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
118 | messages.append(message) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
119 | return '\n'.join(messages) |
29 | 120 | |
17 | 121 | def format_report(report, model, test_suite): |
18 | 122 | import colorama |
123 | colorama.init() | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
124 | messages = [] |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
125 | for problem in report['problems']: |
18 | 126 | if problem['type'] == 'error': |
127 | text_colour = colorama.Fore.LIGHTRED_EX | |
128 | elif problem['type'] == 'warning': | |
129 | text_colour = colorama.Fore.LIGHTYELLOW_EX | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
130 | elif problem['type'] == 'notice': |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
131 | text_colour = colorama.Fore.LIGHTBLUE_EX |
18 | 132 | else: |
133 | text_colour = '' | |
19 | 134 | ldraw_code = model.body[problem['body-index']].textual_representation() |
17 | 135 | message = str.format( |
18 | 136 | '{text_colour}{model_name}:{line_number}: {problem_type}: {message}' |
19 | 137 | '{colour_reset}\n\t{ldraw_code}', |
18 | 138 | text_colour = text_colour, |
139 | model_name = model.name, | |
140 | line_number = problem['line-number'], | |
141 | problem_type = problem['type'], | |
29 | 142 | message = problem_text(problem, test_suite), |
18 | 143 | colour_reset = colorama.Fore.RESET, |
19 | 144 | ldraw_code = ldraw_code, |
17 | 145 | ) |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
146 | messages.append(message) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
147 | return '\n'.join(messages) |
17 | 148 | |
16 | 149 | if __name__ == '__main__': |
150 | from pprint import pprint | |
151 | pprint(load_tests()) |