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
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test(Init): cover _ask_tag test
  • Loading branch information
bearomorphism committed Jun 16, 2025
commit 90365a0c9d2ed0bc8c2d66ba00bc668aa194d2c0
122 changes: 122 additions & 0 deletions tests/commands/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,125 @@ 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()


def test_init_with_valid_tag_selection(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")

# Mock all questionary.select calls in the exact order they appear in Init.__call__
mocker.patch(
"questionary.select",
side_effect=[
FakeQuestion("pyproject.toml"), # _ask_config_path
FakeQuestion("cz_conventional_commits"), # _ask_name
FakeQuestion("commitizen"), # _ask_version_provider
FakeQuestion("v0.9.0"), # _ask_tag (after confirm=False)
FakeQuestion("semver"), # _ask_version_scheme
],
)

mocker.patch(
"questionary.confirm", return_value=FakeQuestion(False)
) # Don't confirm latest tag
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:
content = toml_file.read()
assert 'version = "0.9.0"' in content
assert 'version_scheme = "semver"' in content