Debian Codegraph @

util/test_compare_test_results.py

raw
1 #!/usr/bin/env python3
2 """
3 Unit tests for the GNU test results comparison script.
4 """
5
6 import json
7 import os
8 import tempfile
9 import unittest
10 from io import StringIO
11 from unittest.mock import patch
12
13 from util.compare_test_results import (
14 flatten_test_results,
15 identify_test_changes,
16 load_ignore_list,
17 main,
18 )
19
20
21 class TestFlattenTestResults(unittest.TestCase):
22 """Tests for the flatten_test_results function."""
23
24 def test_basic_flattening(self):
25 """Test basic flattening of nested test results."""
26 test_data = {
27 "ls": {"test1": "PASS", "test2": "FAIL"},
28 "cp": {"test3": "SKIP", "test4": "ERROR"},
29 }
30 expected = {
31 "tests/ls/test1": "PASS",
32 "tests/ls/test2": "FAIL",
33 "tests/cp/test3": "SKIP",
34 "tests/cp/test4": "ERROR",
35 }
36 self.assertEqual(flatten_test_results(test_data), expected)
37
38 def test_empty_dict(self):
39 """Test flattening an empty dictionary."""
40 self.assertEqual(flatten_test_results({}), {})
41
42 def test_single_util(self):
43 """Test flattening results with a single utility."""
44 test_data = {"ls": {"test1": "PASS", "test2": "FAIL"}}
45 expected = {"tests/ls/test1": "PASS", "tests/ls/test2": "FAIL"}
46 self.assertEqual(flatten_test_results(test_data), expected)
47
48 def test_empty_tests(self):
49 """Test flattening with a utility that has no tests."""
50 test_data = {"ls": {}, "cp": {"test1": "PASS"}}
51 expected = {"tests/cp/test1": "PASS"}
52 self.assertEqual(flatten_test_results(test_data), expected)
53
54 def test_log_extension_removal(self):
55 """Test that .log extensions are removed."""
56 test_data = {"ls": {"test1.log": "PASS", "test2": "FAIL"}}
57 expected = {"tests/ls/test1": "PASS", "tests/ls/test2": "FAIL"}
58 self.assertEqual(flatten_test_results(test_data), expected)
59
60
61 class TestLoadIgnoreList(unittest.TestCase):
62 """Tests for the load_ignore_list function."""
63
64 def test_load_ignores(self):
65 """Test loading ignore list from a file."""
66 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
67 tmp.write(
68 "tests/tail/inotify-dir-recreate\ntests/timeout/timeout\ntests/rm/rm1\n"
69 )
70 tmp_path = tmp.name
71 try:
72 ignore_list = load_ignore_list(tmp_path)
73 self.assertEqual(
74 ignore_list,
75 {
76 "tests/tail/inotify-dir-recreate",
77 "tests/timeout/timeout",
78 "tests/rm/rm1",
79 },
80 )
81 finally:
82 os.unlink(tmp_path)
83
84 def test_empty_file(self):
85 """Test loading an empty ignore file."""
86 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
87 tmp_path = tmp.name
88 try:
89 ignore_list = load_ignore_list(tmp_path)
90 self.assertEqual(ignore_list, set())
91 finally:
92 os.unlink(tmp_path)
93
94 def test_with_comments_and_blanks(self):
95 """Test loading ignore file with comments and blank lines."""
96 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
97 tmp.write(
98 "tests/tail/inotify-dir-recreate\n# A comment\n\ntests/timeout/timeout\n#Indented comment\n tests/rm/rm1 \n"
99 )
100 tmp_path = tmp.name
101 try:
102 ignore_list = load_ignore_list(tmp_path)
103 self.assertEqual(
104 ignore_list,
105 {
106 "tests/tail/inotify-dir-recreate",
107 "tests/timeout/timeout",
108 "tests/rm/rm1",
109 },
110 )
111 finally:
112 os.unlink(tmp_path)
113
114 def test_nonexistent_file(self):
115 """Test behavior with a nonexistent file."""
116 result = load_ignore_list("/nonexistent/file/path")
117 self.assertEqual(result, set())
118
119
120 class TestIdentifyTestChanges(unittest.TestCase):
121 """Tests for the identify_test_changes function."""
122
123 def test_regressions(self):
124 """Test identifying regressions."""
125 current = {
126 "tests/ls/test1": "FAIL",
127 "tests/ls/test2": "ERROR",
128 "tests/cp/test3": "PASS",
129 "tests/cp/test4": "SKIP",
130 }
131 reference = {
132 "tests/ls/test1": "PASS",
133 "tests/ls/test2": "PASS",
134 "tests/cp/test3": "PASS",
135 "tests/cp/test4": "FAIL",
136 }
137 regressions, _, _, _, _ = identify_test_changes(current, reference)
138 self.assertEqual(sorted(regressions), ["tests/ls/test1", "tests/ls/test2"])
139
140 def test_fixes(self):
141 """Test identifying fixes."""
142 current = {
143 "tests/ls/test1": "PASS",
144 "tests/ls/test2": "PASS",
145 "tests/cp/test3": "FAIL",
146 "tests/cp/test4": "SKIP",
147 }
148 reference = {
149 "tests/ls/test1": "FAIL",
150 "tests/ls/test2": "ERROR",
151 "tests/cp/test3": "PASS",
152 "tests/cp/test4": "FAIL",
153 }
154 _, fixes, _, _, _ = identify_test_changes(current, reference)
155 self.assertEqual(sorted(fixes), ["tests/ls/test1", "tests/ls/test2"])
156
157 def test_newly_skipped(self):
158 """Test identifying newly skipped tests."""
159 current = {
160 "tests/ls/test1": "SKIP",
161 "tests/ls/test2": "SKIP",
162 "tests/cp/test3": "PASS",
163 }
164 reference = {
165 "tests/ls/test1": "PASS",
166 "tests/ls/test2": "FAIL",
167 "tests/cp/test3": "PASS",
168 }
169 _, _, newly_skipped, _, _ = identify_test_changes(current, reference)
170 self.assertEqual(newly_skipped, ["tests/ls/test1"])
171
172 def test_newly_passing(self):
173 """Test identifying newly passing tests."""
174 current = {
175 "tests/ls/test1": "PASS",
176 "tests/ls/test2": "PASS",
177 "tests/cp/test3": "SKIP",
178 }
179 reference = {
180 "tests/ls/test1": "SKIP",
181 "tests/ls/test2": "FAIL",
182 "tests/cp/test3": "SKIP",
183 }
184 _, _, _, newly_passing, _ = identify_test_changes(current, reference)
185 self.assertEqual(newly_passing, ["tests/ls/test1"])
186
187 def test_all_categories(self):
188 """Test identifying all categories of changes simultaneously."""
189 current = {
190 "tests/ls/test1": "FAIL", # Regression
191 "tests/ls/test2": "PASS", # Fix
192 "tests/cp/test3": "SKIP", # Newly skipped
193 "tests/cp/test4": "PASS", # Newly passing
194 "tests/rm/test5": "PASS", # No change
195 "tests/rm/test6": "FAIL", # Newly failing
196 }
197 reference = {
198 "tests/ls/test1": "PASS", # Regression
199 "tests/ls/test2": "FAIL", # Fix
200 "tests/cp/test3": "PASS", # Newly skipped
201 "tests/cp/test4": "SKIP", # Newly passing
202 "tests/rm/test5": "PASS", # No change
203 "tests/rm/test6": "SKIP", # Newly failing
204 }
205 regressions, fixes, newly_skipped, newly_passing, newly_failing = (
206 identify_test_changes(current, reference)
207 )
208 self.assertEqual(regressions, ["tests/ls/test1"])
209 self.assertEqual(fixes, ["tests/ls/test2"])
210 self.assertEqual(newly_skipped, ["tests/cp/test3"])
211 self.assertEqual(newly_passing, ["tests/cp/test4"])
212 self.assertEqual(newly_failing, ["tests/rm/test6"])
213
214 def test_new_and_removed_tests(self):
215 """Test handling of tests that are only in one of the datasets."""
216 current = {
217 "tests/ls/test1": "PASS",
218 "tests/ls/test2": "FAIL",
219 "tests/cp/new_test": "PASS",
220 }
221 reference = {
222 "tests/ls/test1": "PASS",
223 "tests/ls/test2": "PASS",
224 "tests/rm/old_test": "FAIL",
225 }
226 regressions, fixes, newly_skipped, newly_passing, newly_failing = (
227 identify_test_changes(current, reference)
228 )
229 self.assertEqual(regressions, ["tests/ls/test2"])
230 self.assertEqual(fixes, [])
231 self.assertEqual(newly_skipped, [])
232 self.assertEqual(newly_passing, [])
233 self.assertEqual(newly_failing, [])
234
235 def test_newly_failing(self):
236 """Test identifying newly failing tests (SKIP -> FAIL)."""
237 current = {
238 "tests/ls/test1": "FAIL",
239 "tests/ls/test2": "ERROR",
240 "tests/cp/test3": "PASS",
241 }
242 reference = {
243 "tests/ls/test1": "SKIP",
244 "tests/ls/test2": "SKIP",
245 "tests/cp/test3": "SKIP",
246 }
247 _, _, _, _, newly_failing = identify_test_changes(current, reference)
248 self.assertEqual(sorted(newly_failing), ["tests/ls/test1", "tests/ls/test2"])
249
250 def test_skip_to_fail_not_regression(self):
251 """Test that SKIP -> FAIL is not counted as a regression."""
252 current = {
253 "tests/ls/test1": "FAIL",
254 "tests/ls/test2": "FAIL",
255 }
256 reference = {
257 "tests/ls/test1": "SKIP",
258 "tests/ls/test2": "PASS",
259 }
260 regressions, _, _, _, newly_failing = identify_test_changes(current, reference)
261 self.assertEqual(regressions, ["tests/ls/test2"])
262 self.assertEqual(newly_failing, ["tests/ls/test1"])
263
264
265 class TestMainFunction(unittest.TestCase):
266 """Integration tests for the main function."""
267
268 def setUp(self):
269 """Set up test files needed for main function tests."""
270 self.current_data = {
271 "ls": {
272 "test1": "PASS",
273 "test2": "FAIL",
274 "test3": "PASS",
275 "test4": "SKIP",
276 "test5": "PASS",
277 },
278 "cp": {"test1": "PASS", "test2": "PASS"},
279 }
280 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
281 json.dump(self.current_data, tmp)
282 self.current_json = tmp.name
283
284 self.reference_data = {
285 "ls": {
286 "test1": "PASS",
287 "test2": "PASS",
288 "test3": "FAIL",
289 "test4": "PASS",
290 "test5": "SKIP",
291 },
292 "cp": {"test1": "FAIL", "test2": "ERROR"},
293 }
294 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
295 json.dump(self.reference_data, tmp)
296 self.reference_json = tmp.name
297
298 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
299 tmp.write("tests/ls/test2\ntests/cp/test1\n")
300 self.ignore_file = tmp.name
301
302 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
303 self.output_file = tmp.name
304
305 def tearDown(self):
306 """Clean up test files after tests."""
307 for file_path in [
308 self.current_json,
309 self.reference_json,
310 self.ignore_file,
311 self.output_file,
312 ]:
313 if os.path.exists(file_path):
314 os.unlink(file_path)
315
316 def test_main_exit_code_with_real_regressions(self):
317 """Test main function exit code with real regressions."""
318
319 current_flat = flatten_test_results(self.current_data)
320 reference_flat = flatten_test_results(self.reference_data)
321
322 regressions, _, _, _, _ = identify_test_changes(current_flat, reference_flat)
323
324 self.assertIn("tests/ls/test2", regressions)
325
326 ignore_list = load_ignore_list(self.ignore_file)
327
328 real_regressions = [r for r in regressions if r not in ignore_list]
329
330 self.assertNotIn("tests/ls/test2", real_regressions)
331
332 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
333 tmp.write(
334 "tests/cp/test1\n"
335 ) # only ignore tests/cp/test1, not tests/ls/test2
336 new_ignore_file = tmp.name
337
338 try:
339 new_ignore_list = load_ignore_list(new_ignore_file)
340
341 new_real_regressions = [r for r in regressions if r not in new_ignore_list]
342
343 # tests/ls/test2 should now be in real_regressions
344 self.assertIn("tests/ls/test2", new_real_regressions)
345
346 # In main(), this would cause a non-zero exit code
347 would_exit_with_error = len(new_real_regressions) > 0
348 self.assertTrue(would_exit_with_error)
349 finally:
350 os.unlink(new_ignore_file)
351
352 def test_filter_intermittent_fixes(self):
353 """Test that fixes in the ignore list are filtered properly."""
354 current_flat = flatten_test_results(self.current_data)
355 reference_flat = flatten_test_results(self.reference_data)
356
357 _, fixes, _, _, _ = identify_test_changes(current_flat, reference_flat)
358
359 # tests/cp/test1 and tests/cp/test2 should be fixed but tests/cp/test1 is in ignore list
360 self.assertIn("tests/cp/test1", fixes)
361 self.assertIn("tests/cp/test2", fixes)
362
363 ignore_list = load_ignore_list(self.ignore_file)
364 real_fixes = [f for f in fixes if f not in ignore_list]
365 intermittent_fixes = [f for f in fixes if f in ignore_list]
366
367 # tests/cp/test1 should be identified as intermittent
368 self.assertIn("tests/cp/test1", intermittent_fixes)
369 # tests/cp/test2 should be identified as a real fix
370 self.assertIn("tests/cp/test2", real_fixes)
371
372
373 class TestOutputFunctionality(unittest.TestCase):
374 """Tests focused on the output generation of the script."""
375
376 def setUp(self):
377 """Set up test files needed for output tests."""
378 self.current_data = {
379 "ls": {
380 "test1": "PASS",
381 "test2": "FAIL", # Regression but in ignore list
382 "test3": "PASS", # Fix
383 },
384 "cp": {
385 "test1": "PASS", # Fix but in ignore list
386 "test2": "SKIP", # Newly skipped
387 "test4": "PASS", # Newly passing
388 },
389 }
390 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
391 json.dump(self.current_data, tmp)
392 self.current_json = tmp.name
393
394 self.reference_data = {
395 "ls": {
396 "test1": "PASS", # No change
397 "test2": "PASS", # Regression but in ignore list
398 "test3": "FAIL", # Fix
399 },
400 "cp": {
401 "test1": "FAIL", # Fix but in ignore list
402 "test2": "PASS", # Newly skipped
403 "test4": "SKIP", # Newly passing
404 },
405 }
406 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
407 json.dump(self.reference_data, tmp)
408 self.reference_json = tmp.name
409
410 # Create an ignore file
411 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
412 tmp.write("tests/ls/test2\ntests/cp/test1\n")
413 self.ignore_file = tmp.name
414
415 def tearDown(self):
416 """Clean up test files after tests."""
417 for file_path in [self.current_json, self.reference_json, self.ignore_file]:
418 if os.path.exists(file_path):
419 os.unlink(file_path)
420
421 if hasattr(self, "output_file") and os.path.exists(self.output_file):
422 os.unlink(self.output_file)
423
424 @patch("sys.stdout", new_callable=StringIO)
425 @patch("sys.stderr", new_callable=StringIO)
426 def test_console_output_formatting(self, mock_stderr, mock_stdout):
427 """Test the formatting of console output."""
428 with patch(
429 "sys.argv",
430 [
431 "compare_test_results.py",
432 self.current_json,
433 self.reference_json,
434 "--ignore-file",
435 self.ignore_file,
436 ],
437 ):
438 try:
439 main()
440 except SystemExit:
441 pass # Expected to exit with a status code
442
443 stdout_content = mock_stdout.getvalue()
444 self.assertIn("Total tests in current run:", stdout_content)
445 self.assertIn("New regressions: 0", stdout_content)
446 self.assertIn("Intermittent regressions: 1", stdout_content)
447 self.assertIn("Fixed tests: 1", stdout_content)
448 self.assertIn("Intermittent fixes: 1", stdout_content)
449 self.assertIn("Newly skipped tests: 1", stdout_content)
450 self.assertIn("Newly passing tests (previously skipped): 1", stdout_content)
451
452 stderr_content = mock_stderr.getvalue()
453 self.assertIn("INTERMITTENT ISSUES (ignored regressions):", stderr_content)
454 self.assertIn("Skip an intermittent issue tests/ls/test2", stderr_content)
455 self.assertIn("INTERMITTENT ISSUES (ignored fixes):", stderr_content)
456 self.assertIn("Skipping an intermittent issue tests/cp/test1", stderr_content)
457 self.assertIn("FIXED TESTS:", stderr_content)
458 self.assertIn(
459 "Congrats! The gnu test tests/ls/test3 is no longer failing!",
460 stderr_content,
461 )
462 self.assertIn("NEWLY SKIPPED TESTS:", stderr_content)
463 self.assertIn("Note: The gnu test tests/cp/test2", stderr_content)
464 self.assertIn("NEWLY PASSING TESTS (previously skipped):", stderr_content)
465 self.assertIn(
466 "Congrats! The gnu test tests/cp/test4 is now passing!", stderr_content
467 )
468
469 def test_file_output_generation(self):
470 """Test that the output file is generated correctly."""
471 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
472 self.output_file = tmp.name
473
474 with patch(
475 "sys.argv",
476 [
477 "compare_test_results.py",
478 self.current_json,
479 self.reference_json,
480 "--ignore-file",
481 self.ignore_file,
482 "--output",
483 self.output_file,
484 ],
485 ):
486 try:
487 main()
488 except SystemExit:
489 pass # Expected to exit with a status code
490
491 self.assertTrue(os.path.exists(self.output_file))
492
493 with open(self.output_file, "r") as f:
494 output_content = f.read()
495
496 self.assertIn("Skip an intermittent issue tests/ls/test2", output_content)
497 self.assertIn("Skipping an intermittent issue tests/cp/test1", output_content)
498 self.assertIn(
499 "Congrats! The gnu test tests/ls/test3 is no longer failing!",
500 output_content,
501 )
502 self.assertIn("Note: The gnu test tests/cp/test2", output_content)
503 self.assertIn(
504 "Congrats! The gnu test tests/cp/test4 is now passing!", output_content
505 )
506
507 def test_exit_code_with_no_regressions(self):
508 """Test that the script exits with code 0 when there are no regressions."""
509 with patch(
510 "sys.argv",
511 [
512 "compare_test_results.py",
513 self.current_json,
514 self.reference_json,
515 "--ignore-file",
516 self.ignore_file,
517 ],
518 ):
519 # Instead of assertRaises, just call main() and check its return value
520 exit_code = main()
521 # Since all regressions are in the ignore list, should exit with 0
522 self.assertEqual(exit_code, 0)
523
524 def test_exit_code_with_regressions(self):
525 """Test that the script exits with code 1 when there are real regressions."""
526 with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
527 tmp.write("tests/cp/test1\n") # Only ignore cp/test1
528 new_ignore_file = tmp.name
529
530 try:
531 with patch(
532 "sys.argv",
533 [
534 "compare_test_results.py",
535 self.current_json,
536 self.reference_json,
537 "--ignore-file",
538 new_ignore_file,
539 ],
540 ):
541 # Just call main() and check its return value
542 exit_code = main()
543 # Since ls/test2 is now a real regression, should exit with 1
544 self.assertEqual(exit_code, 1)
545 finally:
546 os.unlink(new_ignore_file)
547
548 def test_github_actions_formatting(self):
549 """Test that the output is formatted for GitHub Actions."""
550 with patch("sys.stderr", new_callable=StringIO) as mock_stderr:
551 with patch(
552 "sys.argv",
553 [
554 "compare_test_results.py",
555 self.current_json,
556 self.reference_json,
557 "--ignore-file",
558 self.ignore_file,
559 ],
560 ):
561 try:
562 main()
563 except SystemExit:
564 pass # Expected to exit with a status code
565
566 stderr_content = mock_stderr.getvalue()
567
568 self.assertIn(
569 "::notice ::", stderr_content
570 ) # For fixes and informational messages
571 self.assertIn("::warning ::", stderr_content) # For newly skipped tests
572
573
574 if __name__ == "__main__":
575 unittest.main()