Skip to content

Commit

Permalink
fix(commands/bump): Add NoneIncrementExit to fix git fatal error when…
Browse files Browse the repository at this point in the history
… creating existing tag

raises NoneIncrementExit if `increment is None and new_tag_version == current_tag_version`

#280
  • Loading branch information
christian-hawk committed Oct 25, 2020
1 parent 5c0dd54 commit 863a785
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DryRunExit,
ExpectedExit,
NoCommitsFoundError,
NoneIncrementExit,
NoPatternMapError,
NotAGitProjectError,
NoVersionSpecifiedError,
Expand Down Expand Up @@ -108,6 +109,12 @@ def __call__(self): # noqa: C901
if increment is None:
increment = self.find_increment(commits)

# if increment != 'PATCH' and increment != 'MINOR':
# if increment != 'MAJOR':
# import ipdb; ipdb.set_trace()

# git.tag_exist()

# Increment is removed when current and next version
# are expected to be prereleases.
if prerelease and current_version_instance.is_prerelease:
Expand All @@ -117,6 +124,7 @@ def __call__(self): # noqa: C901
current_version, increment, prerelease=prerelease
)
new_tag_version = bump.create_tag(new_version, tag_format=tag_format)

message = bump.create_commit_message(
current_version, new_version, bump_commit_message
)
Expand All @@ -128,6 +136,9 @@ def __call__(self): # noqa: C901
f"increment detected: {increment}\n"
)

if increment is None and new_tag_version == current_tag_version:
raise NoneIncrementExit()

# Do not perform operations over files or git.
if dry_run:
raise DryRunExit()
Expand Down Expand Up @@ -157,6 +168,7 @@ def __call__(self): # noqa: C901
c = git.commit(message, args=self._get_commit_args())
if c.return_code != 0:
raise BumpCommitFailedError(f'git.commit error: "{c.err.strip()}"')

c = git.tag(new_tag_version)
if c.return_code != 0:
raise BumpTagFailedError(c.err)
Expand Down
4 changes: 4 additions & 0 deletions commitizen/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class DryRunExit(ExpectedExit):
pass


class NoneIncrementExit(ExpectedExit):
...


class NoCommitizenFoundException(CommitizenException):
exit_code = ExitCode.NO_COMMITIZEN_FOUND

Expand Down
39 changes: 39 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import inspect
import sys
from unittest.mock import MagicMock

import pytest

import commitizen.commands.bump as bump
from commitizen import cli, cmd, git
from commitizen.exceptions import (
BumpTagFailedError,
CurrentVersionNotFoundError,
DryRunExit,
ExpectedExit,
NoCommitsFoundError,
NoneIncrementExit,
NoPatternMapError,
NotAGitProjectError,
NoVersionSpecifiedError,
Expand Down Expand Up @@ -262,3 +266,38 @@ def test_bump_in_non_git_project(tmpdir, config, mocker):
with pytest.raises(NotAGitProjectError):
with pytest.raises(ExpectedExit):
cli.main()


def test_none_increment_exit_should_be_a_class():
assert inspect.isclass(NoneIncrementExit)


def test_none_increment_exit_should_be_expected_exit_subclass():
assert issubclass(NoneIncrementExit, ExpectedExit)


def test_none_increment_exit_should_exist_in_bump():
assert hasattr(bump, "NoneIncrementExit")


def test_none_increment_exit_is_exception():
assert bump.NoneIncrementExit == NoneIncrementExit


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_none_increment_should_not_call_git_tag(mocker, tmp_commitizen_project):
create_file_and_commit("test(test_get_all_droplets): fix bad comparison test")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)

# stash git.tag for later restore
stashed_git_tag = git.tag
dummy_value = git.tag("0.0.2")
git.tag = MagicMock(return_value=dummy_value)

with pytest.raises(NoneIncrementExit):
cli.main()
git.tag.assert_not_called()

# restore pop stashed
git.tag = stashed_git_tag

0 comments on commit 863a785

Please sign in to comment.