1
+ from unittest .mock import Mock , patch , mock_open
2
+
1
3
import pytest
2
- from unittest .mock import Mock , patch
3
4
4
5
import prepare_commit_msg_hooks .add_branch_name as bn
5
6
6
7
7
-
8
8
@pytest .fixture
9
- def mock_repo_class (monkeypatch ):
9
+ def mock_repo_class (monkeypatch , branch_name ):
10
10
def _mock_repo_class ():
11
11
Repo = Mock ()
12
12
Repo .head = Mock ()
13
13
Repo .head .ref = Mock ()
14
- Repo .head .ref .name = "branch-name"
14
+ Repo .head .ref .name = branch_name
15
15
return Repo
16
16
monkeypatch .setattr (bn .git , "Repo" , _mock_repo_class )
17
17
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
20
23
21
24
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 )
27
27
])
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
30
30
31
31
32
32
@pytest .mark .parametrize ("text, prefix, expected_output" , [
@@ -48,9 +48,32 @@ def test_add_prefix(text, prefix, expected_output):
48
48
def test_add_suffix (text , suffix , expected_output ):
49
49
assert bn .add_suffix (text , suffix ) == expected_output
50
50
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
+
54
78
#def test_main():
55
- # assert bn.main
56
- #
79
+ # assert bn.main()
0 commit comments