import typing import subprocess def mock_run( args: typing.Sequence[str], *, stdout: int | None = None, stderr: int | None = None, check: bool, # Force an error if any of our mocked runs doesnt specify check _poison_stdout: bytes = b"", _poison_stderr: bytes = b"", **kwargs: typing.Any, ): if stderr is not subprocess.PIPE: assert _poison_stderr == b"", "Cannot poison STDERR when piping to STDOUT" return subprocess.CompletedProcess( args=args, stdout=None if stdout is None else _poison_stdout, stderr=None if stderr is None else _poison_stderr, returncode=0, )