testsuite.py

Tue, 23 Jan 2018 14:09:52 +0200

author
Santeri Piippo
date
Tue, 23 Jan 2018 14:09:52 +0200
changeset 29
db6ca177c6c4
parent 19
9169bad392c4
child 32
75f44d3063da
permissions
-rw-r--r--

added a simple web frontend

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

mercurial