Skip to content

Commit 774db92

Browse files
committed
Merge #9023: Add logging to bitcoin-util-test.py
32c0d6e Add logging to bitcoin-util-test.py (jnewbery)
2 parents a4fd8df + 32c0d6e commit 774db92

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

src/test/bctest.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import json
88
import sys
99
import binascii
10+
import difflib
11+
import logging
1012

1113
def parse_output(a, fmt):
1214
if fmt == 'json': # json: compare parsed data
@@ -33,53 +35,70 @@ def bctest(testDir, testObj, exeext):
3335
if "output_cmp" in testObj:
3436
outputFn = testObj['output_cmp']
3537
outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare)
36-
outputData = open(testDir + "/" + outputFn).read()
38+
try:
39+
outputData = open(testDir + "/" + outputFn).read()
40+
except:
41+
logging.error("Output file " + outputFn + " can not be opened")
42+
raise
3743
if not outputData:
38-
print("Output data missing for " + outputFn)
39-
sys.exit(1)
44+
logging.error("Output data missing for " + outputFn)
45+
raise Exception
46+
4047
proc = subprocess.Popen(execrun, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True)
4148
try:
4249
outs = proc.communicate(input=inputData)
4350
except OSError:
44-
print("OSError, Failed to execute " + execprog)
45-
sys.exit(1)
51+
logging.error("OSError, Failed to execute " + execprog)
52+
raise
4653

4754
if outputData:
4855
try:
4956
a_parsed = parse_output(outs[0], outputType)
5057
except Exception as e:
51-
print('Error parsing command output as %s: %s' % (outputType,e))
52-
sys.exit(1)
58+
logging.error('Error parsing command output as %s: %s' % (outputType,e))
59+
raise
5360
try:
5461
b_parsed = parse_output(outputData, outputType)
5562
except Exception as e:
56-
print('Error parsing expected output %s as %s: %s' % (outputFn,outputType,e))
57-
sys.exit(1)
63+
logging.error('Error parsing expected output %s as %s: %s' % (outputFn,outputType,e))
64+
raise
5865
if a_parsed != b_parsed:
59-
print("Output data mismatch for " + outputFn + " (format " + outputType + ")")
60-
sys.exit(1)
66+
logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
67+
raise Exception
6168
if outs[0] != outputData:
62-
print("Output formatting mismatch for " + outputFn + " (format " + outputType + ")")
63-
sys.exit(1)
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
6476

6577
wantRC = 0
6678
if "return_code" in testObj:
6779
wantRC = testObj['return_code']
6880
if proc.returncode != wantRC:
69-
print("Return code mismatch for " + outputFn)
70-
sys.exit(1)
81+
logging.error("Return code mismatch for " + outputFn)
82+
raise Exception
7183

72-
def bctester(testDir, input_basename, buildenv, verbose = False):
84+
def bctester(testDir, input_basename, buildenv):
7385
input_filename = testDir + "/" + input_basename
7486
raw_data = open(input_filename).read()
7587
input_data = json.loads(raw_data)
7688

89+
failed_testcases = []
90+
7791
for testObj in input_data:
78-
if verbose and "description" in testObj:
79-
print ("Testing: " + testObj["description"])
80-
bctest(testDir, testObj, buildenv.exeext)
81-
if verbose and "description" in testObj:
82-
print ("PASS")
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"])
8398

84-
sys.exit(0)
99+
if failed_testcases:
100+
logging.error("FAILED TESTCASES: [" + ", ".join(failed_testcases) + "]")
101+
sys.exit(1)
102+
else:
103+
sys.exit(0)
85104

src/test/bitcoin-util-test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
from __future__ import division,print_function,unicode_literals
66
import os
7+
import sys
78
import bctest
89
import buildenv
910
import argparse
11+
import logging
1012

1113
help_text="""Test framework for bitcoin utils.
1214
@@ -19,14 +21,23 @@
1921

2022

2123
if __name__ == '__main__':
22-
verbose = False
2324
try:
2425
srcdir = os.environ["srcdir"]
26+
verbose = False
2527
except:
2628
parser = argparse.ArgumentParser(description=help_text)
2729
parser.add_argument('-s', '--srcdir')
2830
parser.add_argument('-v', '--verbose', action='store_true')
2931
args = parser.parse_args()
3032
srcdir = args.srcdir
3133
verbose = args.verbose
32-
bctest.bctester(srcdir + "/test/data", "bitcoin-util-test.json", buildenv, verbose = verbose)
34+
35+
if verbose:
36+
level = logging.DEBUG
37+
else:
38+
level = logging.ERROR
39+
formatter = '%(asctime)s - %(levelname)s - %(message)s'
40+
# Add the format/level to the logger
41+
logging.basicConfig(format = formatter, level=level)
42+
43+
bctest.bctester(srcdir + "/test/data", "bitcoin-util-test.json", buildenv)

0 commit comments

Comments
 (0)