7
7
import json
8
8
import sys
9
9
import binascii
10
+ import difflib
11
+ import logging
10
12
11
13
def parse_output (a , fmt ):
12
14
if fmt == 'json' : # json: compare parsed data
@@ -33,53 +35,70 @@ def bctest(testDir, testObj, exeext):
33
35
if "output_cmp" in testObj :
34
36
outputFn = testObj ['output_cmp' ]
35
37
outputType = os .path .splitext (outputFn )[1 ][1 :] # output type from file extension (determines how to compare)
36
- outputData = open (testDir + "/" + outputFn ).read ()
38
+ try :
39
+ outputData = open (testDir + "/" + outputFn ).read ()
40
+ except :
41
+ logging .error ("Output file " + outputFn + " can not be opened" )
42
+ raise
37
43
if not outputData :
38
- print ("Output data missing for " + outputFn )
39
- sys .exit (1 )
44
+ logging .error ("Output data missing for " + outputFn )
45
+ raise Exception
46
+
40
47
proc = subprocess .Popen (execrun , stdin = stdinCfg , stdout = subprocess .PIPE , stderr = subprocess .PIPE ,universal_newlines = True )
41
48
try :
42
49
outs = proc .communicate (input = inputData )
43
50
except OSError :
44
- print ("OSError, Failed to execute " + execprog )
45
- sys . exit ( 1 )
51
+ logging . error ("OSError, Failed to execute " + execprog )
52
+ raise
46
53
47
54
if outputData :
48
55
try :
49
56
a_parsed = parse_output (outs [0 ], outputType )
50
57
except Exception as e :
51
- print ('Error parsing command output as %s: %s' % (outputType ,e ))
52
- sys . exit ( 1 )
58
+ logging . error ('Error parsing command output as %s: %s' % (outputType ,e ))
59
+ raise
53
60
try :
54
61
b_parsed = parse_output (outputData , outputType )
55
62
except Exception as e :
56
- print ('Error parsing expected output %s as %s: %s' % (outputFn ,outputType ,e ))
57
- sys . exit ( 1 )
63
+ logging . error ('Error parsing expected output %s as %s: %s' % (outputFn ,outputType ,e ))
64
+ raise
58
65
if a_parsed != b_parsed :
59
- print ("Output data mismatch for " + outputFn + " (format " + outputType + ")" )
60
- sys . exit ( 1 )
66
+ logging . error ("Output data mismatch for " + outputFn + " (format " + outputType + ")" )
67
+ raise Exception
61
68
if outs [0 ] != outputData :
62
- print ("Output formatting mismatch for " + outputFn + " (format " + outputType + ")" )
63
- sys .exit (1 )
69
+ error_message = "Output formatting mismatch for " + outputFn + ":\n "
70
+ error_message += "" .join (difflib .context_diff (outputData .splitlines (True ),
71
+ outs [0 ].splitlines (True ),
72
+ fromfile = outputFn ,
73
+ tofile = "returned" ))
74
+ logging .error (error_message )
75
+ raise Exception
64
76
65
77
wantRC = 0
66
78
if "return_code" in testObj :
67
79
wantRC = testObj ['return_code' ]
68
80
if proc .returncode != wantRC :
69
- print ("Return code mismatch for " + outputFn )
70
- sys . exit ( 1 )
81
+ logging . error ("Return code mismatch for " + outputFn )
82
+ raise Exception
71
83
72
- def bctester (testDir , input_basename , buildenv , verbose = False ):
84
+ def bctester (testDir , input_basename , buildenv ):
73
85
input_filename = testDir + "/" + input_basename
74
86
raw_data = open (input_filename ).read ()
75
87
input_data = json .loads (raw_data )
76
88
89
+ failed_testcases = []
90
+
77
91
for testObj in input_data :
78
- if verbose and "description" in testObj :
79
- print ("Testing: " + testObj ["description" ])
80
- bctest (testDir , testObj , buildenv .exeext )
81
- if verbose and "description" in testObj :
82
- print ("PASS" )
92
+ try :
93
+ bctest (testDir , testObj , buildenv .exeext )
94
+ logging .info ("PASSED: " + testObj ["description" ])
95
+ except :
96
+ logging .info ("FAILED: " + testObj ["description" ])
97
+ failed_testcases .append (testObj ["description" ])
83
98
84
- sys .exit (0 )
99
+ if failed_testcases :
100
+ logging .error ("FAILED TESTCASES: [" + ", " .join (failed_testcases ) + "]" )
101
+ sys .exit (1 )
102
+ else :
103
+ sys .exit (0 )
85
104
0 commit comments