Skip to content

Commit 8e4a3ee

Browse files
committed
feat(customize.py/test_cz_customize.py): inherit from ConventionalCommitsCz directly
enable cz_customize default behavior follow ConventionalCommitsCz without trivial settings
1 parent e5aaec4 commit 8e4a3ee

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

commitizen/cz/customize/customize.py

+36-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
from __future__ import annotations
22

3+
from commitizen.cz.conventional_commits import ConventionalCommitsCz
4+
35
try:
46
from jinja2 import Template
57
except ImportError:
68
from string import Template # type: ignore
79

810

9-
from commitizen import defaults
1011
from commitizen.config import BaseConfig
11-
from commitizen.cz.base import BaseCommitizen
1212
from commitizen.defaults import Questions
1313
from commitizen.exceptions import MissingCzCustomizeConfigError
1414

1515
__all__ = ["CustomizeCommitsCz"]
1616

1717

18-
class CustomizeCommitsCz(BaseCommitizen):
19-
bump_pattern = defaults.bump_pattern
20-
bump_map = defaults.bump_map
21-
bump_map_major_version_zero = defaults.bump_map_major_version_zero
22-
change_type_order = defaults.change_type_order
23-
18+
class CustomizeCommitsCz(ConventionalCommitsCz):
2419
def __init__(self, config: BaseConfig):
2520
super().__init__(config)
2621

@@ -59,25 +54,40 @@ def __init__(self, config: BaseConfig):
5954
self.change_type_map = change_type_map
6055

6156
def questions(self) -> Questions:
62-
return self.custom_settings.get("questions", [{}])
57+
custom_questions = self.custom_settings.get("questions", [{}])
58+
if custom_questions:
59+
return custom_questions
60+
return super().questions()
6361

6462
def message(self, answers: dict) -> str:
65-
message_template = Template(self.custom_settings.get("message_template", ""))
66-
if getattr(Template, "substitute", None):
67-
return message_template.substitute(**answers) # type: ignore
68-
else:
69-
return message_template.render(**answers)
70-
71-
def example(self) -> str | None:
72-
return self.custom_settings.get("example")
73-
74-
def schema_pattern(self) -> str | None:
75-
return self.custom_settings.get("schema_pattern")
76-
77-
def schema(self) -> str | None:
78-
return self.custom_settings.get("schema")
79-
80-
def info(self) -> str | None:
63+
custom_message = self.custom_settings.get("message_template")
64+
if custom_message:
65+
message_template = Template(custom_message)
66+
if getattr(Template, "substitute", None):
67+
return message_template.substitute(**answers) # type: ignore
68+
else:
69+
return message_template.render(**answers)
70+
return super().message(answers)
71+
72+
def example(self) -> str:
73+
custom_example = self.custom_settings.get("example")
74+
if custom_example:
75+
return custom_example
76+
return super().example()
77+
78+
def schema_pattern(self) -> str:
79+
custom_schema_pattern = self.custom_settings.get("schema_pattern")
80+
if custom_schema_pattern:
81+
return custom_schema_pattern
82+
return super().schema_pattern()
83+
84+
def schema(self) -> str:
85+
custom_schema = self.custom_settings.get("schema")
86+
if custom_schema:
87+
return custom_schema
88+
return super().schema()
89+
90+
def info(self) -> str:
8191
info_path = self.custom_settings.get("info_path")
8292
info = self.custom_settings.get("info")
8393
if info_path:
@@ -86,4 +96,4 @@ def info(self) -> str | None:
8696
return content
8797
elif info:
8898
return info
89-
return None
99+
return super().info()

tests/test_cz_customize.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig
4+
from commitizen.cz.conventional_commits import ConventionalCommitsCz
45
from commitizen.cz.customize import CustomizeCommitsCz
56
from commitizen.exceptions import MissingCzCustomizeConfigError
67

@@ -564,7 +565,8 @@ def test_info_with_info_path(tmpdir, config_info):
564565

565566
def test_info_without_info(config_without_info):
566567
cz = CustomizeCommitsCz(config_without_info)
567-
assert cz.info() is None
568+
cz_super = ConventionalCommitsCz(config_without_info)
569+
assert cz.info() == cz_super.info()
568570

569571

570572
def test_commit_parser(config):

0 commit comments

Comments
 (0)