1+ import abc
12import base64
23import datetime
34import itertools
@@ -14,11 +15,22 @@ def by_suite_and_test_path(test):
1415 return (test .suite .name , id (test .suite ), test .path_in_suite )
1516
1617
17- class JsonReport (object ):
18+ class Report (object ):
1819 def __init__ (self , output_file ):
1920 self .output_file = output_file
2021
2122 def write_results (self , tests , elapsed ):
23+ with open (self .output_file , "w" ) as file :
24+ self ._write_results_to_file (tests , elapsed , file )
25+
26+ @abc .abstractmethod
27+ def _write_results_to_file (self , tests , elapsed , file ):
28+ """Write test results to the file object "file"."""
29+ pass
30+
31+
32+ class JsonReport (Report ):
33+ def _write_results_to_file (self , tests , elapsed , file ):
2234 unexecuted_codes = {lit .Test .EXCLUDED , lit .Test .SKIPPED }
2335 tests = [t for t in tests if t .result .code not in unexecuted_codes ]
2436 # Construct the data we will write.
@@ -67,9 +79,8 @@ def write_results(self, tests, elapsed):
6779
6880 tests_data .append (test_data )
6981
70- with open (self .output_file , "w" ) as file :
71- json .dump (data , file , indent = 2 , sort_keys = True )
72- file .write ("\n " )
82+ json .dump (data , file , indent = 2 , sort_keys = True )
83+ file .write ("\n " )
7384
7485
7586_invalid_xml_chars_dict = {
@@ -88,21 +99,18 @@ def remove_invalid_xml_chars(s):
8899 return s .translate (_invalid_xml_chars_dict )
89100
90101
91- class XunitReport (object ):
92- def __init__ (self , output_file ):
93- self .output_file = output_file
94- self .skipped_codes = {lit .Test .EXCLUDED , lit .Test .SKIPPED , lit .Test .UNSUPPORTED }
102+ class XunitReport (Report ):
103+ skipped_codes = {lit .Test .EXCLUDED , lit .Test .SKIPPED , lit .Test .UNSUPPORTED }
95104
96- def write_results (self , tests , elapsed ):
105+ def _write_results_to_file (self , tests , elapsed , file ):
97106 tests .sort (key = by_suite_and_test_path )
98107 tests_by_suite = itertools .groupby (tests , lambda t : t .suite )
99108
100- with open (self .output_file , "w" ) as file :
101- file .write ('<?xml version="1.0" encoding="UTF-8"?>\n ' )
102- file .write ('<testsuites time="{time:.2f}">\n ' .format (time = elapsed ))
103- for suite , test_iter in tests_by_suite :
104- self ._write_testsuite (file , suite , list (test_iter ))
105- file .write ("</testsuites>\n " )
109+ file .write ('<?xml version="1.0" encoding="UTF-8"?>\n ' )
110+ file .write ('<testsuites time="{time:.2f}">\n ' .format (time = elapsed ))
111+ for suite , test_iter in tests_by_suite :
112+ self ._write_testsuite (file , suite , list (test_iter ))
113+ file .write ("</testsuites>\n " )
106114
107115 def _write_testsuite (self , file , suite , tests ):
108116 skipped = 0
@@ -206,11 +214,8 @@ def gen_resultdb_test_entry(
206214 return test_data
207215
208216
209- class ResultDBReport (object ):
210- def __init__ (self , output_file ):
211- self .output_file = output_file
212-
213- def write_results (self , tests , elapsed ):
217+ class ResultDBReport (Report ):
218+ def _write_results_to_file (self , tests , elapsed , file ):
214219 unexecuted_codes = {lit .Test .EXCLUDED , lit .Test .SKIPPED }
215220 tests = [t for t in tests if t .result .code not in unexecuted_codes ]
216221 data = {}
@@ -249,17 +254,14 @@ def write_results(self, tests, elapsed):
249254 )
250255 )
251256
252- with open (self .output_file , "w" ) as file :
253- json .dump (data , file , indent = 2 , sort_keys = True )
254- file .write ("\n " )
257+ json .dump (data , file , indent = 2 , sort_keys = True )
258+ file .write ("\n " )
255259
256260
257- class TimeTraceReport (object ):
258- def __init__ (self , output_file ):
259- self .output_file = output_file
260- self .skipped_codes = {lit .Test .EXCLUDED , lit .Test .SKIPPED , lit .Test .UNSUPPORTED }
261+ class TimeTraceReport (Report ):
262+ skipped_codes = {lit .Test .EXCLUDED , lit .Test .SKIPPED , lit .Test .UNSUPPORTED }
261263
262- def write_results (self , tests , elapsed ):
264+ def _write_results_to_file (self , tests , elapsed , file ):
263265 # Find when first test started so we can make start times relative.
264266 first_start_time = min ([t .result .start for t in tests ])
265267 events = [
@@ -270,8 +272,7 @@ def write_results(self, tests, elapsed):
270272
271273 json_data = {"traceEvents" : events }
272274
273- with open (self .output_file , "w" ) as time_trace_file :
274- json .dump (json_data , time_trace_file , indent = 2 , sort_keys = True )
275+ json .dump (json_data , time_trace_file , indent = 2 , sort_keys = True )
275276
276277 def _get_test_event (self , test , first_start_time ):
277278 test_name = test .getFullName ()
0 commit comments