|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | + |
| 5 | +"""Generate test failure and skip reports from a Go test output .txt file. |
| 6 | +
|
| 7 | +Usage: go-test-report.py <test-output-txt> |
| 8 | +
|
| 9 | +Given an input file <name>.txt, generates alongside it: |
| 10 | + <name>.failed.txt - Failed test blocks (only written if there are failures) |
| 11 | + <name>.skipped.txt - Skipped test blocks (only written if there are skips) |
| 12 | +""" |
| 13 | + |
| 14 | +import re |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | + |
| 19 | +RUN_RE = re.compile(r"^=== RUN\s+(\S+)") |
| 20 | +PARENT_RESULT_RE = re.compile(r"^--- \w+:\s+(\S+)") |
| 21 | +FAIL_RE = re.compile(r"^\s*--- FAIL:") |
| 22 | +SKIP_RE = re.compile(r"^\s*--- SKIP:") |
| 23 | + |
| 24 | + |
| 25 | +def _extract_blocks(lines: list[str], marker_re: re.Pattern[str]) -> list[str]: |
| 26 | + """Extract Go test blocks whose result lines match marker_re. |
| 27 | +
|
| 28 | + A block starts at a top-level '=== RUN' and includes all nested subtest |
| 29 | + '=== RUN' lines, body output, the parent '--- <result>:' line, and any |
| 30 | + indented child '--- <result>:' lines. A block is emitted when any result |
| 31 | + line (parent or child) matches marker_re. |
| 32 | + """ |
| 33 | + blocks: list[str] = [] |
| 34 | + buf: list[str] = [] |
| 35 | + matched = False |
| 36 | + run_name = "" |
| 37 | + state = "idle" # idle | in_block | in_results |
| 38 | + |
| 39 | + for line in lines: |
| 40 | + if state == "idle": |
| 41 | + m = RUN_RE.match(line) |
| 42 | + if m: |
| 43 | + buf = [line] |
| 44 | + run_name = m.group(1) |
| 45 | + matched = False |
| 46 | + state = "in_block" |
| 47 | + |
| 48 | + elif state == "in_block": |
| 49 | + buf.append(line) |
| 50 | + m = PARENT_RESULT_RE.match(line) |
| 51 | + if m: |
| 52 | + if m.group(1) != run_name: |
| 53 | + raise ValueError( |
| 54 | + f"Parent result name {m.group(1)!r} does not match " |
| 55 | + f"block run name {run_name!r}: {line!r}" |
| 56 | + ) |
| 57 | + |
| 58 | + if marker_re.match(line): |
| 59 | + matched = True |
| 60 | + |
| 61 | + state = "in_results" |
| 62 | + |
| 63 | + elif state == "in_results": |
| 64 | + m = RUN_RE.match(line) |
| 65 | + if m: |
| 66 | + # New block started; finalize current one. |
| 67 | + if matched: |
| 68 | + blocks.append("\n".join(buf)) |
| 69 | + |
| 70 | + buf = [line] |
| 71 | + run_name = m.group(1) |
| 72 | + matched = False |
| 73 | + state = "in_block" |
| 74 | + else: |
| 75 | + buf.append(line) |
| 76 | + if marker_re.match(line): |
| 77 | + matched = True |
| 78 | + |
| 79 | + # Finalize any pending block at EOF. |
| 80 | + if state == "in_results" and matched: |
| 81 | + blocks.append("\n".join(buf)) |
| 82 | + |
| 83 | + return blocks |
| 84 | + |
| 85 | + |
| 86 | +def main() -> None: |
| 87 | + if len(sys.argv) != 2: |
| 88 | + print(f"Usage: {sys.argv[0]} <test-output-txt>", file=sys.stderr) |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + test_output = Path(sys.argv[1]) |
| 92 | + |
| 93 | + if test_output.suffix != ".txt": |
| 94 | + print(f"ERROR: Test output file must have a .txt extension: {test_output!r}", file=sys.stderr) |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + if not test_output.is_file(): |
| 98 | + print(f"ERROR: Test output file not found: {test_output!r}", file=sys.stderr) |
| 99 | + sys.exit(1) |
| 100 | + |
| 101 | + lines = test_output.read_text(encoding="utf-8", errors="replace").splitlines() |
| 102 | + |
| 103 | + failed_blocks = _extract_blocks(lines, FAIL_RE) |
| 104 | + skipped_blocks = _extract_blocks(lines, SKIP_RE) |
| 105 | + |
| 106 | + failed_report = test_output.with_suffix(".failed.txt") |
| 107 | + skipped_report = test_output.with_suffix(".skipped.txt") |
| 108 | + |
| 109 | + failed_text = "\n".join(failed_blocks) + "\n" if failed_blocks else "" |
| 110 | + skipped_text = "\n".join(skipped_blocks) + "\n" if skipped_blocks else "" |
| 111 | + |
| 112 | + # Only write report files when non-empty; otherwise remove any stale file |
| 113 | + # so downstream artifact uploads don't include empty placeholders. |
| 114 | + for report_path, text in ((failed_report, failed_text), (skipped_report, skipped_text)): |
| 115 | + if text: |
| 116 | + report_path.write_text(text, encoding="utf-8") |
| 117 | + elif report_path.exists(): |
| 118 | + report_path.unlink() |
| 119 | + |
| 120 | + failed_bytes = len(failed_text.encode("utf-8")) |
| 121 | + skipped_bytes = len(skipped_text.encode("utf-8")) |
| 122 | + |
| 123 | + print(f"Test reports generated for {test_output}:") |
| 124 | + print(f" Failed tests: {len(failed_blocks)} ({failed_report}, {failed_bytes} bytes)") |
| 125 | + print(f" Skipped tests: {len(skipped_blocks)} ({skipped_report}, {skipped_bytes} bytes)") |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments