Skip to content

Commit 2b175d4

Browse files
committed
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
1 parent 6a1343f commit 2b175d4

File tree

2 files changed

+101
-81
lines changed

2 files changed

+101
-81
lines changed

src/test/bctest.py

Lines changed: 96 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright 2014 BitPay, Inc.
2+
# Copyright 2016 The Bitcoin Core developers
23
# Distributed under the MIT software license, see the accompanying
34
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
45
from __future__ import division,print_function,unicode_literals
@@ -11,94 +12,110 @@
1112
import logging
1213

1314
def parse_output(a, fmt):
14-
if fmt == 'json': # json: compare parsed data
15-
return json.loads(a)
16-
elif fmt == 'hex': # hex: parse and compare binary data
17-
return binascii.a2b_hex(a.strip())
18-
else:
19-
raise NotImplementedError("Don't know how to compare %s" % fmt)
15+
"""Parse the output according to specified format.
16+
17+
Raise an error if the output can't be parsed."""
18+
if fmt == 'json': # json: compare parsed data
19+
return json.loads(a)
20+
elif fmt == 'hex': # hex: parse and compare binary data
21+
return binascii.a2b_hex(a.strip())
22+
else:
23+
raise NotImplementedError("Don't know how to compare %s" % fmt)
2024

2125
def bctest(testDir, testObj, exeext):
26+
"""Runs a single test, comparing output and RC to expected output and RC.
2227
23-
execprog = testObj['exec'] + exeext
24-
execargs = testObj['args']
25-
execrun = [execprog] + execargs
26-
stdinCfg = None
27-
inputData = None
28-
if "input" in testObj:
29-
filename = testDir + "/" + testObj['input']
30-
inputData = open(filename).read()
31-
stdinCfg = subprocess.PIPE
28+
Raises an error if input can't be read, executable fails, or output/RC
29+
are not as expected. Error is caught by bctester() and reported.
30+
"""
31+
# Get the exec names and arguments
32+
execprog = testObj['exec'] + exeext
33+
execargs = testObj['args']
34+
execrun = [execprog] + execargs
3235

33-
outputFn = None
34-
outputData = None
35-
if "output_cmp" in testObj:
36-
outputFn = testObj['output_cmp']
37-
outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare)
38-
try:
39-
outputData = open(testDir + "/" + outputFn).read()
40-
except:
41-
logging.error("Output file " + outputFn + " can not be opened")
42-
raise
43-
if not outputData:
44-
logging.error("Output data missing for " + outputFn)
45-
raise Exception
36+
# Read the input data (if there is any)
37+
stdinCfg = None
38+
inputData = None
39+
if "input" in testObj:
40+
filename = testDir + "/" + testObj['input']
41+
inputData = open(filename).read()
42+
stdinCfg = subprocess.PIPE
4643

47-
proc = subprocess.Popen(execrun, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True)
48-
try:
49-
outs = proc.communicate(input=inputData)
50-
except OSError:
51-
logging.error("OSError, Failed to execute " + execprog)
52-
raise
44+
# Read the expected output data (if there is any)
45+
outputFn = None
46+
outputData = None
47+
if "output_cmp" in testObj:
48+
outputFn = testObj['output_cmp']
49+
outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare)
50+
try:
51+
outputData = open(testDir + "/" + outputFn).read()
52+
except:
53+
logging.error("Output file " + outputFn + " can not be opened")
54+
raise
55+
if not outputData:
56+
logging.error("Output data missing for " + outputFn)
57+
raise Exception
5358

54-
if outputData:
55-
try:
56-
a_parsed = parse_output(outs[0], outputType)
57-
except Exception as e:
58-
logging.error('Error parsing command output as %s: %s' % (outputType,e))
59-
raise
60-
try:
61-
b_parsed = parse_output(outputData, outputType)
62-
except Exception as e:
63-
logging.error('Error parsing expected output %s as %s: %s' % (outputFn,outputType,e))
64-
raise
65-
if a_parsed != b_parsed:
66-
logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
67-
raise Exception
68-
if outs[0] != outputData:
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
59+
# Run the test
60+
proc = subprocess.Popen(execrun, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True)
61+
try:
62+
outs = proc.communicate(input=inputData)
63+
except OSError:
64+
logging.error("OSError, Failed to execute " + execprog)
65+
raise
7666

77-
wantRC = 0
78-
if "return_code" in testObj:
79-
wantRC = testObj['return_code']
80-
if proc.returncode != wantRC:
81-
logging.error("Return code mismatch for " + outputFn)
82-
raise Exception
67+
if outputData:
68+
# Parse command output and expected output
69+
try:
70+
a_parsed = parse_output(outs[0], outputType)
71+
except Exception as e:
72+
logging.error('Error parsing command output as %s: %s' % (outputType,e))
73+
raise
74+
try:
75+
b_parsed = parse_output(outputData, outputType)
76+
except Exception as e:
77+
logging.error('Error parsing expected output %s as %s: %s' % (outputFn,outputType,e))
78+
raise
79+
# Compare data
80+
if a_parsed != b_parsed:
81+
logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
82+
raise Exception
83+
# Compare formatting
84+
if outs[0] != outputData:
85+
error_message = "Output formatting mismatch for " + outputFn + ":\n"
86+
error_message += "".join(difflib.context_diff(outputData.splitlines(True),
87+
outs[0].splitlines(True),
88+
fromfile=outputFn,
89+
tofile="returned"))
90+
logging.error(error_message)
91+
raise Exception
8392

84-
def bctester(testDir, input_basename, buildenv):
85-
input_filename = testDir + "/" + input_basename
86-
raw_data = open(input_filename).read()
87-
input_data = json.loads(raw_data)
93+
# Compare the return code to the expected return code
94+
wantRC = 0
95+
if "return_code" in testObj:
96+
wantRC = testObj['return_code']
97+
if proc.returncode != wantRC:
98+
logging.error("Return code mismatch for " + outputFn)
99+
raise Exception
88100

89-
failed_testcases = []
101+
def bctester(testDir, input_basename, buildenv):
102+
""" Loads and parses the input file, runs all tests and reports results"""
103+
input_filename = testDir + "/" + input_basename
104+
raw_data = open(input_filename).read()
105+
input_data = json.loads(raw_data)
90106

91-
for testObj in input_data:
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"])
107+
failed_testcases = []
98108

99-
if failed_testcases:
100-
logging.error("FAILED TESTCASES: [" + ", ".join(failed_testcases) + "]")
101-
sys.exit(1)
102-
else:
103-
sys.exit(0)
109+
for testObj in input_data:
110+
try:
111+
bctest(testDir, testObj, buildenv.exeext)
112+
logging.info("PASSED: " + testObj["description"])
113+
except:
114+
logging.info("FAILED: " + testObj["description"])
115+
failed_testcases.append(testObj["description"])
104116

117+
if failed_testcases:
118+
logging.error("FAILED TESTCASES: [" + ", ".join(failed_testcases) + "]")
119+
sys.exit(1)
120+
else:
121+
sys.exit(0)

src/test/bitcoin-util-test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# Copyright 2014 BitPay, Inc.
3+
# Copyright 2016 The Bitcoin Core developers
34
# Distributed under the MIT software license, see the accompanying
45
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
56
from __future__ import division,print_function,unicode_literals
@@ -16,11 +17,13 @@
1617
1718
Can also be run manually from the src directory by specifiying the source directory:
1819
19-
test/bitcoin-util-test.py --src=[srcdir]
20+
test/bitcoin-util-test.py --srcdir='srcdir' [--verbose]
2021
"""
2122

22-
2323
if __name__ == '__main__':
24+
# Try to get the source directory from the environment variables. This will
25+
# be set for `make check` automated runs. If environment variable is not set,
26+
# then get the source directory from command line args.
2427
try:
2528
srcdir = os.environ["srcdir"]
2629
verbose = False

0 commit comments

Comments
 (0)