-
Notifications
You must be signed in to change notification settings - Fork 116
/
sailtest.py
184 lines (161 loc) · 5.61 KB
/
sailtest.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
import os
import re
import sys
import subprocess
import datetime
import argparse
parser = argparse.ArgumentParser("run_tests.py")
parser.add_argument("--hide-error-output", help="Hide error information.", action='store_true')
parser.add_argument("--compact", help="Compact output.", action='store_true')
parser.add_argument("--targets", help="Targets to use (where supported).", action='append')
args = parser.parse_args()
def is_compact():
return args.compact
def get_targets(default_targets):
if args.targets is None:
return default_targets
else:
return args.targets
def compact_char(code, char):
print('{}{}{}'.format(code, char, color.END), end='')
sys.stdout.flush()
class color:
NOTICE = '\033[94m'
PASS = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
END = '\033[0m'
def get_sail_dir():
try:
return os.environ['SAIL_DIR']
except KeyError:
try:
p = subprocess.run(["opam", "var", "sail:share"], capture_output=True, text=True)
except Exception as e:
print('{}Unable to get Sail library directory from opam{}'.format(color.FAIL, color.END))
print(e)
sys.exit(1)
if p.returncode == 0:
return p.stdout.strip()
else:
print('{}Unable to get Sail library directory from opam{}'.format(color.FAIL, color.END))
print('{}stdout{}:'.format(color.NOTICE, color.END))
print(p.stdout)
print('{}stderr{}:'.format(color.NOTICE, color.END))
print(p.stderr)
sys.exit(1)
def print_ok(name):
if is_compact():
compact_char(color.PASS, '.')
else:
print('{} {}{}{}'.format('{} '.format(name).ljust(40, '.'), color.PASS, 'ok', color.END))
def print_skip(name):
if is_compact():
compact_char(color.WARNING, 's')
else:
print('{} {}{}{}'.format('{} '.format(name).ljust(40, '.'), color.WARNING, 'skip', color.END))
def get_sail():
try:
return os.environ['SAIL']
except KeyError:
return 'sail'
def parallel():
try:
return int(os.environ['TEST_PAR'])
except Exception as e:
print("Running 16 tests in parallel. Set TEST_PAR to configure")
return 16
def chunks(filenames, cores):
ys = []
chunk = []
for filename in filenames:
if re.match('.+\.sail$', filename):
chunk.append(filename)
if len(chunk) >= cores:
ys.append(list(chunk))
chunk = []
ys.append(list(chunk))
return ys
def directory_chunks(filenames, cores):
ys = []
chunk = []
for filename in filenames:
if os.path.isdir(filename):
chunk.append(filename)
if len(chunk) >= cores:
ys.append(list(chunk))
chunk = []
ys.append(list(chunk))
return ys
def project_chunks(filenames, cores):
ys = []
chunk = []
for filename in filenames:
if re.match('.+\.sail_project$', filename):
chunk.append(filename)
if len(chunk) >= cores:
ys.append(list(chunk))
chunk = []
ys.append(list(chunk))
return ys
def step(string, expected_status=0):
p = subprocess.Popen(string, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate()
status = p.wait()
if status != expected_status:
if is_compact():
compact_char(color.FAIL, 'X')
else:
print("{}Failed{}: {}".format(color.FAIL, color.END, string))
if not args.hide_error_output:
print('{}stdout{}:'.format(color.NOTICE, color.END))
print(out.decode('utf-8'))
print('{}stderr{}:'.format(color.NOTICE, color.END))
print(err.decode('utf-8'))
sys.exit(1)
def banner(string):
print('-' * len(string))
print(string)
print('-' * len(string))
sys.stdout.flush()
class Results:
def __init__(self, name):
self.passes = 0
self.failures = 0
self.xfails = 0
self._xfail_reasons = {}
self.xml = ""
self.name = name
def expect_failure(self, test, reason):
self._xfail_reasons[test] = reason
def _add_status(self, test, result, msg):
self.xml += f' <testcase name="{test}">\n <{result} message="{msg}">{msg}</{result}>\n </testcase>\n'
def _add_failure(self, test, msg):
self.failures += 1
self._add_status(test, "error", msg)
def collect(self, tests):
for test in tests:
_, status = os.waitpid(tests[test], 0)
if test in self._xfail_reasons:
reason = self._xfail_reasons[test]
if status == 0:
self._add_failure(test, "XPASS: " + reason)
else:
self.xfails += 1
self._add_status(test, "skipped", "XFAIL: " + reason)
continue
if status != 0:
self._add_failure(test, "fail")
else:
self.passes += 1
self.xml += ' <testcase name="{}"/>\n'.format(test)
sys.stdout.flush()
def finish(self):
xfail_msg = f' ({self.xfails} expected failures)' if self.xfails else ''
if is_compact():
print()
print('{}{} passes and {} failures{}{}'.format(color.NOTICE, self.passes, self.failures, xfail_msg, color.END))
time = datetime.datetime.utcnow()
suite = ' <testsuite name="{}" tests="{}" failures="{}" timestamp="{}">\n{} </testsuite>\n'
self.xml = suite.format(self.name, self.passes + self.failures, self.failures, time, self.xml)
return self.xml