Skip to content

Commit 1250eba

Browse files
test(init): cover cz without descriptions (#1829)
1 parent 4cc280e commit 1250eba

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

commitizen/commands/init.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,31 @@ def _ask_config_path(self) -> Path:
171171
def _ask_name(self) -> str:
172172
name: str = questionary.select(
173173
f"Please choose a cz (commit rule): (default: {DEFAULT_SETTINGS['name']})",
174-
choices=self._construct_name_choice_with_description(),
174+
choices=self._construct_name_choices_from_registry(),
175175
default=DEFAULT_SETTINGS["name"],
176176
style=self.cz.style,
177177
).unsafe_ask()
178178
return name
179179

180-
def _construct_name_choice_with_description(self) -> list[questionary.Choice]:
180+
def _construct_name_choices_from_registry(self) -> list[questionary.Choice]:
181+
"""
182+
Construct questionary choices of cz names from registry.
183+
"""
181184
choices = []
182185
for cz_name, cz_class in registry.items():
183186
try:
184187
cz_obj = cz_class(self.config)
185188
except MissingCzCustomizeConfigError:
189+
# Fallback if description is not available
186190
choices.append(questionary.Choice(title=cz_name, value=cz_name))
187191
continue
188-
first_example = cz_obj.schema().partition("\n")[0]
192+
193+
# Get the first line of the schema as the description
194+
# TODO(bearomorphism): schema is a workaround. Add a description method to the cz class.
195+
description = cz_obj.schema().partition("\n")[0]
189196
choices.append(
190197
questionary.Choice(
191-
title=cz_name, value=cz_name, description=first_example
198+
title=cz_name, value=cz_name, description=description
192199
)
193200
)
194201
return choices

tests/commands/test_init_command.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from commitizen import cmd, commands
1111
from commitizen.__version__ import __version__
12-
from commitizen.cz import registry
1312
from commitizen.exceptions import InitFailedError, NoAnswersError
1413

1514
if TYPE_CHECKING:
@@ -465,20 +464,18 @@ def test_init_configuration_with_version_provider(
465464
) # Version should not be set when using version_provider
466465

467466

468-
def test_construct_name_choice_with_description(
469-
config: BaseConfig, mocker: MockFixture
470-
):
467+
def test_construct_name_choice_from_registry(config: BaseConfig):
471468
"""Test the construction of cz name choices with descriptions."""
472-
init = commands.Init(config)
473-
# mock the registry to have only one cz for testing
474-
mocker.patch.dict(
475-
"commitizen.cz.registry",
476-
{"cz_conventional_commits": registry["cz_conventional_commits"]},
477-
clear=True,
469+
choices = commands.Init(config)._construct_name_choices_from_registry()
470+
assert choices[0].title == "cz_conventional_commits"
471+
assert choices[0].value == "cz_conventional_commits"
472+
assert choices[0].description == "<type>(<scope>): <subject>"
473+
assert choices[1].title == "cz_customize"
474+
assert choices[1].value == "cz_customize"
475+
assert choices[1].description is None
476+
assert choices[2].title == "cz_jira"
477+
assert choices[2].value == "cz_jira"
478+
assert (
479+
choices[2].description
480+
== "<ignored text> <ISSUE_KEY> <ignored text> #<COMMAND> <optional COMMAND_ARGUMENTS>"
478481
)
479-
choices = init._construct_name_choice_with_description()
480-
assert len(choices) == 1
481-
choice = choices[0]
482-
assert choice.title == "cz_conventional_commits"
483-
assert choice.value == "cz_conventional_commits"
484-
assert choice.description == "<type>(<scope>): <subject>"

0 commit comments

Comments
 (0)