From c1eabd5062fc4f4b585d3fe2ea30c676c051c657 Mon Sep 17 00:00:00 2001 From: Dom Sekotill Date: Thu, 26 May 2022 02:00:02 +0100 Subject: [PATCH 1/2] Add regression tests for #22 --- tests/unit/proc/test_async.py | 56 +++++++++++++++++++++++++++++++++++ tests/unit/proc/test_proc.py | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/tests/unit/proc/test_async.py b/tests/unit/proc/test_async.py index 43363f9..866b568 100644 --- a/tests/unit/proc/test_async.py +++ b/tests/unit/proc/test_async.py @@ -71,6 +71,33 @@ class ExecIOTests(TestCase): with self.assertRaises(TypeError): await proc.aexec_io(ECHO_OUTPUT, input=TEST_BYTES, stdin=sys.stdin) + @trio_test() + async def test_stdin(self) -> None: + """ + Check that calling with the "stdin" argument works as expected + + Regression test for #22 + """ + with self.subTest(stdin="IO[bytes]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "rb") as pipe: + recv = await proc.aexec_io(ECHO_OUTPUT, stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + + with self.subTest(stdin="IO[str]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "r") as pipe: + recv = await proc.aexec_io(ECHO_OUTPUT, stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + @trio_test() async def test_stdout(self) -> None: """ @@ -136,6 +163,35 @@ class ExecutorTests(TestCase): self.assertEqual(output, TEST_BYTES) + @trio_test() + async def test_stdin(self) -> None: + """ + Check that calling with the "stdin" argument works as expected + + Regression test for #22 + """ + exe = proc.AsyncExecutor(*FIXTURE_CMD) + + with self.subTest(stdin="IO[bytes]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "rb") as pipe: + recv = await exe("echo", stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + + with self.subTest(stdin="IO[str]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "r") as pipe: + recv = await exe("echo", stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + @trio_test() async def test_stdout(self) -> None: """ diff --git a/tests/unit/proc/test_proc.py b/tests/unit/proc/test_proc.py index e7b4868..884849f 100644 --- a/tests/unit/proc/test_proc.py +++ b/tests/unit/proc/test_proc.py @@ -88,6 +88,32 @@ class ExecIOTests(TestCase): with self.assertRaises(TypeError): proc.exec_io(ECHO_OUTPUT, input=TEST_BYTES, stdin=sys.stdin) + def test_stdin(self) -> None: + """ + Check that calling with the "stdin" argument works as expected + + Regression test for #22 + """ + with self.subTest(stdin="IO[bytes]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "rb") as pipe: + recv = proc.exec_io(ECHO_OUTPUT, stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + + with self.subTest(stdin="IO[str]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "r") as pipe: + recv = proc.exec_io(ECHO_OUTPUT, stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + def test_stdout(self) -> None: """ Check that calling with the "stdout" argument receives bytes from stdout @@ -158,6 +184,34 @@ class ExecutorTests(TestCase): self.assertEqual(output, TEST_BYTES) + def test_stdin(self) -> None: + """ + Check that calling with the "stdin" argument works as expected + + Regression test for #22 + """ + exe = proc.Executor(*FIXTURE_CMD) + + with self.subTest(stdin="IO[bytes]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "rb") as pipe: + recv = exe("echo", stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + + with self.subTest(stdin="IO[str]"): + rfd, wfd = os.pipe() + with open(wfd, "wb") as pipe: + pipe.write(TEST_BYTES) + + with open(rfd, "r") as pipe: + recv = exe("echo", stdin=pipe, deserialiser=bytes) + + self.assertEqual(recv, TEST_BYTES) + def test_stdout(self) -> None: """ Check that calling with the "stdout" argument receives bytes from stdout -- GitLab From 3c66679594948806eabc74d9f7bb4be68fe82c53 Mon Sep 17 00:00:00 2001 From: Dom Sekotill Date: Thu, 26 May 2022 02:01:16 +0100 Subject: [PATCH 2/2] Use stdin in proc if provided Closes #22 --- behave_utils/proc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/behave_utils/proc.py b/behave_utils/proc.py index fc9d08f..c29bdeb 100644 --- a/behave_utils/proc.py +++ b/behave_utils/proc.py @@ -170,10 +170,11 @@ async def _exec_io( kwargs: dict[str, Any], ) -> trio.Process: async with trio.open_nursery() as nursery: + stdin = kwargs.pop('stdin') if 'stdin' in kwargs else PIPE if data else DEVNULL proc: trio.Process = await nursery.start( partial( trio.run_process, [*coerce_args(cmd)], - stdin=PIPE if data else DEVNULL, + stdin=stdin, stdout=PIPE, stderr=PIPE, check=False, **kwargs, -- GitLab