|
| 1 | +import attr |
| 2 | +import pytest |
| 3 | + |
1 | 4 | from labels import utils
|
2 | 5 |
|
3 | 6 |
|
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 | + |
10 | 14 |
|
| 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.""" |
11 | 18 |
|
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 | + ) |
17 | 22 |
|
18 |
| - gotten_owner, gotten_repo = utils._extract_o_and_r(remote_url) |
19 | 23 |
|
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.""" |
22 | 34 |
|
| 35 | + repo = utils.load_repository_info() |
| 36 | + assert repo.owner == "hackebrot" |
| 37 | + assert repo.name == "pytest-emoji" |
| 38 | + assert mock_repo_info.called |
23 | 39 |
|
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" |
29 | 40 |
|
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.""" |
31 | 48 |
|
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 |
0 commit comments