Skip to content

Commit b295189

Browse files
aapelivclaude
andcommitted
Backend: add check_config coverage for stale config key references
check_config() only runs at app boot (app.py), so the rest of the test suite never exercises it. A reference to a key removed from CONFIG_OPTIONS (e.g. a toggle migrated to a feature flag) raises KeyError at startup but is invisible to CI. This test runs check_config() against a fully-populated config in both dev and prod modes to catch that class of bug. This commit intentionally contains only the test (not the fix), so CI shows it failing against the current ENABLE_DONATIONS KeyError on staging. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent f3e159d commit b295189

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Any
2+
3+
import pytest
4+
5+
from couchers.config import CONFIG_OPTIONS, check_config
6+
7+
8+
def _complete_config(dev: bool) -> dict[str, Any]:
9+
"""Build a config dict with every CONFIG_OPTIONS key populated with a valid, truthy value.
10+
11+
This mirrors what make_config() produces when every env var is set, so check_config() must be
12+
able to run against it without touching any key outside CONFIG_OPTIONS.
13+
"""
14+
cfg: dict[str, Any] = {}
15+
for name, type_, *_ in CONFIG_OPTIONS:
16+
if type_ is bool:
17+
cfg[name] = True
18+
elif type_ is int:
19+
cfg[name] = 1
20+
elif type_ is bytes:
21+
cfg[name] = b"x"
22+
elif isinstance(type_, list):
23+
cfg[name] = type_[0]
24+
else:
25+
cfg[name] = "x"
26+
27+
cfg["DEV"] = dev
28+
if not dev:
29+
# production invariants that aren't satisfiable by a generic truthy value
30+
cfg["BASE_URL"] = "https://example.com"
31+
cfg["ENABLE_EMAIL"] = True
32+
cfg["IN_TEST"] = False
33+
return cfg
34+
35+
36+
@pytest.mark.parametrize("dev", [True, False])
37+
def test_check_config_only_references_known_keys(dev):
38+
"""check_config() must only access config keys that are declared in CONFIG_OPTIONS.
39+
40+
A reference to a key that was removed from CONFIG_OPTIONS (e.g. a toggle migrated to a feature
41+
flag) would raise KeyError at app boot but is invisible to the rest of the test suite, since
42+
check_config() only runs in app.py's startup path. Exercising it here catches that.
43+
"""
44+
check_config(_complete_config(dev=dev))

0 commit comments

Comments
 (0)