From 5d8abe9990af2408049f5db98dfa99e27e5db380 Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Sat, 17 May 2025 03:09:58 +0800 Subject: [PATCH 1/2] refactor(bump): clean up --- commitizen/commands/bump.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 0a2bbe37f..d022599c7 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -105,19 +105,18 @@ def is_initial_tag( self, current_tag: git.GitTag | None, is_yes: bool = False ) -> bool: """Check if reading the whole git tree up to HEAD is needed.""" - is_initial = False - if not current_tag: - if is_yes: - is_initial = True - else: - out.info("No tag matching configuration could not be found.") - out.info( - "Possible causes:\n" - "- version in configuration is not the current version\n" - "- tag_format or legacy_tag_formats is missing, check them using 'git tag --list'\n" - ) - is_initial = questionary.confirm("Is this the first tag created?").ask() - return is_initial + if current_tag: + return False + if is_yes: + return True + + out.info("No tag matching configuration could not be found.") + out.info( + "Possible causes:\n" + "- version in configuration is not the current version\n" + "- tag_format or legacy_tag_formats is missing, check them using 'git tag --list'\n" + ) + return bool(questionary.confirm("Is this the first tag created?").ask()) def find_increment(self, commits: list[git.GitCommit]) -> Increment | None: # Update the bump map to ensure major version doesn't increment. @@ -134,10 +133,7 @@ def find_increment(self, commits: list[git.GitCommit]) -> Increment | None: raise NoPatternMapError( f"'{self.config.settings['name']}' rule does not support bump" ) - increment = bump.find_increment( - commits, regex=bump_pattern, increments_map=bump_map - ) - return increment + return bump.find_increment(commits, regex=bump_pattern, increments_map=bump_map) def __call__(self) -> None: # noqa: C901 """Steps executed to bump.""" @@ -148,7 +144,7 @@ def __call__(self) -> None: # noqa: C901 except TypeError: raise NoVersionSpecifiedError() - bump_commit_message: str = self.bump_settings["bump_message"] + bump_commit_message: str | None = self.bump_settings["bump_message"] version_files: list[str] = self.bump_settings["version_files"] major_version_zero: bool = self.bump_settings["major_version_zero"] prerelease_offset: int = self.bump_settings["prerelease_offset"] From cae1410312daea57a88255a7371c90192999cc80 Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Sat, 17 May 2025 03:14:56 +0800 Subject: [PATCH 2/2] test(bump): improve test coverage --- commitizen/commands/bump.py | 4 +-- tests/commands/test_bump_command.py | 55 +++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index d022599c7..a2d98c245 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -65,7 +65,7 @@ def __init__(self, config: BaseConfig, arguments: dict): "template", "file_name", ] - if arguments[key] is not None + if arguments.get(key) is not None }, } self.cz = factory.committer_factory(self.config) @@ -110,7 +110,7 @@ def is_initial_tag( if is_yes: return True - out.info("No tag matching configuration could not be found.") + out.info("No tag matching configuration could be found.") out.info( "Possible causes:\n" "- version in configuration is not the current version\n" diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index e15539d8a..2af7bec12 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -12,8 +12,9 @@ from pytest_mock import MockFixture import commitizen.commands.bump as bump -from commitizen import cli, cmd, git, hooks +from commitizen import cli, cmd, defaults, git, hooks from commitizen.changelog_formats import ChangelogFormat +from commitizen.config.base_config import BaseConfig from commitizen.cz.base import BaseCommitizen from commitizen.exceptions import ( BumpTagFailedError, @@ -41,8 +42,8 @@ "fix(user): username exception", "refactor: remove ini configuration support", "refactor(config): remove ini configuration support", - "perf: update to use multiproess", - "perf(worker): update to use multiproess", + "perf: update to use multiprocess", + "perf(worker): update to use multiprocess", ), ) @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1688,3 +1689,51 @@ def test_bump_warn_but_dont_fail_on_invalid_tags( assert err.count("Invalid version tag: '0.4.3.deadbeaf'") == 1 assert git.tag_exist("0.4.3") + + +def test_is_initial_tag(mocker: MockFixture, tmp_commitizen_project): + """Test the is_initial_tag method behavior.""" + # Create a commit but no tags + create_file_and_commit("feat: initial commit") + + # Initialize Bump with minimal config + config = BaseConfig() + config.settings.update( + { + "name": defaults.DEFAULT_SETTINGS["name"], + "encoding": "utf-8", + "pre_bump_hooks": [], + "post_bump_hooks": [], + } + ) + + # Initialize with required arguments + arguments = { + "changelog": False, + "changelog_to_stdout": False, + "git_output_to_stderr": False, + "no_verify": False, + "check_consistency": False, + "retry": False, + "version_scheme": None, + "file_name": None, + "template": None, + "extras": None, + } + + bump_cmd = bump.Bump(config, arguments) + + # Test case 1: No current tag, not yes mode + mocker.patch("questionary.confirm", return_value=mocker.Mock(ask=lambda: True)) + assert bump_cmd.is_initial_tag(None, is_yes=False) is True + + # Test case 2: No current tag, yes mode + assert bump_cmd.is_initial_tag(None, is_yes=True) is True + + # Test case 3: Has current tag + mock_tag = mocker.Mock() + assert bump_cmd.is_initial_tag(mock_tag, is_yes=False) is False + + # Test case 4: No current tag, user denies + mocker.patch("questionary.confirm", return_value=mocker.Mock(ask=lambda: False)) + assert bump_cmd.is_initial_tag(None, is_yes=False) is False