1
1
import pytest
2
2
import subprocess
3
3
from commit_check .util import get_branch_name
4
+ from commit_check .util import has_commits
4
5
from commit_check .util import git_merge_base
5
6
from commit_check .util import get_commit_info
6
7
from commit_check .util import cmd_output
@@ -46,6 +47,43 @@ def test_get_branch_name_with_exception(self, mocker):
46
47
]
47
48
assert retval == ""
48
49
50
+ class TestHasCommits :
51
+ def test_has_commits_true (self , mocker ):
52
+ # Must return True when git rev-parse HEAD succeeds
53
+ m_subprocess_run = mocker .patch (
54
+ "subprocess.run" ,
55
+ return_value = None
56
+ )
57
+ retval = has_commits ()
58
+ assert m_subprocess_run .call_count == 1
59
+ assert m_subprocess_run .call_args [0 ][0 ] == [
60
+ "git" , "rev-parse" , "--verify" , "HEAD"
61
+ ]
62
+ assert m_subprocess_run .call_args [1 ] == {
63
+ 'stdout' : subprocess .DEVNULL ,
64
+ 'stderr' : subprocess .DEVNULL ,
65
+ 'check' : True
66
+ }
67
+ assert retval is True
68
+
69
+ def test_has_commits_false (self , mocker ):
70
+ # Must return False when git rev-parse HEAD fails
71
+ m_subprocess_run = mocker .patch (
72
+ "subprocess.run" ,
73
+ side_effect = subprocess .CalledProcessError (128 , "git rev-parse" )
74
+ )
75
+ retval = has_commits ()
76
+ assert m_subprocess_run .call_count == 1
77
+ assert m_subprocess_run .call_args [0 ][0 ] == [
78
+ "git" , "rev-parse" , "--verify" , "HEAD"
79
+ ]
80
+ assert m_subprocess_run .call_args [1 ] == {
81
+ 'stdout' : subprocess .DEVNULL ,
82
+ 'stderr' : subprocess .DEVNULL ,
83
+ 'check' : True
84
+ }
85
+ assert retval is False
86
+
49
87
class TestGitMergeBase :
50
88
@pytest .mark .parametrize ("returncode,expected" , [
51
89
(0 , 0 ), # ancestor exists
@@ -78,20 +116,45 @@ class TestGetCommitInfo:
78
116
]
79
117
)
80
118
def test_get_commit_info (self , mocker , format_string ):
81
- # Must call get_commit_info with given argument.
119
+ # Must call get_commit_info with given argument when there are commits.
120
+ m_has_commits = mocker .patch (
121
+ "commit_check.util.has_commits" ,
122
+ return_value = True
123
+ )
82
124
m_cmd_output = mocker .patch (
83
125
"commit_check.util.cmd_output" ,
84
126
return_value = " fake commit message "
85
127
)
86
128
retval = get_commit_info (format_string )
129
+ assert m_has_commits .call_count == 1
87
130
assert m_cmd_output .call_count == 1
88
131
assert m_cmd_output .call_args [0 ][0 ] == [
89
132
"git" , "log" , "-n" , "1" , f"--pretty=format:%{ format_string } " , "HEAD"
90
133
]
91
134
assert retval == " fake commit message "
92
135
136
+ def test_get_commit_info_no_commits (self , mocker ):
137
+ # Must return 'Repo has no commits yet.' when there are no commits.
138
+ m_has_commits = mocker .patch (
139
+ "commit_check.util.has_commits" ,
140
+ return_value = False
141
+ )
142
+ m_cmd_output = mocker .patch (
143
+ "commit_check.util.cmd_output" ,
144
+ return_value = " fake commit message "
145
+ )
146
+ format_string = "s"
147
+ retval = get_commit_info (format_string )
148
+ assert m_has_commits .call_count == 1
149
+ assert m_cmd_output .call_count == 0 # Should not call cmd_output
150
+ assert retval == "Repo has no commits yet."
151
+
93
152
def test_get_commit_info_with_exception (self , mocker ):
94
153
# Must return empty string when exception raises in cmd_output.
154
+ m_has_commits = mocker .patch (
155
+ "commit_check.util.has_commits" ,
156
+ return_value = True
157
+ )
95
158
m_cmd_output = mocker .patch (
96
159
"commit_check.util.cmd_output" ,
97
160
return_value = " fake commit message "
@@ -104,6 +167,7 @@ def test_get_commit_info_with_exception(self, mocker):
104
167
)
105
168
format_string = "s"
106
169
retval = get_commit_info (format_string )
170
+ assert m_has_commits .call_count == 1
107
171
assert m_cmd_output .call_count == 1
108
172
assert m_cmd_output .call_args [0 ][0 ] == [
109
173
"git" , "log" , "-n" , "1" , f"--pretty=format:%{ format_string } " , "HEAD"
0 commit comments