Skip to content

Commit

Permalink
Remove fake run code by using testfixtures library
Browse files Browse the repository at this point in the history
  • Loading branch information
turekj committed Feb 13, 2020
1 parent 28b4f1f commit e31cf91
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pylint = "^2.4.4"
jinja2 = "^2.10.3"
pyfakefs = "^3.7.1"
stringcase = "^1.2.0"
testfixtures = "^6.12.0"

[tool.poetry.scripts]
danger-python = 'danger_python.cli:cli'
Expand Down
38 changes: 9 additions & 29 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import subprocess
import sys
from io import StringIO
from typing import Any, Dict, Iterator, List, Optional, Tuple
from typing import Dict, Iterator, List, Optional
from unittest import mock

import pytest
from pyfakefs.fake_filesystem_unittest import Patcher
from testfixtures.popen import MockPopen

from danger_python.danger import Danger
from tests.fixtures.danger import danger_input_file_fixture
Expand Down Expand Up @@ -66,7 +66,6 @@ def subprocesses(
) -> List[SubprocessFixture]:
which_danger_subprocess = SubprocessFixture(
commands=["which", "danger"],
kwargs={"capture_output": True},
exit_code=0 if danger_js_path else 1,
output=danger_js_path if danger_js_path else "danger not found",
)
Expand All @@ -76,7 +75,6 @@ def subprocesses(
if danger_js_path:
danger_subprocess = SubprocessFixture(
commands=[danger_js_path] + danger_js_arguments,
kwargs={"capture_output": True},
exit_code=0,
output=danger_js_output,
)
Expand All @@ -86,32 +84,14 @@ def subprocesses(

@pytest.fixture
def run_subprocess(subprocesses: List[SubprocessFixture]) -> Iterator[None]:
def match_fixture(
fixture: SubprocessFixture, commands: List[str], **kwargs
) -> bool:
def kwarg_match(kwarg: Tuple[str, Any]):
return kwargs.get(kwarg[0], None) == kwarg[1]

kwargs_match = all(map(kwarg_match, fixture.kwargs.items()))
return fixture.commands == commands and kwargs_match

def completed_process(fixture: SubprocessFixture) -> subprocess.CompletedProcess:
return subprocess.CompletedProcess(
fixture.commands, fixture.exit_code, stdout=fixture.output.encode("utf-8")
)

def fake_run(commands, **kwargs):
def matcher(fixture: SubprocessFixture) -> bool:
return match_fixture(fixture, commands, **kwargs)

try:
return completed_process(next(filter(matcher, subprocesses)))
except StopIteration:
msg = f"Could not find fixture for call with {commands} and {kwargs}"
raise ValueError(msg)
with mock.patch("subprocess.Popen", new_callable=MockPopen) as mock_popen:
for process in subprocesses:
mock_popen.set_command(
" ".join(process.commands),
returncode=process.exit_code,
stdout=process.output.encode("utf-8") if process.output else None,
)

with mock.patch("subprocess.run") as mock_subprocess_run:
mock_subprocess_run.side_effect = fake_run
yield


Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/shell.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from dataclasses import dataclass
from typing import Any, Dict, List, Optional
from typing import List, Optional


@dataclass
class SubprocessFixture:
commands: List[str]
kwargs: Dict[str, Any]
exit_code: int
output: Optional[str]

0 comments on commit e31cf91

Please sign in to comment.