conftest.py 1000 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import logging
  2. import pytest
  3. import subprocess
  4. import typing
  5. import unittest.mock
  6. from cipy import _handler
  7. from mocks import mock_run
  8. @pytest.fixture(scope="function")
  9. def mock_subprocess() -> typing.Iterator[unittest.mock.Mock]:
  10. mock = unittest.mock.Mock()
  11. def run(*args, **kwargs):
  12. return mock_run(
  13. *args,
  14. **kwargs,
  15. _poison_stdout=mock.stdout.encode("utf-8"),
  16. _poison_stderr=mock.stderr.encode("utf-8"),
  17. )
  18. mock.side_effect = run
  19. mock.stderr = ""
  20. mock.stdout = ""
  21. with unittest.mock.patch("subprocess.run", mock):
  22. yield mock
  23. @pytest.fixture(scope="function")
  24. def ci_logger(monkeypatch: pytest.MonkeyPatch) -> typing.Iterator[unittest.mock.Mock]:
  25. mock = unittest.mock.Mock(spec=logging.Formatter)
  26. monkeypatch.setattr(_handler, "formatter", mock)
  27. level = logging.getLogger().level
  28. logging.getLogger().setLevel(logging.NOTSET)
  29. yield mock
  30. logging.getLogger().setLevel(level)