-
Notifications
You must be signed in to change notification settings - Fork 59
/
tester.py
executable file
·215 lines (181 loc) · 6.19 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
#
# Copyright (C) 2016 Ben Smith
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
#
from __future__ import print_function
import argparse
import collections
import json
import multiprocessing
import os
import sys
import time
import common
TEST_JSON = os.path.join(common.SCRIPT_DIR, 'test.json')
TEST_RESULT_DIR = os.path.join(common.OUT_DIR, 'test_results')
TEST_RESULTS_MD = os.path.join(common.ROOT_DIR, 'test_results.md')
OK = '[OK] '
FAIL = '[X] '
UNKNOWN = '[?] '
Test = collections.namedtuple('Test', ['suite', 'rom', 'frames', 'hash'])
TestResult = collections.namedtuple('TestResult',
['test', 'passed', 'ok', 'message',
'duration'])
def RunTest(test, options):
start_time = time.time()
ppm = os.path.join(TEST_RESULT_DIR,
os.path.basename(os.path.splitext(test.rom)[0]) + '.ppm')
try:
common.RunTester(test.rom, test.frames, ppm, exe=options.exe)
actual = common.HashFile(ppm)
if test.hash.startswith('!'):
expect_fail = True
expected = test.hash[1:]
else:
expect_fail = False
expected = test.hash
message = ''
ok = actual == expected
if ok:
if expect_fail and options.verbose > 0:
message = FAIL + test.rom
elif options.verbose > 1:
message = OK + test.rom
else:
if expected == '' or expect_fail:
message = UNKNOWN + '%s => %s' % (test.rom, actual)
else:
message = FAIL + '%s => %s' % (test.rom, actual)
passed = ok and not expect_fail
duration = time.time() - start_time
return TestResult(test, passed, ok, message, duration)
except (common.Error, KeyboardInterrupt) as e:
duration = time.time() - start_time
message = FAIL + '%s => %s' % (test.rom, str(e))
return TestResult(test, False, False, message, duration)
last_message_len = 0
def PrintReplace(s, newline=False):
global last_message_len
if sys.stdout.isatty():
sys.stdout.write('\r' + s)
if len(s) < last_message_len:
sys.stdout.write(' ' * (last_message_len - len(s)))
if newline:
sys.stdout.write('\n')
else:
sys.stdout.flush()
last_message_len = len(s)
else:
sys.stdout.write(s)
sys.stdout.write('\n')
def RunAllTests(tests, options):
results = []
pool = multiprocessing.Pool(options.num_processes)
try:
tasks = [pool.apply_async(RunTest, (test, options)) for test in tests]
passed = 0
failed = 0
while tasks:
new_tasks = []
for task in tasks:
if task.ready():
result = task.get(0)
results.append(result)
if result.ok:
passed += 1
else:
failed += 1
if result.message:
PrintReplace(result.message, newline=True)
PrintReplace('[+%d|-%d|%%%d] %s (%.3fs)' % (
passed, failed, 100 * ((passed + failed) / len(tests)),
result.test.rom, result.duration))
else:
new_tasks.append(task)
time.sleep(0.01)
tasks = new_tasks
pool.close()
except KeyboardInterrupt:
pass
finally:
pool.terminate()
pool.join()
return results
def MDLink(text, url):
return '[%s](%s)' % (text, url)
def MDTableRow(*columns):
return '| %s |\n' % ' | '.join(columns)
def MDTestName(rom, prefix):
assert rom.startswith(prefix)
name = rom[len(prefix):]
name = os.path.splitext(name)[0]
name = name.replace('_', '\\_')
return name
def MDTestResult(passed):
return ':ok:' if passed else ':x:'
def SuiteHeader(out_file, name, url):
out_file.write('%s:\n\n' % MDLink(name, url))
out_file.write(MDTableRow('Test', 'Result'))
out_file.write(MDTableRow('---', '---'))
def Suite(out_file, results, suite, prefix):
for result in sorted(results, key=lambda x: x.test.rom):
if result.test.suite != suite:
continue
name = MDTestName(result.test.rom, prefix)
out_file.write(MDTableRow(name, MDTestResult(result.passed)))
out_file.write('\n')
def GenerateTestResults(results):
SUITES = [
{'name': 'Blargg\'s tests',
'url': 'http://gbdev.gg8.se/wiki/articles/Test_ROMs',
'suite': 'blargg',
'prefix': 'test/blargg/'},
{'name': 'Gekkio\'s mooneye-gb tests',
'url': 'https://github.com/Gekkio/mooneye-gb',
'suite': 'mooneye',
'prefix': 'test/mooneye-gb/build/'},
{'name': 'Wilbert Pol\'s mooneye-gb tests',
'url': 'https://github.com/wilbertpol/mooneye-gb',
'suite': 'wilbertpol',
'prefix': 'test/mooneye-gb-wp/build/'},
]
with open(TEST_RESULTS_MD, 'w') as out_file:
out_file.write('# Test status\n\n')
for suite in SUITES:
SuiteHeader(out_file, suite['name'], suite['url'])
Suite(out_file, results, suite['suite'], suite['prefix'])
def main(args):
parser = argparse.ArgumentParser()
parser.add_argument('patterns', metavar='pattern', nargs='*',
help='test patterns.')
parser.add_argument('-j', '--num-processes',
type=int, default=multiprocessing.cpu_count(),
help='num processes.')
parser.add_argument('-e', '--exe', help='path to tester')
parser.add_argument('-v', '--verbose', action='count', default=0,
help='show more info')
parser.add_argument('-g', '--generate', action='store_true',
help='generate test result markdown')
options = parser.parse_args(args)
pattern_re = common.MakePatternRE(options.patterns)
passed = 0
if not os.path.exists(TEST_RESULT_DIR):
os.makedirs(TEST_RESULT_DIR)
tests = [Test(*test) for test in json.load(open(TEST_JSON))]
tests = [test for test in tests if pattern_re.match(test.rom)]
start_time = time.time()
results = RunAllTests(tests, options)
duration = time.time() - start_time
PrintReplace('total time: %.3fs' % duration, newline=True)
ok = sum(1 for result in results if result.ok)
passed = sum(1 for result in results if result.passed)
completed = len(results)
print('Passed %d/%d' % (passed, completed))
if options.generate:
GenerateTestResults(results)
return 0 if ok == completed else 1
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))