Clean up bctest.py and bitcoin-util-test.py

- remove newlines
- change tabs for spaces, to align with convention in other py files
- add comments
- add 'Bitcoin Core Developers' copyright notice
pull/9069/head
John Newbery 8 years ago
parent 6a1343f73b
commit 2b175d4b01

@ -1,4 +1,5 @@
# Copyright 2014 BitPay, Inc.
# Copyright 2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from __future__ import division,print_function,unicode_literals
@ -11,6 +12,9 @@ import difflib
import logging
def parse_output(a, fmt):
"""Parse the output according to specified format.
Raise an error if the output can't be parsed."""
if fmt == 'json': # json: compare parsed data
return json.loads(a)
elif fmt == 'hex': # hex: parse and compare binary data
@ -19,10 +23,17 @@ def parse_output(a, fmt):
raise NotImplementedError("Don't know how to compare %s" % fmt)
def bctest(testDir, testObj, exeext):
"""Runs a single test, comparing output and RC to expected output and RC.
Raises an error if input can't be read, executable fails, or output/RC
are not as expected. Error is caught by bctester() and reported.
"""
# Get the exec names and arguments
execprog = testObj['exec'] + exeext
execargs = testObj['args']
execrun = [execprog] + execargs
# Read the input data (if there is any)
stdinCfg = None
inputData = None
if "input" in testObj:
@ -30,6 +41,7 @@ def bctest(testDir, testObj, exeext):
inputData = open(filename).read()
stdinCfg = subprocess.PIPE
# Read the expected output data (if there is any)
outputFn = None
outputData = None
if "output_cmp" in testObj:
@ -44,6 +56,7 @@ def bctest(testDir, testObj, exeext):
logging.error("Output data missing for " + outputFn)
raise Exception
# Run the test
proc = subprocess.Popen(execrun, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True)
try:
outs = proc.communicate(input=inputData)
@ -52,6 +65,7 @@ def bctest(testDir, testObj, exeext):
raise
if outputData:
# Parse command output and expected output
try:
a_parsed = parse_output(outs[0], outputType)
except Exception as e:
@ -62,9 +76,11 @@ def bctest(testDir, testObj, exeext):
except Exception as e:
logging.error('Error parsing expected output %s as %s: %s' % (outputFn,outputType,e))
raise
# Compare data
if a_parsed != b_parsed:
logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
raise Exception
# Compare formatting
if outs[0] != outputData:
error_message = "Output formatting mismatch for " + outputFn + ":\n"
error_message += "".join(difflib.context_diff(outputData.splitlines(True),
@ -74,6 +90,7 @@ def bctest(testDir, testObj, exeext):
logging.error(error_message)
raise Exception
# Compare the return code to the expected return code
wantRC = 0
if "return_code" in testObj:
wantRC = testObj['return_code']
@ -82,6 +99,7 @@ def bctest(testDir, testObj, exeext):
raise Exception
def bctester(testDir, input_basename, buildenv):
""" Loads and parses the input file, runs all tests and reports results"""
input_filename = testDir + "/" + input_basename
raw_data = open(input_filename).read()
input_data = json.loads(raw_data)
@ -101,4 +119,3 @@ def bctester(testDir, input_basename, buildenv):
sys.exit(1)
else:
sys.exit(0)

@ -1,5 +1,6 @@
#!/usr/bin/env python
# Copyright 2014 BitPay, Inc.
# Copyright 2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from __future__ import division,print_function,unicode_literals
@ -16,11 +17,13 @@ Runs automatically during `make check`.
Can also be run manually from the src directory by specifiying the source directory:
test/bitcoin-util-test.py --src=[srcdir]
test/bitcoin-util-test.py --srcdir='srcdir' [--verbose]
"""
if __name__ == '__main__':
# Try to get the source directory from the environment variables. This will
# be set for `make check` automated runs. If environment variable is not set,
# then get the source directory from command line args.
try:
srcdir = os.environ["srcdir"]
verbose = False

Loading…
Cancel
Save