|
1 | 1 | # type: ignore
|
2 | 2 | import argparse
|
3 |
| -import itertools |
| 3 | +import functools |
| 4 | +import json |
4 | 5 | import pathlib
|
5 | 6 | import textwrap
|
| 7 | +from dataclasses import dataclass |
6 | 8 |
|
7 |
| -parser = argparse.ArgumentParser() |
8 |
| -parser.add_argument("filepaths", nargs="+", type=pathlib.Path) |
9 |
| -args = parser.parse_args() |
10 |
| - |
11 |
| -filepaths = sorted(p for p in args.filepaths if p.is_file()) |
12 |
| - |
13 |
| - |
14 |
| -def extract_short_test_summary_info(lines): |
15 |
| - up_to_start_of_section = itertools.dropwhile( |
16 |
| - lambda l: "=== short test summary info ===" not in l, |
17 |
| - lines, |
18 |
| - ) |
19 |
| - up_to_section_content = itertools.islice(up_to_start_of_section, 1, None) |
20 |
| - section_content = itertools.takewhile( |
21 |
| - lambda l: l.startswith("FAILED"), up_to_section_content |
22 |
| - ) |
23 |
| - content = "\n".join(section_content) |
24 |
| - |
25 |
| - return content |
26 |
| - |
27 |
| - |
28 |
| -def extract_warnings(lines): |
29 |
| - up_to_start_of_section = itertools.dropwhile( |
30 |
| - lambda l: "=== warnings summary ===" not in l, |
31 |
| - lines, |
32 |
| - ) |
33 |
| - up_to_section_content = itertools.islice(up_to_start_of_section, 1, None) |
34 |
| - section_content = itertools.takewhile( |
35 |
| - lambda l: not l.startswith("==="), |
36 |
| - up_to_section_content, |
37 |
| - ) |
38 |
| - content = "\n".join(section_content) |
39 |
| - return content |
40 |
| - |
41 |
| - |
42 |
| -def format_log_message(path): |
43 |
| - py_version = path.name.split("-")[1] |
44 |
| - summary = f"Python {py_version} Test Summary Info" |
45 |
| - with open(path) as f: |
46 |
| - lines = [line.rstrip() for line in f] |
47 |
| - data = extract_short_test_summary_info(lines) |
48 |
| - warnings = extract_warnings(lines) |
49 |
| - |
50 |
| - message = ( |
51 |
| - textwrap.dedent( |
52 |
| - """\ |
53 |
| - <details><summary>{summary}</summary> |
54 |
| -
|
55 |
| - ``` |
56 |
| - {data} |
57 |
| - ``` |
58 |
| -
|
59 |
| - </details> |
60 |
| - """ |
61 |
| - ) |
62 |
| - .rstrip() |
63 |
| - .format(summary=summary, data=data) |
64 |
| - ) |
65 |
| - |
66 |
| - if warnings: |
67 |
| - message += ( |
68 |
| - textwrap.dedent( |
69 |
| - """ |
70 |
| -
|
71 |
| - <details><summary>Warnings</summary> |
72 |
| -
|
73 |
| - ``` |
74 |
| - {warnings} |
75 |
| - ``` |
76 |
| -
|
77 |
| - </details> |
78 |
| - """ |
79 |
| - ) |
80 |
| - .rstrip() |
81 |
| - .format(warnings=warnings) |
82 |
| - ) |
| 9 | +from pytest import CollectReport, TestReport |
83 | 10 |
|
| 11 | + |
| 12 | +@dataclass |
| 13 | +class SessionStart: |
| 14 | + pytest_version: str |
| 15 | + outcome: str = "status" |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def _from_json(cls, json): |
| 19 | + json_ = json.copy() |
| 20 | + json_.pop("$report_type") |
| 21 | + return cls(**json_) |
| 22 | + |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class SessionFinish: |
| 26 | + exitstatus: str |
| 27 | + outcome: str = "status" |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def _from_json(cls, json): |
| 31 | + json_ = json.copy() |
| 32 | + json_.pop("$report_type") |
| 33 | + return cls(**json_) |
| 34 | + |
| 35 | + |
| 36 | +def parse_record(record): |
| 37 | + report_types = { |
| 38 | + "TestReport": TestReport, |
| 39 | + "CollectReport": CollectReport, |
| 40 | + "SessionStart": SessionStart, |
| 41 | + "SessionFinish": SessionFinish, |
| 42 | + } |
| 43 | + cls = report_types.get(record["$report_type"]) |
| 44 | + if cls is None: |
| 45 | + raise ValueError(f"unknown report type: {record['$report_type']}") |
| 46 | + |
| 47 | + return cls._from_json(record) |
| 48 | + |
| 49 | + |
| 50 | +@functools.singledispatch |
| 51 | +def format_summary(report): |
| 52 | + return f"{report.nodeid}: {report}" |
| 53 | + |
| 54 | + |
| 55 | +@format_summary.register |
| 56 | +def _(report: TestReport): |
| 57 | + message = report.longrepr.chain[0][1].message |
| 58 | + return f"{report.nodeid}: {message}" |
| 59 | + |
| 60 | + |
| 61 | +@format_summary.register |
| 62 | +def _(report: CollectReport): |
| 63 | + message = report.longrepr.split("\n")[-1].removeprefix("E").lstrip() |
| 64 | + return f"{report.nodeid}: {message}" |
| 65 | + |
| 66 | + |
| 67 | +def format_report(reports, py_version): |
| 68 | + newline = "\n" |
| 69 | + summaries = newline.join(format_summary(r) for r in reports) |
| 70 | + message = textwrap.dedent( |
| 71 | + """\ |
| 72 | + <details><summary>Python {py_version} Test Summary</summary> |
| 73 | +
|
| 74 | + ``` |
| 75 | + {summaries} |
| 76 | + ``` |
| 77 | +
|
| 78 | + </details> |
| 79 | + """ |
| 80 | + ).format(summaries=summaries, py_version=py_version) |
84 | 81 | return message
|
85 | 82 |
|
86 | 83 |
|
87 |
| -print("Parsing logs ...") |
88 |
| -message = "\n\n".join(format_log_message(path) for path in filepaths) |
| 84 | +if __name__ == "__main__": |
| 85 | + parser = argparse.ArgumentParser() |
| 86 | + parser.add_argument("filepath", type=pathlib.Path) |
| 87 | + args = parser.parse_args() |
| 88 | + |
| 89 | + py_version = args.filepath.stem.split("-")[1] |
| 90 | + |
| 91 | + print("Parsing logs ...") |
| 92 | + |
| 93 | + lines = args.filepath.read_text().splitlines() |
| 94 | + reports = [parse_record(json.loads(line)) for line in lines] |
| 95 | + |
| 96 | + failed = [report for report in reports if report.outcome == "failed"] |
| 97 | + |
| 98 | + message = format_report(failed, py_version=py_version) |
89 | 99 |
|
90 |
| -output_file = pathlib.Path("pytest-logs.txt") |
91 |
| -print(f"Writing output file to: {output_file.absolute()}") |
92 |
| -output_file.write_text(message) |
| 100 | + output_file = pathlib.Path("pytest-logs.txt") |
| 101 | + print(f"Writing output file to: {output_file.absolute()}") |
| 102 | + output_file.write_text(message) |
0 commit comments