Debian Codegraph @

util/compare_test_results.py

raw
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 # Build the full test path
28 test_path = f"tests/{util}/{test_name}"
29 # Remove the .log extension
30 test_path = test_path.replace(".log", "")
31 flattened[test_path] = status
32 return flattened
33
34
35 def load_ignore_list(ignore_file):
36 """Load list of tests to ignore from file."""
37 if not os.path.exists(ignore_file):
38 return set()
39
40 with open(ignore_file, "r") as f:
41 return {line.strip() for line in f if line.strip() and not line.startswith("#")}
42
43
44 def identify_test_changes(current_flat, reference_flat):
45 """
46 Identify different categories of test changes between current and reference results.
47
48 Args:
49 current_flat (dict): Flattened dictionary of current test results
50 reference_flat (dict): Flattened dictionary of reference test results
51
52 Returns:
53 tuple: Five lists containing regressions, fixes, newly_skipped, newly_passing, and newly_failing tests
54 """
55 # Find regressions (tests that were passing but now failing)
56 regressions = []
57 for test_path, status in current_flat.items():
58 if (
59 status in ("FAIL", "ERROR")
60 and test_path in reference_flat
61 and reference_flat[test_path] == "PASS"
62 ):
63 regressions.append(test_path)
64
65 # Find fixes (tests that were failing but now passing)
66 fixes = []
67 for test_path, status in reference_flat.items():
68 if (
69 status in ("FAIL", "ERROR")
70 and test_path in current_flat
71 and current_flat[test_path] == "PASS"
72 ):
73 fixes.append(test_path)
74
75 # Find newly skipped tests (were passing, now skipped)
76 newly_skipped = []
77 for test_path, status in current_flat.items():
78 if (
79 status == "SKIP"
80 and test_path in reference_flat
81 and reference_flat[test_path] == "PASS"
82 ):
83 newly_skipped.append(test_path)
84
85 # Find newly passing tests (were skipped, now passing)
86 newly_passing = []
87 for test_path, status in current_flat.items():
88 if (
89 status == "PASS"
90 and test_path in reference_flat
91 and reference_flat[test_path] == "SKIP"
92 ):
93 newly_passing.append(test_path)
94
95 # Find newly failing tests (were skipped, now failing)
96 newly_failing = []
97 for test_path, status in current_flat.items():
98 if (
99 status in ("FAIL", "ERROR")
100 and test_path in reference_flat
101 and reference_flat[test_path] == "SKIP"
102 ):
103 newly_failing.append(test_path)
104
105 return regressions, fixes, newly_skipped, newly_passing, newly_failing
106
107
108 def main():
109 parser = argparse.ArgumentParser(
110 description="Compare GNU test results and identify regressions and fixes"
111 )
112 parser.add_argument("current_json", help="Path to current run JSON results")
113 parser.add_argument("reference_json", help="Path to reference JSON results")
114 parser.add_argument(
115 "--ignore-file",
116 required=True,
117 help="Path to file with tests to ignore (for intermittent issues)",
118 )
119 parser.add_argument("--output", help="Path to output file for GitHub comment")
120
121 args = parser.parse_args()
122
123 # Load test results
124 try:
125 with open(args.current_json, "r") as f:
126 current_results = json.load(f)
127 except (FileNotFoundError, json.JSONDecodeError) as e:
128 sys.stderr.write(f"Error loading current results: {e}\n")
129 return 1
130
131 try:
132 with open(args.reference_json, "r") as f:
133 reference_results = json.load(f)
134 except (FileNotFoundError, json.JSONDecodeError) as e:
135 sys.stderr.write(f"Error loading reference results: {e}\n")
136 sys.stderr.write("Skipping comparison as reference is not available.\n")
137 return 0
138
139 # Load ignore list (required)
140 if not os.path.exists(args.ignore_file):
141 sys.stderr.write(f"Error: Ignore file {args.ignore_file} does not exist\n")
142 return 1
143
144 ignore_list = load_ignore_list(args.ignore_file)
145 print(f"Loaded {len(ignore_list)} tests to ignore from {args.ignore_file}")
146
147 # Flatten result structures for easier comparison
148 current_flat = flatten_test_results(current_results)
149 reference_flat = flatten_test_results(reference_results)
150
151 # Identify different categories of test changes
152 regressions, fixes, newly_skipped, newly_passing, newly_failing = (
153 identify_test_changes(current_flat, reference_flat)
154 )
155
156 # Filter out intermittent issues from regressions
157 real_regressions = [r for r in regressions if r not in ignore_list]
158 intermittent_regressions = [r for r in regressions if r in ignore_list]
159
160 # Filter out intermittent issues from fixes
161 real_fixes = [f for f in fixes if f not in ignore_list]
162 intermittent_fixes = [f for f in fixes if f in ignore_list]
163
164 # Filter out intermittent issues from newly failing
165 real_newly_failing = [n for n in newly_failing if n not in ignore_list]
166 intermittent_newly_failing = [n for n in newly_failing if n in ignore_list]
167
168 # Print summary stats
169 print(f"Total tests in current run: {len(current_flat)}")
170 print(f"Total tests in reference: {len(reference_flat)}")
171 print(f"New regressions: {len(real_regressions)}")
172 print(f"Intermittent regressions: {len(intermittent_regressions)}")
173 print(f"Fixed tests: {len(real_fixes)}")
174 print(f"Intermittent fixes: {len(intermittent_fixes)}")
175 print(f"Newly skipped tests: {len(newly_skipped)}")
176 print(f"Newly passing tests (previously skipped): {len(newly_passing)}")
177 print(f"Newly failing tests (previously skipped): {len(real_newly_failing)}")
178 print(f"Intermittent newly failing: {len(intermittent_newly_failing)}")
179
180 output_lines = []
181
182 # Report regressions
183 if real_regressions:
184 print("\nREGRESSIONS (non-intermittent failures):", file=sys.stderr)
185 for test in sorted(real_regressions):
186 msg = f"GNU test failed: {test}. {test} is passing on 'main'. Maybe you have to rebase?"
187 print(f"::error ::{msg}", file=sys.stderr)
188 output_lines.append(msg)
189
190 # Report intermittent issues (regressions)
191 if intermittent_regressions:
192 print("\nINTERMITTENT ISSUES (ignored regressions):", file=sys.stderr)
193 for test in sorted(intermittent_regressions):
194 msg = f"Skip an intermittent issue {test} (fails in this run but passes in the 'main' branch)"
195 print(f"::notice ::{msg}", file=sys.stderr)
196 output_lines.append(msg)
197
198 # Report intermittent issues (fixes)
199 if intermittent_fixes:
200 print("\nINTERMITTENT ISSUES (ignored fixes):", file=sys.stderr)
201 for test in sorted(intermittent_fixes):
202 msg = f"Skipping an intermittent issue {test} (passes in this run but fails in the 'main' branch)"
203 print(f"::notice ::{msg}", file=sys.stderr)
204 output_lines.append(msg)
205
206 # Report fixes
207 if real_fixes:
208 print("\nFIXED TESTS:", file=sys.stderr)
209 for test in sorted(real_fixes):
210 msg = f"Congrats! The gnu test {test} is no longer failing!"
211 print(f"::notice ::{msg}", file=sys.stderr)
212 output_lines.append(msg)
213
214 # Report newly skipped and passing tests
215 if newly_skipped:
216 print("\nNEWLY SKIPPED TESTS:", file=sys.stderr)
217 for test in sorted(newly_skipped):
218 msg = f"Note: The gnu test {test} is now being skipped but was previously passing."
219 print(f"::warning ::{msg}", file=sys.stderr)
220 output_lines.append(msg)
221
222 if newly_passing:
223 print("\nNEWLY PASSING TESTS (previously skipped):", file=sys.stderr)
224 for test in sorted(newly_passing):
225 msg = f"Congrats! The gnu test {test} is now passing!"
226 print(f"::notice ::{msg}", file=sys.stderr)
227 output_lines.append(msg)
228
229 # Report newly failing tests (were skipped, now failing)
230 if real_newly_failing:
231 print("\nNEWLY FAILING TESTS (previously skipped):", file=sys.stderr)
232 for test in sorted(real_newly_failing):
233 msg = f"Note: The gnu test {test} was skipped on 'main' but is now failing."
234 print(f"::warning ::{msg}", file=sys.stderr)
235 output_lines.append(msg)
236
237 if intermittent_newly_failing:
238 print("\nINTERMITTENT NEWLY FAILING (ignored):", file=sys.stderr)
239 for test in sorted(intermittent_newly_failing):
240 msg = f"Skip an intermittent issue {test} (was skipped on 'main', now failing)"
241 print(f"::notice ::{msg}", file=sys.stderr)
242 output_lines.append(msg)
243
244 if args.output and output_lines:
245 with open(args.output, "w") as f:
246 f.writelines(f"{line}\n" for line in output_lines)
247
248 # Return exit code based on whether we found regressions
249 return 1 if real_regressions else 0
250
251
252 if __name__ == "__main__":
253 sys.exit(main())