-
Notifications
You must be signed in to change notification settings - Fork 23
/
google.py
193 lines (171 loc) · 6.8 KB
/
google.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
from __future__ import annotations
import os
import subprocess
import tempfile
from typing import Sequence
from xml.etree import ElementTree
import pytest
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 GoogleTestFacade(AbstractFacade):
"""
Facade for GoogleTests.
"""
@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 "--gtest_list_tests" in output
def list_tests(
self,
executable: str,
harness_collect: Sequence[str] = (),
) -> list[str]:
"""
Executes google-test with "--gtest_list_tests" and gets list of tests
parsing output like this:
PrimeTableTest/0. # TypeParam = class OnTheFlyPrimeTable
ReturnsFalseForNonPrimes
ReturnsTrueForPrimes
CanGetNextPrime
"""
args = make_cmdline(harness_collect, executable, ["--gtest_list_tests"])
output = subprocess.check_output(
args,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
def strip_comment(x: str) -> str:
comment_start = x.find("#")
if comment_start != -1:
x = x[:comment_start]
return x
test_suite: str | None = None
result = []
for line in output.splitlines():
has_indent = line.startswith(" ")
if not has_indent and "." in line:
test_suite = strip_comment(line).strip()
elif has_indent:
assert test_suite is not None
result.append(test_suite + strip_comment(line).strip())
return result
def run_test(
self,
executable: str,
test_id: str,
test_args: Sequence[str] = (),
harness: Sequence[str] = (),
) -> tuple[list[GoogleTestFailure] | None, str]:
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:
xml_filename = os.path.join(os.path.relpath(temp_dir), "cpp-report.xml")
except ValueError:
xml_filename = os.path.join(temp_dir, "cpp-report.xml")
args = list(
make_cmdline(
harness,
executable,
[f"--gtest_filter={test_id}", f"--gtest_output=xml:{xml_filename}"],
)
)
args.extend(test_args)
try:
output = subprocess.check_output(
args, stderr=subprocess.STDOUT, universal_newlines=True
)
except subprocess.CalledProcessError as e:
output = e.output
if e.returncode != 1:
msg = (
"Internal Error: calling {executable} "
"for test {test_id} failed (returncode={returncode}):\n"
"{output}"
)
failure = GoogleTestFailure(
msg.format(
executable=executable,
test_id=test_id,
output=e.output,
returncode=e.returncode,
)
)
return [failure], output
results = self._parse_xml(xml_filename)
for executed_test_id, failures, skipped in results:
if executed_test_id == test_id:
if failures:
return [GoogleTestFailure(x) for x in failures], output
elif skipped:
pytest.skip("\n".join(skipped))
else:
return None, output
msg = "Internal Error: could not find test " "{test_id} in results:\n{results}"
results_list = "\n".join(x for (x, f, s) in results)
failure = GoogleTestFailure(msg.format(test_id=test_id, results=results_list))
return [failure], output
def _parse_xml(
self, xml_filename: str
) -> Sequence[tuple[str, Sequence[str], Sequence[str]]]:
root = ElementTree.parse(xml_filename)
result = []
for test_suite in root.findall("testsuite"):
test_suite_name = test_suite.attrib["name"]
for test_case in test_suite.findall("testcase"):
test_name = test_case.attrib["name"]
failures = []
failure_elements = test_case.findall("failure")
for failure_elem in failure_elements:
failures.append(failure_elem.text or "")
skippeds = []
if test_case.attrib.get("result", None) == "skipped":
# In gtest 1.11 a skipped message was added to
# the output file
skipped_elements = test_case.findall("skipped")
for skipped_elem in skipped_elements:
skippeds.append(skipped_elem.text or "")
# In gtest 1.10 the skipped message is not dump,
# so if no skipped message was found just
# append a "skipped" keyword
if not skipped_elements:
skippeds.append("Skipped")
elif test_case.attrib.get("status", None) == "notrun":
skippeds.append("Disabled")
result.append((test_suite_name + "." + test_name, failures, skippeds))
return result
class GoogleTestFailure(CppTestFailure):
def __init__(self, contents: str) -> None:
self.lines = contents.splitlines()
self.filename = "unknown file"
self.linenum = 0
if self.lines:
fields = self.lines[0].rsplit(":", 1)
if len(fields) == 2:
try:
linenum = int(fields[1])
except ValueError:
return
self.filename = fields[0]
self.linenum = linenum
self.lines.pop(0)
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