Skip to content

Commit c7a6d92

Browse files
committed
test(tests): add test functions to test YAMLConfig class at init and customization
314
1 parent bdd6dc0 commit c7a6d92

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

tests/commands/test_init_command.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def ask(self):
3131
'tag_format = "$version"\n'
3232
)
3333

34-
EXPECTED_JSON_CONFIG = {
34+
EXPECTED_DICT_CONFIG = {
3535
"commitizen": {
3636
"name": "cz_conventional_commits",
3737
"version": "0.0.1",
@@ -94,7 +94,7 @@ def test_init_without_choosing_tag(config, mocker, tmpdir):
9494

9595

9696
class TestPreCommitCases:
97-
@pytest.fixture(scope="function", params=["pyproject.toml", ".cz.json"])
97+
@pytest.fixture(scope="function", params=["pyproject.toml", ".cz.json", ".cz.yaml"])
9898
def default_choice(_, request, mocker):
9999
mocker.patch(
100100
"questionary.select",
@@ -108,13 +108,15 @@ def default_choice(_, request, mocker):
108108
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
109109
return request.param
110110

111-
def test_no_existing_pre_commit_json_conifg(_, default_choice, tmpdir, config):
111+
def test_no_existing_pre_commit_conifg(_, default_choice, tmpdir, config):
112112
with tmpdir.as_cwd():
113113
commands.Init(config)()
114114

115115
with open(default_choice, "r") as file:
116116
if "json" in default_choice:
117-
assert json.load(file) == EXPECTED_JSON_CONFIG
117+
assert json.load(file) == EXPECTED_DICT_CONFIG
118+
elif "yaml" in default_choice:
119+
assert yaml.load(file) == EXPECTED_DICT_CONFIG
118120
else:
119121
config_data = file.read()
120122
assert config_data == expected_config
@@ -132,7 +134,9 @@ def test_empty_pre_commit_config(_, default_choice, tmpdir, config):
132134

133135
with open(default_choice, "r") as file:
134136
if "json" in default_choice:
135-
assert json.load(file) == EXPECTED_JSON_CONFIG
137+
assert json.load(file) == EXPECTED_DICT_CONFIG
138+
elif "yaml" in default_choice:
139+
assert yaml.load(file) == EXPECTED_DICT_CONFIG
136140
else:
137141
config_data = file.read()
138142
assert config_data == expected_config
@@ -156,7 +160,9 @@ def test_pre_commit_config_without_cz_hook(_, default_choice, tmpdir, config):
156160

157161
with open(default_choice, "r") as file:
158162
if "json" in default_choice:
159-
assert json.load(file) == EXPECTED_JSON_CONFIG
163+
assert json.load(file) == EXPECTED_DICT_CONFIG
164+
elif "yaml" in default_choice:
165+
assert yaml.load(file) == EXPECTED_DICT_CONFIG
160166
else:
161167
config_data = file.read()
162168
assert config_data == expected_config
@@ -176,7 +182,9 @@ def test_cz_hook_exists_in_pre_commit_config(_, default_choice, tmpdir, config):
176182

177183
with open(default_choice, "r") as file:
178184
if "json" in default_choice:
179-
assert json.load(file) == EXPECTED_JSON_CONFIG
185+
assert json.load(file) == EXPECTED_DICT_CONFIG
186+
elif "yaml" in default_choice:
187+
assert yaml.load(file) == EXPECTED_DICT_CONFIG
180188
else:
181189
config_data = file.read()
182190
assert config_data == expected_config

tests/test_cz_customize.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from commitizen.config import BaseConfig, JsonConfig, TomlConfig
3+
from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig
44
from commitizen.cz.customize import CustomizeCommitsCz
55
from commitizen.exceptions import MissingCzCustomizeConfigError
66

@@ -89,6 +89,43 @@
8989
}
9090
"""
9191

92+
YAML_STR = """
93+
commitizen:
94+
name: cz_jira
95+
version: 1.0.0
96+
version_files:
97+
- commitizen/__version__.py
98+
- pyproject.toml
99+
customize:
100+
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
101+
example: 'feature: this feature enable customize through config file'
102+
schema: "<type>: <body>"
103+
schema_pattern: "(feature|bug fix):(\\s.*)"
104+
bump_pattern: "^(break|new|fix|hotfix)"
105+
bump_map:
106+
break: MAJOR
107+
new: MINOR
108+
fix: PATCH
109+
hotfix: PATCH
110+
info: This is a customized cz.
111+
questions:
112+
- type: list
113+
name: change_type
114+
choices:
115+
- value: feature
116+
name: 'feature: A new feature.'
117+
- value: bug fix
118+
name: 'bug fix: A bug fix.'
119+
message: Select the type of change you are committing
120+
- type: input
121+
name: message
122+
message: Body.
123+
- type: confirm
124+
name: show_message
125+
message: Do you want to add body message in commit?
126+
"""
127+
128+
92129
TOML_STR_INFO_PATH = """
93130
[tool.commitizen.customize]
94131
message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
@@ -119,6 +156,21 @@
119156
}
120157
"""
121158

159+
YAML_STR_INFO_PATH = """
160+
commitizen:
161+
customize:
162+
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
163+
example: 'feature: this feature enable customize through config file'
164+
schema: "<type>: <body>"
165+
bump_pattern: "^(break|new|fix|hotfix)"
166+
bump_map:
167+
break: MAJOR
168+
new: MINOR
169+
fix: PATCH
170+
hotfix: PATCH
171+
info_path: info.txt
172+
"""
173+
122174
TOML_STR_WITHOUT_INFO = """
123175
[tool.commitizen.customize]
124176
message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
@@ -147,6 +199,20 @@
147199
}
148200
"""
149201

202+
YAML_STR_WITHOUT_PATH = """
203+
commitizen:
204+
customize:
205+
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
206+
example: 'feature: this feature enable customize through config file'
207+
schema: "<type>: <body>"
208+
bump_pattern: "^(break|new|fix|hotfix)"
209+
bump_map:
210+
break: MAJOR
211+
new: MINOR
212+
fix: PATCH
213+
hotfix: PATCH
214+
"""
215+
150216

151217
@pytest.fixture(
152218
params=[
@@ -167,6 +233,7 @@ def config(request):
167233
params=[
168234
TomlConfig(data=TOML_STR_INFO_PATH, path="not_exist.toml"),
169235
JsonConfig(data=JSON_STR_INFO_PATH, path="not_exist.json"),
236+
YAMLConfig(data=YAML_STR_INFO_PATH, path="not_exist.yaml"),
170237
]
171238
)
172239
def config_info(request):
@@ -177,6 +244,7 @@ def config_info(request):
177244
params=[
178245
TomlConfig(data=TOML_STR_WITHOUT_INFO, path="not_exist.toml"),
179246
JsonConfig(data=JSON_STR_WITHOUT_PATH, path="not_exist.json"),
247+
YAMLConfig(data=YAML_STR_WITHOUT_PATH, path="not_exist.yaml"),
180248
]
181249
)
182250
def config_without_info(request):

0 commit comments

Comments
 (0)