Skip to content

Commit b58ec7b

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 b58ec7b

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

commitizen/git.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,13 @@ def commit(message: str, args: str = "") -> cmd.Command:
7878
return c
7979

8080

81-
def get_commits(
82-
start: Optional[str] = None,
83-
end: str = "HEAD",
84-
*,
85-
log_format: str = "%H%n%s%n%an%n%ae%n%b",
86-
delimiter: str = "----------commit-delimiter----------",
87-
args: str = "",
88-
) -> List[GitCommit]:
89-
"""Get the commits between start and end."""
81+
def _get_log_as_str_list(start: Optional[str], end: str, args: str) -> List[str]:
82+
"""Get string representation of each log entry"""
83+
delimiter = "----------commit-delimiter----------"
84+
log_format: str = "%H%n%s%n%an%n%ae%n%b"
9085
git_log_cmd = (
9186
f"git -c log.showSignature=False log --pretty={log_format}{delimiter} {args}"
9287
)
93-
9488
if start:
9589
command = f"{git_log_cmd} {start}..{end}"
9690
else:
@@ -100,9 +94,19 @@ def get_commits(
10094
raise GitCommandError(c.err)
10195
if not c.out:
10296
return []
97+
return c.out.split(f"{delimiter}\n")
10398

99+
100+
def get_commits(
101+
start: Optional[str] = None,
102+
end: str = "HEAD",
103+
*,
104+
args: str = "",
105+
) -> List[GitCommit]:
106+
"""Get the commits between start and end."""
107+
git_log_entries = _get_log_as_str_list(start, end, args)
104108
git_commits = []
105-
for rev_and_commit in c.out.split(f"{delimiter}\n"):
109+
for rev_and_commit in git_log_entries:
106110
if not rev_and_commit:
107111
continue
108112
rev, title, author, author_email, *body_list = rev_and_commit.split("\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)