Debian Codegraph @

tests/test_cz_customize.py

raw
1 import re
2 from pathlib import Path
3
4 import pytest
5
6 from commitizen.config import BaseConfig
7 from commitizen.config.json_config import JsonConfig
8 from commitizen.config.toml_config import TomlConfig
9 from commitizen.config.yaml_config import YAMLConfig
10 from commitizen.cz.customize import CustomizeCommitsCz
11 from commitizen.exceptions import MissingCzCustomizeConfigError
12
13 TOML_STR = r"""
14 [tool.commitizen.customize]
15 message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
16 example = "feature: this feature enables customization through a config file"
17 schema = "<type>: <body>"
18 schema_pattern = "(feature|bug fix):(\\s.*)"
19 commit_parser = "^(?P<change_type>feature|bug fix):\\s(?P<message>.*)?"
20 changelog_pattern = "^(feature|bug fix)?(!)?"
21 change_type_map = {"feature" = "Feat", "bug fix" = "Fix"}
22
23 bump_pattern = "^(break|new|fix|hotfix)"
24 bump_map = {"break" = "MAJOR", "new" = "MINOR", "fix" = "PATCH", "hotfix" = "PATCH"}
25 change_type_order = ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"]
26 info = "This is a customized cz."
27
28 [[tool.commitizen.customize.questions]]
29 type = "list"
30 name = "change_type"
31 choices = [
32 {value = "feature", name = "feature: A new feature."},
33 {value = "bug fix", name = "bug fix: A bug fix."}
34 ]
35 message = "Select the type of change you are committing"
36
37 [[tool.commitizen.customize.questions]]
38 type = "input"
39 name = "message"
40 message = "Body."
41
42 [[tool.commitizen.customize.questions]]
43 type = "confirm"
44 name = "show_message"
45 message = "Do you want to add body message in commit?"
46 """
47
48 JSON_STR = r"""
49 {
50 "commitizen": {
51 "name": "cz_jira",
52 "version": "1.0.0",
53 "version_files": [
54 "commitizen/__version__.py",
55 "pyproject.toml"
56 ],
57 "customize": {
58 "message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
59 "example": "feature: this feature enables customization through a config file",
60 "schema": "<type>: <body>",
61 "schema_pattern": "(feature|bug fix):(\\s.*)",
62 "bump_pattern": "^(break|new|fix|hotfix)",
63 "bump_map": {
64 "break": "MAJOR",
65 "new": "MINOR",
66 "fix": "PATCH",
67 "hotfix": "PATCH"
68 },
69 "commit_parser": "^(?P<change_type>feature|bug fix):\\s(?P<message>.*)?",
70 "changelog_pattern": "^(feature|bug fix)?(!)?",
71 "change_type_map": {"feature": "Feat", "bug fix": "Fix"},
72 "change_type_order": ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"],
73 "info": "This is a customized cz.",
74 "questions": [
75 {
76 "type": "list",
77 "name": "change_type",
78 "choices": [
79 {
80 "value": "feature",
81 "name": "feature: A new feature."
82 },
83 {
84 "value": "bug fix",
85 "name": "bug fix: A bug fix."
86 }
87 ],
88 "message": "Select the type of change you are committing"
89 },
90 {
91 "type": "input",
92 "name": "message",
93 "message": "Body."
94 },
95 {
96 "type": "confirm",
97 "name": "show_message",
98 "message": "Do you want to add body message in commit?"
99 }
100 ]
101 }
102 }
103 }
104 """
105
106 YAML_STR = """
107 commitizen:
108 name: cz_jira
109 version: 1.0.0
110 version_files:
111 - commitizen/__version__.py
112 - pyproject.toml
113 customize:
114 message_template: '{{change_type}}:{% if show_message %} {{message}}{% endif %}'
115 example: 'feature: this feature enables customization through a config file'
116 schema: '<type>: <body>'
117 schema_pattern: '(feature|bug fix):(\\s.*)'
118 bump_pattern: '^(break|new|fix|hotfix)'
119 commit_parser: '^(?P<change_type>feature|bug fix):\\s(?P<message>.*)?'
120 changelog_pattern: '^(feature|bug fix)?(!)?'
121 change_type_map:
122 feature: Feat
123 bug fix: Fix
124 bump_map:
125 break: MAJOR
126 new: MINOR
127 fix: PATCH
128 hotfix: PATCH
129 change_type_order: ['perf', 'BREAKING CHANGE', 'feat', 'fix', 'refactor']
130 info: This is a customized cz.
131 questions:
132 - type: list
133 name: change_type
134 choices:
135 - value: feature
136 name: 'feature: A new feature.'
137 - value: bug fix
138 name: 'bug fix: A bug fix.'
139 message: Select the type of change you are committing
140 - type: input
141 name: message
142 message: Body.
143 - type: confirm
144 name: show_message
145 message: Do you want to add body message in commit?
146 """
147
148 TOML_WITH_UNICODE = r"""
149 [tool.commitizen]
150 name = "cz_customize"
151 version = "1.0.0"
152 version_files = [
153 "commitizen/__version__.py",
154 "pyproject.toml:version"
155 ]
156 [tool.commitizen.customize]
157 message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
158 example = "✨ feature: this feature enables customization through a config file"
159 schema = "<type>: <body>"
160 schema_pattern = "(✨ feature|🐛 bug fix):(\\s.*)"
161 commit_parser = "^(?P<change_type>✨ feature|🐛 bug fix):\\s(?P<message>.*)?"
162 changelog_pattern = "^(✨ feature|🐛 bug fix)?(!)?"
163 change_type_map = {"✨ feature" = "Feat", "🐛 bug fix" = "Fix"}
164 bump_pattern = "^(✨ feat|🐛 bug fix)"
165 bump_map = {"break" = "MAJOR", "✨ feat" = "MINOR", "🐛 bug fix" = "MINOR"}
166 change_type_order = ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"]
167 info = "This is a customized cz with emojis 🎉!"
168 [[tool.commitizen.customize.questions]]
169 type = "list"
170 name = "change_type"
171 choices = [
172 {value = "✨ feature", name = "✨ feature: A new feature."},
173 {value = "🐛 bug fix", name = "🐛 bug fix: A bug fix."}
174 ]
175 message = "Select the type of change you are committing"
176 [[tool.commitizen.customize.questions]]
177 type = "input"
178 name = "message"
179 message = "Body."
180 [[tool.commitizen.customize.questions]]
181 type = "confirm"
182 name = "show_message"
183 message = "Do you want to add body message in commit?"
184 """
185
186 JSON_WITH_UNICODE = r"""
187 {
188 "commitizen": {
189 "name": "cz_customize",
190 "version": "1.0.0",
191 "version_files": [
192 "commitizen/__version__.py",
193 "pyproject.toml:version"
194 ],
195 "customize": {
196 "message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
197 "example": "✨ feature: this feature enables customization through a config file",
198 "schema": "<type>: <body>",
199 "schema_pattern": "(✨ feature|🐛 bug fix):(\\s.*)",
200 "bump_pattern": "^(✨ feat|🐛 bug fix)",
201 "bump_map": {
202 "break": "MAJOR",
203 "✨ feat": "MINOR",
204 "🐛 bug fix": "MINOR"
205 },
206 "commit_parser": "^(?P<change_type>✨ feature|🐛 bug fix):\\s(?P<message>.*)?",
207 "changelog_pattern": "^(✨ feature|🐛 bug fix)?(!)?",
208 "change_type_map": {"✨ feature": "Feat", "🐛 bug fix": "Fix"},
209 "change_type_order": ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"],
210 "info": "This is a customized cz with emojis 🎉!",
211 "questions": [
212 {
213 "type": "list",
214 "name": "change_type",
215 "choices": [
216 {
217 "value": "✨ feature",
218 "name": "✨ feature: A new feature."
219 },
220 {
221 "value": "🐛 bug fix",
222 "name": "🐛 bug fix: A bug fix."
223 }
224 ],
225 "message": "Select the type of change you are committing"
226 },
227 {
228 "type": "input",
229 "name": "message",
230 "message": "Body."
231 },
232 {
233 "type": "confirm",
234 "name": "show_message",
235 "message": "Do you want to add body message in commit?"
236 }
237 ]
238 }
239 }
240 }
241 """
242
243 TOML_STR_INFO_PATH = """
244 [tool.commitizen.customize]
245 message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
246 example = "feature: this feature enables customization through a config file"
247 schema = "<type>: <body>"
248 bump_pattern = "^(break|new|fix|hotfix)"
249 bump_map = {"break" = "MAJOR", "new" = "MINOR", "fix" = "PATCH", "hotfix" = "PATCH"}
250 info_path = "info.txt"
251 """
252
253 JSON_STR_INFO_PATH = r"""
254 {
255 "commitizen": {
256 "customize": {
257 "message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
258 "example": "feature: this feature enables customization through a config file",
259 "schema": "<type>: <body>",
260 "bump_pattern": "^(break|new|fix|hotfix)",
261 "bump_map": {
262 "break": "MAJOR",
263 "new": "MINOR",
264 "fix": "PATCH",
265 "hotfix": "PATCH"
266 },
267 "info_path": "info.txt"
268 }
269 }
270 }
271 """
272
273 YAML_STR_INFO_PATH = """
274 commitizen:
275 customize:
276 message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
277 example: 'feature: this feature enables customization through a config file'
278 schema: "<type>: <body>"
279 bump_pattern: "^(break|new|fix|hotfix)"
280 bump_map:
281 break: MAJOR
282 new: MINOR
283 fix: PATCH
284 hotfix: PATCH
285 info_path: info.txt
286 """
287
288 TOML_STR_WITHOUT_INFO = """
289 [tool.commitizen.customize]
290 message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
291 example = "feature: this feature enables customization through a config file"
292 schema = "<type>: <body>"
293 bump_pattern = "^(break|new|fix|hotfix)"
294 bump_map = {"break" = "MAJOR", "new" = "MINOR", "fix" = "PATCH", "hotfix" = "PATCH"}
295 """
296
297 JSON_STR_WITHOUT_PATH = r"""
298 {
299 "commitizen": {
300 "customize": {
301 "message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
302 "example": "feature: this feature enables customization through a config file",
303 "schema": "<type>: <body>",
304 "bump_pattern": "^(break|new|fix|hotfix)",
305 "bump_map": {
306 "break": "MAJOR",
307 "new": "MINOR",
308 "fix": "PATCH",
309 "hotfix": "PATCH"
310 }
311 }
312 }
313 }
314 """
315
316 YAML_STR_WITHOUT_PATH = """
317 commitizen:
318 customize:
319 message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
320 example: 'feature: this feature enables customization through a config file'
321 schema: "<type>: <body>"
322 bump_pattern: "^(break|new|fix|hotfix)"
323 bump_map:
324 break: MAJOR
325 new: MINOR
326 fix: PATCH
327 hotfix: PATCH
328 """
329
330
331 @pytest.fixture(
332 params=[
333 TomlConfig(data=TOML_STR, path=Path("not_exist.toml")),
334 JsonConfig(data=JSON_STR, path=Path("not_exist.json")),
335 YAMLConfig(data=YAML_STR, path=Path("not_exist.yaml")),
336 ]
337 )
338 def config(request):
339 """Parametrize the config fixture
340
341 This fixture allow to test multiple config formats,
342 without add the builtin parametrize decorator
343 """
344 return request.param
345
346
347 @pytest.fixture(
348 params=[
349 TomlConfig(data=TOML_STR_INFO_PATH, path=Path("not_exist.toml")),
350 JsonConfig(data=JSON_STR_INFO_PATH, path=Path("not_exist.json")),
351 YAMLConfig(data=YAML_STR_INFO_PATH, path=Path("not_exist.yaml")),
352 ]
353 )
354 def config_info(request):
355 return request.param
356
357
358 @pytest.fixture(
359 params=[
360 TomlConfig(data=TOML_STR_WITHOUT_INFO, path=Path("not_exist.toml")),
361 JsonConfig(data=JSON_STR_WITHOUT_PATH, path=Path("not_exist.json")),
362 YAMLConfig(data=YAML_STR_WITHOUT_PATH, path=Path("not_exist.yaml")),
363 ]
364 )
365 def config_without_info(request):
366 return request.param
367
368
369 @pytest.fixture(
370 params=[
371 TomlConfig(data=TOML_WITH_UNICODE, path=Path("not_exist.toml")),
372 JsonConfig(data=JSON_WITH_UNICODE, path=Path("not_exist.json")),
373 ]
374 )
375 def config_with_unicode(request):
376 return request.param
377
378
379 def test_initialize_cz_customize_failed():
380 config = BaseConfig()
381 with pytest.raises(
382 MissingCzCustomizeConfigError,
383 match=re.escape(MissingCzCustomizeConfigError.message),
384 ):
385 CustomizeCommitsCz(config)
386
387
388 def test_bump_pattern(config):
389 cz = CustomizeCommitsCz(config)
390 assert cz.bump_pattern == "^(break|new|fix|hotfix)"
391
392
393 def test_bump_pattern_unicode(config_with_unicode):
394 cz = CustomizeCommitsCz(config_with_unicode)
395 assert cz.bump_pattern == "^(✨ feat|🐛 bug fix)"
396
397
398 def test_bump_map(config):
399 cz = CustomizeCommitsCz(config)
400 assert cz.bump_map == {
401 "break": "MAJOR",
402 "new": "MINOR",
403 "fix": "PATCH",
404 "hotfix": "PATCH",
405 }
406
407
408 def test_bump_map_unicode(config_with_unicode):
409 cz = CustomizeCommitsCz(config_with_unicode)
410 assert cz.bump_map == {
411 "break": "MAJOR",
412 "✨ feat": "MINOR",
413 "🐛 bug fix": "MINOR",
414 }
415
416
417 def test_bump_map_major_version_zero_is_derived_from_bump_map():
418 """Regression test for #1728: when the user provides ``bump_map`` but no
419 explicit ``bump_map_major_version_zero``, the latter is derived from the
420 former (``MAJOR`` → ``MINOR``) instead of falling through to the default
421 ``defaults.BUMP_MAP_MAJOR_VERSION_ZERO``."""
422 config = BaseConfig()
423 config.settings.update(
424 {
425 "name": "cz_customize",
426 "customize": {
427 "bump_pattern": r"^(feat|fix|docs)",
428 "bump_map": {
429 "break": "MAJOR",
430 "feat": "PATCH",
431 "docs": "PATCH",
432 },
433 },
434 }
435 )
436
437 cz = CustomizeCommitsCz(config)
438
439 # Same patterns, MAJOR demoted to MINOR.
440 assert dict(cz.bump_map_major_version_zero) == {
441 "break": "MINOR",
442 "feat": "PATCH",
443 "docs": "PATCH",
444 }
445
446
447 def test_bump_map_major_version_zero_explicit_user_value_wins():
448 """If the user explicitly sets ``bump_map_major_version_zero``, that
449 value is used verbatim (no derivation)."""
450 config = BaseConfig()
451 config.settings.update(
452 {
453 "name": "cz_customize",
454 "customize": {
455 "bump_pattern": r"^(feat|fix|docs)",
456 "bump_map": {
457 "break": "MAJOR",
458 "feat": "PATCH",
459 },
460 "bump_map_major_version_zero": {
461 "break": "MAJOR", # NB: kept as MAJOR
462 "feat": "PATCH",
463 },
464 },
465 }
466 )
467
468 cz = CustomizeCommitsCz(config)
469
470 assert dict(cz.bump_map_major_version_zero) == {
471 "break": "MAJOR",
472 "feat": "PATCH",
473 }
474
475
476 def test_bump_map_major_version_zero_falls_back_to_defaults_without_bump_map():
477 """When the user provides neither ``bump_map`` nor
478 ``bump_map_major_version_zero``, the class default applies."""
479 from commitizen import defaults
480
481 config = BaseConfig()
482 config.settings.update(
483 {
484 "name": "cz_customize",
485 "customize": {
486 # No bump_map, no bump_map_major_version_zero.
487 "schema_pattern": r"^(feat|fix): (.*)$",
488 },
489 }
490 )
491
492 cz = CustomizeCommitsCz(config)
493
494 assert cz.bump_map_major_version_zero is defaults.BUMP_MAP_MAJOR_VERSION_ZERO
495
496
497 def test_change_type_order(config):
498 cz = CustomizeCommitsCz(config)
499 assert cz.change_type_order == [
500 "perf",
501 "BREAKING CHANGE",
502 "feat",
503 "fix",
504 "refactor",
505 ]
506
507
508 def test_change_type_order_unicode(config_with_unicode):
509 cz = CustomizeCommitsCz(config_with_unicode)
510 assert cz.change_type_order == [
511 "perf",
512 "BREAKING CHANGE",
513 "feat",
514 "fix",
515 "refactor",
516 ]
517
518
519 def test_questions(config):
520 cz = CustomizeCommitsCz(config)
521 questions = cz.questions()
522 expected_questions = [
523 {
524 "type": "list",
525 "name": "change_type",
526 "choices": [
527 {"value": "feature", "name": "feature: A new feature."},
528 {"value": "bug fix", "name": "bug fix: A bug fix."},
529 ],
530 "message": "Select the type of change you are committing",
531 },
532 {"type": "input", "name": "message", "message": "Body."},
533 {
534 "type": "confirm",
535 "name": "show_message",
536 "message": "Do you want to add body message in commit?",
537 },
538 ]
539 assert list(questions) == expected_questions
540
541
542 def test_questions_unicode(config_with_unicode):
543 cz = CustomizeCommitsCz(config_with_unicode)
544 questions = cz.questions()
545 expected_questions = [
546 {
547 "type": "list",
548 "name": "change_type",
549 "choices": [
550 {"value": "✨ feature", "name": "✨ feature: A new feature."},
551 {"value": "🐛 bug fix", "name": "🐛 bug fix: A bug fix."},
552 ],
553 "message": "Select the type of change you are committing",
554 },
555 {"type": "input", "name": "message", "message": "Body."},
556 {
557 "type": "confirm",
558 "name": "show_message",
559 "message": "Do you want to add body message in commit?",
560 },
561 ]
562 assert list(questions) == expected_questions
563
564
565 def test_answer(config):
566 cz = CustomizeCommitsCz(config)
567 answers = {
568 "change_type": "feature",
569 "message": "this feature enable customize through config file",
570 "show_message": True,
571 }
572 message = cz.message(answers)
573 assert message == "feature: this feature enable customize through config file"
574
575 cz = CustomizeCommitsCz(config)
576 answers = {
577 "change_type": "feature",
578 "message": "this feature enable customize through config file",
579 "show_message": False,
580 }
581 message = cz.message(answers)
582 assert message == "feature:"
583
584
585 def test_answer_unicode(config_with_unicode):
586 cz = CustomizeCommitsCz(config_with_unicode)
587 answers = {
588 "change_type": "✨ feature",
589 "message": "this feature enables customization through a config file",
590 "show_message": True,
591 }
592 message = cz.message(answers)
593 assert (
594 message
595 == "✨ feature: this feature enables customization through a config file"
596 )
597
598 cz = CustomizeCommitsCz(config_with_unicode)
599 answers = {
600 "change_type": "✨ feature",
601 "message": "this feature enables customization through a config file",
602 "show_message": False,
603 }
604 message = cz.message(answers)
605 assert message == "✨ feature:"
606
607
608 def test_example(config):
609 cz = CustomizeCommitsCz(config)
610 assert (
611 "feature: this feature enables customization through a config file"
612 in cz.example()
613 )
614
615
616 def test_example_unicode(config_with_unicode):
617 cz = CustomizeCommitsCz(config_with_unicode)
618 assert (
619 "✨ feature: this feature enables customization through a config file"
620 in cz.example()
621 )
622
623
624 def test_schema(config):
625 cz = CustomizeCommitsCz(config)
626 assert "<type>: <body>" in cz.schema()
627
628
629 def test_schema_pattern(config):
630 cz = CustomizeCommitsCz(config)
631 assert r"(feature|bug fix):(\s.*)" in cz.schema_pattern()
632
633
634 def test_schema_pattern_unicode(config_with_unicode):
635 cz = CustomizeCommitsCz(config_with_unicode)
636 assert r"(✨ feature|🐛 bug fix):(\s.*)" in cz.schema_pattern()
637
638
639 def test_info(config):
640 cz = CustomizeCommitsCz(config)
641 assert "This is a customized cz." in cz.info()
642
643
644 def test_info_unicode(config_with_unicode):
645 cz = CustomizeCommitsCz(config_with_unicode)
646 assert "This is a customized cz with emojis 🎉!" in cz.info()
647
648
649 def test_info_with_info_path(tmp_path, monkeypatch, config_info):
650 monkeypatch.chdir(tmp_path)
651 tmpfile = tmp_path / "info.txt"
652 tmpfile.write_text("Test info")
653
654 cz = CustomizeCommitsCz(config_info)
655 assert "Test info" in cz.info()
656
657
658 def test_info_without_info(config_without_info):
659 cz = CustomizeCommitsCz(config_without_info)
660 assert cz.info() == ""
661
662
663 def test_commit_parser(config):
664 cz = CustomizeCommitsCz(config)
665 assert cz.commit_parser == "^(?P<change_type>feature|bug fix):\\s(?P<message>.*)?"
666
667
668 def test_commit_parser_unicode(config_with_unicode):
669 cz = CustomizeCommitsCz(config_with_unicode)
670 assert (
671 cz.commit_parser
672 == "^(?P<change_type>✨ feature|🐛 bug fix):\\s(?P<message>.*)?"
673 )
674
675
676 def test_changelog_pattern(config):
677 cz = CustomizeCommitsCz(config)
678 assert cz.changelog_pattern == "^(feature|bug fix)?(!)?"
679
680
681 def test_changelog_pattern_unicode(config_with_unicode):
682 cz = CustomizeCommitsCz(config_with_unicode)
683 assert cz.changelog_pattern == "^(✨ feature|🐛 bug fix)?(!)?"
684
685
686 def test_change_type_map(config):
687 cz = CustomizeCommitsCz(config)
688 assert cz.change_type_map == {"feature": "Feat", "bug fix": "Fix"}
689
690
691 def test_change_type_map_unicode(config_with_unicode):
692 cz = CustomizeCommitsCz(config_with_unicode)
693 assert cz.change_type_map == {"✨ feature": "Feat", "🐛 bug fix": "Fix"}