Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ce4421

Browse files
authored
Merge pull request #139 from Lee-W/fix-init-clean-config-file
Fix init clean config file
2 parents d32351d + 7317194 commit 7ce4421

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

commitizen/commands/init.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from commitizen.cz import registry
77
from commitizen.config import BaseConfig, TomlConfig, IniConfig
88
from commitizen.git import get_latest_tag_name, get_tag_names
9-
from commitizen.defaults import config_files
9+
from commitizen.defaults import long_term_support_config_files
1010

1111

1212
class Init:
@@ -17,7 +17,7 @@ def __init__(self, config: BaseConfig, *args):
1717
def __call__(self):
1818
values_to_add = {}
1919

20-
# No config file exist
20+
# No config for commitizen exist
2121
if not self.config.path:
2222
config_path = self._ask_config_path()
2323

@@ -26,7 +26,7 @@ def __call__(self):
2626
else:
2727
self.config = IniConfig(data="", path=config_path)
2828

29-
self.config.init_empty_config_file()
29+
self.config.init_empty_config_content()
3030

3131
values_to_add["name"] = self._ask_name()
3232
tag = self._ask_tag()
@@ -41,7 +41,7 @@ def __call__(self):
4141
def _ask_config_path(self) -> str:
4242
name = questionary.select(
4343
"Please choose a supported config file: (default: pyproject.tml)",
44-
choices=config_files,
44+
choices=long_term_support_config_files,
4545
default="pyproject.toml",
4646
style=self.cz.style,
4747
).ask()

commitizen/config/toml_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def __init__(self, *, data: str, path: str):
1010
self._parse_setting(data)
1111
self.add_path(path)
1212

13-
def init_empty_config_file(self):
14-
with open(self.path, "w") as toml_file:
13+
def init_empty_config_content(self):
14+
with open(self.path, "a") as toml_file:
1515
toml_file.write("[tool.commitizen]")
1616

1717
def set_key(self, key, value):

commitizen/defaults.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: str = "cz_conventional_commits"
22
# TODO: .cz, setup.cfg, .cz.cfg should be removed in 2.0
3-
config_files: list = ["pyproject.toml", ".cz.toml", ".cz", "setup.cfg", ".cz.cfg"]
3+
long_term_support_config_files: list = ["pyproject.toml", ".cz.toml"]
4+
deprcated_config_files: list = [".cz", "setup.cfg", ".cz.cfg"]
5+
config_files: list = long_term_support_config_files + deprcated_config_files
46

57
DEFAULT_SETTINGS = {
68
"name": "cz_conventional_commits",

pyproject.toml

+21
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,24 @@ git-cz = "commitizen.cli:main"
7373
[build-system]
7474
requires = ["poetry>=0.12"]
7575
build-backend = "poetry.masonry.api"
76+
77+
[tool.coverage]
78+
[tool.coverage.report]
79+
show_missing = true
80+
exclude_lines = [
81+
# Have to re-enable the standard pragma
82+
'pragma: no cover',
83+
84+
# Don't complain about missing debug-only code:
85+
'def __repr__',
86+
'if self\.debug',
87+
88+
# Don't complain if tests don't hit defensive assertion code:
89+
'raise AssertionError',
90+
'raise NotImplementedError',
91+
92+
# Don't complain if non-runnable code isn't run:
93+
'if 0:',
94+
'if __name__ == .__main__.:'
95+
]
96+

tests/test_conf.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,21 @@ def test_read_cfg_when_not_in_a_git_project(tmpdir):
144144

145145

146146
class TestTomlConfig:
147-
def test_init_empty_config_file(self, tmpdir):
147+
def test_init_empty_config_content(self, tmpdir):
148148
path = tmpdir.mkdir("commitizen").join(".cz.toml")
149149
toml_config = config.TomlConfig(data="", path=path)
150-
toml_config.init_empty_config_file()
150+
toml_config.init_empty_config_content()
151151

152152
with open(path, "r") as toml_file:
153153
assert toml_file.read() == "[tool.commitizen]"
154+
155+
def test_init_empty_config_content_with_existing_content(self, tmpdir):
156+
existing_content = "[tool.black]\n" "line-length = 88\n"
157+
158+
path = tmpdir.mkdir("commitizen").join(".cz.toml")
159+
path.write(existing_content)
160+
toml_config = config.TomlConfig(data="", path=path)
161+
toml_config.init_empty_config_content()
162+
163+
with open(path, "r") as toml_file:
164+
assert toml_file.read() == existing_content + "[tool.commitizen]"

0 commit comments

Comments
 (0)