-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathboost.py
More file actions
164 lines (140 loc) · 5.35 KB
/
Copy pathboost.py
File metadata and controls
164 lines (140 loc) · 5.35 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
from __future__ import annotations
import io
import os
import subprocess
import tempfile
from typing import Sequence
from xml.etree import ElementTree
from pytest_cpp.error import CppTestFailure
from pytest_cpp.error import Markup
from pytest_cpp.facade_abc import AbstractFacade
from pytest_cpp.helpers import make_cmdline
class BoostTestFacade(AbstractFacade):
"""
Facade for BoostTests.
"""
@classmethod
def is_test_suite(
cls,
executable: str,
harness_collect: Sequence[str] = (),
) -> bool:
args = make_cmdline(harness_collect, executable, ["--help"])
try:
output = subprocess.check_output(
args,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
except (subprocess.CalledProcessError, OSError):
return False
else:
return "--output_format" in output and "log_format" in output
def list_tests(
self,
executable: str,
harness_collect: Sequence[str] = (),
) -> list[str]:
del harness_collect
# unfortunately boost doesn't provide us with a way to list the tests
# inside the executable, so the test_id is a dummy placeholder :(
return [os.path.basename(os.path.splitext(executable)[0])]
def run_test(
self,
executable: str,
test_id: str,
test_args: Sequence[str] = (),
harness: Sequence[str] = (),
) -> tuple[Sequence[BoostTestFailure] | None, str]:
def read_file(name: str) -> str:
try:
with io.open(name) as f:
return f.read()
except IOError:
return ""
with tempfile.TemporaryDirectory(prefix="pytest-cpp") as temp_dir:
# On Windows, ValueError is raised when path and start are on different drives.
# In this case failing back to the absolute path.
try:
log_xml = os.path.join(os.path.relpath(temp_dir), "log.xml")
report_xml = os.path.join(os.path.relpath(temp_dir), "report.xml")
except ValueError:
log_xml = os.path.join(temp_dir, "log.xml")
report_xml = os.path.join(temp_dir, "report.xml")
args = list(
make_cmdline(
harness,
executable,
[
"--output_format=XML",
f"--log_sink={log_xml}",
f"--report_sink={report_xml}",
],
)
)
args.extend(test_args)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
raw_stdout, _ = p.communicate()
stdout = raw_stdout.decode("utf-8") if raw_stdout else ""
log = read_file(log_xml)
report = read_file(report_xml)
if p.returncode not in (0, 200, 201):
msg = (
"Internal Error: calling {executable} "
"for test {test_id} failed (returncode={returncode}):\n"
"output:{stdout}\n"
"log:{log}\n"
"report:{report}"
)
failure = BoostTestFailure(
"<no source file>",
linenum=0,
contents=msg.format(
executable=executable,
test_id=test_id,
stdout=stdout,
log=log,
report=report,
returncode=p.returncode,
),
)
return [failure], stdout
results = self._parse_log(log=log)
if results:
return results, stdout
return None, stdout
def _parse_log(self, log: str) -> list[BoostTestFailure]:
"""
Parse the "log" section produced by BoostTest.
This is always a XML file, and from this we produce most of the
failures possible when running BoostTest.
"""
# Boosttest will sometimes generate unparseable XML
# so we surround it with xml tags.
parsed_elements = []
log = "<xml>{}</xml>".format(log)
log_root = ElementTree.fromstring(log)
testlog = log_root.find("TestLog")
parsed_elements.extend(log_root.findall("Exception"))
parsed_elements.extend(log_root.findall("Error"))
parsed_elements.extend(log_root.findall("FatalError"))
if testlog is not None:
parsed_elements.extend(testlog.findall("Exception"))
parsed_elements.extend(testlog.findall("Error"))
parsed_elements.extend(testlog.findall("FatalError"))
result = []
for elem in parsed_elements:
filename = elem.attrib["file"]
linenum = int(elem.attrib["line"])
result.append(BoostTestFailure(filename, linenum, elem.text or ""))
return result
class BoostTestFailure(CppTestFailure):
def __init__(self, filename: str, linenum: int, contents: str) -> None:
self.filename = filename
self.linenum = linenum
self.lines = contents.splitlines()
def get_lines(self) -> list[tuple[str, Markup]]:
m = ("red", "bold")
return [(x, m) for x in self.lines]
def get_file_reference(self) -> tuple[str, int]:
return self.filename, self.linenum