Skip to content

Commit 6f93267

Browse files
committed
add tests
1 parent 9e5bfae commit 6f93267

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

tests/test_add_branch_name.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1+
from unittest.mock import Mock, patch, mock_open
2+
13
import pytest
2-
from unittest.mock import Mock, patch
34

45
import prepare_commit_msg_hooks.add_branch_name as bn
56

67

7-
88
@pytest.fixture
9-
def mock_repo_class(monkeypatch):
9+
def mock_repo_class(monkeypatch, branch_name):
1010
def _mock_repo_class():
1111
Repo = Mock()
1212
Repo.head = Mock()
1313
Repo.head.ref = Mock()
14-
Repo.head.ref.name = "branch-name"
14+
Repo.head.ref.name = branch_name
1515
return Repo
1616
monkeypatch.setattr(bn.git, "Repo", _mock_repo_class)
1717

18-
def test_get_branch_name(mock_repo_class):
19-
assert bn.get_branch_name() == "branch-name"
18+
@pytest.mark.parametrize("branch_name", [
19+
("main"), ("AB-123")
20+
])
21+
def test_get_branch_name(mock_repo_class, branch_name):
22+
assert bn.get_branch_name() == branch_name
2023

2124

22-
@pytest.mark.parametrize("branch, pattern, expected_output", [
23-
("AB-123", "[A-Z]{2,3}-[0-9]+", True),
24-
("ab-123", "[A-Z]{2,3}-[0-9]+", False),
25-
("aAB-123b", "[A-Z]{2,3}-[0-9]+", False),
26-
("main", "[A-Z]{2,3}-[0-9]+", False)
25+
@pytest.mark.parametrize("branch, expected_output", [
26+
("AB-123", True), ("ab-123", False), ("aAB-123b", False), ("main", False)
2727
])
28-
def test_check_if_branch_match_pattern(branch, pattern, expected_output):
29-
assert bn.check_if_branch_match_pattern(branch, pattern) == expected_output
28+
def test_check_if_branch_match_pattern(branch, expected_output):
29+
assert bn.check_if_branch_match_pattern(branch, "[A-Z]{2,3}-[0-9]+") == expected_output
3030

3131

3232
@pytest.mark.parametrize("text, prefix, expected_output", [
@@ -48,9 +48,32 @@ def test_add_prefix(text, prefix, expected_output):
4848
def test_add_suffix(text, suffix, expected_output):
4949
assert bn.add_suffix(text, suffix) == expected_output
5050

51-
#def test_add_branch_name():
52-
# assert bn.add_branch_name
53-
#
51+
52+
@pytest.mark.parametrize("branch_name, loc", [
53+
("main", "prefix"), ("main", "suffix")
54+
])
55+
def test_add_branch_name_without_pattern(
56+
capsys, mock_repo_class, branch_name, loc
57+
):
58+
return_value = bn.add_branch_name("filename", "[a-c]{2}-[0-9]+", loc)
59+
captured = capsys.readouterr()
60+
assert captured.out == "Branch doesn't match pattern. Addition skipped.\n"
61+
assert return_value == 0
62+
63+
64+
@patch("builtins.open", new_callable=mock_open, read_data="commit mssg")
65+
@pytest.mark.parametrize("branch_name, pattern, loc", [
66+
("main", None, "prefix"),
67+
("main", None, "suffix"),
68+
("ab-123", "[a-c]{2}-[0-9]+", "prefix"),
69+
("ab-123", "[a-c]{2}-[0-9]+", "suffix"),
70+
])
71+
def test_add_branch_name_with_pattern(
72+
capsys, mock_repo_class, branch_name, pattern, loc
73+
):
74+
return_value = bn.add_branch_name("filename", pattern, loc)
75+
assert return_value == 0
76+
77+
5478
#def test_main():
55-
# assert bn.main
56-
#
79+
# assert bn.main()

0 commit comments

Comments
 (0)