Skip to content

refactor(Init): remove unnecessary methods from ProjectInfo and refactor _ask_tag, improve test coverage #1526

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
47 changes: 19 additions & 28 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@
def is_php_composer(self) -> bool:
return os.path.isfile("composer.json")

@property
def latest_tag(self) -> str | None:
return get_latest_tag_name()

def tags(self) -> list | None:
"""Not a property, only use if necessary"""
if self.latest_tag is None:
return None
return get_tag_names()

@property
def is_pre_commit_installed(self) -> bool:
return bool(shutil.which("pre-commit"))
Expand Down Expand Up @@ -181,31 +171,32 @@
return name

def _ask_tag(self) -> str:
latest_tag = self.project_info.latest_tag
latest_tag = get_latest_tag_name()
if not latest_tag:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"

is_correct_tag = questionary.confirm(
if questionary.confirm(
f"Is {latest_tag} the latest tag?", style=self.cz.style, default=False
).unsafe_ask():
return latest_tag

existing_tags = get_tag_names()
if not existing_tags:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"

answer: str = questionary.select(
"Please choose the latest tag: ",
# The latest tag is most likely with the largest number.
# Thus, listing the existing_tags in reverse order makes more sense.
choices=sorted(existing_tags, reverse=True),
style=self.cz.style,
).unsafe_ask()
if not is_correct_tag:
tags = self.project_info.tags()
if not tags:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"

# the latest tag is most likely with the largest number. Thus list the tags in reverse order makes more sense
sorted_tags = sorted(tags, reverse=True)
latest_tag = questionary.select(
"Please choose the latest tag: ",
choices=sorted_tags,
style=self.cz.style,
).unsafe_ask()

if not latest_tag:
raise NoAnswersError("Tag is required!")
return latest_tag
if not answer:
raise NoAnswersError("Tag is required!")
return answer

Check warning on line 199 in commitizen/commands/init.py

View check run for this annotation

Codecov / codecov/patch

commitizen/commands/init.py#L199

Added line #L199 was not covered by tests

def _ask_tag_format(self, latest_tag: str) -> str:
is_correct_format = False
Expand Down
91 changes: 91 additions & 0 deletions tests/commands/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,94 @@ def test_init_command_shows_description_when_use_help_option(

out, _ = capsys.readouterr()
file_regression.check(out, extension=".txt")


def test_init_with_confirmed_tag_format(config, mocker: MockFixture, tmpdir):
mocker.patch(
"commitizen.commands.init.get_tag_names", return_value=["v0.0.2", "v0.0.1"]
)
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v0.0.2")
mocker.patch(
"questionary.select",
side_effect=[
FakeQuestion("pyproject.toml"),
FakeQuestion("cz_conventional_commits"),
FakeQuestion("commitizen"),
FakeQuestion("semver"),
],
)
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))

with tmpdir.as_cwd():
commands.Init(config)()
with open("pyproject.toml", encoding="utf-8") as toml_file:
assert 'tag_format = "v$version"' in toml_file.read()


def test_init_with_no_existing_tags(config, mocker: MockFixture, tmpdir):
mocker.patch("commitizen.commands.init.get_tag_names", return_value=[])
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0")
mocker.patch(
"questionary.select",
side_effect=[
FakeQuestion("pyproject.toml"),
FakeQuestion("cz_conventional_commits"),
FakeQuestion("commitizen"),
FakeQuestion("semver"),
],
)
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))

with tmpdir.as_cwd():
commands.Init(config)()
with open("pyproject.toml", encoding="utf-8") as toml_file:
assert 'version = "0.0.1"' in toml_file.read()


def test_init_with_no_existing_latest_tag(config, mocker: MockFixture, tmpdir):
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value=None)
mocker.patch(
"questionary.select",
side_effect=[
FakeQuestion("pyproject.toml"),
FakeQuestion("cz_conventional_commits"),
FakeQuestion("commitizen"),
FakeQuestion("semver"),
],
)
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))

with tmpdir.as_cwd():
commands.Init(config)()
with open("pyproject.toml", encoding="utf-8") as toml_file:
assert 'version = "0.0.1"' in toml_file.read()


def test_init_with_existing_tags(config, mocker: MockFixture, tmpdir):
expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"]
mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags)
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0")
mocker.patch(
"questionary.select",
side_effect=[
FakeQuestion("pyproject.toml"),
FakeQuestion("cz_conventional_commits"),
FakeQuestion("commitizen"),
FakeQuestion("semver"), # Select version scheme first
FakeQuestion("v1.0.0"), # Then select the latest tag
],
)
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))

with tmpdir.as_cwd():
commands.Init(config)()
with open("pyproject.toml", encoding="utf-8") as toml_file:
assert 'version = "1.0.0"' in toml_file.read()