Skip to content

Commit 7ffb895

Browse files
Bogaywoile
andauthored
fix(conf): handle parse error when init (#856)
* test(conf): invalid contents already exist in file handle parse error so that the error message looks better. * fix(conf): handle parse error when init * refactor(json_config): add parse error message from json lib --------- Co-authored-by: Santiago Fraire Willemoes <santiwilly@gmail.com>
1 parent a378c2a commit 7ffb895

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

commitizen/config/json_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def _parse_setting(self, data: bytes | str) -> None:
4747
"""
4848
try:
4949
doc = json.loads(data)
50-
except json.JSONDecodeError:
51-
raise InvalidConfigurationError(f"Failed to parse {self.path}")
50+
except json.JSONDecodeError as e:
51+
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")
5252

5353
try:
5454
self.settings.update(doc["commitizen"])

commitizen/config/toml_config.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
from tomlkit import exceptions, parse, table
77

8+
from commitizen.exceptions import InvalidConfigurationError
89
from .base_config import BaseConfig
910

1011

1112
class TomlConfig(BaseConfig):
1213
def __init__(self, *, data: bytes | str, path: Path | str):
1314
super(TomlConfig, self).__init__()
1415
self.is_empty_config = False
15-
self._parse_setting(data)
1616
self.add_path(path)
17+
self._parse_setting(data)
1718

1819
def init_empty_config_content(self):
1920
if os.path.isfile(self.path):
@@ -50,7 +51,11 @@ def _parse_setting(self, data: bytes | str) -> None:
5051
name = "cz_conventional_commits"
5152
```
5253
"""
53-
doc = parse(data)
54+
try:
55+
doc = parse(data)
56+
except exceptions.ParseError as e:
57+
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")
58+
5459
try:
5560
self.settings.update(doc["tool"]["commitizen"]) # type: ignore
5661
except exceptions.NonExistentKey:

commitizen/config/yaml_config.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import yaml
66

77
from commitizen.git import smart_open
8+
from commitizen.exceptions import InvalidConfigurationError
89

910
from .base_config import BaseConfig
1011

@@ -13,8 +14,8 @@ class YAMLConfig(BaseConfig):
1314
def __init__(self, *, data: bytes | str, path: Path | str):
1415
super(YAMLConfig, self).__init__()
1516
self.is_empty_config = False
16-
self._parse_setting(data)
1717
self.add_path(path)
18+
self._parse_setting(data)
1819

1920
def init_empty_config_content(self):
2021
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
@@ -28,7 +29,13 @@ def _parse_setting(self, data: bytes | str) -> None:
2829
name: cz_conventional_commits
2930
```
3031
"""
31-
doc = yaml.safe_load(data)
32+
import yaml.scanner
33+
34+
try:
35+
doc = yaml.safe_load(data)
36+
except yaml.YAMLError as e:
37+
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")
38+
3239
try:
3340
self.settings.update(doc["commitizen"])
3441
except (KeyError, TypeError):

tests/test_conf.py

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import yaml
77

88
from commitizen import config, defaults, git
9+
from commitizen.exceptions import InvalidConfigurationError
910

1011
PYPROJECT = """
1112
[tool.commitizen]
@@ -177,6 +178,13 @@ def test_init_empty_config_content_with_existing_content(self, tmpdir):
177178
with open(path, "r", encoding="utf-8") as toml_file:
178179
assert toml_file.read() == existing_content + "\n[tool.commitizen]\n"
179180

181+
def test_init_with_invalid_config_content(self, tmpdir):
182+
existing_content = "invalid toml content"
183+
path = tmpdir.mkdir("commitizen").join(".cz.toml")
184+
185+
with pytest.raises(InvalidConfigurationError, match=r"\.cz\.toml"):
186+
config.TomlConfig(data=existing_content, path=path)
187+
180188

181189
class TestJsonConfig:
182190
def test_init_empty_config_content(self, tmpdir):
@@ -187,6 +195,13 @@ def test_init_empty_config_content(self, tmpdir):
187195
with open(path, "r", encoding="utf-8") as json_file:
188196
assert json.load(json_file) == {"commitizen": {}}
189197

198+
def test_init_with_invalid_config_content(self, tmpdir):
199+
existing_content = "invalid json content"
200+
path = tmpdir.mkdir("commitizen").join(".cz.json")
201+
202+
with pytest.raises(InvalidConfigurationError, match=r"\.cz\.json"):
203+
config.JsonConfig(data=existing_content, path=path)
204+
190205

191206
class TestYamlConfig:
192207
def test_init_empty_config_content(self, tmpdir):
@@ -196,3 +211,10 @@ def test_init_empty_config_content(self, tmpdir):
196211

197212
with open(path, "r") as yaml_file:
198213
assert yaml.safe_load(yaml_file) == {"commitizen": {}}
214+
215+
def test_init_with_invalid_content(self, tmpdir):
216+
existing_content = "invalid: .cz.yaml: content: maybe?"
217+
path = tmpdir.mkdir("commitizen").join(".cz.yaml")
218+
219+
with pytest.raises(InvalidConfigurationError, match=r"\.cz\.yaml"):
220+
config.YAMLConfig(data=existing_content, path=path)

0 commit comments

Comments
 (0)