Print return code when subprocess fails (MR 2161)

It can be very useful to figure out why a particular subprocess might be
failing ;)
This commit is contained in:
Bart Ribbers 2022-01-21 16:41:18 +01:00
parent ff0942b12d
commit 36aabcc4fe
No known key found for this signature in database
GPG Key ID: 0BF4C1B5988C50D8
2 changed files with 5 additions and 3 deletions

View File

@ -216,7 +216,8 @@ def check_return_code(args, code, log_message):
logging.debug("^" * 70)
logging.info("NOTE: The failed command's output is above the ^^^ line"
" in the log file: " + args.log)
raise RuntimeError("Command failed: " + log_message)
raise RuntimeError(f"Command failed (exit code {str(code)}): " +
log_message)
def sudo_timer_iterate():

View File

@ -1,6 +1,7 @@
# Copyright 2022 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
""" Test pmb.helpers.run_core """
import re
import sys
import subprocess
import pytest
@ -146,13 +147,13 @@ def test_core(args):
# Check the return code
with pytest.raises(RuntimeError) as e:
func(args, msg, ["false"], output="log")
assert str(e.value).startswith("Command failed:")
assert re.search(r"^Command failed \(exit code -?\d*\): ", str(e.value))
# Kill with timeout
args.timeout = 0.2
with pytest.raises(RuntimeError) as e:
func(args, msg, ["sleep", "1"], output="log")
assert str(e.value).startswith("Command failed:")
assert re.search(r"^Command failed \(exit code -?\d*\): ", str(e.value))
@pytest.mark.skip_ci