-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathtest_conf.py
156 lines (126 loc) · 4.4 KB
/
test_conf.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
import json
import os
from pathlib import Path
import pytest
import yaml
from commitizen import config, defaults, git
PYPROJECT = """
[tool.commitizen]
name = "cz_jira"
version = "1.0.0"
version_files = [
"commitizen/__version__.py",
"pyproject.toml"
]
style = [
["pointer", "reverse"],
["question", "underline"]
]
[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"]],
}
}
_settings = {
"name": "cz_jira",
"version": "1.0.0",
"tag_format": None,
"bump_message": None,
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"changelog_file": "CHANGELOG.md",
"changelog_incremental": False,
"changelog_start_rev": None,
"update_changelog_on_bump": False,
}
_new_settings = {
"name": "cz_jira",
"version": "2.0.0",
"tag_format": None,
"bump_message": None,
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"changelog_file": "CHANGELOG.md",
"changelog_incremental": False,
"changelog_start_rev": None,
"update_changelog_on_bump": False,
}
_read_settings = {
"name": "cz_jira",
"version": "1.0.0",
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"changelog_file": "CHANGELOG.md",
}
@pytest.fixture
def config_files_manager(request, tmpdir):
with tmpdir.as_cwd():
filename = request.param
with open(filename, "w") 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
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
class TestTomlConfig:
def test_init_empty_config_content(self, tmpdir):
path = tmpdir.mkdir("commitizen").join(".cz.toml")
toml_config = config.TomlConfig(data="", path=path)
toml_config.init_empty_config_content()
with open(path, "r") as toml_file:
assert toml_file.read() == "[tool.commitizen]"
def test_init_empty_config_content_with_existing_content(self, tmpdir):
existing_content = "[tool.black]\n" "line-length = 88\n"
path = tmpdir.mkdir("commitizen").join(".cz.toml")
path.write(existing_content)
toml_config = config.TomlConfig(data="", path=path)
toml_config.init_empty_config_content()
with open(path, "r") as toml_file:
assert toml_file.read() == existing_content + "[tool.commitizen]"
class TestJsonConfig:
def test_init_empty_config_content(self, tmpdir):
path = tmpdir.mkdir("commitizen").join(".cz.json")
json_config = config.JsonConfig(data="{}", path=path)
json_config.init_empty_config_content()
with open(path, "r") as json_file:
assert json.load(json_file) == {"commitizen": {}}