6
6
import os
7
7
import json
8
8
import sys
9
+ import binascii
10
+
11
+ def parse_output (a , fmt ):
12
+ if fmt == 'json' : # json: compare parsed data
13
+ return json .loads (a )
14
+ elif fmt == 'hex' : # hex: parse and compare binary data
15
+ return binascii .a2b_hex (a .strip ())
16
+ else :
17
+ raise NotImplementedError ("Don't know how to compare %s" % fmt )
9
18
10
19
def bctest (testDir , testObj , exeext ):
11
20
@@ -23,6 +32,7 @@ def bctest(testDir, testObj, exeext):
23
32
outputData = None
24
33
if "output_cmp" in testObj :
25
34
outputFn = testObj ['output_cmp' ]
35
+ outputType = os .path .splitext (outputFn )[1 ][1 :] # output type from file extension (determines how to compare)
26
36
outputData = open (testDir + "/" + outputFn ).read ()
27
37
if not outputData :
28
38
print ("Output data missing for " + outputFn )
@@ -34,9 +44,23 @@ def bctest(testDir, testObj, exeext):
34
44
print ("OSError, Failed to execute " + execprog )
35
45
sys .exit (1 )
36
46
37
- if outputData and (outs [0 ] != outputData ):
38
- print ("Output data mismatch for " + outputFn )
39
- sys .exit (1 )
47
+ if outputData :
48
+ try :
49
+ a_parsed = parse_output (outs [0 ], outputType )
50
+ except Exception as e :
51
+ print ('Error parsing command output as %s: %s' % (outputType ,e ))
52
+ sys .exit (1 )
53
+ try :
54
+ b_parsed = parse_output (outputData , outputType )
55
+ except Exception as e :
56
+ print ('Error parsing expected output %s as %s: %s' % (outputFn ,outputType ,e ))
57
+ sys .exit (1 )
58
+ if a_parsed != b_parsed :
59
+ print ("Output data mismatch for " + outputFn + " (format " + outputType + ")" )
60
+ sys .exit (1 )
61
+ if outs [0 ] != outputData :
62
+ print ("Output formatting mismatch for " + outputFn + " (format " + outputType + ")" )
63
+ sys .exit (1 )
40
64
41
65
wantRC = 0
42
66
if "return_code" in testObj :
0 commit comments