|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """Summarize PHPUnit TeamCity logs for a GitHub Actions job. |
3 | 3 |
|
4 | | -Usage: report.py <directory with *.log files> [title] |
| 4 | +Usage: report.py <directory with *.log files> [title] [reports url] |
5 | 5 |
|
6 | 6 | Each *.log file is the TeamCity log (phpunit --log-teamcity) of one suite, named after it. PHPUnit writes that log |
7 | 7 | while the tests run, so it also lists the failures of a run that PHPUnit aborted, which the JUnit log, written at |
8 | 8 | the end, does not. |
9 | 9 |
|
10 | 10 | The Markdown summary goes to $GITHUB_STEP_SUMMARY, or to stdout when that is not set. Error annotations for the |
11 | 11 | first failures go to stdout as workflow commands. Exits 0: reporting never fails the job, the test step does. |
| 12 | +The reports url, if given, is where the summary sends readers for the failures it leaves out. |
12 | 13 | """ |
13 | 14 |
|
14 | 15 | import glob |
|
17 | 18 | import sys |
18 | 19 |
|
19 | 20 | MAX_ROWS_PER_SUITE = 100 |
| 21 | +# GitHub drops a step summary larger than 1 MiB. The failure rows get at most half of that, the rest of the summary |
| 22 | +# takes a few hundred bytes per suite. |
| 23 | +MAX_FAILURE_BYTES = 512 * 1024 |
20 | 24 | MAX_EXPANDED_SUITES = 3 |
21 | 25 | MAX_TABLE_ROWS = 10 |
22 | 26 | MAX_ANNOTATIONS = 10 |
@@ -114,64 +118,102 @@ def broken(suite): |
114 | 118 | return suite["ended_early"] or suite["failed_tests"] > 0 |
115 | 119 |
|
116 | 120 |
|
| 121 | +def no_results(suite): |
| 122 | + """An empty log: either no test matched, or PHPUnit stopped while loading the tests, say on a syntax error. |
| 123 | +
|
| 124 | + PHPUnit only writes to the log once the first suite starts, so the log alone can't tell the two apart. |
| 125 | + """ |
| 126 | + return suite["planned"] is None and not broken(suite) |
| 127 | + |
| 128 | + |
117 | 129 | def result(suite): |
118 | 130 | if suite["ended_early"]: |
119 | 131 | return ":warning: ended early (PHPUnit aborted or the job stopped), see the job log" |
120 | 132 | if suite["failed_tests"]: |
121 | 133 | return ":x: failed" |
| 134 | + if no_results(suite): |
| 135 | + return ":grey_question: no results (no test ran, or PHPUnit stopped before the first one), see the job log" |
122 | 136 | if suite["planned"] == 0: |
123 | 137 | return ":heavy_minus_sign: no tests" |
124 | 138 | return ":white_check_mark: passed" |
125 | 139 |
|
126 | 140 |
|
127 | 141 | def headline(suites): |
128 | 142 | failing = [suite for suite in suites if broken(suite)] |
129 | | - if not failing: |
130 | | - return ":white_check_mark: {} passed: {}, {} skipped.".format( |
| 143 | + without_results = sum(1 for suite in suites if no_results(suite)) |
| 144 | + if failing: |
| 145 | + line = ":x: {} of {} failed: {}".format( |
| 146 | + len(failing), |
131 | 147 | plural(len(suites), "suite"), |
132 | | - plural(sum(suite["planned"] or 0 for suite in suites), "test"), |
133 | | - sum(suite["skipped"] for suite in suites), |
| 148 | + plural(sum(suite["failed_tests"] for suite in failing), "failed test"), |
134 | 149 | ) |
135 | | - line = ":x: {} of {} failed: {}".format( |
136 | | - len(failing), |
137 | | - plural(len(suites), "suite"), |
138 | | - plural(sum(suite["failed_tests"] for suite in failing), "failed test"), |
139 | | - ) |
140 | | - ended_early = sum(1 for suite in failing if suite["ended_early"]) |
141 | | - if ended_early: |
142 | | - line += ", {} ended early".format(plural(ended_early, "suite")) |
| 150 | + ended_early = sum(1 for suite in failing if suite["ended_early"]) |
| 151 | + if ended_early: |
| 152 | + line += ", {} ended early".format(plural(ended_early, "suite")) |
| 153 | + else: |
| 154 | + tests = plural(sum(suite["planned"] or 0 for suite in suites), "test") |
| 155 | + skipped = sum(suite["skipped"] for suite in suites) |
| 156 | + if without_results: |
| 157 | + line = ":grey_question: {} of {} passed: {}, {} skipped".format( |
| 158 | + len(suites) - without_results, plural(len(suites), "suite"), tests, skipped |
| 159 | + ) |
| 160 | + else: |
| 161 | + line = ":white_check_mark: {} passed: {}, {} skipped".format(plural(len(suites), "suite"), tests, skipped) |
| 162 | + if without_results: |
| 163 | + line += ", {} without results".format(plural(without_results, "suite")) |
143 | 164 | return line + "." |
144 | 165 |
|
145 | 166 |
|
146 | | -def failure_details(suite, expanded): |
| 167 | +def failure_rows(failing): |
| 168 | + """The table rows of each failing suite, in order, as many as fit in MAX_FAILURE_BYTES.""" |
| 169 | + left = MAX_FAILURE_BYTES |
| 170 | + tables = [] |
| 171 | + for suite in failing: |
| 172 | + rows = [] |
| 173 | + for failure in suite["failures"][:MAX_ROWS_PER_SUITE]: |
| 174 | + row = "| `{}` | {} |".format(cell(failure["test"]).replace("`", "'"), cell(failure["message"])) |
| 175 | + left -= len(row.encode("utf-8")) + 1 |
| 176 | + if left < 0: |
| 177 | + break |
| 178 | + rows.append(row) |
| 179 | + tables.append(rows) |
| 180 | + return tables |
| 181 | + |
| 182 | + |
| 183 | +def failure_details(suite, rows, expanded): |
147 | 184 | problems = [plural(suite["failed_tests"], "failed test")] if suite["failed_tests"] else [] |
148 | 185 | if suite["ended_early"]: |
149 | 186 | problems.append("ended early") |
150 | 187 | summary = "<b>{}</b>: {}".format(suite["name"], ", ".join(problems)) |
151 | 188 | lines = ["", "<details{}><summary>{}</summary>".format(" open" if expanded else "", summary), ""] |
152 | 189 | if suite["ended_early"]: |
153 | 190 | lines += ["PHPUnit stopped before the suite finished. The job log shows why.", ""] |
154 | | - if suite["failures"]: |
155 | | - lines += ["| Test | Message |", "|---|---|"] |
156 | | - for failure in suite["failures"][:MAX_ROWS_PER_SUITE]: |
157 | | - lines.append("| `{}` | {} |".format(cell(failure["test"]).replace("`", "'"), cell(failure["message"]))) |
158 | | - hidden = len(suite["failures"]) - MAX_ROWS_PER_SUITE |
159 | | - if hidden > 0: |
160 | | - lines.append("| … | and {} more |".format(hidden)) |
| 191 | + left_out = len(suite["failures"]) - len(rows) |
| 192 | + if rows: |
| 193 | + lines += ["| Test | Message |", "|---|---|"] + rows |
| 194 | + if left_out: |
| 195 | + lines.append("| … | and {} more |".format(left_out)) |
161 | 196 | lines.append("") |
| 197 | + elif left_out: |
| 198 | + lines += ["{} not listed: the summary reached its size limit.".format(plural(left_out, "failure")), ""] |
162 | 199 | return lines + ["</details>"] |
163 | 200 |
|
164 | 201 |
|
165 | | -def markdown(suites, title, directory): |
| 202 | +def markdown(suites, title, directory, reports_url=""): |
166 | 203 | lines = ["## " + title, ""] |
167 | 204 | if not suites: |
168 | | - lines.append("No PHPUnit TeamCity logs in `{}`: no suite ran.".format(directory)) |
| 205 | + # PHPUnit writes no log when it stops in its bootstrap |
| 206 | + lines.append(":grey_question: No PHPUnit TeamCity logs found in `{}`, see the job log.".format(directory)) |
169 | 207 | return "\n".join(lines) + "\n" |
170 | 208 |
|
171 | 209 | lines.append(headline(suites)) |
172 | 210 | failing = [suite for suite in suites if broken(suite)] |
173 | | - for suite in failing: |
174 | | - lines += failure_details(suite, len(failing) <= MAX_EXPANDED_SUITES) |
| 211 | + tables = failure_rows(failing) |
| 212 | + for suite, rows in zip(failing, tables): |
| 213 | + lines += failure_details(suite, rows, len(failing) <= MAX_EXPANDED_SUITES) |
| 214 | + if sum(len(rows) for rows in tables) < sum(len(suite["failures"]) for suite in failing): |
| 215 | + reports = "[test reports]({})".format(reports_url) if reports_url else "TeamCity logs" |
| 216 | + lines += ["", "Not all failures are listed. The {} have all of them.".format(reports)] |
175 | 217 |
|
176 | 218 | table = ["| Suite | Tests | Failed | Skipped | Result |", "|---|---:|---:|---:|---|"] |
177 | 219 | for suite in suites: |
@@ -212,13 +254,14 @@ def annotations(suites): |
212 | 254 |
|
213 | 255 | def main(argv): |
214 | 256 | if len(argv) < 2: |
215 | | - print("Usage: report.py <directory with *.log files> [title]", file=sys.stderr) |
| 257 | + print("Usage: report.py <directory with *.log files> [title] [reports url]", file=sys.stderr) |
216 | 258 | return 2 |
217 | 259 | directory = argv[1] |
218 | 260 | title = argv[2] if len(argv) > 2 and argv[2] else "PHPUnit results" |
| 261 | + reports_url = argv[3] if len(argv) > 3 else "" |
219 | 262 | suites = [parse(path) for path in sorted(glob.glob(os.path.join(directory, "*.log")))] |
220 | 263 |
|
221 | | - summary = markdown(suites, title, directory) |
| 264 | + summary = markdown(suites, title, directory, reports_url) |
222 | 265 | summary_file = os.environ.get("GITHUB_STEP_SUMMARY") |
223 | 266 | if summary_file: |
224 | 267 | with open(summary_file, "a", encoding="utf-8") as handle: |
|
0 commit comments