From 863a785b595dc61cd6f22b93ce15fa9c3bf0571f Mon Sep 17 00:00:00 2001 From: Christian Eland Date: Sat, 24 Oct 2020 23:47:52 -0300 Subject: [PATCH 1/6] fix(commands/bump): Add NoneIncrementExit to fix git fatal error when creating existing tag raises NoneIncrementExit if `increment is None and new_tag_version == current_tag_version` #280 --- commitizen/commands/bump.py | 12 +++++++++ commitizen/exceptions.py | 4 +++ tests/commands/test_bump_command.py | 39 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 36ea7e252..512a8a130 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -12,6 +12,7 @@ DryRunExit, ExpectedExit, NoCommitsFoundError, + NoneIncrementExit, NoPatternMapError, NotAGitProjectError, NoVersionSpecifiedError, @@ -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: @@ -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 ) @@ -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() @@ -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) diff --git a/commitizen/exceptions.py b/commitizen/exceptions.py index 9a63002c6..6a490c521 100644 --- a/commitizen/exceptions.py +++ b/commitizen/exceptions.py @@ -53,6 +53,10 @@ class DryRunExit(ExpectedExit): pass +class NoneIncrementExit(ExpectedExit): + ... + + class NoCommitizenFoundException(CommitizenException): exit_code = ExitCode.NO_COMMITIZEN_FOUND diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index aff9dc680..73982a336 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -1,7 +1,10 @@ +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, @@ -9,6 +12,7 @@ DryRunExit, ExpectedExit, NoCommitsFoundError, + NoneIncrementExit, NoPatternMapError, NotAGitProjectError, NoVersionSpecifiedError, @@ -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 From 9795466a7d7a243a1ac8614ae92a7ef98cff2abe Mon Sep 17 00:00:00 2001 From: Christian Eland Date: Sun, 25 Oct 2020 00:30:25 -0300 Subject: [PATCH 2/6] refactor(commands/bump): Remove comment and changed ... for pass --- commitizen/commands/bump.py | 7 ------- commitizen/exceptions.py | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 512a8a130..5e72f0a0e 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -109,11 +109,6 @@ 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. @@ -124,7 +119,6 @@ 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 ) @@ -168,7 +162,6 @@ 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) diff --git a/commitizen/exceptions.py b/commitizen/exceptions.py index 6a490c521..84219f7fd 100644 --- a/commitizen/exceptions.py +++ b/commitizen/exceptions.py @@ -54,7 +54,7 @@ class DryRunExit(ExpectedExit): class NoneIncrementExit(ExpectedExit): - ... + pass class NoCommitizenFoundException(CommitizenException): From 74738af8e7b6865ae2df9e12e733ad704d0a367f Mon Sep 17 00:00:00 2001 From: Christian Eland Date: Sun, 25 Oct 2020 00:34:48 -0300 Subject: [PATCH 3/6] style(commands/bump): fix format --- commitizen/commands/bump.py | 1 - 1 file changed, 1 deletion(-) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 5e72f0a0e..b9597f322 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -109,7 +109,6 @@ def __call__(self): # noqa: C901 if increment is None: increment = self.find_increment(commits) - # Increment is removed when current and next version # are expected to be prereleases. if prerelease and current_version_instance.is_prerelease: From 2a94361fde13c3c138db23c3d9c43c25f49dee18 Mon Sep 17 00:00:00 2001 From: Christian Eland Date: Sun, 25 Oct 2020 00:52:59 -0300 Subject: [PATCH 4/6] fix(init.py): mypy error (types) closes #285 --- commitizen/commands/init.py | 4 ++-- commitizen/git.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commitizen/commands/init.py b/commitizen/commands/init.py index 1c459d5ed..1bfb643f0 100644 --- a/commitizen/commands/init.py +++ b/commitizen/commands/init.py @@ -47,7 +47,7 @@ def __call__(self): def _ask_config_path(self) -> str: name = questionary.select( "Please choose a supported config file: (default: pyproject.toml)", - choices=config_files, + choices=config_files, # type: ignore default="pyproject.toml", style=self.cz.style, ).ask() @@ -79,7 +79,7 @@ def _ask_tag(self) -> str: latest_tag = questionary.select( "Please choose the latest tag: ", - choices=get_tag_names(), + choices=get_tag_names(), # type: ignore style=self.cz.style, ).ask() diff --git a/commitizen/git.py b/commitizen/git.py index 81aa9166c..06ad24036 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -123,7 +123,7 @@ def get_latest_tag_name() -> Optional[str]: return c.out.strip() -def get_tag_names() -> Optional[List[str]]: +def get_tag_names() -> List[Optional[str]]: c = cmd.run("git tag --list") if c.err: return [] From a62354f2cf0c7eeab59673df9ec175a302875d51 Mon Sep 17 00:00:00 2001 From: Christian Eland Date: Sun, 25 Oct 2020 02:07:41 -0300 Subject: [PATCH 5/6] test(test_git): get_tag_names should have List[Optional[str]] annotation issue #285 --- tests/test_git.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_git.py b/tests/test_git.py index 92abc2f04..650b720d3 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -1,3 +1,6 @@ +import inspect +from typing import List, Optional, Union + import pytest from commitizen import git @@ -68,3 +71,9 @@ def test_get_commits_author_and_email(): assert commit.author is not "" assert "@" in commit.author_email + + +def test_get_tag_names_has_correct_arrow_annotation(): + arrow_annotation = inspect.getfullargspec(git.get_tag_names).annotations["return"] + + assert arrow_annotation == List[Optional[str]] From 6370be030f3c3bf4c75f6c2165fd0c698185f771 Mon Sep 17 00:00:00 2001 From: Christian Eland Date: Sun, 25 Oct 2020 02:25:43 -0300 Subject: [PATCH 6/6] test(test_git.py): F401 'typing.Union' imported but unused --- tests/test_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_git.py b/tests/test_git.py index 650b720d3..bcbca58bb 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -1,5 +1,5 @@ import inspect -from typing import List, Optional, Union +from typing import List, Optional import pytest