2018-08-27 11:15:50 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
import pytest_mock
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def Popen(mocker: 'pytest_mock.MockFixture'):
|
2018-08-29 07:57:08 +00:00
|
|
|
real_Popen = subprocess.Popen
|
2018-08-27 11:15:50 +00:00
|
|
|
|
2018-08-29 07:57:08 +00:00
|
|
|
def popen_factory(*args, **kwargs):
|
|
|
|
popen = mocker.create_autospec(real_Popen)
|
2018-08-29 08:08:05 +00:00
|
|
|
|
|
|
|
popen.stdin = mocker.mock_open()(os.devnull, "wb")
|
|
|
|
popen.stdout = mocker.mock_open()(os.devnull, "rb")
|
|
|
|
assert popen.stdin != popen.stdout
|
|
|
|
|
2018-08-29 07:57:08 +00:00
|
|
|
popen.wait.return_value = 0
|
|
|
|
return popen
|
2018-08-27 11:15:50 +00:00
|
|
|
|
2018-08-29 07:57:08 +00:00
|
|
|
Popen = mocker.patch.object(subprocess, 'Popen', autospec=True)
|
|
|
|
Popen.side_effect = popen_factory
|
2018-08-27 11:15:50 +00:00
|
|
|
yield Popen
|