corrscope/tests/conftest.py

65 wiersze
1.9 KiB
Python
Czysty Zwykły widok Historia

"""
Integration tests found in:
- test_cli.py
- test_renderer.py
- test_output.py
"""
import os
import subprocess
from pathlib import Path
2019-05-08 11:57:17 +00:00
from typing import TYPE_CHECKING, Optional
import pytest
if TYPE_CHECKING:
import pytest_mock
# Pycharm sets cwd to /tests/.
# To ensure tests can find WAV files (based on /), jump from /tests/conftest.py to /.
os.chdir(Path(__file__).parent.parent)
@pytest.fixture
2019-01-03 08:57:30 +00:00
def Popen(mocker: "pytest_mock.MockFixture"):
2019-05-08 11:57:17 +00:00
"""
- This fixture function is called Popen.
- pytest names our yield-value Popen too.
- The yielded Popen imitates `class Popen`, but is actually a MagicMock instance.
- We can't make Popen a class, because a test uses `Popen.call_args`.
- To "add class-methods" to Popen, I create a `class MockPopen`
and give its classmethods to Popen.
"""
real_Popen = subprocess.Popen
2019-05-08 11:57:17 +00:00
class MockPopen(real_Popen):
exception_on_write: Optional[Exception] = None
2019-05-08 11:57:17 +00:00
@classmethod
def set_exception(cls, exc: Exception):
cls.exception_on_write = exc
2019-05-08 11:57:17 +00:00
@classmethod
def __new__(cls, *args, **kwargs):
popen = mocker.create_autospec(real_Popen)
popen.stdin = mocker.mock_open()(os.devnull, "wb")
popen.stdout = mocker.mock_open()(os.devnull, "rb")
assert popen.stdin != popen.stdout
if cls.exception_on_write is not None:
popen.stdin.write.side_effect = cls.exception_on_write
popen.wait.return_value = 0
return popen
# Popen acts exactly the same as MockPopen
# when calling classmethods or creating instances.
# But Popen is a MagicMock, and captures Popen() call arguments.
2019-01-03 08:57:30 +00:00
Popen = mocker.patch.object(subprocess, "Popen", autospec=True)
2019-05-08 11:57:17 +00:00
Popen.set_exception = MockPopen.set_exception
Popen.side_effect = MockPopen.__new__
yield Popen