Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#31067: test: Print CompletedProcess object on e…
Browse files Browse the repository at this point in the history
…rror

fa43c4f test: Print CompletedProcess object on error (MarcoFalke)

Pull request description:

  It would be good to know the output on `Error parsing command output`. Otherwise test failures are meaningless: bitcoin/bitcoin#30792 (comment)

  Fix it by just printing the full `CompletedProcess` object.

  Also, use the modern `subprocess.run` to simplify the code.

ACKs for top commit:
  BrandonOdiwuor:
    Code Review ACK fa43c4f
  laanwj:
    This contains some useful information, so ACK fa43c4f

Tree-SHA512: ae7c1cb1f48af2a6feae6d1a5a967c0720f6c6675c1ce20ace7cac18c00f3d4069b8abcc58204855e92ff5303158b9a942bab3b71acae0737768d941a5773c91
  • Loading branch information
fanquake committed Oct 16, 2024
2 parents dea9fb9 + fa43c4f commit 21e2f06
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions test/util/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ def bctest(testDir, testObj, buildenv):
execrun = [execprog] + execargs

# Read the input data (if there is any)
stdinCfg = None
inputData = None
if "input" in testObj:
filename = os.path.join(testDir, testObj["input"])
with open(filename, encoding="utf8") as f:
inputData = f.read()
stdinCfg = subprocess.PIPE

# Read the expected output data (if there is any)
outputFn = None
Expand All @@ -112,9 +110,8 @@ def bctest(testDir, testObj, buildenv):
raise Exception

# Run the test
proc = subprocess.Popen(execrun, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
try:
outs = proc.communicate(input=inputData)
res = subprocess.run(execrun, capture_output=True, text=True, input=inputData)
except OSError:
logging.error("OSError, Failed to execute " + execprog)
raise
Expand All @@ -123,9 +120,9 @@ def bctest(testDir, testObj, buildenv):
data_mismatch, formatting_mismatch = False, False
# Parse command output and expected output
try:
a_parsed = parse_output(outs[0], outputType)
a_parsed = parse_output(res.stdout, outputType)
except Exception as e:
logging.error('Error parsing command output as %s: %s' % (outputType, e))
logging.error(f"Error parsing command output as {outputType}: '{str(e)}'; res: {str(res)}")
raise
try:
b_parsed = parse_output(outputData, outputType)
Expand All @@ -134,13 +131,13 @@ def bctest(testDir, testObj, buildenv):
raise
# Compare data
if a_parsed != b_parsed:
logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
logging.error(f"Output data mismatch for {outputFn} (format {outputType}); res: {str(res)}")
data_mismatch = True
# Compare formatting
if outs[0] != outputData:
error_message = "Output formatting mismatch for " + outputFn + ":\n"
if res.stdout != outputData:
error_message = f"Output formatting mismatch for {outputFn}:\nres: {str(res)}\n"
error_message += "".join(difflib.context_diff(outputData.splitlines(True),
outs[0].splitlines(True),
res.stdout.splitlines(True),
fromfile=outputFn,
tofile="returned"))
logging.error(error_message)
Expand All @@ -152,8 +149,8 @@ def bctest(testDir, testObj, buildenv):
wantRC = 0
if "return_code" in testObj:
wantRC = testObj['return_code']
if proc.returncode != wantRC:
logging.error("Return code mismatch for " + outputFn)
if res.returncode != wantRC:
logging.error(f"Return code mismatch for {outputFn}; res: {str(res)}")
raise Exception

if "error_txt" in testObj:
Expand All @@ -164,8 +161,8 @@ def bctest(testDir, testObj, buildenv):
# emits DISPLAY errors when running as a windows application on
# linux through wine. Just assert that the expected error text appears
# somewhere in stderr.
if want_error not in outs[1]:
logging.error("Error mismatch:\n" + "Expected: " + want_error + "\nReceived: " + outs[1].rstrip())
if want_error not in res.stderr:
logging.error(f"Error mismatch:\nExpected: {want_error}\nReceived: {res.stderr.rstrip()}\nres: {str(res)}")
raise Exception

def parse_output(a, fmt):
Expand Down

0 comments on commit 21e2f06

Please sign in to comment.