diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 36ea7e252..b9597f322 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -12,6 +12,7 @@ DryRunExit, ExpectedExit, NoCommitsFoundError, + NoneIncrementExit, NoPatternMapError, NotAGitProjectError, NoVersionSpecifiedError, @@ -128,6 +129,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() 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/exceptions.py b/commitizen/exceptions.py index 9a63002c6..84219f7fd 100644 --- a/commitizen/exceptions.py +++ b/commitizen/exceptions.py @@ -53,6 +53,10 @@ class DryRunExit(ExpectedExit): pass +class NoneIncrementExit(ExpectedExit): + pass + + class NoCommitizenFoundException(CommitizenException): exit_code = ExitCode.NO_COMMITIZEN_FOUND 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 [] 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 diff --git a/tests/test_git.py b/tests/test_git.py index 92abc2f04..bcbca58bb 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -1,3 +1,6 @@ +import inspect +from typing import List, Optional + 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]]