Skip to content

Commit fda3017

Browse files
committed
fix: prevent prerelase from creating a bump when there are no commits
Closes #281
1 parent d42f744 commit fda3017

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

commitizen/commands/bump.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ def __call__(self): # noqa: C901
112112
if increment is None:
113113
increment = self.find_increment(commits)
114114

115+
# It may happen that there are commits, but they are not elegible
116+
# for an increment, this generate a problem when using prerelease (#281)
117+
if prerelease and increment is None:
118+
raise NoCommitsFoundError(
119+
"[NO_COMMITS_FOUND]\n" "No commits found to generate a pre-release."
120+
)
121+
115122
# Increment is removed when current and next version
116123
# are expected to be prereleases.
117124
if prerelease and current_version_instance.is_prerelease:

tests/commands/test_bump_command.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,29 @@ def test_bump_with_changelog_config(mocker, changelog_path, config_path):
358358
out = f.read()
359359
assert out.startswith("#")
360360
assert "0.2.0" in out
361+
362+
363+
def test_prevent_prerelease_when_no_increment_detected(
364+
mocker, capsys, tmp_commitizen_project
365+
):
366+
create_file_and_commit("feat: new file")
367+
368+
testargs = ["cz", "bump", "--yes"]
369+
mocker.patch.object(sys, "argv", testargs)
370+
371+
cli.main()
372+
out, _ = capsys.readouterr()
373+
374+
assert "0.2.0" in out
375+
376+
create_file_and_commit("test: new file")
377+
testargs = ["cz", "bump", "-pr", "beta"]
378+
mocker.patch.object(sys, "argv", testargs)
379+
380+
with pytest.raises(NoCommitsFoundError) as excinfo:
381+
cli.main()
382+
383+
expected_error_message = (
384+
"[NO_COMMITS_FOUND]\n" "No commits found to generate a pre-release."
385+
)
386+
assert expected_error_message in str(excinfo.value)

0 commit comments

Comments
 (0)