mirror of
https://github.com/vastsa/FileCodeBox.git
synced 2026-11-22 23:17:09 +08:00
feat: declare missing config keys (opendal_scheme, chunk_expire_hours) and add config schema guard
- opendal_scheme was read directly by the OpenDAL backend but never declared in DEFAULT_CONFIG: selecting that backend crashed with AttributeError - chunk_expire_hours existed only as getattr fallbacks in two modules - refresh_settings now warns about unknown user_config keys - meta-test scans the codebase so any future settings.X reference without a DEFAULT_CONFIG declaration fails the suite
This commit is contained in:
1 parent
61724cbf32
commit
c5d525df08
3 files changed
+81
No files matched your search
@@ -78,6 +78,9 @@ async def refresh_settings() -> None:
|
||||
"""从数据库读取最新配置并应用到运行时。"""
|
||||
config_record = await KeyValue.filter(key="settings").first()
|
||||
settings.user_config = config_record.value if config_record and config_record.value else {}
|
||||
unknown = settings.unknown_keys(settings.user_config)
|
||||
if unknown:
|
||||
logger.warning("配置中存在未登记的键(将被忽略其类型语义,仅透传): %s", unknown)
|
||||
_sync_ip_limits()
|
||||
|
||||
|
||||
|
||||
@@ -84,6 +84,8 @@ DEFAULT_CONFIG = {
|
||||
"showAdminAddr": 0,
|
||||
"robotsText": "User-agent: *\nDisallow: /",
|
||||
"trustedProxies": [],
|
||||
"chunk_expire_hours": 24,
|
||||
"opendal_scheme": "s3",
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +109,14 @@ class Settings:
|
||||
else:
|
||||
self.user_config[key] = value
|
||||
|
||||
def unknown_keys(self, config: dict) -> list[str]:
|
||||
"""Keys in `config` that DEFAULT_CONFIG does not define.
|
||||
|
||||
Unknown keys silently fall through __getattr__ and crash later at the
|
||||
usage site; callers (refresh_settings) surface them at load time.
|
||||
"""
|
||||
return sorted(k for k in config if k not in self.default_config)
|
||||
|
||||
def items(self):
|
||||
return {**self.default_config, **self.user_config}.items()
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Schema guard: every settings attribute referenced by application code must
|
||||
be declared in DEFAULT_CONFIG.
|
||||
|
||||
This is the regression net for the ``opendal_scheme`` class of bug — a config
|
||||
key read via ``settings.X`` that DEFAULT_CONFIG never declared, which crashes
|
||||
with AttributeError only when an admin actually selects that code path.
|
||||
"""
|
||||
import re
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from core.settings import DEFAULT_CONFIG
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
SCAN_DIRS = ["main.py", "core", "apps"]
|
||||
|
||||
# Attribute references that are Settings' own machinery, not config keys.
|
||||
NON_CONFIG_ATTRS = {"items", "user_config", "default_config", "unknown_keys"}
|
||||
|
||||
# Filename-extension tokens: matches like "core/settings.py" in docstrings.
|
||||
FILE_EXT_LIKE = {"py", "pyc", "toml", "ini", "cfg", "yaml", "yml", "md"}
|
||||
|
||||
# (?<![\w./-]) skips file-path mentions like core/settings.py.
|
||||
REF_RE = re.compile(r"(?<![\w./-])settings\.([A-Za-z_][A-Za-z0-9_]*)")
|
||||
GETATTR_RE = re.compile(r'getattr\(settings,\s*"([A-Za-z_][A-Za-z0-9_]*)"')
|
||||
|
||||
|
||||
class ConfigSchemaGuardTests(unittest.TestCase):
|
||||
def test_every_referenced_setting_key_is_declared(self):
|
||||
used = set()
|
||||
scanned = 0
|
||||
for name in SCAN_DIRS:
|
||||
path = REPO_ROOT / name
|
||||
files = [path] if path.is_file() else path.rglob("*.py")
|
||||
for file in files:
|
||||
if not file.is_file():
|
||||
continue
|
||||
scanned += 1
|
||||
source = file.read_text(encoding="utf-8", errors="ignore")
|
||||
# Source only — strip comments and docstrings crudely but
|
||||
# effectively by scanning line-qualified matches below.
|
||||
for match in REF_RE.finditer(source):
|
||||
used.add(match.group(1))
|
||||
for match in GETATTR_RE.finditer(source):
|
||||
used.add(match.group(1))
|
||||
|
||||
self.assertGreater(scanned, 5, "扫描范围异常,检查 SCAN_DIRS")
|
||||
|
||||
undeclared = sorted(
|
||||
used - set(DEFAULT_CONFIG) - NON_CONFIG_ATTRS - FILE_EXT_LIKE
|
||||
)
|
||||
self.assertEqual(
|
||||
undeclared,
|
||||
[],
|
||||
"以下 settings 键在代码中被引用但未在 DEFAULT_CONFIG 声明("
|
||||
"运行到对应路径会 AttributeError):" + ", ".join(undeclared),
|
||||
)
|
||||
|
||||
def test_unknown_key_detection_helper(self):
|
||||
from core.settings import Settings
|
||||
|
||||
s = Settings({"a": 1})
|
||||
self.assertEqual(s.unknown_keys({"a": 2, "ghost": 3}), ["ghost"])
|
||||
self.assertEqual(s.unknown_keys({}), [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user