forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_conf.py
More file actions
417 lines (353 loc) · 13.9 KB
/
test_conf.py
File metadata and controls
417 lines (353 loc) · 13.9 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from __future__ import annotations
import json
import os
from pathlib import Path
from typing import Any
import pytest
import yaml
from commitizen import config, defaults, git
from commitizen.config.json_config import JsonConfig
from commitizen.config.toml_config import TomlConfig
from commitizen.config.yaml_config import YAMLConfig
from commitizen.exceptions import ConfigFileIsEmpty, InvalidConfigurationError
PYPROJECT = """
[tool.commitizen]
name = "cz_jira"
version = "1.0.0"
version_files = [
"commitizen/__version__.py",
"pyproject.toml"
]
style = [
["pointer", "reverse"],
["question", "underline"]
]
pre_bump_hooks = [
"scripts/generate_documentation.sh"
]
post_bump_hooks = ["scripts/slack_notification.sh"]
[tool.black]
line-length = 88
target-version = ['py36', 'py37', 'py38']
"""
DICT_CONFIG = {
"commitizen": {
"name": "cz_jira",
"version": "1.0.0",
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"pre_bump_hooks": ["scripts/generate_documentation.sh"],
"post_bump_hooks": ["scripts/slack_notification.sh"],
}
}
JSON_STR = r"""
{
"commitizen": {
"name": "cz_jira",
"version": "1.0.0",
"version_files": [
"commitizen/__version__.py",
"pyproject.toml"
]
}
}
"""
YAML_STR = """
commitizen:
name: cz_jira
version: 1.0.0
version_files:
- commitizen/__version__.py
- pyproject.toml
"""
_settings: dict[str, Any] = {
"name": "cz_jira",
"version": "1.0.0",
"version_provider": "commitizen",
"version_scheme": None,
"tag_format": "$version",
"legacy_tag_formats": [],
"ignored_tag_formats": [],
"bump_message": None,
"retry_after_failure": False,
"allow_abort": False,
"allowed_prefixes": [
"Merge",
"Revert",
"Pull request",
"fixup!",
"squash!",
"amend!",
],
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"changelog_file": "CHANGELOG.md",
"changelog_format": None,
"changelog_incremental": False,
"changelog_start_rev": None,
"changelog_merge_prerelease": False,
"update_changelog_on_bump": False,
"use_shortcuts": False,
"major_version_zero": False,
"pre_bump_hooks": ["scripts/generate_documentation.sh"],
"post_bump_hooks": ["scripts/slack_notification.sh"],
"prerelease_offset": 0,
"encoding": "utf-8",
"always_signoff": False,
"template": None,
"extras": {},
"breaking_change_exclamation_in_title": False,
"message_length_limit": None,
}
_new_settings: dict[str, Any] = {
"name": "cz_jira",
"version": "2.0.0",
"version_provider": "commitizen",
"version_scheme": None,
"tag_format": "$version",
"legacy_tag_formats": [],
"ignored_tag_formats": [],
"bump_message": None,
"retry_after_failure": False,
"allow_abort": False,
"allowed_prefixes": [
"Merge",
"Revert",
"Pull request",
"fixup!",
"squash!",
"amend!",
],
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"changelog_file": "CHANGELOG.md",
"changelog_format": None,
"changelog_incremental": False,
"changelog_start_rev": None,
"changelog_merge_prerelease": False,
"update_changelog_on_bump": False,
"use_shortcuts": False,
"major_version_zero": False,
"pre_bump_hooks": ["scripts/generate_documentation.sh"],
"post_bump_hooks": ["scripts/slack_notification.sh"],
"prerelease_offset": 0,
"encoding": "utf-8",
"always_signoff": False,
"template": None,
"extras": {},
"breaking_change_exclamation_in_title": False,
"message_length_limit": None,
}
@pytest.fixture
def config_files_manager(request, tmpdir):
with tmpdir.as_cwd():
filename = request.param
with open(filename, "w", encoding="utf-8") as f:
if "toml" in filename:
f.write(PYPROJECT)
elif "json" in filename:
json.dump(DICT_CONFIG, f)
elif "yaml" in filename:
yaml.dump(DICT_CONFIG, f)
yield
@pytest.mark.usefixtures("in_repo_root")
def test_find_git_project_root(tmpdir):
assert git.find_git_project_root() == Path(os.getcwd())
with tmpdir.as_cwd() as _:
assert git.find_git_project_root() is None
@pytest.mark.parametrize(
"config_files_manager", defaults.CONFIG_FILES.copy(), indirect=True
)
def test_set_key(config_files_manager):
_conf = config.read_cfg()
_conf.set_key("version", "2.0.0")
cfg = config.read_cfg()
assert cfg.settings == _new_settings
class TestReadCfg:
@pytest.mark.parametrize(
"config_files_manager", defaults.CONFIG_FILES.copy(), indirect=True
)
def test_load_conf(_, config_files_manager):
cfg = config.read_cfg()
assert cfg.settings == _settings
def test_conf_returns_default_when_no_files(_, tmpdir):
with tmpdir.as_cwd():
cfg = config.read_cfg()
assert cfg.settings == defaults.DEFAULT_SETTINGS
def test_load_empty_pyproject_toml_and_cz_toml_with_config(_, tmpdir):
with tmpdir.as_cwd():
p = tmpdir.join("pyproject.toml")
p.write("")
p = tmpdir.join(".cz.toml")
p.write(PYPROJECT)
cfg = config.read_cfg()
assert cfg.settings == _settings
def test_load_pyproject_toml_from_config_argument(_, tmpdir):
with tmpdir.as_cwd():
_not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml")
_not_root_path.write(PYPROJECT)
cfg = config.read_cfg(filepath="./not_in_root/pyproject.toml")
assert cfg.settings == _settings
def test_load_cz_json_not_from_config_argument(_, tmpdir):
with tmpdir.as_cwd():
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.json")
_not_root_path.write(JSON_STR)
cfg = config.read_cfg(filepath="./not_in_root/.cz.json")
json_cfg_by_class = JsonConfig(data=JSON_STR, path=_not_root_path)
assert cfg.settings == json_cfg_by_class.settings
def test_load_cz_yaml_not_from_config_argument(_, tmpdir):
with tmpdir.as_cwd():
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.yaml")
_not_root_path.write(YAML_STR)
cfg = config.read_cfg(filepath="./not_in_root/.cz.yaml")
yaml_cfg_by_class = YAMLConfig(data=YAML_STR, path=_not_root_path)
assert cfg.settings == yaml_cfg_by_class._settings
def test_load_empty_pyproject_toml_from_config_argument(_, tmpdir):
with tmpdir.as_cwd():
_not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml")
_not_root_path.write("")
with pytest.raises(ConfigFileIsEmpty):
config.read_cfg(filepath="./not_in_root/pyproject.toml")
def test_warn_multiple_config_files(_, tmpdir, capsys):
"""Test that a warning is issued when multiple config files exist."""
with tmpdir.as_cwd():
# Create multiple config files
tmpdir.join(".cz.toml").write(PYPROJECT)
tmpdir.join(".cz.json").write(JSON_STR)
# Read config
cfg = config.read_cfg()
# Check that the warning was issued
captured = capsys.readouterr()
assert "Multiple config files detected" in captured.err
assert ".cz.toml" in captured.err
assert ".cz.json" in captured.err
assert "Using" in captured.err
# Verify the correct config is loaded (first in priority order)
assert cfg.settings == _settings
def test_warn_multiple_config_files_with_pyproject(_, tmpdir, capsys):
"""Test warning excludes pyproject.toml from the warning message."""
with tmpdir.as_cwd():
# Create multiple config files including pyproject.toml
tmpdir.join("pyproject.toml").write(PYPROJECT)
tmpdir.join(".cz.json").write(JSON_STR)
# Read config - should use pyproject.toml (first in priority)
cfg = config.read_cfg()
# No warning should be issued as only one non-pyproject config exists
captured = capsys.readouterr()
assert "Multiple config files detected" not in captured.err
# Verify the correct config is loaded
assert cfg.settings == _settings
def test_warn_multiple_config_files_uses_correct_one(_, tmpdir, capsys):
"""Test that the correct config file is used when multiple exist."""
with tmpdir.as_cwd():
# Create .cz.json with different settings
json_different = """
{
"commitizen": {
"name": "cz_conventional_commits",
"version": "2.0.0"
}
}
"""
tmpdir.join(".cz.json").write(json_different)
tmpdir.join(".cz.toml").write(PYPROJECT)
# Read config - should use pyproject.toml (first in defaults.CONFIG_FILES)
# But since pyproject.toml doesn't exist, .cz.toml is second in priority
cfg = config.read_cfg()
# Check that warning mentions both files
captured = capsys.readouterr()
assert "Multiple config files detected" in captured.err
assert ".cz.toml" in captured.err
assert ".cz.json" in captured.err
# Verify .cz.toml was used (second in priority after pyproject.toml)
assert cfg.settings["name"] == "cz_jira" # from PYPROJECT
assert cfg.settings["version"] == "1.0.0"
def test_no_warn_with_explicit_config_path(_, tmpdir, capsys):
"""Test that no warning is issued when user explicitly specifies config."""
with tmpdir.as_cwd():
# Create multiple config files
tmpdir.join(".cz.toml").write(PYPROJECT)
tmpdir.join(".cz.json").write(JSON_STR)
# Read config with explicit path
cfg = config.read_cfg(filepath=".cz.json")
# No warning should be issued
captured = capsys.readouterr()
assert "Multiple config files detected" not in captured.err
# Verify the explicitly specified config is loaded (compare to expected JSON config)
json_cfg_expected = JsonConfig(data=JSON_STR, path=Path(".cz.json"))
assert cfg.settings == json_cfg_expected.settings
@pytest.mark.parametrize(
"config_file, exception_string",
[
(".cz.toml", r"\.cz\.toml"),
("cz.toml", r"cz\.toml"),
("pyproject.toml", r"pyproject\.toml"),
],
ids=[".cz.toml", "cz.toml", "pyproject.toml"],
)
class TestTomlConfig:
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
path = tmpdir.mkdir("commitizen").join(config_file)
toml_config = TomlConfig(data="", path=path)
toml_config.init_empty_config_content()
with open(path, encoding="utf-8") as toml_file:
assert toml_file.read() == "[tool.commitizen]\n"
def test_init_empty_config_content_with_existing_content(
self, tmpdir, config_file, exception_string
):
existing_content = "[tool.black]\nline-length = 88\n"
path = tmpdir.mkdir("commitizen").join(config_file)
path.write(existing_content)
toml_config = TomlConfig(data="", path=path)
toml_config.init_empty_config_content()
with open(path, encoding="utf-8") as toml_file:
assert toml_file.read() == existing_content + "\n[tool.commitizen]\n"
def test_init_with_invalid_config_content(
self, tmpdir, config_file, exception_string
):
existing_content = "invalid toml content"
path = tmpdir.mkdir("commitizen").join(config_file)
with pytest.raises(InvalidConfigurationError, match=exception_string):
TomlConfig(data=existing_content, path=path)
@pytest.mark.parametrize(
"config_file, exception_string",
[
(".cz.json", r"\.cz\.json"),
("cz.json", r"cz\.json"),
],
ids=[".cz.json", "cz.json"],
)
class TestJsonConfig:
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
path = tmpdir.mkdir("commitizen").join(config_file)
json_config = JsonConfig(data="{}", path=path)
json_config.init_empty_config_content()
with open(path, encoding="utf-8") as json_file:
assert json.load(json_file) == {"commitizen": {}}
def test_init_with_invalid_config_content(
self, tmpdir, config_file, exception_string
):
existing_content = "invalid json content"
path = tmpdir.mkdir("commitizen").join(config_file)
with pytest.raises(InvalidConfigurationError, match=exception_string):
JsonConfig(data=existing_content, path=path)
@pytest.mark.parametrize(
"config_file, exception_string",
[
(".cz.yaml", r"\.cz\.yaml"),
("cz.yaml", r"cz\.yaml"),
],
ids=[".cz.yaml", "cz.yaml"],
)
class TestYamlConfig:
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
path = tmpdir.mkdir("commitizen").join(config_file)
yaml_config = YAMLConfig(data="{}", path=path)
yaml_config.init_empty_config_content()
with open(path) as yaml_file:
assert yaml.safe_load(yaml_file) == {"commitizen": {}}
def test_init_with_invalid_content(self, tmpdir, config_file, exception_string):
existing_content = "invalid: .cz.yaml: content: maybe?"
path = tmpdir.mkdir("commitizen").join(config_file)
with pytest.raises(InvalidConfigurationError, match=exception_string):
YAMLConfig(data=existing_content, path=path)