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

refactor(Init): remove the variable values_to_add and the _update_config_file function for readability #1537

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
33 changes: 12 additions & 21 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os
import shutil
from typing import Any

import questionary
import yaml
Expand Down Expand Up @@ -120,21 +119,6 @@ def __call__(self) -> None:
self.config = JsonConfig(data="{}", path=config_path)
elif "yaml" in config_path:
self.config = YAMLConfig(data="", path=config_path)
values_to_add: dict[str, Any] = {}
values_to_add["name"] = cz_name
values_to_add["tag_format"] = tag_format
values_to_add["version_scheme"] = version_scheme

if version_provider == "commitizen":
values_to_add["version"] = version.public
else:
values_to_add["version_provider"] = version_provider

if update_changelog_on_bump:
values_to_add["update_changelog_on_bump"] = update_changelog_on_bump

if major_version_zero:
values_to_add["major_version_zero"] = major_version_zero

# Collect hook data
hook_types = questionary.checkbox(
Expand All @@ -152,7 +136,18 @@ def __call__(self) -> None:

# Create and initialize config
self.config.init_empty_config_content()
self._update_config_file(values_to_add)

self.config.set_key("name", cz_name)
self.config.set_key("tag_format", tag_format)
self.config.set_key("version_scheme", version_scheme)
if version_provider == "commitizen":
self.config.set_key("version", version.public)
else:
self.config.set_key("version_provider", version_provider)
if update_changelog_on_bump:
self.config.set_key("update_changelog_on_bump", update_changelog_on_bump)
if major_version_zero:
self.config.set_key("major_version_zero", major_version_zero)

out.write("\nYou can bump the version running:\n")
out.info("\tcz bump\n")
Expand Down Expand Up @@ -368,7 +363,3 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
hook_types = ["commit-msg", "pre-push"]
self._exec_install_pre_commit_hook(hook_types)
out.write("commitizen pre-commit hook is now installed in your '.git'\n")

def _update_config_file(self, values: dict[str, Any]) -> None:
for key, value in values.items():
self.config.set_key(key, value)
63 changes: 63 additions & 0 deletions tests/commands/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,66 @@ def test_init_command_shows_description_when_use_help_option(

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


def test_init_configuration_settings(tmpdir, mocker: MockFixture, config):
"""Test that all configuration settings are properly initialized."""
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:
config_data = toml_file.read()

# Verify all expected settings are present
assert 'name = "cz_conventional_commits"' in config_data
assert 'tag_format = "$version"' in config_data
assert 'version_scheme = "semver"' in config_data
assert 'version = "0.0.1"' in config_data
assert "update_changelog_on_bump = true" in config_data
assert "major_version_zero = true" in config_data


def test_init_configuration_with_version_provider(tmpdir, mocker: MockFixture, config):
"""Test configuration initialization with a different version provider."""
mocker.patch(
"questionary.select",
side_effect=[
FakeQuestion("pyproject.toml"),
FakeQuestion("cz_conventional_commits"),
FakeQuestion("pep621"), # Different version provider
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:
config_data = toml_file.read()

# Verify version provider is set instead of version
assert 'name = "cz_conventional_commits"' in config_data
assert 'tag_format = "$version"' in config_data
assert 'version_scheme = "semver"' in config_data
assert 'version_provider = "pep621"' in config_data
assert "update_changelog_on_bump = true" in config_data
assert "major_version_zero = true" in config_data
assert (
"version = " not in config_data
) # Version should not be set when using version_provider