Skip to content

Commit 988c81f

Browse files
bwachtendorfwoile
authored andcommitted
fix(config): fix contains_commitizen_section failing for completely empty files
1 parent 44ec9b8 commit 988c81f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

commitizen/config/json_config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def __init__(self, *, data: bytes | str, path: Path) -> None:
2727

2828
def contains_commitizen_section(self) -> bool:
2929
with self.path.open("rb") as json_file:
30-
config_doc = json.load(json_file)
30+
try:
31+
config_doc = json.load(json_file)
32+
except json.JSONDecodeError:
33+
return False
3134
return config_doc.get("commitizen") is not None
3235

3336
def init_empty_config_content(self) -> None:

commitizen/config/yaml_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def init_empty_config_content(self) -> None:
3535
def contains_commitizen_section(self) -> bool:
3636
with self.path.open("rb") as yaml_file:
3737
config_doc = yaml.load(yaml_file, Loader=yaml.FullLoader)
38-
return config_doc.get("commitizen") is not None
38+
return config_doc is not None and config_doc.get("commitizen") is not None
3939

4040
def _parse_setting(self, data: bytes | str) -> None:
4141
"""We expect to have a section in cz.yaml looking like

tests/test_conf.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,22 @@ def test_load_empty_pyproject_toml_from_config_argument(self, tmpdir):
242242
with pytest.raises(ConfigFileIsEmpty):
243243
config.read_cfg(filepath="./not_in_root/pyproject.toml")
244244

245+
def test_load_empty_json_from_config_argument(self, tmpdir):
246+
with tmpdir.as_cwd():
247+
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.json")
248+
_not_root_path.write("")
249+
250+
with pytest.raises(ConfigFileIsEmpty):
251+
config.read_cfg(filepath="./not_in_root/.cz.json")
252+
253+
def test_load_empty_yaml_from_config_argument(self, tmpdir):
254+
with tmpdir.as_cwd():
255+
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.yaml")
256+
_not_root_path.write("")
257+
258+
with pytest.raises(ConfigFileIsEmpty):
259+
config.read_cfg(filepath="./not_in_root/.cz.yaml")
260+
245261

246262
class TestWarnMultipleConfigFiles:
247263
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)