diff options
author | Yifan Hong <elsk@google.com> | 2024-04-30 13:37:45 -0700 |
---|---|---|
committer | Yifan Hong <elsk@google.com> | 2024-05-09 00:17:34 +0000 |
commit | 9faa74175c526f47ba3363c8c8b617e12f50ef60 (patch) | |
tree | 25ae807fcc5910af899be30fc94f4c08b8270948 | |
parent | e890214955dee0de3bbad07c1c720cc01405f808 (diff) | |
download | build-9faa74175c526f47ba3363c8c8b617e12f50ef60.tar.gz |
kleaf: wrapper handles return code when running as subprocess
When the internal bazel binary is executed as a subprocess,
the return code from process.wait() is never checked. Wrap
it with a coroutine that actually checks the return code.
Test: integration test
Change-Id: Ifaa134658f823fab2dbf534092e84700fa9dc29c
-rwxr-xr-x | kleaf/bazel.py | 16 | ||||
-rw-r--r-- | kleaf/tests/integration_test/integration_test.py | 9 |
2 files changed, 24 insertions, 1 deletions
diff --git a/kleaf/bazel.py b/kleaf/bazel.py index 2aa7cb60..3ccaaba5 100755 --- a/kleaf/bazel.py +++ b/kleaf/bazel.py @@ -56,6 +56,13 @@ class BazelWrapperException(Exception): super().__init__(self, self.message) +class BazelSubprocessException(BazelWrapperException): + """The internal `bazel` call fails.""" + + def __init__(self, code: int): + super().__init__(code=code) + + class MultipleBazelWrapperException(BazelWrapperException): """Wraps multiple BazelWrapperException into one.""" @@ -647,6 +654,13 @@ class OutputMutator: output_stream.flush() +async def _wait_for_subprocess(process): + """Wraps process.wait() and raises if exit code is non-zero.""" + return_code = await process.wait() + if return_code != 0: + raise BazelSubprocessException(code=return_code) + + async def run(command, env, epilog_coroutine, output_mutator): """Runs command with env asynchronously. @@ -675,7 +689,7 @@ async def run(command, env, epilog_coroutine, output_mutator): coroutines = [ stderr_coroutine, stdout_coroutine, - process.wait(), + _wait_for_subprocess(process), ] tasks = [asyncio.Task(coroutine) for coroutine in coroutines] done, _ = await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED) diff --git a/kleaf/tests/integration_test/integration_test.py b/kleaf/tests/integration_test/integration_test.py index 021a8040..587db63c 100644 --- a/kleaf/tests/integration_test/integration_test.py +++ b/kleaf/tests/integration_test/integration_test.py @@ -622,6 +622,15 @@ class QuickIntegrationTest(KleafIntegrationTestBase): self._check_output("build", ["--nobuild", "--strip_execroot", "//build/kernel:hermetic-tools"]) + def test_strip_execroot_error(self): + """Tests that if cmd with --strip_execroot fails, exit code is set.""" + popen = self._popen("what", + ["--strip_execroot"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + popen.communicate() + self.assertNotEqual(popen.returncode, 0) + class ScmversionIntegrationTest(KleafIntegrationTestBase): |