1+ """ Checks if the expected test results are present in the output JSON file.
2+ usage: python test/validate-test-results.py \
3+ --results <results.json> \
4+ --problem <problem_name> \
5+ --expected-write <expected_write_count> \
6+ --expected-source-valid <expected_source_valid_count> \
7+ --expected-build <expected_build_count> \
8+ --expected-run <expected_run_count> \
9+ --expected-correct <expected_correct_count>
10+ """
11+ from argparse import ArgumentParser
12+ import json
13+ from collections import Counter
14+
15+
16+ def parse_args ():
17+ parser = ArgumentParser (description = "Validate test results." )
18+ parser .add_argument (
19+ "--results" ,
20+ type = str ,
21+ required = True ,
22+ help = "Path to the results JSON file." ,
23+ )
24+ parser .add_argument (
25+ "--problem" ,
26+ type = str ,
27+ required = True ,
28+ help = "Name of the problem to validate." ,
29+ )
30+ parser .add_argument (
31+ "--expected-write" ,
32+ type = int ,
33+ required = True ,
34+ help = "Expected number of write operations." ,
35+ )
36+ parser .add_argument (
37+ "--expected-source-valid" ,
38+ type = int ,
39+ required = True ,
40+ help = "Expected number of source valid operations." ,
41+ )
42+ parser .add_argument (
43+ "--expected-build" ,
44+ type = int ,
45+ required = True ,
46+ help = "Expected number of build operations." ,
47+ )
48+ parser .add_argument (
49+ "--expected-run" ,
50+ type = int ,
51+ required = True ,
52+ help = "Expected number of run operations." ,
53+ )
54+ parser .add_argument (
55+ "--expected-correct" ,
56+ type = int ,
57+ required = True ,
58+ help = "Expected number of correct operations." ,
59+ )
60+
61+ return parser .parse_args ()
62+
63+
64+ def validate_outputs (outputs , expected_counts ):
65+ actual_counts = Counter ()
66+
67+ for output in outputs :
68+ if output .get ("source_write_success" , False ):
69+ actual_counts ["write" ] += 1
70+ if output .get ("is_source_valid" , False ):
71+ actual_counts ["source_valid" ] += 1
72+ if output .get ("did_build" , False ):
73+ actual_counts ["build" ] += 1
74+ if output .get ("did_all_run" , False ):
75+ actual_counts ["run" ] += 1
76+ if output .get ("are_all_valid" , False ):
77+ actual_counts ["correct" ] += 1
78+
79+ for key , expected in expected_counts .items ():
80+ actual = actual_counts [key ]
81+ if actual != expected :
82+ print (f"Expected { expected } for { key } , but got { actual } ." )
83+ return False
84+ return True
85+
86+
87+ def main ():
88+ args = parse_args ()
89+
90+ # Load the results JSON file
91+ with open (args .results , "r" ) as f :
92+ results = json .load (f )
93+
94+ # Validate the results
95+ expected_counts = {
96+ "write" : args .expected_write ,
97+ "source_valid" : args .expected_source_valid ,
98+ "build" : args .expected_build ,
99+ "run" : args .expected_run ,
100+ "correct" : args .expected_correct ,
101+ }
102+
103+ results = [r for r in results if r ["name" ] == args .problem ][0 ]
104+
105+ if not validate_outputs (results ["outputs" ], expected_counts ):
106+ print (f"Validation failed for problem { args .problem } ." )
107+ return 1
108+
109+
110+ if __name__ == "__main__" :
111+ main ()
112+
0 commit comments