-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathtest_cz_customize.py
365 lines (316 loc) · 10.9 KB
/
test_cz_customize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import pytest
from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig
from commitizen.cz.customize import CustomizeCommitsCz
from commitizen.exceptions import MissingCzCustomizeConfigError
TOML_STR = r"""
[tool.commitizen.customize]
message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example = "feature: this feature enable customize through config file"
schema = "<type>: <body>"
schema_pattern = "(feature|bug fix):(\\s.*)"
bump_pattern = "^(break|new|fix|hotfix)"
bump_map = {"break" = "MAJOR", "new" = "MINOR", "fix" = "PATCH", "hotfix" = "PATCH"}
change_type_order = ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"]
info = "This is a customized cz."
[[tool.commitizen.customize.questions]]
type = "list"
name = "change_type"
choices = [
{value = "feature", name = "feature: A new feature."},
{value = "bug fix", name = "bug fix: A bug fix."}
]
message = "Select the type of change you are committing"
[[tool.commitizen.customize.questions]]
type = "input"
name = "message"
message = "Body."
[[tool.commitizen.customize.questions]]
type = "confirm"
name = "show_message"
message = "Do you want to add body message in commit?"
"""
JSON_STR = r"""
{
"commitizen": {
"name": "cz_jira",
"version": "1.0.0",
"version_files": [
"commitizen/__version__.py",
"pyproject.toml"
],
"customize": {
"message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
"example": "feature: this feature enable customize through config file",
"schema": "<type>: <body>",
"schema_pattern": "(feature|bug fix):(\\s.*)",
"bump_pattern": "^(break|new|fix|hotfix)",
"bump_map": {
"break": "MAJOR",
"new": "MINOR",
"fix": "PATCH",
"hotfix": "PATCH"
},
"change_type_order": ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"],
"info": "This is a customized cz.",
"questions": [
{
"type": "list",
"name": "change_type",
"choices": [
{
"value": "feature",
"name": "feature: A new feature."
},
{
"value": "bug fix",
"name": "bug fix: A bug fix."
}
],
"message": "Select the type of change you are committing"
},
{
"type": "input",
"name": "message",
"message": "Body."
},
{
"type": "confirm",
"name": "show_message",
"message": "Do you want to add body message in commit?"
}
]
}
}
}
"""
YAML_STR = """
commitizen:
name: cz_jira
version: 1.0.0
version_files:
- commitizen/__version__.py
- pyproject.toml
customize:
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example: 'feature: this feature enable customize through config file'
schema: "<type>: <body>"
schema_pattern: "(feature|bug fix):(\\s.*)"
bump_pattern: "^(break|new|fix|hotfix)"
bump_map:
break: MAJOR
new: MINOR
fix: PATCH
hotfix: PATCH
change_type_order: ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"]
info: This is a customized cz.
questions:
- type: list
name: change_type
choices:
- value: feature
name: 'feature: A new feature.'
- value: bug fix
name: 'bug fix: A bug fix.'
message: Select the type of change you are committing
- type: input
name: message
message: Body.
- type: confirm
name: show_message
message: Do you want to add body message in commit?
"""
TOML_STR_INFO_PATH = """
[tool.commitizen.customize]
message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example = "feature: this feature enable customize through config file"
schema = "<type>: <body>"
bump_pattern = "^(break|new|fix|hotfix)"
bump_map = {"break" = "MAJOR", "new" = "MINOR", "fix" = "PATCH", "hotfix" = "PATCH"}
info_path = "info.txt"
"""
JSON_STR_INFO_PATH = r"""
{
"commitizen": {
"customize": {
"message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
"example": "feature: this feature enable customize through config file",
"schema": "<type>: <body>",
"bump_pattern": "^(break|new|fix|hotfix)",
"bump_map": {
"break": "MAJOR",
"new": "MINOR",
"fix": "PATCH",
"hotfix": "PATCH"
},
"info_path": "info.txt"
}
}
}
"""
YAML_STR_INFO_PATH = """
commitizen:
customize:
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example: 'feature: this feature enable customize through config file'
schema: "<type>: <body>"
bump_pattern: "^(break|new|fix|hotfix)"
bump_map:
break: MAJOR
new: MINOR
fix: PATCH
hotfix: PATCH
info_path: info.txt
"""
TOML_STR_WITHOUT_INFO = """
[tool.commitizen.customize]
message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example = "feature: this feature enable customize through config file"
schema = "<type>: <body>"
bump_pattern = "^(break|new|fix|hotfix)"
bump_map = {"break" = "MAJOR", "new" = "MINOR", "fix" = "PATCH", "hotfix" = "PATCH"}
"""
JSON_STR_WITHOUT_PATH = r"""
{
"commitizen": {
"customize": {
"message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
"example": "feature: this feature enable customize through config file",
"schema": "<type>: <body>",
"bump_pattern": "^(break|new|fix|hotfix)",
"bump_map": {
"break": "MAJOR",
"new": "MINOR",
"fix": "PATCH",
"hotfix": "PATCH"
}
}
}
}
"""
YAML_STR_WITHOUT_PATH = """
commitizen:
customize:
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example: 'feature: this feature enable customize through config file'
schema: "<type>: <body>"
bump_pattern: "^(break|new|fix|hotfix)"
bump_map:
break: MAJOR
new: MINOR
fix: PATCH
hotfix: PATCH
"""
@pytest.fixture(
params=[
TomlConfig(data=TOML_STR, path="not_exist.toml"),
JsonConfig(data=JSON_STR, path="not_exist.json"),
]
)
def config(request):
"""Parametrize the config fixture
This fixture allow to test multiple config formats,
without add the builtin parametrize decorator
"""
return request.param
@pytest.fixture(
params=[
TomlConfig(data=TOML_STR_INFO_PATH, path="not_exist.toml"),
JsonConfig(data=JSON_STR_INFO_PATH, path="not_exist.json"),
YAMLConfig(data=YAML_STR_INFO_PATH, path="not_exist.yaml"),
]
)
def config_info(request):
return request.param
@pytest.fixture(
params=[
TomlConfig(data=TOML_STR_WITHOUT_INFO, path="not_exist.toml"),
JsonConfig(data=JSON_STR_WITHOUT_PATH, path="not_exist.json"),
YAMLConfig(data=YAML_STR_WITHOUT_PATH, path="not_exist.yaml"),
]
)
def config_without_info(request):
return request.param
def test_initialize_cz_customize_failed():
with pytest.raises(MissingCzCustomizeConfigError) as excinfo:
config = BaseConfig()
_ = CustomizeCommitsCz(config)
assert MissingCzCustomizeConfigError.message in str(excinfo.value)
def test_bump_pattern(config):
cz = CustomizeCommitsCz(config)
assert cz.bump_pattern == "^(break|new|fix|hotfix)"
def test_bump_map(config):
cz = CustomizeCommitsCz(config)
assert cz.bump_map == {
"break": "MAJOR",
"new": "MINOR",
"fix": "PATCH",
"hotfix": "PATCH",
}
def test_change_type_order(config):
cz = CustomizeCommitsCz(config)
assert cz.change_type_order == [
"perf",
"BREAKING CHANGE",
"feat",
"fix",
"refactor",
]
def test_questions(config):
cz = CustomizeCommitsCz(config)
questions = cz.questions()
expected_questions = [
{
"type": "list",
"name": "change_type",
"choices": [
{"value": "feature", "name": "feature: A new feature."},
{"value": "bug fix", "name": "bug fix: A bug fix."},
],
"message": "Select the type of change you are committing",
},
{"type": "input", "name": "message", "message": "Body."},
{
"type": "confirm",
"name": "show_message",
"message": "Do you want to add body message in commit?",
},
]
assert list(questions) == expected_questions
def test_answer(config):
cz = CustomizeCommitsCz(config)
answers = {
"change_type": "feature",
"message": "this feature enaable customize through config file",
"show_message": True,
}
message = cz.message(answers)
assert message == "feature: this feature enaable customize through config file"
cz = CustomizeCommitsCz(config)
answers = {
"change_type": "feature",
"message": "this feature enaable customize through config file",
"show_message": False,
}
message = cz.message(answers)
assert message == "feature:"
def test_example(config):
cz = CustomizeCommitsCz(config)
assert "feature: this feature enable customize through config file" in cz.example()
def test_schema(config):
cz = CustomizeCommitsCz(config)
assert "<type>: <body>" in cz.schema()
def test_schema_pattern(config):
cz = CustomizeCommitsCz(config)
assert r"(feature|bug fix):(\s.*)" in cz.schema_pattern()
def test_info(config):
cz = CustomizeCommitsCz(config)
assert "This is a customized cz." in cz.info()
def test_info_with_info_path(tmpdir, config_info):
with tmpdir.as_cwd():
tmpfile = tmpdir.join("info.txt")
tmpfile.write("Test info")
cz = CustomizeCommitsCz(config_info)
assert "Test info" in cz.info()
def test_info_without_info(config_without_info):
cz = CustomizeCommitsCz(config_without_info)
assert cz.info() is None