|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Compare GNU test results between current run and reference to identify |
| 4 | +regressions and fixes. |
| 5 | +
|
| 6 | +
|
| 7 | +Arguments: |
| 8 | + CURRENT_JSON Path to the current run's aggregated results JSON file |
| 9 | + REFERENCE_JSON Path to the reference (main branch) aggregated |
| 10 | + results JSON file |
| 11 | + --ignore-file Path to file containing list of tests to ignore |
| 12 | + (for intermittent issues) |
| 13 | + --output Path to output file for GitHub comment content |
| 14 | +""" |
| 15 | + |
| 16 | +import argparse |
| 17 | +import json |
| 18 | +import os |
| 19 | +import sys |
| 20 | + |
| 21 | + |
| 22 | +def flatten_test_results(results): |
| 23 | + """Convert nested JSON test results to a flat dictionary of test paths to statuses.""" |
| 24 | + flattened = {} |
| 25 | + for util, tests in results.items(): |
| 26 | + for test_name, status in tests.items(): |
| 27 | + test_path = f"{util}/{test_name}" |
| 28 | + flattened[test_path] = status |
| 29 | + return flattened |
| 30 | + |
| 31 | + |
| 32 | +def load_ignore_list(ignore_file): |
| 33 | + """Load list of tests to ignore from file.""" |
| 34 | + if not os.path.exists(ignore_file): |
| 35 | + return set() |
| 36 | + |
| 37 | + with open(ignore_file, "r") as f: |
| 38 | + return {line.strip() for line in f if line.strip() and not line.startswith("#")} |
| 39 | + |
| 40 | + |
| 41 | +def identify_test_changes(current_flat, reference_flat): |
| 42 | + """ |
| 43 | + Identify different categories of test changes between current and reference results. |
| 44 | +
|
| 45 | + Args: |
| 46 | + current_flat (dict): Flattened dictionary of current test results |
| 47 | + reference_flat (dict): Flattened dictionary of reference test results |
| 48 | +
|
| 49 | + Returns: |
| 50 | + tuple: Four lists containing regressions, fixes, newly_skipped, and newly_passing tests |
| 51 | + """ |
| 52 | + # Find regressions (tests that were passing but now failing) |
| 53 | + regressions = [] |
| 54 | + for test_path, status in current_flat.items(): |
| 55 | + if status in ("FAIL", "ERROR"): |
| 56 | + if test_path in reference_flat: |
| 57 | + if reference_flat[test_path] in ("PASS", "SKIP"): |
| 58 | + |
| 59 | + regressions.append(test_path) |
| 60 | + |
| 61 | + # Find fixes (tests that were failing but now passing) |
| 62 | + fixes = [] |
| 63 | + for test_path, status in reference_flat.items(): |
| 64 | + if status in ("FAIL", "ERROR"): |
| 65 | + if test_path in current_flat: |
| 66 | + if current_flat[test_path] == "PASS": |
| 67 | + fixes.append(test_path) |
| 68 | + |
| 69 | + # Find newly skipped tests (were passing, now skipped) |
| 70 | + newly_skipped = [] |
| 71 | + for test_path, status in current_flat.items(): |
| 72 | + if ( |
| 73 | + status == "SKIP" |
| 74 | + and test_path in reference_flat |
| 75 | + and reference_flat[test_path] == "PASS" |
| 76 | + ): |
| 77 | + newly_skipped.append(test_path) |
| 78 | + |
| 79 | + # Find newly passing tests (were skipped, now passing) |
| 80 | + newly_passing = [] |
| 81 | + for test_path, status in current_flat.items(): |
| 82 | + if ( |
| 83 | + status == "PASS" |
| 84 | + and test_path in reference_flat |
| 85 | + and reference_flat[test_path] == "SKIP" |
| 86 | + ): |
| 87 | + newly_passing.append(test_path) |
| 88 | + |
| 89 | + return regressions, fixes, newly_skipped, newly_passing |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + parser = argparse.ArgumentParser( |
| 94 | + description="Compare GNU test results and identify regressions and fixes" |
| 95 | + ) |
| 96 | + parser.add_argument("current_json", help="Path to current run JSON results") |
| 97 | + parser.add_argument("reference_json", help="Path to reference JSON results") |
| 98 | + parser.add_argument( |
| 99 | + "--ignore-file", |
| 100 | + required=True, |
| 101 | + help="Path to file with tests to ignore (for intermittent issues)", |
| 102 | + ) |
| 103 | + parser.add_argument("--output", help="Path to output file for GitHub comment") |
| 104 | + |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + # Load test results |
| 108 | + try: |
| 109 | + with open(args.current_json, "r") as f: |
| 110 | + current_results = json.load(f) |
| 111 | + except (FileNotFoundError, json.JSONDecodeError) as e: |
| 112 | + sys.stderr.write(f"Error loading current results: {e}\n") |
| 113 | + return 1 |
| 114 | + |
| 115 | + try: |
| 116 | + with open(args.reference_json, "r") as f: |
| 117 | + reference_results = json.load(f) |
| 118 | + except (FileNotFoundError, json.JSONDecodeError) as e: |
| 119 | + sys.stderr.write(f"Error loading reference results: {e}\n") |
| 120 | + sys.stderr.write("Skipping comparison as reference is not available.\n") |
| 121 | + return 0 |
| 122 | + |
| 123 | + # Load ignore list (required) |
| 124 | + if not os.path.exists(args.ignore_file): |
| 125 | + sys.stderr.write(f"Error: Ignore file {args.ignore_file} does not exist\n") |
| 126 | + return 1 |
| 127 | + |
| 128 | + ignore_list = load_ignore_list(args.ignore_file) |
| 129 | + print(f"Loaded {len(ignore_list)} tests to ignore from {args.ignore_file}") |
| 130 | + |
| 131 | + # Flatten result structures for easier comparison |
| 132 | + current_flat = flatten_test_results(current_results) |
| 133 | + reference_flat = flatten_test_results(reference_results) |
| 134 | + |
| 135 | + # Identify different categories of test changes |
| 136 | + regressions, fixes, newly_skipped, newly_passing = identify_test_changes( |
| 137 | + current_flat, reference_flat |
| 138 | + ) |
| 139 | + |
| 140 | + # Filter out intermittent issues from regressions |
| 141 | + real_regressions = [r for r in regressions if r not in ignore_list] |
| 142 | + intermittent_regressions = [r for r in regressions if r in ignore_list] |
| 143 | + |
| 144 | + # Print summary stats |
| 145 | + print(f"Total tests in current run: {len(current_flat)}") |
| 146 | + print(f"Total tests in reference: {len(reference_flat)}") |
| 147 | + print(f"New regressions: {len(real_regressions)}") |
| 148 | + print(f"Intermittent regressions: {len(intermittent_regressions)}") |
| 149 | + print(f"Fixed tests: {len(fixes)}") |
| 150 | + print(f"Newly skipped tests: {len(newly_skipped)}") |
| 151 | + print(f"Newly passing tests (previously skipped): {len(newly_passing)}") |
| 152 | + |
| 153 | + output_lines = [] |
| 154 | + |
| 155 | + # Report regressions |
| 156 | + if real_regressions: |
| 157 | + print("\nREGRESSIONS (non-intermittent failures):", file=sys.stderr) |
| 158 | + for test in sorted(real_regressions): |
| 159 | + msg = f"GNU test failed: {test}. {test} is passing on 'main'. Maybe you have to rebase?" |
| 160 | + print(f"::error ::{msg}", file=sys.stderr) |
| 161 | + output_lines.append(msg) |
| 162 | + |
| 163 | + # Report intermittent issues |
| 164 | + if intermittent_regressions: |
| 165 | + print("\nINTERMITTENT ISSUES (ignored):", file=sys.stderr) |
| 166 | + for test in sorted(intermittent_regressions): |
| 167 | + msg = f"Skip an intermittent issue {test} (fails in this run but passes in the 'main' branch)" |
| 168 | + print(f"::notice ::{msg}", file=sys.stderr) |
| 169 | + output_lines.append(msg) |
| 170 | + |
| 171 | + # Report fixes |
| 172 | + if fixes: |
| 173 | + print("\nFIXED TESTS:", file=sys.stderr) |
| 174 | + for test in sorted(fixes): |
| 175 | + msg = f"Congrats! The gnu test {test} is no longer failing!" |
| 176 | + print(f"::notice ::{msg}", file=sys.stderr) |
| 177 | + output_lines.append(msg) |
| 178 | + |
| 179 | + # Report newly skipped and passing tests |
| 180 | + if newly_skipped: |
| 181 | + print("\nNEWLY SKIPPED TESTS:", file=sys.stderr) |
| 182 | + for test in sorted(newly_skipped): |
| 183 | + msg = f"Note: The gnu test {test} is now being skipped but was previously passing." |
| 184 | + print(f"::warning ::{msg}", file=sys.stderr) |
| 185 | + output_lines.append(msg) |
| 186 | + |
| 187 | + if newly_passing: |
| 188 | + print("\nNEWLY PASSING TESTS (previously skipped):", file=sys.stderr) |
| 189 | + for test in sorted(newly_passing): |
| 190 | + msg = f"Congrats! The gnu test {test} is now passing!" |
| 191 | + print(f"::notice ::{msg}", file=sys.stderr) |
| 192 | + output_lines.append(msg) |
| 193 | + |
| 194 | + if args.output and output_lines: |
| 195 | + with open(args.output, "w") as f: |
| 196 | + for line in output_lines: |
| 197 | + f.write(f"{line}\n") |
| 198 | + |
| 199 | + # Return exit code based on whether we found regressions |
| 200 | + return 1 if real_regressions else 0 |
| 201 | + |
| 202 | + |
| 203 | +if __name__ == "__main__": |
| 204 | + sys.exit(main()) |
0 commit comments