Skip to content

Commit 84635c9

Browse files
committed
test(init): cover cz without descriptions
1 parent 615b888 commit 84635c9

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
"Please choose a cz (commit rule): (default: cz_conventional_commits)",
174-
choices=self._construct_name_choice_with_description(),
174+
choices=self._construct_name_choices_from_registry(),
175175
default="cz_conventional_commits",
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:
187+
# TODO(bearomorphism): can we get the description from the cz class without initiating an instance?
184188
cz_obj = cz_class(self.config)
185189
except MissingCzCustomizeConfigError:
190+
# workaround for cz_customize
186191
choices.append(questionary.Choice(title=cz_name, value=cz_name))
187192
continue
188-
first_example = cz_obj.schema().partition("\n")[0]
193+
194+
# Get the first line of the schema as the description
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
@@ -10,7 +10,6 @@
1010

1111
from commitizen import cmd, commands
1212
from commitizen.__version__ import __version__
13-
from commitizen.cz import registry
1413
from commitizen.exceptions import InitFailedError, NoAnswersError
1514

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

468467

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

0 commit comments

Comments
 (0)