From ae81982a3a58142f63afc2866bc5c59d3599bdfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Wachtendorf?= Date: Wed, 18 Feb 2026 10:48:39 +0100 Subject: [PATCH] fix(config): fix contains_commitizen_section failing for completely empty files --- commitizen/config/json_config.py | 5 ++++- commitizen/config/yaml_config.py | 2 +- tests/test_conf.py | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) 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(