-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_meson.py
More file actions
executable file
·206 lines (174 loc) · 5.84 KB
/
Copy pathtest_meson.py
File metadata and controls
executable file
·206 lines (174 loc) · 5.84 KB
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
#!/usr/bin/env python3
#
# magic to run tests via vscode native testing
import dis
import glob
import logging
import os
import shelve
import subprocess
from xml.etree.ElementTree import fromstring
import pytest
PROJECTS = ["prometheus", "protobuf", "mysql", "cache"]
LOGGER = logging.getLogger(__name__)
def shelve_cache(f):
def inner(*args):
name = args[0].replace("/", "_")
key = f.__name__ + ";" + ";".join(args)
with shelve.open(f"__pycache__/shelve_{name}") as db:
if key in db:
return db[key]
res = f(*args)
db[key] = res
return res
return inner
@shelve_cache
def meson_test_names(dirname):
names = []
p = run_with_args(dirname, True, ("test", "--no-rebuild", "--list"))
for name in p.stdout.decode().splitlines():
names.append(name)
return names
@shelve_cache
def boost_test_names(dirname, testname):
names = []
current = []
run_with_args(
dirname,
True,
("test", testname, "--no-rebuild", "--test-args", "\\--list_content"),
)
logname = os.path.join(dirname, "meson-logs/testlog.txt")
with open(logname) as f:
for line in f:
if "----------------------------------- stderr" in line:
line = ""
break
for line in f:
if "===================================" in line:
break
c = int((len(line) - len(line.lstrip())) / 4)
current = current[0:c]
current.append(line.strip().strip("*"))
names.append("/".join(current))
names = list(filter(lambda x: not any(y.startswith(x + "/") for y in names), names))
return names
def prepare_cases():
cases = {}
for d in PROJECTS:
for x in glob.glob(f"{d}/build*/build.ninja"):
dirname = os.path.dirname(x)
if dirname not in cases:
cases[dirname] = {}
for name in meson_test_names(dirname):
if name not in cases[dirname]:
cases[dirname][name] = []
if name.startswith("fuzz"):
cases[dirname][name].append(("fuzz", name))
else:
for bt in boost_test_names(dirname, name):
cases[dirname][name].append((name, bt))
return cases
def load_xml(filename):
rows = ""
with open(filename) as f:
for line in f:
if "----------------------------------- stdout" in line:
line = ""
break
for line in f:
if "----------------------------------- stderr" in line or "===================================" in line:
break
rows += line
return fromstring(rows)
def assert_xml(dirname, doc):
__tracebackhide__ = True
def step(dirname, doc, name):
__tracebackhide__ = True
def f():
pass
for x in doc.findall(f".//{name}"):
parent = doc.find(f".//{name}[@line=\"{x.attrib['line']}\"]..")
PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
f.__code__ = f.__code__.replace(
co_firstlineno=int(x.attrib["line"]),
co_filename=os.path.abspath(os.path.join(dirname, x.attrib["file"])),
co_name=parent.attrib["name"],
co_code=bytes(
[
dis.opmap["RESUME"],
0,
dis.opmap["LOAD_ASSERTION_ERROR"],
0,
dis.opmap["RAISE_VARARGS"],
1,
]
),
co_linetable=bytes(
[
(1 << 7) | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3) | (3 - 1),
0,
]
),
)
f()
step(dirname, doc, "Error")
step(dirname, doc, "FatalError")
step(dirname, doc, "Exception")
def run_with_args(dirname, check, cmd):
LOGGER.info(f"[{dirname}]: run {cmd}")
env = os.environ.copy()
env["DIRENV_LOG_FORMAT"] = ""
p = subprocess.run(
["/usr/bin/direnv", "exec", ".", "meson", *cmd],
cwd=dirname,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=check,
env=env,
)
return p
def ids(tc):
return tc[1].replace(" ", "_")
def param(x):
if x[1].startswith("Bench"):
return pytest.param(x, marks=pytest.mark.slow)
return x
# generate classes
for dirname, cases in prepare_cases().items():
name = "Test" + dirname.replace("/", " ").replace("-", " ").title().replace(" ", "").replace("Build", "")
if not name.endswith("Release"):
name += "Debug"
t = type(
name,
(object,),
{"dirname": dirname},
)
globals()[name] = t
# generate methods
for testname, bt in cases.items():
methodname = testname.replace(" ", "_")
def impl(self, tc):
__tracebackhide__ = True
testname, bt = tc
if testname == "fuzz":
run_with_args(self.dirname, True, ("test", bt))
else:
run_with_args(
self.dirname,
False,
(
"test",
testname,
"--test-args",
f"\\--color_output=no -f XML -t '{bt}'",
),
)
logname = os.path.join(self.dirname, "meson-logs/testlog.txt")
xml = load_xml(logname)
assert_xml(self.dirname, xml)
f = pytest.mark.parametrize("tc", [param(x) for x in bt], ids=ids)(impl)
if testname.startswith("fuzz"):
f = pytest.mark.slow(f)
setattr(t, f"test_{methodname}", f)