Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

refactor(Init): fix unbounded variable in _ask_tag_format #1529

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 2 commits into
base: v4-9-0-test
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
23 changes: 10 additions & 13 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,20 @@ def _ask_tag(self) -> str:
return latest_tag

def _ask_tag_format(self, latest_tag: str) -> str:
is_correct_format = False
if latest_tag.startswith("v"):
tag_format = r"v$version"
is_correct_format = questionary.confirm(
f'Is "{tag_format}" the correct tag format?', style=self.cz.style
).unsafe_ask()
v_tag_format = r"v$version"
if questionary.confirm(
f'Is "{v_tag_format}" the correct tag format?', style=self.cz.style
).unsafe_ask():
return v_tag_format

default_format = DEFAULT_SETTINGS["tag_format"]
if not is_correct_format:
tag_format = questionary.text(
f'Please enter the correct version format: (default: "{default_format}")',
style=self.cz.style,
).unsafe_ask()
tag_format: str = questionary.text(
f'Please enter the correct version format: (default: "{default_format}")',
style=self.cz.style,
).unsafe_ask()

if not tag_format:
tag_format = default_format
return tag_format
return tag_format or default_format

def _ask_version_provider(self) -> str:
"""Ask for setting: version_provider"""
Expand Down
32 changes: 32 additions & 0 deletions tests/commands/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,38 @@ def test_pre_commit_exec_failed(
commands.Init(config)()


class TestAskTagFormat:
def test_confirm_v_tag_format(self, mocker: MockFixture, config):
init = commands.Init(config)
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))

result = init._ask_tag_format("v1.0.0")
assert result == r"v$version"

def test_reject_v_tag_format(self, mocker: MockFixture, config):
init = commands.Init(config)
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version"))

result = init._ask_tag_format("v1.0.0")
assert result == "custom-$version"

def test_non_v_tag_format(self, mocker: MockFixture, config):
init = commands.Init(config)
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version"))

result = init._ask_tag_format("1.0.0")
assert result == "custom-$version"

def test_empty_input_returns_default(self, mocker: MockFixture, config):
init = commands.Init(config)
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
mocker.patch("questionary.text", return_value=FakeQuestion(""))

result = init._ask_tag_format("v1.0.0")
assert result == "$version" # This is the default format from DEFAULT_SETTINGS


@skip_below_py_3_10
def test_init_command_shows_description_when_use_help_option(
mocker: MockFixture, capsys, file_regression
Expand Down