diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-10-27 14:05:59 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-10-28 14:20:59 +0200 |
commit | 6c5cd9d022347abed8b61ca4ae4dc101461582fe (patch) | |
tree | ccee0f9fbc278d7e0e9522b381f9f1ab456cce05 | |
parent | 86f9e3dbba416a142df47dbd646c8e8ba772c955 (diff) |
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.
-rw-r--r-- | src/test/bctest.py | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/test/bctest.py b/src/test/bctest.py index d801415c70..eab4fb734a 100644 --- a/src/test/bctest.py +++ b/src/test/bctest.py @@ -6,6 +6,15 @@ import subprocess import os import json import sys +import binascii + +def parse_output(a, fmt): + if fmt == 'json': # json: compare parsed data + return json.loads(a) + elif fmt == 'hex': # hex: parse and compare binary data + return binascii.a2b_hex(a.strip()) + else: + raise NotImplementedError("Don't know how to compare %s" % fmt) def bctest(testDir, testObj, exeext): @@ -23,6 +32,7 @@ def bctest(testDir, testObj, exeext): outputData = None if "output_cmp" in testObj: outputFn = testObj['output_cmp'] + outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare) outputData = open(testDir + "/" + outputFn).read() if not outputData: print("Output data missing for " + outputFn) @@ -34,9 +44,23 @@ def bctest(testDir, testObj, exeext): print("OSError, Failed to execute " + execprog) sys.exit(1) - if outputData and (outs[0] != outputData): - print("Output data mismatch for " + outputFn) - sys.exit(1) + if outputData: + try: + a_parsed = parse_output(outs[0], outputType) + except Exception as e: + print('Error parsing command output as %s: %s' % (outputType,e)) + sys.exit(1) + try: + b_parsed = parse_output(outputData, outputType) + except Exception as e: + print('Error parsing expected output %s as %s: %s' % (outputFn,outputType,e)) + sys.exit(1) + if a_parsed != b_parsed: + print("Output data mismatch for " + outputFn + " (format " + outputType + ")") + sys.exit(1) + if outs[0] != outputData: + print("Output formatting mismatch for " + outputFn + " (format " + outputType + ")") + sys.exit(1) wantRC = 0 if "return_code" in testObj: |