Skip to content

Commit 4317d9f

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 4461921 commit 4317d9f

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

commitizen/git.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,12 @@ def get_commits(
8282
start: Optional[str] = None,
8383
end: str = "HEAD",
8484
*,
85-
log_format: str = "%H%n%s%n%an%n%ae%n%b",
86-
delimiter: str = "----------commit-delimiter----------",
8785
args: str = "",
8886
) -> List[GitCommit]:
8987
"""Get the commits between start and end."""
90-
git_log_cmd = (
91-
f"git -c log.showSignature=False log --pretty={log_format}{delimiter} {args}"
92-
)
93-
94-
if start:
95-
command = f"{git_log_cmd} {start}..{end}"
96-
else:
97-
command = f"{git_log_cmd} {end}"
98-
c = cmd.run(command)
99-
if c.return_code != 0:
100-
raise GitCommandError(c.err)
101-
if not c.out:
102-
return []
103-
88+
git_log_entries = _get_log_as_str_list(start, end, args)
10489
git_commits = []
105-
for rev_and_commit in c.out.split(f"{delimiter}\n"):
90+
for rev_and_commit in git_log_entries:
10691
if not rev_and_commit:
10792
continue
10893
rev, title, author, author_email, *body_list = rev_and_commit.split("\n")
@@ -185,3 +170,22 @@ def is_git_project() -> bool:
185170
if c.out.strip() == "true":
186171
return True
187172
return False
173+
174+
175+
def _get_log_as_str_list(start: Optional[str], end: str, args: str) -> List[str]:
176+
"""Get string representation of each log entry"""
177+
delimiter = "----------commit-delimiter----------"
178+
log_format: str = "%H%n%s%n%an%n%ae%n%b"
179+
git_log_cmd = (
180+
f"git -c log.showSignature=False log --pretty={log_format}{delimiter} {args}"
181+
)
182+
if start:
183+
command = f"{git_log_cmd} {start}..{end}"
184+
else:
185+
command = f"{git_log_cmd} {end}"
186+
c = cmd.run(command)
187+
if c.return_code != 0:
188+
raise GitCommandError(c.err)
189+
if not c.out:
190+
return []
191+
return c.out.split(f"{delimiter}\n")

tests/commands/test_changelog_command.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from commitizen.commands.changelog import Changelog
88
from commitizen.exceptions import (
99
DryRunExit,
10-
GitCommandError,
1110
NoCommitsFoundError,
1211
NoRevisionError,
1312
NotAGitProjectError,
@@ -16,19 +15,6 @@
1615
from tests.utils import create_file_and_commit, wait_for_tag
1716

1817

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

tests/test_git.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from commitizen import cmd, git
7+
from commitizen import cmd, exceptions, git
88
from tests.utils import FakeCommand, create_file_and_commit
99

1010

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

5959

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

0 commit comments

Comments
 (0)