From 36aabcc4fe0d8fda7236a5db4b301f8c9ccbb6fc Mon Sep 17 00:00:00 2001 From: Bart Ribbers Date: Fri, 21 Jan 2022 16:41:18 +0100 Subject: [PATCH] Print return code when subprocess fails (MR 2161) It can be very useful to figure out why a particular subprocess might be failing ;) --- pmb/helpers/run_core.py | 3 ++- test/test_run_core.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pmb/helpers/run_core.py b/pmb/helpers/run_core.py index 789ec7f9..ccf6383f 100644 --- a/pmb/helpers/run_core.py +++ b/pmb/helpers/run_core.py @@ -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(): diff --git a/test/test_run_core.py b/test/test_run_core.py index fd269537..c2d5b884 100644 --- a/test/test_run_core.py +++ b/test/test_run_core.py @@ -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