Skip to content

Commit 6c5cd9d

Browse files
committed
test: Add format-dependent comparison to bctest
This splits the output comparison for `bitcoin-tx` into two steps: - First, check for data mismatch, parsing the data as json or hex depending on the extension of the output file - Then, check if the literal string matches For either of these cases give a different error. This prevents wild goose chases when e.g. a trailing space doesn't match exactly, and makes sure that both test output and examples are valid data of the purported format.
1 parent 86f9e3d commit 6c5cd9d

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/test/bctest.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
import os
77
import json
88
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)
918

1019
def bctest(testDir, testObj, exeext):
1120

@@ -23,6 +32,7 @@ def bctest(testDir, testObj, exeext):
2332
outputData = None
2433
if "output_cmp" in testObj:
2534
outputFn = testObj['output_cmp']
35+
outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare)
2636
outputData = open(testDir + "/" + outputFn).read()
2737
if not outputData:
2838
print("Output data missing for " + outputFn)
@@ -34,9 +44,23 @@ def bctest(testDir, testObj, exeext):
3444
print("OSError, Failed to execute " + execprog)
3545
sys.exit(1)
3646

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)
4064

4165
wantRC = 0
4266
if "return_code" in testObj:

0 commit comments

Comments
 (0)