From 216334058f60313bb4603e54d9dfad688aad74e6 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 21 Apr 2021 20:22:13 -0400 Subject: utils: run: fix crash with failing tools & combined stderr If we try to run a program that doesn't exist, don't crash while trying to show the errors to the user. Bug: 185946409 Test: pre-upload unittests pass Change-Id: I70e00412bd000e907536011d245eaa82d36634f6 --- rh/utils.py | 4 ++-- rh/utils_unittest.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/rh/utils.py b/rh/utils.py index f59998c..aeab52f 100644 --- a/rh/utils.py +++ b/rh/utils.py @@ -417,9 +417,9 @@ def run(cmd, redirect_stdout=False, redirect_stderr=False, cwd=None, input=None, stderr=ensure_text(result.stderr)) except OSError as e: # Avoid leaking tempfiles. - if popen_stdout is not None: + if popen_stdout is not None and not isinstance(popen_stdout, int): popen_stdout.close() - if popen_stderr is not None: + if popen_stderr is not None and not isinstance(popen_stderr, int): popen_stderr.close() estr = str(e) diff --git a/rh/utils_unittest.py b/rh/utils_unittest.py index e6ecc97..ea2ddaa 100755 --- a/rh/utils_unittest.py +++ b/rh/utils_unittest.py @@ -206,6 +206,15 @@ class RunCommandTests(unittest.TestCase): self.assertNotEqual(0, ret.returncode) self.assertIn('a/b/c/d', str(ret)) + def test_check_false_missing_prog_combined_output(self): + """Verify handling of combined output capturing w/missing progs.""" + with self.assertRaises(rh.utils.CalledProcessError) as e: + rh.utils.run(['./!~a/b/c/d/'], check=True, + combine_stdout_stderr=True) + err = e.exception + self.assertNotEqual(0, err.returncode) + self.assertIn('a/b/c/d', str(err)) + if __name__ == '__main__': unittest.main() -- cgit v1.2.3