Skip to content

refactor(bump): code cleanup and better test coverage #1422

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 15 additions & 19 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 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.
Expand All @@ -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."""
Expand All @@ -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"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type should be str | None because the default value is None. Not sure if we can improve the type safety here.

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"]
Expand Down
56 changes: 54 additions & 2 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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")
Expand Down Expand Up @@ -1688,3 +1688,55 @@ 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."""
from commitizen import defaults
from commitizen.commands.bump import Bump
from commitizen.config import BaseConfig

# 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(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