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

Skip to content

Commit 9fb3476

Browse files
committed
fix(commands/init): fix clean up file when initialize commitizen config
#138
1 parent d7ffd79 commit 9fb3476

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
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):

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)