-
-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathtest_cz_conventional_commits.py
More file actions
267 lines (225 loc) · 8.73 KB
/
Copy pathtest_cz_conventional_commits.py
File metadata and controls
267 lines (225 loc) · 8.73 KB
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
import pytest
from commitizen.cz.conventional_commits.conventional_commits import (
ConventionalCommitsCz,
_parse_scope,
_parse_subject,
)
from commitizen.cz.exceptions import AnswerRequiredError
@pytest.mark.parametrize(
"valid_scope", ["", "simple", "dash-separated", "camelCaseUPPERCASE"]
)
def test_parse_scope_valid_values(valid_scope):
assert valid_scope == _parse_scope(valid_scope)
@pytest.mark.parametrize(
"scopes_transformation", [["with spaces", "with-spaces"], ["", ""]]
)
def test_scopes_transformations(scopes_transformation):
invalid_scope, transformed_scope = scopes_transformation
assert transformed_scope == _parse_scope(invalid_scope)
@pytest.mark.parametrize("valid_subject", ["this is a normal text", "aword"])
def test_parse_subject_valid_values(valid_subject):
assert valid_subject == _parse_subject(valid_subject)
@pytest.mark.parametrize("invalid_subject", ["", " ", ".", " .", "\t\t."])
def test_parse_subject_invalid_values(invalid_subject):
with pytest.raises(AnswerRequiredError):
_parse_subject(invalid_subject)
@pytest.mark.parametrize("subject_transformation", [["with dot.", "with dot"]])
def test_subject_transformations(subject_transformation):
invalid_subject, transformed_subject = subject_transformation
assert transformed_subject == _parse_subject(invalid_subject)
def test_questions(config):
conventional_commits = ConventionalCommitsCz(config)
questions = conventional_commits.questions()
assert isinstance(questions, list)
assert isinstance(questions[0], dict)
def test_choices_all_have_keyboard_shortcuts(config):
conventional_commits = ConventionalCommitsCz(config)
questions = conventional_commits.questions()
list_questions = (q for q in questions if q["type"] == "list")
for select in list_questions:
assert all("key" in choice for choice in select["choices"])
def test_small_answer(config):
conventional_commits = ConventionalCommitsCz(config)
answers = {
"prefix": "fix",
"scope": "users",
"subject": "email pattern corrected",
"is_breaking_change": False,
"body": "",
"footer": "",
}
message = conventional_commits.message(answers)
assert message == "fix(users): email pattern corrected"
def test_long_answer(config):
conventional_commits = ConventionalCommitsCz(config)
answers = {
"prefix": "fix",
"scope": "users",
"subject": "email pattern corrected",
"is_breaking_change": False,
"body": "complete content",
"footer": "closes #24",
}
message = conventional_commits.message(answers)
assert (
message
== "fix(users): email pattern corrected\n\ncomplete content\n\ncloses #24"
)
def test_breaking_change_in_footer(config):
conventional_commits = ConventionalCommitsCz(config)
answers = {
"prefix": "fix",
"scope": "users",
"subject": "email pattern corrected",
"is_breaking_change": True,
"body": "complete content",
"footer": "migrate by renaming user to users",
}
message = conventional_commits.message(answers)
print(message)
assert (
message
== "fix(users): email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users"
)
@pytest.mark.parametrize(
("scope", "breaking_change_exclamation_in_title", "expected_message"),
[
# Test with scope and breaking_change_exclamation_in_title enabled
(
"users",
True,
"feat(users)!: email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users",
),
# Test without scope and breaking_change_exclamation_in_title enabled
(
"",
True,
"feat!: email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users",
),
# Test with scope and breaking_change_exclamation_in_title disabled
(
"users",
False,
"feat(users): email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users",
),
# Test without scope and breaking_change_exclamation_in_title disabled
(
"",
False,
"feat: email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users",
),
],
)
def test_breaking_change_message_formats(
config, scope, breaking_change_exclamation_in_title, expected_message
):
# Set the breaking_change_exclamation_in_title setting
config.settings["breaking_change_exclamation_in_title"] = (
breaking_change_exclamation_in_title
)
conventional_commits = ConventionalCommitsCz(config)
answers = {
"prefix": "feat",
"scope": scope,
"subject": "email pattern corrected",
"is_breaking_change": True,
"body": "complete content",
"footer": "migrate by renaming user to users",
}
message = conventional_commits.message(answers)
assert message == expected_message
def test_example(config):
"""just testing a string is returned. not the content"""
conventional_commits = ConventionalCommitsCz(config)
example = conventional_commits.example()
assert isinstance(example, str)
def test_schema(config):
"""just testing a string is returned. not the content"""
conventional_commits = ConventionalCommitsCz(config)
schema = conventional_commits.schema()
assert isinstance(schema, str)
def test_info(config):
"""just testing a string is returned. not the content"""
conventional_commits = ConventionalCommitsCz(config)
info = conventional_commits.info()
assert isinstance(info, str)
def test_override_takes_precedence_over_extend(config):
config.settings["override"] = {
"bump_map": {
"^bar": "PATCH",
}
}
config.settings["extend"] = {
"bump_map": {
"^foo": "MINOR",
}
}
conventional_commits = ConventionalCommitsCz(config)
assert conventional_commits.bump_map == {"^bar": "PATCH"}
@pytest.mark.parametrize("mode", ["override", "extend"])
def test_apply_supported_settings(mode, config):
config.settings[mode] = {
"bump_pattern": r"^foo:",
"bump_map": {r"^foo": "MINOR"},
"bump_map_major_version_zero": {r"^foo": "PATCH"},
"commit_parser": r"^(?P<change_type>foo):\s(?P<message>.*)$",
"changelog_pattern": r"^(foo)",
"change_type_map": {"foo": "Foo"},
"change_type_choices": [
{
"value": "foo",
"name": "foo: custom type",
"key": "o",
}
],
}
conventional_commits = ConventionalCommitsCz(config)
assert conventional_commits.bump_pattern == r"^foo:"
assert (
conventional_commits.commit_parser
== r"^(?P<change_type>foo):\s(?P<message>.*)$"
)
assert conventional_commits.changelog_pattern == r"^(foo)"
if mode == "override":
assert conventional_commits.bump_map == {r"^foo": "MINOR"}
assert conventional_commits.bump_map_major_version_zero == {r"^foo": "PATCH"}
assert conventional_commits.change_type_map == {"foo": "Foo"}
assert conventional_commits.change_type_choices == [
{
"value": "foo",
"name": "foo: custom type",
"key": "o",
}
]
else:
assert conventional_commits.bump_map[r"^foo"] == "MINOR"
assert conventional_commits.bump_map_major_version_zero[r"^foo"] == "PATCH"
assert conventional_commits.change_type_map["foo"] == "Foo"
assert r"^feat" in conventional_commits.bump_map
assert any(
choice["value"] == "foo"
for choice in conventional_commits.change_type_choices
)
assert any(
choice["value"] == "fix"
for choice in conventional_commits.change_type_choices
)
def test_extend_settings_do_not_leak_to_other_instances(config):
config.settings["extend"] = {
"bump_map": {r"^foo": "MINOR"},
"change_type_choices": [
{
"value": "foo",
"name": "foo: custom type",
"key": "o",
}
],
}
first = ConventionalCommitsCz(config)
clean_config = config.__class__()
clean_config.settings.update({"name": "cz_conventional_commits"})
second = ConventionalCommitsCz(clean_config)
assert r"^foo" in first.bump_map
assert any(choice["value"] == "foo" for choice in first.change_type_choices)
assert r"^foo" not in second.bump_map
assert all(choice["value"] != "foo" for choice in second.change_type_choices)