Skip to content

Commit 8067d00

Browse files
committed
Run tests for utils against patched subprocess
1 parent f1f6aaf commit 8067d00

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

tests/test_utils.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
1+
import attr
2+
import pytest
3+
14
from labels import utils
25

36

4-
def test_get_owner_and_repo_from_cwd(tmp_local_repo, owner: str, repo: str) -> None:
5-
"""Test that repo owner and name can be inferred from the
6-
local git repo in the current working directory.
7-
"""
8-
with tmp_local_repo.as_cwd():
9-
assert utils.get_owner_and_repo_from_cwd() == (owner, repo)
7+
@attr.s(auto_attribs=True, kw_only=True, frozen=True)
8+
class FakeProc:
9+
"""Fake for a CompletedProcess instance."""
10+
11+
code: int = 0
12+
stdout: str = ""
13+
1014

15+
@pytest.fixture(name="mock_repo_info")
16+
def fixture_mock_repo_info(mocker, return_value):
17+
"""Patch the subprocess call to git remote get-url."""
1118

12-
def test_extract_o_and_r_from_remote_https_url() -> None:
13-
"""Test extraction of owner and repo names from HTTPS remote url string."""
14-
remote_url = "https://github.com/hackebrot/pytest-covfefe.git"
15-
expected_owner = "hackebrot"
16-
expected_repo = "pytest-covfefe"
19+
return mocker.patch(
20+
"labels.utils.subprocess.run", autospec=True, return_value=return_value
21+
)
1722

18-
gotten_owner, gotten_repo = utils._extract_o_and_r(remote_url)
1923

20-
assert gotten_owner == expected_owner
21-
assert gotten_repo == expected_repo
24+
@pytest.mark.parametrize(
25+
"return_value",
26+
[
27+
FakeProc(code=0, stdout="[email protected]:hackebrot/pytest-emoji.git\n"),
28+
FakeProc(code=0, stdout="https://github.com/hackebrot/pytest-emoji.git\n"),
29+
],
30+
ids=["ssh", "https"],
31+
)
32+
def test_load_repository_info(mock_repo_info):
33+
"""Test that load_repository_info() works for both SSH and HTTPS URLs."""
2234

35+
repo = utils.load_repository_info()
36+
assert repo.owner == "hackebrot"
37+
assert repo.name == "pytest-emoji"
38+
assert mock_repo_info.called
2339

24-
def test_extract_o_and_r_from_remote_ssh_url() -> None:
25-
"""Test extraction of owner and repo names from SSH remote url string."""
26-
remote_url = "[email protected]:hackebrot/pytest-covfefe.git"
27-
expected_owner = "hackebrot"
28-
expected_repo = "pytest-covfefe"
2940

30-
gotten_owner, gotten_repo = utils._extract_o_and_r(remote_url)
41+
@pytest.mark.parametrize(
42+
"return_value",
43+
[FakeProc(code=1, stdout=""), FakeProc(code=0, stdout="abcd")],
44+
ids=["run_error", "no_match"],
45+
)
46+
def test_load_repository_info_run_error(mock_repo_info):
47+
"""Test that load_repository_info() handles errors."""
3148

32-
assert gotten_owner == expected_owner
33-
assert gotten_repo == expected_repo
49+
repo = utils.load_repository_info()
50+
assert repo is None
51+
assert mock_repo_info.called

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ envlist = py36,py37,mypy,flake8
44
[testenv]
55
deps =
66
pytest
7+
pytest-mock
78
responses
89
commands = pytest {posargs:tests} -v
910

0 commit comments

Comments
 (0)