1
1
import pytest
2
+ from pytest_mock import MockFixture
2
3
4
+ from commitizen import cmd , commands
3
5
from commitizen .config import BaseConfig , JsonConfig , TomlConfig , YAMLConfig
4
6
from commitizen .cz .customize import CustomizeCommitsCz
7
+ from commitizen .cz .utils import multiple_line_breaker , required_validator_subject_strip
5
8
from commitizen .exceptions import MissingCzCustomizeConfigError
6
9
7
10
TOML_STR = r"""
36
39
]
37
40
message = "Select the type of change you are committing"
38
41
42
+ [[tool.commitizen.customize.questions]]
43
+ type = "input"
44
+ name = "subject"
45
+ message = "Subject."
46
+ filter = "required_validator_subject_strip"
47
+
39
48
[[tool.commitizen.customize.questions]]
40
49
type = "input"
41
50
name = "message"
42
51
message = "Body."
52
+ filter = "multiple_line_breaker"
43
53
44
54
[[tool.commitizen.customize.questions]]
45
55
type = "confirm"
89
99
],
90
100
"message": "Select the type of change you are committing"
91
101
},
102
+ {
103
+ "type": "input",
104
+ "name": "subject",
105
+ "message": "Subject.",
106
+ "filter": "required_validator_subject_strip"
107
+ },
92
108
{
93
109
"type": "input",
94
110
"name": "message",
95
- "message": "Body."
111
+ "message": "Body.",
112
+ "filter": "multiple_line_breaker"
96
113
},
97
114
{
98
115
"type": "confirm",
139
156
- value: bug fix
140
157
name: 'bug fix: A bug fix.'
141
158
message: Select the type of change you are committing
159
+ - type: input
160
+ name: subject
161
+ message: Subject.
162
+ filter: required_validator_subject_strip
142
163
- type: input
143
164
name: message
144
165
message: Body.
166
+ filter: multiple_line_breaker
145
167
- type: confirm
146
168
name: show_message
147
169
message: Do you want to add body message in commit?
330
352
"""
331
353
332
354
355
+ @pytest .fixture
356
+ def staging_is_clean (mocker : MockFixture , tmp_git_project ):
357
+ is_staging_clean_mock = mocker .patch ("commitizen.git.is_staging_clean" )
358
+ is_staging_clean_mock .return_value = False
359
+ return tmp_git_project
360
+
361
+
333
362
@pytest .fixture (
334
363
params = [
335
364
TomlConfig (data = TOML_STR , path = "not_exist.toml" ),
@@ -437,7 +466,7 @@ def test_change_type_order_unicode(config_with_unicode):
437
466
]
438
467
439
468
440
- def test_questions (config ):
469
+ def test_questions_default (config ):
441
470
cz = CustomizeCommitsCz (config )
442
471
questions = cz .questions ()
443
472
expected_questions = [
@@ -450,7 +479,18 @@ def test_questions(config):
450
479
],
451
480
"message" : "Select the type of change you are committing" ,
452
481
},
453
- {"type" : "input" , "name" : "message" , "message" : "Body." },
482
+ {
483
+ "type" : "input" ,
484
+ "name" : "subject" ,
485
+ "message" : "Subject." ,
486
+ "filter" : "required_validator_subject_strip" ,
487
+ },
488
+ {
489
+ "type" : "input" ,
490
+ "name" : "message" ,
491
+ "message" : "Body." ,
492
+ "filter" : "multiple_line_breaker" ,
493
+ },
454
494
{
455
495
"type" : "confirm" ,
456
496
"name" : "show_message" ,
@@ -460,6 +500,38 @@ def test_questions(config):
460
500
assert list (questions ) == expected_questions
461
501
462
502
503
+ @pytest .mark .usefixtures ("staging_is_clean" )
504
+ def test_questions_filter (config , mocker : MockFixture ):
505
+ is_staging_clean_mock = mocker .patch ("commitizen.git.is_staging_clean" )
506
+ is_staging_clean_mock .return_value = False
507
+
508
+ prompt_mock = mocker .patch ("questionary.prompt" )
509
+ prompt_mock .return_value = {
510
+ "change_type" : "feature" ,
511
+ "subject" : "user created" ,
512
+ "message" : "body of the commit" ,
513
+ "show_message" : True ,
514
+ }
515
+
516
+ commit_mock = mocker .patch ("commitizen.git.commit" )
517
+ commit_mock .return_value = cmd .Command ("success" , "" , b"" , b"" , 0 )
518
+
519
+ commands .Commit (config , {})()
520
+
521
+ prompts_questions = prompt_mock .call_args [0 ][0 ]
522
+ assert prompts_questions [0 ]["type" ] == "list"
523
+ assert prompts_questions [0 ]["name" ] == "change_type"
524
+ assert prompts_questions [0 ]["use_shortcuts" ] is False
525
+ assert prompts_questions [1 ]["type" ] == "input"
526
+ assert prompts_questions [1 ]["name" ] == "subject"
527
+ assert prompts_questions [1 ]["filter" ] == required_validator_subject_strip
528
+ assert prompts_questions [2 ]["type" ] == "input"
529
+ assert prompts_questions [2 ]["name" ] == "message"
530
+ assert prompts_questions [2 ]["filter" ] == multiple_line_breaker
531
+ assert prompts_questions [3 ]["type" ] == "confirm"
532
+ assert prompts_questions [3 ]["name" ] == "show_message"
533
+
534
+
463
535
def test_questions_unicode (config_with_unicode ):
464
536
cz = CustomizeCommitsCz (config_with_unicode )
465
537
questions = cz .questions ()
0 commit comments