Skip to content

Commit

Permalink
Merge pull request #284 from christian-hawk/christian-hawk-patch
Browse files Browse the repository at this point in the history
fix(commands/bump): Add NoneIncrementExit to avoid git fatal error
  • Loading branch information
woile committed Oct 26, 2020
2 parents 5c0dd54 + 6370be0 commit 4b7c175
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
4 changes: 4 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 @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

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):
pass


class NoCommitizenFoundException(CommitizenException):
exit_code = ExitCode.NO_COMMITIZEN_FOUND

Expand Down
2 changes: 1 addition & 1 deletion commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
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
9 changes: 9 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import inspect
from typing import List, Optional

import pytest

from commitizen import git
Expand Down Expand Up @@ -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]]

0 comments on commit 4b7c175

Please sign in to comment.