Skip to content

Commit 7562808

Browse files
committed
refactor(git): test the git log parser behaves properly when the repository has no commits
We should expect an exception raised or an empty list (no matter what version of git was used).
1 parent d5b67b0 commit 7562808

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

commitizen/git.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,12 @@ def get_commits(
105105
start: Optional[str] = None,
106106
end: str = "HEAD",
107107
*,
108-
log_format: str = "%H%n%s%n%an%n%ae%n%b",
109-
delimiter: str = "----------commit-delimiter----------",
110108
args: str = "",
111109
) -> List[GitCommit]:
112110
"""Get the commits between start and end."""
113-
git_log_cmd = (
114-
f"git -c log.showSignature=False log --pretty={log_format}{delimiter} {args}"
115-
)
116-
117-
if start:
118-
command = f"{git_log_cmd} {start}..{end}"
119-
else:
120-
command = f"{git_log_cmd} {end}"
121-
c = cmd.run(command)
122-
if c.return_code != 0:
123-
raise GitCommandError(c.err)
124-
if not c.out:
125-
return []
126-
111+
git_log_entries = _get_log_as_str_list(start, end, args)
127112
git_commits = []
128-
for rev_and_commit in c.out.split(f"{delimiter}\n"):
113+
for rev_and_commit in git_log_entries:
129114
if not rev_and_commit:
130115
continue
131116
rev, title, author, author_email, *body_list = rev_and_commit.split("\n")
@@ -236,3 +221,22 @@ def get_eol_style() -> EOLTypes:
236221
def smart_open(*args, **kargs):
237222
"""Open a file with the EOL style determined from Git."""
238223
return open(*args, newline=get_eol_style().get_eol_for_open(), **kargs)
224+
225+
226+
def _get_log_as_str_list(start: Optional[str], end: str, args: str) -> List[str]:
227+
"""Get string representation of each log entry"""
228+
delimiter = "----------commit-delimiter----------"
229+
log_format: str = "%H%n%s%n%an%n%ae%n%b"
230+
git_log_cmd = (
231+
f"git -c log.showSignature=False log --pretty={log_format}{delimiter} {args}"
232+
)
233+
if start:
234+
command = f"{git_log_cmd} {start}..{end}"
235+
else:
236+
command = f"{git_log_cmd} {end}"
237+
c = cmd.run(command)
238+
if c.return_code != 0:
239+
raise GitCommandError(c.err)
240+
if not c.out:
241+
return []
242+
return c.out.split(f"{delimiter}\n")

tests/commands/test_changelog_command.py

-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from commitizen.commands.changelog import Changelog
77
from commitizen.exceptions import (
88
DryRunExit,
9-
GitCommandError,
109
NoCommitsFoundError,
1110
NoRevisionError,
1211
NotAGitProjectError,
@@ -15,19 +14,6 @@
1514
from tests.utils import create_file_and_commit, wait_for_tag
1615

1716

18-
@pytest.mark.usefixtures("tmp_commitizen_project")
19-
def test_changelog_on_empty_project(mocker):
20-
testargs = ["cz", "changelog", "--dry-run"]
21-
mocker.patch.object(sys, "argv", testargs)
22-
23-
with pytest.raises(GitCommandError):
24-
cli.main()
25-
26-
# git will error out with something like:
27-
# "your current branch 'XYZ' does not have any commits yet"
28-
# is it wise to assert the exception contains a message like this?
29-
30-
3117
@pytest.mark.usefixtures("tmp_commitizen_project")
3218
def test_changelog_from_version_zero_point_two(mocker, capsys, file_regression):
3319
create_file_and_commit("feat: new file")

tests/test_git.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from commitizen import cmd, git
8+
from commitizen import cmd, exceptions, git
99
from tests.utils import FakeCommand, create_file_and_commit
1010

1111

@@ -58,6 +58,16 @@ def test_git_message_with_empty_body():
5858
assert commit.message == commit_title
5959

6060

61+
@pytest.mark.usefixtures("tmp_commitizen_project")
62+
def test_get_log_as_str_list_empty():
63+
"""ensure an exception or empty list in an empty project"""
64+
try:
65+
gitlog = git._get_log_as_str_list(start=None, end="HEAD", args="")
66+
except exceptions.GitCommandError:
67+
return
68+
assert len(gitlog) == 0, "list should be empty if no assert"
69+
70+
6171
@pytest.mark.usefixtures("tmp_commitizen_project")
6272
def test_get_commits():
6373
create_file_and_commit("feat(users): add username")

0 commit comments

Comments
 (0)