51 lines
2.1 KiB
Python
51 lines
2.1 KiB
Python
"""Architecture rules that should fail before coupling becomes entrenched."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class TestCliBackendBoundaries(unittest.TestCase):
|
|
def test_cli_does_not_import_a_concrete_backend(self) -> None:
|
|
forbidden = (
|
|
"backend.docker", "backend.firecracker", "backend.macos_container",
|
|
"bot_bottle.backend.docker", "bot_bottle.backend.firecracker",
|
|
"bot_bottle.backend.macos_container",
|
|
)
|
|
violations: list[str] = []
|
|
for path in (ROOT / "bot_bottle" / "cli").rglob("*.py"):
|
|
tree = ast.parse(path.read_text(), filename=str(path))
|
|
for node in ast.walk(tree):
|
|
module = node.module if isinstance(node, ast.ImportFrom) else None
|
|
if module and module.startswith(forbidden):
|
|
violations.append(f"{path.relative_to(ROOT)}:{node.lineno}: {module}")
|
|
if isinstance(node, ast.Import):
|
|
violations.extend(
|
|
f"{path.relative_to(ROOT)}:{node.lineno}: {alias.name}"
|
|
for alias in node.names if alias.name.startswith(forbidden)
|
|
)
|
|
self.assertEqual([], violations, "generic CLI imports concrete backend internals:\n" +
|
|
"\n".join(violations))
|
|
|
|
|
|
class TestRuntimeModuleSizes(unittest.TestCase):
|
|
def test_egress_modules_stay_focused(self) -> None:
|
|
caps = {
|
|
"addon_core.py": 900, # compatibility + policy-schema parsing only
|
|
"types.py": 180,
|
|
"matching.py": 180,
|
|
"dlp.py": 180,
|
|
"context.py": 140,
|
|
}
|
|
directory = ROOT / "bot_bottle" / "gateway" / "egress"
|
|
oversized = [f"{name} ({len((directory / name).read_text().splitlines())}>{cap})"
|
|
for name, cap in caps.items()
|
|
if len((directory / name).read_text().splitlines()) > cap]
|
|
self.assertEqual([], oversized, "split a module rather than raising its cap: " +
|
|
", ".join(oversized))
|