Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def __init__(self, *, data: bytes | str, path: Path) -> None:

def contains_commitizen_section(self) -> bool:
with self.path.open("rb") as json_file:
config_doc = json.load(json_file)
try:
config_doc = json.load(json_file)
except json.JSONDecodeError:
return False
return config_doc.get("commitizen") is not None

def init_empty_config_content(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def init_empty_config_content(self) -> None:
def contains_commitizen_section(self) -> bool:
with self.path.open("rb") as yaml_file:
config_doc = yaml.load(yaml_file, Loader=yaml.FullLoader)
return config_doc.get("commitizen") is not None
return config_doc is not None and config_doc.get("commitizen") is not None

def _parse_setting(self, data: bytes | str) -> None:
"""We expect to have a section in cz.yaml looking like
Expand Down
16 changes: 16 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ def test_load_empty_pyproject_toml_from_config_argument(self, tmpdir):
with pytest.raises(ConfigFileIsEmpty):
config.read_cfg(filepath="./not_in_root/pyproject.toml")

def test_load_empty_json_from_config_argument(self, tmpdir):
with tmpdir.as_cwd():
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.json")
_not_root_path.write("")

with pytest.raises(ConfigFileIsEmpty):
config.read_cfg(filepath="./not_in_root/.cz.json")

def test_load_empty_yaml_from_config_argument(self, tmpdir):
with tmpdir.as_cwd():
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.yaml")
_not_root_path.write("")

with pytest.raises(ConfigFileIsEmpty):
config.read_cfg(filepath="./not_in_root/.cz.yaml")


class TestWarnMultipleConfigFiles:
@pytest.mark.parametrize(
Expand Down