-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse_logs.py
248 lines (187 loc) · 6.78 KB
/
parse_logs.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# type: ignore
import argparse
import functools
import json
import pathlib
import re
import sys
import textwrap
from dataclasses import dataclass
import more_itertools
from pytest import CollectReport, TestReport
test_collection_stage = "test collection session"
@dataclass
class SessionStart:
pytest_version: str
outcome: str = "status"
@classmethod
def _from_json(cls, json):
json_ = json.copy()
json_.pop("$report_type")
return cls(**json_)
@dataclass
class SessionFinish:
exitstatus: str
outcome: str = "status"
@classmethod
def _from_json(cls, json):
json_ = json.copy()
json_.pop("$report_type")
return cls(**json_)
@dataclass
class PreformattedReport:
filepath: str
name: str
variant: str | None
message: str
@dataclass
class CollectionError:
name: str
repr_: str
def parse_record(record):
report_types = {
"TestReport": TestReport,
"CollectReport": CollectReport,
"SessionStart": SessionStart,
"SessionFinish": SessionFinish,
}
cls = report_types.get(record["$report_type"])
if cls is None:
raise ValueError(f"unknown report type: {record['$report_type']}")
return cls._from_json(record)
nodeid_re = re.compile(r"(?P<filepath>.+?)::(?P<name>.+?)(?:\[(?P<variant>.+)\])?")
def parse_nodeid(nodeid):
match = nodeid_re.fullmatch(nodeid)
if match is None:
raise ValueError(f"unknown test id: {nodeid}")
return match.groupdict()
@functools.singledispatch
def preformat_report(report):
parsed = parse_nodeid(report.nodeid)
return PreformattedReport(message=str(report), **parsed)
@preformat_report.register
def _(report: TestReport):
parsed = parse_nodeid(report.nodeid)
if isinstance(report.longrepr, str):
message = report.longrepr
else:
message = report.longrepr.reprcrash.message
return PreformattedReport(message=message, **parsed)
@preformat_report.register
def _(report: CollectReport):
if report.nodeid == "":
return CollectionError(name=test_collection_stage, repr_=str(report.longrepr))
if "::" not in report.nodeid:
parsed = {
"filepath": report.nodeid,
"name": None,
"variant": None,
}
else:
parsed = parse_nodeid(report.nodeid)
if isinstance(report.longrepr, str):
message = report.longrepr.split("\n")[-1].removeprefix("E").lstrip()
else:
message = report.longrepr.reprcrash.message
return PreformattedReport(message=message, **parsed)
def format_summary(report):
if report.variant is not None:
return f"{report.filepath}::{report.name}[{report.variant}]: {report.message}"
elif report.name is not None:
return f"{report.filepath}::{report.name}: {report.message}"
else:
return f"{report.filepath}: {report.message}"
def format_report(summaries, py_version):
template = textwrap.dedent(
"""\
<details><summary>Python {py_version} Test Summary</summary>
```
{summaries}
```
</details>
"""
)
# can't use f-strings because that would format *before* the dedenting
message = template.format(summaries="\n".join(summaries), py_version=py_version)
return message
def merge_variants(reports, max_chars, **formatter_kwargs):
def format_variant_group(name, group):
filepath, test_name, message = name
n_variants = len(group)
if n_variants != 1:
return f"{filepath}::{test_name}[{n_variants} failing variants]: {message}"
elif n_variants == 1 and group[0].variant is not None:
report = more_itertools.one(group)
return f"{filepath}::{test_name}[{report.variant}]: {message}"
else:
return f"{filepath}::{test_name}: {message}"
bucket = more_itertools.bucket(reports, lambda r: (r.filepath, r.name, r.message))
summaries = [format_variant_group(name, list(bucket[name])) for name in bucket]
formatted = format_report(summaries, **formatter_kwargs)
return formatted
def truncate(reports, max_chars, **formatter_kwargs):
fractions = [0.95, 0.75, 0.5, 0.25, 0.1, 0.01]
n_reports = len(reports)
for fraction in fractions:
n_selected = int(n_reports * fraction)
selected_reports = reports[: int(n_reports * fraction)]
report_messages = [format_summary(report) for report in selected_reports]
summary = report_messages + [f"+ {n_reports - n_selected} failing tests"]
formatted = format_report(summary, **formatter_kwargs)
if len(formatted) <= max_chars:
return formatted
return None
def summarize(reports, **formatter_kwargs):
summary = [f"{len(reports)} failing tests"]
return format_report(summary, **formatter_kwargs)
def compressed_report(reports, max_chars, **formatter_kwargs):
strategies = [
merge_variants,
# merge_test_files,
# merge_tests,
truncate,
]
summaries = [format_summary(report) for report in reports]
formatted = format_report(summaries, **formatter_kwargs)
if len(formatted) <= max_chars:
return formatted
for strategy in strategies:
formatted = strategy(reports, max_chars=max_chars, **formatter_kwargs)
if formatted is not None and len(formatted) <= max_chars:
return formatted
return summarize(reports, **formatter_kwargs)
def format_collection_error(error, **formatter_kwargs):
return textwrap.dedent(
"""\
<details><summary>Python {py_version} Test Summary</summary>
{name} failed:
```
{traceback}
```
</details>
"""
).format(py_version=py_version, name=error.name, traceback=error.repr_)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("filepath", type=pathlib.Path)
args = parser.parse_args()
py_version = ".".join(str(_) for _ in sys.version_info[:2])
print("Parsing logs ...")
lines = args.filepath.read_text().splitlines()
parsed_lines = [json.loads(line) for line in lines]
reports = [
parse_record(data)
for data in parsed_lines
if data["$report_type"] != "WarningMessage"
]
failed = [report for report in reports if report.outcome == "failed"]
preformatted = [preformat_report(report) for report in failed]
if len(preformatted) == 1 and isinstance(preformatted[0], CollectionError):
message = format_collection_error(preformatted[0], py_version=py_version)
else:
message = compressed_report(
preformatted, max_chars=65535, py_version=py_version
)
output_file = pathlib.Path("pytest-logs.txt")
print(f"Writing output file to: {output_file.absolute()}")
output_file.write_text(message)