Skip to content

feat(customize.py/test_cz_customize.py): inherit from ConventionalCom… #1273

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

Closed
wants to merge 3 commits into from
Closed
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
62 changes: 36 additions & 26 deletions commitizen/cz/customize/customize.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
from __future__ import annotations

from commitizen.cz.conventional_commits import ConventionalCommitsCz

try:
from jinja2 import Template
except ImportError:
from string import Template # type: ignore


from commitizen import defaults
from commitizen.config import BaseConfig
from commitizen.cz.base import BaseCommitizen
from commitizen.defaults import Questions
from commitizen.exceptions import MissingCzCustomizeConfigError

__all__ = ["CustomizeCommitsCz"]


class CustomizeCommitsCz(BaseCommitizen):
bump_pattern = defaults.bump_pattern
bump_map = defaults.bump_map
bump_map_major_version_zero = defaults.bump_map_major_version_zero
change_type_order = defaults.change_type_order

class CustomizeCommitsCz(ConventionalCommitsCz):
def __init__(self, config: BaseConfig):
super().__init__(config)

Expand Down Expand Up @@ -59,25 +54,40 @@ def __init__(self, config: BaseConfig):
self.change_type_map = change_type_map

def questions(self) -> Questions:
return self.custom_settings.get("questions", [{}])
custom_questions = self.custom_settings.get("questions")
if custom_questions:
return custom_questions
return super().questions()

def message(self, answers: dict) -> str:
message_template = Template(self.custom_settings.get("message_template", ""))
if getattr(Template, "substitute", None):
return message_template.substitute(**answers) # type: ignore
else:
return message_template.render(**answers)

def example(self) -> str | None:
return self.custom_settings.get("example")

def schema_pattern(self) -> str | None:
return self.custom_settings.get("schema_pattern")

def schema(self) -> str | None:
return self.custom_settings.get("schema")

def info(self) -> str | None:
custom_message = self.custom_settings.get("message_template")
if custom_message:
message_template = Template(custom_message)
if getattr(Template, "substitute", None):
return message_template.substitute(**answers) # type: ignore
else:
return message_template.render(**answers)
return super().message(answers)

def example(self) -> str:
custom_example = self.custom_settings.get("example")
if custom_example:
return custom_example
return super().example()

def schema_pattern(self) -> str:
custom_schema_pattern = self.custom_settings.get("schema_pattern")
if custom_schema_pattern:
return custom_schema_pattern
return super().schema_pattern()

def schema(self) -> str:
custom_schema = self.custom_settings.get("schema")
if custom_schema:
return custom_schema
return super().schema()

def info(self) -> str:
info_path = self.custom_settings.get("info_path")
info = self.custom_settings.get("info")
if info_path:
Expand All @@ -86,4 +96,4 @@ def info(self) -> str | None:
return content
elif info:
return info
return None
return super().info()
85 changes: 84 additions & 1 deletion tests/test_cz_customize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest

from commitizen import defaults
from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig
from commitizen.cz.conventional_commits import ConventionalCommitsCz
from commitizen.cz.customize import CustomizeCommitsCz
from commitizen.exceptions import MissingCzCustomizeConfigError

Expand Down Expand Up @@ -315,6 +317,30 @@
fix: PATCH
hotfix: PATCH
"""
EMPTY_TOML_STR = """
[tool.commitizen]
name = "cz_customize"
[tool.commitizen.customize]
info = "This is a customized cz with emojis 🎉!"
"""

EMPTY_JSON_STR = """
{
"commitizen": {
"name": "cz_customize",
"customize": {
"info": "This is a customized cz with emojis 🎉!"
}
}
}
"""

EMPTY_YAML_STR = """
commitizen:
name: cz_customize
customize:
info: This is a customized cz with emojis 🎉!
"""


@pytest.fixture(
Expand Down Expand Up @@ -364,6 +390,17 @@ def config_with_unicode(request):
return request.param


@pytest.fixture(
params=[
TomlConfig(data=EMPTY_TOML_STR, path="not_exist.toml"),
JsonConfig(data=EMPTY_JSON_STR, path="not_exist.json"),
YAMLConfig(data=EMPTY_YAML_STR, path="not_exist.yaml"),
]
)
def empty_config(request):
return request.param


def test_initialize_cz_customize_failed():
with pytest.raises(MissingCzCustomizeConfigError) as excinfo:
config = BaseConfig()
Expand Down Expand Up @@ -564,7 +601,8 @@ def test_info_with_info_path(tmpdir, config_info):

def test_info_without_info(config_without_info):
cz = CustomizeCommitsCz(config_without_info)
assert cz.info() is None
conventional_commits = ConventionalCommitsCz(config_without_info)
assert cz.info() == conventional_commits.info()


def test_commit_parser(config):
Expand Down Expand Up @@ -598,3 +636,48 @@ def test_change_type_map(config):
def test_change_type_map_unicode(config_with_unicode):
cz = CustomizeCommitsCz(config_with_unicode)
assert cz.change_type_map == {"✨ feature": "Feat", "🐛 bug fix": "Fix"}


def test_questions_without_config(empty_config):
cz = CustomizeCommitsCz(empty_config)
empty_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
conventional_commits = ConventionalCommitsCz(empty_config)
assert cz.questions() == conventional_commits.questions()


def test_message_without_config(empty_config):
cz = CustomizeCommitsCz(empty_config)
empty_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
conventional_commits = ConventionalCommitsCz(empty_config)
answers = {
"prefix": "fix",
"scope": "users",
"subject": "email pattern corrected",
"is_breaking_change": False,
"body": "",
"footer": "",
}
message = conventional_commits.message(answers)
assert message == "fix(users): email pattern corrected"
assert cz.message(answers) == message


def test_example_with_config(empty_config):
cz = CustomizeCommitsCz(empty_config)
empty_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
conventional_commits = ConventionalCommitsCz(empty_config)
assert cz.example() == conventional_commits.example()


def test_schema_pattern_with_config(empty_config):
cz = CustomizeCommitsCz(empty_config)
empty_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
conventional_commits = ConventionalCommitsCz(empty_config)
assert cz.schema_pattern() == conventional_commits.schema_pattern()


def test_schema_without_config(empty_config):
cz = CustomizeCommitsCz(empty_config)
empty_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
conventional_commits = ConventionalCommitsCz(empty_config)
assert cz.schema() == conventional_commits.schema()
Loading