refactor(parsers): 提取 _validate_path helper(T7 架构审查整改,I8 DRY)

- source_aggregator.py 三处重复校验(扩展名白名单 + 存在性)提取为
  _validate_path(path, allowed_exts) -> Path,保留 ValueError/FileNotFoundError
  语义与错误信息,parse 三分支改用 helper
- 外部契约不变:未知扩展名→ValueError、不存在→FileNotFoundError
- 新增 3 用例直接测 helper
- TDD: RED(helper 不存在)→ GREEN(聚焦 13 passed)→ 全量 248 passed / 100.00%(1356 stmts/332 br)
This commit is contained in:
lhl
2026-08-12 23:01:58 +08:00
parent e3c714d5d9
commit 3ae1f5f38f
3 changed files with 48 additions and 15 deletions
+29
View File
@@ -1,4 +1,5 @@
import pytest
from pathlib import Path
from genesis.data_models import StructuredSource
from genesis.parsers.source_aggregator import SourceParser
@@ -96,3 +97,31 @@ def test_parse_extensionless_rule(tmp_path):
bad.write_text("hello", encoding="utf-8")
with pytest.raises(ValueError, match="无扩展名"):
SourceParser().parse(rule_paths=[bad])
# ---------- T7: DRY _validate_path helperI8 ----------
def test_validate_path_rejects_bad_extension(tmp_path):
from genesis.parsers.source_aggregator import _validate_path
bad = tmp_path / "note.txt"
bad.write_text("x", encoding="utf-8")
with pytest.raises(ValueError, match="不支持的文件类型"):
_validate_path(bad, (".xlsx",))
def test_validate_path_rejects_missing_file(tmp_path):
from genesis.parsers.source_aggregator import _validate_path
with pytest.raises(FileNotFoundError):
_validate_path(tmp_path / "missing.xlsx", (".xlsx",))
def test_validate_path_returns_resolved_path(tmp_path):
from genesis.parsers.source_aggregator import _validate_path
good = tmp_path / "ok.xlsx"
good.write_bytes(b"x")
result = _validate_path(str(good), (".xlsx",))
assert isinstance(result, Path)
assert result.suffix.lower() == ".xlsx"