From 975e9b0ed374f12729ea3104b0013911d968c9e1 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Tue, 28 Sep 2021 22:57:44 +0530 Subject: [PATCH] Make zxpy raise on error --- tests/zxpy_test.py | 7 +++++++ zx.py | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/zxpy_test.py b/tests/zxpy_test.py index 2da8834..d819cb4 100644 --- a/tests/zxpy_test.py +++ b/tests/zxpy_test.py @@ -76,3 +76,10 @@ def test_prints(capsys: pytest.CaptureFixture[str]) -> None: def test_argv() -> None: test_file = "./tests/test_files/argv.py" subprocess.run(["zxpy", test_file]) + + +def test_raise() -> None: + with pytest.raises(ChildProcessError) as exc: + zx.run_shell("exit 1") + + assert exc.value.args == (1,) diff --git a/zx.py b/zx.py index 3b5f18f..afd8cb2 100644 --- a/zx.py +++ b/zx.py @@ -59,8 +59,15 @@ def cli() -> None: def create_shell_process(command: str) -> IO[bytes]: """Creates a shell process, returning its stdout to read data from.""" process = subprocess.Popen( - command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True + command, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + shell=True, ) + process.wait() + if process.returncode != 0: + raise ChildProcessError(process.returncode) + assert process.stdout is not None return process.stdout