From 8e4a3ee2602e1df3ddb9e502a2d9400e96b4e769 Mon Sep 17 00:00:00 2001 From: Atticuszz <1831768457@qq.com> Date: Wed, 23 Oct 2024 18:12:07 +0800 Subject: [PATCH 1/3] feat(customize.py/test_cz_customize.py): inherit from ConventionalCommitsCz directly enable cz_customize default behavior follow ConventionalCommitsCz without trivial settings --- commitizen/cz/customize/customize.py | 62 ++++++++++++++++------------ tests/test_cz_customize.py | 4 +- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/commitizen/cz/customize/customize.py b/commitizen/cz/customize/customize.py index 5c3b4e76b4..53c6ace177 100644 --- a/commitizen/cz/customize/customize.py +++ b/commitizen/cz/customize/customize.py @@ -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) @@ -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: @@ -86,4 +96,4 @@ def info(self) -> str | None: return content elif info: return info - return None + return super().info() diff --git a/tests/test_cz_customize.py b/tests/test_cz_customize.py index 20a17b3d9c..0c95321de5 100644 --- a/tests/test_cz_customize.py +++ b/tests/test_cz_customize.py @@ -1,6 +1,7 @@ import pytest 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 @@ -564,7 +565,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 + cz_super = ConventionalCommitsCz(config_without_info) + assert cz.info() == cz_super.info() def test_commit_parser(config): From dda73ba5afd89018fea1def2bec9fbc2a304c9f9 Mon Sep 17 00:00:00 2001 From: Atticuszz <1831768457@qq.com> Date: Wed, 23 Oct 2024 18:42:25 +0800 Subject: [PATCH 2/3] fix(customize.py): default questions as super().questions() instead of [{}] --- commitizen/cz/customize/customize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commitizen/cz/customize/customize.py b/commitizen/cz/customize/customize.py index 53c6ace177..8003e2321a 100644 --- a/commitizen/cz/customize/customize.py +++ b/commitizen/cz/customize/customize.py @@ -54,7 +54,7 @@ def __init__(self, config: BaseConfig): self.change_type_map = change_type_map def questions(self) -> Questions: - custom_questions = self.custom_settings.get("questions", [{}]) + custom_questions = self.custom_settings.get("questions") if custom_questions: return custom_questions return super().questions() From ce095ab4422957679954acf4401dbe7cc79b7839 Mon Sep 17 00:00:00 2001 From: Atticuszz <1831768457@qq.com> Date: Wed, 23 Oct 2024 19:29:59 +0800 Subject: [PATCH 3/3] test(test_cz_customize.py): add more tests --- tests/test_cz_customize.py | 85 +++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) diff --git a/tests/test_cz_customize.py b/tests/test_cz_customize.py index 0c95321de5..67707a807a 100644 --- a/tests/test_cz_customize.py +++ b/tests/test_cz_customize.py @@ -1,5 +1,6 @@ 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 @@ -316,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( @@ -365,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() @@ -565,8 +601,8 @@ def test_info_with_info_path(tmpdir, config_info): def test_info_without_info(config_without_info): cz = CustomizeCommitsCz(config_without_info) - cz_super = ConventionalCommitsCz(config_without_info) - assert cz.info() == cz_super.info() + conventional_commits = ConventionalCommitsCz(config_without_info) + assert cz.info() == conventional_commits.info() def test_commit_parser(config): @@ -600,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()