Skip to content

Commit b22b53f

Browse files
committed
test: add more test for util
1 parent 148dc32 commit b22b53f

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

tests/util_test.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
import subprocess
33
from commit_check.util import get_branch_name
4+
from commit_check.util import has_commits
45
from commit_check.util import git_merge_base
56
from commit_check.util import get_commit_info
67
from commit_check.util import cmd_output
@@ -46,6 +47,43 @@ def test_get_branch_name_with_exception(self, mocker):
4647
]
4748
assert retval == ""
4849

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+
4987
class TestGitMergeBase:
5088
@pytest.mark.parametrize("returncode,expected", [
5189
(0, 0), # ancestor exists
@@ -78,20 +116,45 @@ class TestGetCommitInfo:
78116
]
79117
)
80118
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+
)
82124
m_cmd_output = mocker.patch(
83125
"commit_check.util.cmd_output",
84126
return_value=" fake commit message "
85127
)
86128
retval = get_commit_info(format_string)
129+
assert m_has_commits.call_count == 1
87130
assert m_cmd_output.call_count == 1
88131
assert m_cmd_output.call_args[0][0] == [
89132
"git", "log", "-n", "1", f"--pretty=format:%{format_string}", "HEAD"
90133
]
91134
assert retval == " fake commit message "
92135

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+
93152
def test_get_commit_info_with_exception(self, mocker):
94153
# 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+
)
95158
m_cmd_output = mocker.patch(
96159
"commit_check.util.cmd_output",
97160
return_value=" fake commit message "
@@ -104,6 +167,7 @@ def test_get_commit_info_with_exception(self, mocker):
104167
)
105168
format_string = "s"
106169
retval = get_commit_info(format_string)
170+
assert m_has_commits.call_count == 1
107171
assert m_cmd_output.call_count == 1
108172
assert m_cmd_output.call_args[0][0] == [
109173
"git", "log", "-n", "1", f"--pretty=format:%{format_string}", "HEAD"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy