From 9971a32b2d214786643f8395344fdac30282bf81 Mon Sep 17 00:00:00 2001 From: Lyonel Martinez Date: Fri, 29 Nov 2024 16:09:07 +0100 Subject: [PATCH 1/2] fix(get-next-bump): fix to permit usage of --get-next options even when update_changelog_on_bump is set to true --- commitizen/commands/bump.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index a3682df8f..1f98c1995 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -66,9 +66,8 @@ def __init__(self, config: BaseConfig, arguments: dict): }, } self.cz = factory.commiter_factory(self.config) - self.changelog = arguments["changelog"] or self.config.settings.get( - "update_changelog_on_bump" - ) + self.changelog_flag = arguments["changelog"] + self.changelog_config = self.config.settings.get("update_changelog_on_bump") self.changelog_to_stdout = arguments["changelog_to_stdout"] self.git_output_to_stderr = arguments["git_output_to_stderr"] self.no_verify = arguments["no_verify"] @@ -207,17 +206,24 @@ def __call__(self) -> None: # noqa: C901 "--local-version cannot be combined with --build-metadata" ) - # 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) - if get_next: - if self.changelog: + # if trying to use --get-next, we should not allow --changelog or --changelog-to-stdout + if self.changelog_flag or bool(self.changelog_to_stdout): raise NotAllowed( "--changelog or --changelog-to-stdout is not allowed with --get-next" ) + # --get-next is a special case, taking precedence over config for 'update_changelog_on_bump' + self.changelog_config = False # Setting dry_run to prevent any unwanted changes to the repo or files self.dry_run = True + else: + # If user specified changelog_to_stdout, they probably want the + # changelog to be generated as well, this is the most intuitive solution + self.changelog_flag = ( + self.changelog_flag + or bool(self.changelog_to_stdout) + or self.changelog_config + ) current_tag_version: str = bump.normalize_tag( current_version, @@ -309,7 +315,7 @@ def __call__(self) -> None: # noqa: C901 ) files: list[str] = [] - if self.changelog: + if self.changelog_flag: args = { "unreleased_version": new_tag_version, "template": self.template, @@ -356,7 +362,9 @@ def __call__(self) -> None: # noqa: C901 new_tag_version=new_tag_version, message=message, increment=increment, - changelog_file_name=changelog_cmd.file_name if self.changelog else None, + changelog_file_name=changelog_cmd.file_name + if self.changelog_flag + else None, ) if is_files_only: @@ -365,7 +373,7 @@ def __call__(self) -> None: # noqa: C901 # FIXME: check if any changes have been staged git.add(*files) c = git.commit(message, args=self._get_commit_args()) - if self.retry and c.return_code != 0 and self.changelog: + if self.retry and c.return_code != 0 and self.changelog_flag: # Maybe pre-commit reformatted some files? Retry once logger.debug("1st git.commit error: %s", c.err) logger.info("1st commit attempt failed; retrying once") @@ -410,7 +418,9 @@ def __call__(self) -> None: # noqa: C901 current_tag_version=new_tag_version, message=message, increment=increment, - changelog_file_name=changelog_cmd.file_name if self.changelog else None, + changelog_file_name=changelog_cmd.file_name + if self.changelog_flag + else None, ) # TODO: For v3 output this only as diagnostic and remove this if From e5d6797769b9de4a30ec8c63729f0f4601e35522 Mon Sep 17 00:00:00 2001 From: Lyonel Martinez Date: Fri, 29 Nov 2024 16:27:21 +0100 Subject: [PATCH 2/2] fix(get-next-bump): add a test case --- tests/commands/test_bump_command.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 81273764d..8acb5143c 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -1481,6 +1481,26 @@ def test_bump_get_next(mocker: MockFixture, capsys): assert tag_exists is False +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_get_next_update_changelog_on_bump( + mocker: MockFixture, capsys, config_path +): + create_file_and_commit("feat: new file") + with open(config_path, "a", encoding="utf-8") as fp: + fp.write("update_changelog_on_bump = true\n") + + testargs = ["cz", "bump", "--yes", "--get-next"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(GetNextExit): + cli.main() + + out, _ = capsys.readouterr() + assert "0.2.0" in out + + tag_exists = git.tag_exist("0.2.0") + assert tag_exists is False + + @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_get_next__changelog_is_not_allowed(mocker: MockFixture): create_file_and_commit("feat: new file")