-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathci_junit_summary.py
More file actions
200 lines (165 loc) · 6.93 KB
/
Copy pathci_junit_summary.py
File metadata and controls
200 lines (165 loc) · 6.93 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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import glob
import json
import sys
import xml.etree.ElementTree as ET
from collections import defaultdict
from pathlib import Path
from typing import Any
def _case_status(case: ET.Element) -> str:
if case.findall("error"):
return "error"
if case.findall("failure"):
return "failed"
if case.findall("skipped"):
return "skipped"
return "passed"
def _message(case: ET.Element) -> str:
for tag in ("error", "failure", "skipped"):
node = case.find(tag)
if node is not None:
message = node.attrib.get("message") or (node.text or "")
return " ".join(message.split())[:500]
return ""
def _annotation_escape(value: str) -> str:
return value.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")
def _read_cases(path: Path) -> list[dict[str, Any]]:
root = ET.parse(path).getroot()
cases: list[dict[str, Any]] = []
for case in root.iter("testcase"):
classname = case.attrib.get("classname", "")
name = case.attrib.get("name", "")
status = _case_status(case)
cases.append(
{
"classname": classname,
"name": name,
"status": status,
"time": float(case.attrib.get("time") or 0.0),
"message": _message(case),
"file": str(path),
}
)
return cases
def _expand_patterns(patterns: list[str]) -> list[Path]:
paths: set[Path] = set()
for pattern in patterns:
for match in glob.glob(pattern, recursive=True):
path = Path(match)
if path.is_file():
paths.add(path)
return sorted(paths)
def summarize(patterns: list[str]) -> dict[str, Any]:
files = _expand_patterns(patterns)
parse_errors: list[dict[str, str]] = []
cases: list[dict[str, Any]] = []
for path in files:
try:
cases.extend(_read_cases(path))
except ET.ParseError as exc:
parse_errors.append({"file": str(path), "error": str(exc)})
counts = {"passed": 0, "failed": 0, "error": 0, "skipped": 0}
by_key: dict[tuple[str, str], set[str]] = defaultdict(set)
for case in cases:
status = case["status"]
counts[status] += 1
by_key[(case["classname"], case["name"])].add(status)
failed_cases = [case for case in cases if case["status"] in {"failed", "error"}]
flaky_candidates = [
{
"classname": classname,
"name": name,
"statuses": sorted(statuses),
}
for (classname, name), statuses in sorted(by_key.items())
if "passed" in statuses and ({"failed", "error"} & statuses)
]
return {
"files": len(files),
"tests": len(cases),
"passed": counts["passed"],
"failures": counts["failed"],
"errors": counts["error"],
"skipped": counts["skipped"],
"failed_cases": failed_cases,
"flaky_candidates": flaky_candidates,
"parse_errors": parse_errors,
"status": "failed" if failed_cases or parse_errors else "passed",
}
def write_markdown(summary: dict[str, Any], path: Path) -> None:
lines = [
"# JUnit Summary",
"",
"| Metric | Count |",
"| --- | ---: |",
f"| XML files | {summary['files']} |",
f"| Tests | {summary['tests']} |",
f"| Passed | {summary['passed']} |",
f"| Failures | {summary['failures']} |",
f"| Errors | {summary['errors']} |",
f"| Skipped | {summary['skipped']} |",
f"| Flaky candidates | {len(summary['flaky_candidates'])} |",
"",
]
if summary["failed_cases"]:
lines.extend(["## Failed Cases", ""])
for case in summary["failed_cases"]:
label = f"{case['classname']}.{case['name']}".strip(".")
message = f" - {case['message']}" if case.get("message") else ""
lines.append(f"- `{case['status']}` `{label}` in `{case['file']}`{message}")
lines.append("")
if summary["flaky_candidates"]:
lines.extend(["## Flaky Candidates", ""])
for case in summary["flaky_candidates"]:
label = f"{case['classname']}.{case['name']}".strip(".")
statuses = ", ".join(case["statuses"])
lines.append(f"- `{label}` had mixed statuses: `{statuses}`")
lines.append("")
if summary["parse_errors"]:
lines.extend(["## Parse Errors", ""])
for err in summary["parse_errors"]:
lines.append(f"- `{err['file']}`: {err['error']}")
lines.append("")
path.write_text("\n".join(lines), encoding="utf-8")
def emit_annotations(summary: dict[str, Any]) -> None:
for case in summary["failed_cases"]:
label = f"{case['classname']}.{case['name']}".strip(".")
detail = case.get("message") or "JUnit reported a test failure."
print(f"::error title=JUnit {case['status']}::{_annotation_escape(label + ' - ' + detail)}")
for case in summary["flaky_candidates"]:
label = f"{case['classname']}.{case['name']}".strip(".")
statuses = ", ".join(case["statuses"])
print(f"::warning title=Flaky test candidate::{_annotation_escape(label + ' had mixed statuses: ' + statuses)}")
for err in summary["parse_errors"]:
print(f"::error title=JUnit parse error::{_annotation_escape(err['file'] + ': ' + err['error'])}")
def main() -> int:
parser = argparse.ArgumentParser(description="Summarize JUnit XML into CI evidence artifacts.")
parser.add_argument("patterns", nargs="+", help="JUnit XML glob pattern(s).")
parser.add_argument("--json-out", required=True, help="Path to write summary JSON.")
parser.add_argument("--markdown-out", required=True, help="Path to write summary Markdown.")
parser.add_argument("--require-files", action="store_true", help="Fail if no XML files match.")
parser.add_argument("--github-annotations", action="store_true", help="Emit GitHub workflow annotations.")
args = parser.parse_args()
summary = summarize(args.patterns)
json_path = Path(args.json_out)
markdown_path = Path(args.markdown_out)
json_path.parent.mkdir(parents=True, exist_ok=True)
markdown_path.parent.mkdir(parents=True, exist_ok=True)
json_path.write_text(json.dumps(summary, indent=2, sort_keys=True) + "\n", encoding="utf-8")
write_markdown(summary, markdown_path)
if args.github_annotations:
emit_annotations(summary)
print(
"JUnit summary: "
f"files={summary['files']} tests={summary['tests']} failures={summary['failures']} "
f"errors={summary['errors']} skipped={summary['skipped']} "
f"flaky_candidates={len(summary['flaky_candidates'])}"
)
if args.require_files and summary["files"] == 0:
print("No JUnit XML files matched required patterns.", file=sys.stderr)
return 2
return 0
if __name__ == "__main__":
raise SystemExit(main())