Skip to content

Commit 88ca3dc

Browse files
authored
fix: refuse a quoted "false" for a true/false setting (#636)
conventional_commits = "false" is a non-empty string, so the rule it meant to turn off stayed on and the commit was rejected (exit 1). A boolean setting given anything but true or false is now a config error (exit 2) that names the setting.
1 parent 049bf16 commit 88ca3dc

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎commit_check/rule_builder.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
GIT_MERGE_SUBJECT_PATTERN,
2222
)
2323
from commit_check.config import ConfigError
24+
from commit_check.config_merger import get_default_config
2425
from commit_check.rules_catalog import (
2526
BRANCH_RULES,
2627
COMMIT_RULES,
@@ -194,6 +195,32 @@ def _string_list(
194195
)
195196

196197

198+
#: The settings that take ``true`` or ``false``, as (section, key).
199+
_BOOLEAN_SETTINGS = tuple(
200+
(section, key)
201+
for section, defaults in get_default_config().items()
202+
for key, default in defaults.items()
203+
if isinstance(default, bool)
204+
)
205+
206+
207+
def _check_booleans(config: dict[str, Any]) -> None:
208+
"""Refuse a boolean setting given anything but ``true`` or ``false``.
209+
210+
``"false"`` is a non-empty string, so it used to turn the rule on.
211+
"""
212+
for section, key in _BOOLEAN_SETTINGS:
213+
values = config.get(section)
214+
if not isinstance(values, dict):
215+
continue
216+
value = values.get(key)
217+
if value is not None and not isinstance(value, bool):
218+
setting = f"[{section}] {key}"
219+
raise ConfigError(
220+
f"{setting} must be true or false, got {value!r}", setting=setting
221+
)
222+
223+
197224
class RuleBuilder:
198225
"""Builds validation rules from config and catalog entries."""
199226

@@ -204,6 +231,7 @@ def __init__(self, config: dict[str, Any]):
204231
self.push_config = config.get("push", {})
205232
self.tag_config = config.get("tag", {})
206233
self.files_config = config.get("files", {})
234+
_check_booleans(config)
207235
self.warn_checks = self._resolve_warn_list(config.get("warn", []))
208236

209237
def build_all_rules(self) -> list[ValidationRule]:

‎tests/rule_builder_test.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,16 @@ def test_a_non_integer_length_names_the_setting(self, key, value):
11381138
RuleBuilder({"commit": {key: value}}).build_all_rules()
11391139
assert excinfo.value.setting == f"[commit] {key}"
11401140

1141+
@pytest.mark.parametrize(
1142+
"section, key",
1143+
[("commit", "conventional_commits"), ("branch", "conventional_branch")],
1144+
)
1145+
@pytest.mark.parametrize("value", ["false", 0, "yes"])
1146+
def test_a_non_boolean_switch_names_the_setting(self, section, key, value):
1147+
with pytest.raises(ConfigError, match="must be true or false") as excinfo:
1148+
RuleBuilder({section: {key: value}}).build_all_rules()
1149+
assert excinfo.value.setting == f"[{section}] {key}"
1150+
11411151
def test_an_unset_length_builds_no_rule(self):
11421152
checks = {r.check for r in RuleBuilder({"commit": {}}).build_all_rules()}
11431153
assert "subject_max_length" not in checks

0 commit comments

Comments
 (0)