Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commands/bump): Add NoneIncrementExit to avoid git fatal error #284

Merged
merged 6 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
woile marked this conversation as resolved.
Show resolved Hide resolved
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]]