diff --git a/commitizen/config/json_config.py b/commitizen/config/json_config.py index 3951c5285..688a6b9fe 100644 --- a/commitizen/config/json_config.py +++ b/commitizen/config/json_config.py @@ -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: diff --git a/commitizen/config/yaml_config.py b/commitizen/config/yaml_config.py index 0e79735f2..1e9610e17 100644 --- a/commitizen/config/yaml_config.py +++ b/commitizen/config/yaml_config.py @@ -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 diff --git a/tests/test_conf.py b/tests/test_conf.py index 3b7937603..e558f6c39 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -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(