Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b8bc952

Browse files
committed
fix(commands/init): fix toml config format error
#367
1 parent 32916ab commit b8bc952

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

commitizen/config/toml_config.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import os
12
from pathlib import Path
23
from typing import Union
34

4-
from tomlkit import exceptions, parse
5+
from tomlkit import exceptions, parse, table
56

67
from .base_config import BaseConfig
78

@@ -14,8 +15,17 @@ def __init__(self, *, data: Union[bytes, str], path: Union[Path, str]):
1415
self.add_path(path)
1516

1617
def init_empty_config_content(self):
17-
with open(self.path, "a") as toml_file:
18-
toml_file.write("[tool.commitizen]")
18+
if os.path.isfile(self.path):
19+
with open(self.path, "rb") as input_toml_file:
20+
parser = parse(input_toml_file.read())
21+
else:
22+
parser = parse("")
23+
24+
with open(self.path, "wb") as output_toml_file:
25+
if parser.get("tool") is None:
26+
parser["tool"] = table()
27+
parser["tool"]["commitizen"] = table()
28+
output_toml_file.write(parser.as_string().encode("utf-8"))
1929

2030
def set_key(self, key, value):
2131
"""Set or update a key in the conf.

tests/commands/test_init_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def ask(self):
2525
}
2626

2727
expected_config = (
28+
"[tool]\n"
2829
"[tool.commitizen]\n"
2930
'name = "cz_conventional_commits"\n'
3031
'version = "0.0.1"\n'

tests/test_conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_init_empty_config_content(self, tmpdir):
132132
toml_config.init_empty_config_content()
133133

134134
with open(path, "r") as toml_file:
135-
assert toml_file.read() == "[tool.commitizen]"
135+
assert toml_file.read() == "[tool]\n[tool.commitizen]\n"
136136

137137
def test_init_empty_config_content_with_existing_content(self, tmpdir):
138138
existing_content = "[tool.black]\n" "line-length = 88\n"
@@ -143,7 +143,7 @@ def test_init_empty_config_content_with_existing_content(self, tmpdir):
143143
toml_config.init_empty_config_content()
144144

145145
with open(path, "r") as toml_file:
146-
assert toml_file.read() == existing_content + "[tool.commitizen]"
146+
assert toml_file.read() == existing_content + "\n[tool.commitizen]\n"
147147

148148

149149
class TestJsonConfig:

0 commit comments

Comments
 (0)