From b6f685881d1d3f434202bad0a0e3f923ce4b4f8e Mon Sep 17 00:00:00 2001 From: "Axel H." Date: Thu, 7 Mar 2024 14:42:31 +0100 Subject: [PATCH] fix(bump): pre and post bump hooks were failing when an increment was provided (fix #1004) --- commitizen/commands/bump.py | 3 ++- tests/commands/test_bump_command.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index c29c4c35c5..efbe1e0c3c 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -209,6 +209,8 @@ def __call__(self) -> None: # noqa: C901 scheme=self.scheme, ) + is_initial = self.is_initial_tag(current_tag_version, is_yes) + # If user specified changelog_to_stdout, they probably want the # changelog to be generated as well, this is the most intuitive solution self.changelog = self.changelog or bool(self.changelog_to_stdout) @@ -223,7 +225,6 @@ def __call__(self) -> None: # noqa: C901 ) from exc else: if increment is None: - is_initial = self.is_initial_tag(current_tag_version, is_yes) if is_initial: commits = git.get_commits() else: diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index b39271f284..c9e73649f4 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -1007,6 +1007,29 @@ def test_bump_with_pre_bump_hooks( ) +def test_bump_with_hooks_and_increment(mocker: MockFixture, tmp_commitizen_project): + pre_bump_hook = "scripts/pre_bump_hook.sh" + post_bump_hook = "scripts/post_bump_hook.sh" + + tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_commitizen_cfg_file.write( + f"{tmp_commitizen_cfg_file.read()}\n" + f'pre_bump_hooks = ["{pre_bump_hook}"]\n' + f'post_bump_hooks = ["{post_bump_hook}"]\n' + ) + + run_mock = mocker.Mock() + mocker.patch.object(hooks, "run", run_mock) + + create_file_and_commit("test: some test") + testargs = ["cz", "bump", "--yes", "--increment", "MINOR"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist("0.2.0") + assert tag_exists is True + + @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_manual_version_disallows_prerelease_offset(mocker): create_file_and_commit("feat: new file")