Skip to content

Commit

Permalink
Add unit test for anyio.run_process(stdin=) invocation.
Browse files Browse the repository at this point in the history
This new unit test may be able to replace the previously existing `test_run_process_connect_to_file` test, as `run_process()` internally just delegates to `open_process()`, so both are proven working by the new test.
  • Loading branch information
jmehnle committed Jan 23, 2025
1 parent 7645e37 commit b6d57ee
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion tests/test_subprocesses.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async def test_process_new_session_sid() -> None:
assert result.stdout.decode().strip() != str(sid)


async def test_run_process_connect_to_file(tmp_path: Path) -> None:
async def test_open_process_connect_to_file(tmp_path: Path) -> None:
stdinfile = tmp_path / "stdin"
stdinfile.write_text("Hello, process!\n")
stdoutfile = tmp_path / "stdout"
Expand Down Expand Up @@ -162,6 +162,38 @@ async def test_run_process_connect_to_file(tmp_path: Path) -> None:
)


async def test_run_process_connect_to_file(tmp_path: Path) -> None:
stdinfile = tmp_path / "stdin"
stdinfile.write_text("Hello, process!\n")
stdoutfile = tmp_path / "stdout"
stderrfile = tmp_path / "stderr"
with (
stdinfile.open("rb") as fin,
stdoutfile.open("wb") as fout,
stderrfile.open("wb") as ferr,
):
await run_process(
[
sys.executable,
"-c",
"import sys; txt = sys.stdin.read().strip(); "
'print("stdin says", repr(txt), "but stderr says NO!", '
"file=sys.stderr); "
'print("stdin says", repr(txt), "and stdout says YES!")',
],
stdin=fin,
stdout=fout,
stderr=ferr,
)

assert (
stdoutfile.read_text() == "stdin says 'Hello, process!' and stdout says YES!\n"
)
assert (
stderrfile.read_text() == "stdin says 'Hello, process!' but stderr says NO!\n"
)


async def test_run_process_inherit_stdout(capfd: pytest.CaptureFixture[str]) -> None:
await run_process(
[
Expand Down

0 comments on commit b6d57ee

Please sign in to comment.