mocks.py 653 B

1234567891011121314151617181920212223
  1. import typing
  2. import subprocess
  3. def mock_run(
  4. args: typing.Sequence[str],
  5. *,
  6. stdout: int | None = None,
  7. stderr: int | None = None,
  8. check: bool, # Force an error if any of our mocked runs doesnt specify check
  9. _poison_stdout: bytes = b"",
  10. _poison_stderr: bytes = b"",
  11. **kwargs: typing.Any,
  12. ):
  13. if stderr is not subprocess.PIPE:
  14. assert _poison_stderr == b"", "Cannot poison STDERR when piping to STDOUT"
  15. return subprocess.CompletedProcess(
  16. args=args,
  17. stdout=None if stdout is None else _poison_stdout,
  18. stderr=None if stderr is None else _poison_stderr,
  19. returncode=0,
  20. )