This commit is contained in:
Mateusz Gruszczyński committed 2026-10-12 11:22:42 +02:00
1 parent 68ada6680d
commit 180a0e4877
56 files changed
+9866 -205

No files matched your search

+37 -10
View File
@@ -2,7 +2,7 @@
"""Static structure contracts for the split Rust source tree.
These checks are intentionally compiler-independent. They prevent the module split
from regressing into large monoliths, keep unit tests out of implementation files,
from regressing into large implementation monoliths, allow full-sized test files,
and catch the class of dangling-attribute/broken-include mistakes that can otherwise
surface only at cargo check time.
"""
@@ -19,8 +19,12 @@ MAX_LINES = 500
def is_test_source(path: Path) -> bool:
relative = path.relative_to(SRC)
return path.name == "tests.rs" or path.stem.endswith("_tests") or "tests" in relative.parts
relative = path.relative_to(ROOT)
if relative.parts[0] == "tests":
return True
return relative.parts[0] == "src" and (
path.name == "tests.rs" or path.stem.endswith("_tests") or "tests" in relative.parts[1:]
)
include_re = re.compile(r'include!\(\s*"([^"]+\.rs)"\s*\)')
@@ -89,10 +93,12 @@ def delimiters_balanced(text: str) -> bool:
return not stack and state in {"code", "line"}
# The 500-line rule applies to every Rust source in the repository, including integration tests.
# The 500-line rule applies to production Rust sources, not unit/integration tests.
for path in sorted(ROOT.rglob("*.rs")):
if any(part in {"target", ".git"} for part in path.parts):
continue
if is_test_source(path):
continue
lines = path.read_text(encoding="utf-8").splitlines()
assert len(lines) <= MAX_LINES, f"{path.relative_to(ROOT)} has {len(lines)} lines (max {MAX_LINES})"
@@ -126,6 +132,22 @@ for path in sorted(SRC.rglob("*.rs")):
f"test cfg in implementation must target a separate test file: {path.relative_to(ROOT)}"
)
# Unit tests are intentionally consolidated rather than mechanically numbered.
for folder in (
"admin/tests", "balance/tests", "config/tests", "web_server/tests",
"cache/tests/tests", "cache/tests/v022_cache_tests", "cache/tests/cold_tier_tests",
):
assert not (SRC / folder).exists(), f"obsolete split-test directory: {folder}"
for relative in (
"admin/tests.rs", "balance/tests.rs", "config/tests.rs", "web_server/tests.rs",
"cache/tests/tests.rs", "cache/tests/v022_cache_tests.rs", "cache/tests/cold_tier_tests.rs",
):
assert (SRC / relative).is_file(), f"missing consolidated test file: {relative}"
assert 'include_str!("web/login-keys.json")' in (SRC / "admin/tests.rs").read_text(), (
"admin test asset path must be relative to the consolidated test file"
)
assert (SRC / "admin/web/login-keys.json").is_file()
# Explicit regressions from the first Web Server split.
response = (SRC / "web_server/response.rs").read_text(encoding="utf-8")
assert not response.rstrip().endswith("#[derive(Debug, Clone)]"), "dangling derive returned in Web Server response module"
@@ -150,16 +172,21 @@ server_tests = (SRC / "server/serve/tests.rs").read_text(encoding="utf-8")
assert server_tests.count("serve_inner(") == server_tests.count("Http2Connections::default()"), (
"direct serve_inner test calls must pass the Http2Connections argument"
)
cache_policy_tests = (SRC / "cache/tests/tests/part_03.rs").read_text(encoding="utf-8")
assert "let plan = plan(" not in cache_policy_tests, "cache policy test shadows the plan() function"
cold_tests = (SRC / "cache/tests/cold_tier_tests/part_02.rs").read_text(encoding="utf-8")
cache_policy_tests = (SRC / "cache/tests/tests.rs").read_text(encoding="utf-8")
# Older cache tests legitimately use `let plan = plan(...)`. The regression
# applies to the Vary/revalidation group formerly stored in part_03.rs.
revalidation_tests = cache_policy_tests.split(
"async fn vary_selects_variants_without_fragmenting_the_primary_hash", 1
)[1]
assert "let plan = plan(" not in revalidation_tests, "cache policy test shadows the plan() function"
cold_tests = (SRC / "cache/tests/cold_tier_tests.rs").read_text(encoding="utf-8")
for field in ("scheme:", "authority:", "target:", "tags:", "created_at:"):
assert field in cold_tests, f"ColdEntry test fixture is missing current field {field}"
admin_tests = (SRC / "admin/tests/part_02.rs").read_text(encoding="utf-8")
admin_tests = (SRC / "admin/tests.rs").read_text(encoding="utf-8")
assert "let configuration = router\n .clone()\n .oneshot(" in admin_tests, (
"admin persistence test must retain Router before Tower::oneshot consumes a clone"
)
web_tests_1 = (SRC / "web_server/tests/part_01.rs").read_text(encoding="utf-8")
web_tests_1 = (SRC / "web_server/tests.rs").read_text(encoding="utf-8")
assert "ETAG, EXPIRES," not in web_tests_1, "unused EXPIRES import returned in Web Server tests"
print(f"PASS: Rust modules are <= {MAX_LINES} lines, test code is isolated, include! targets resolve, and split-module/all-target regressions are guarded.")
print(f"PASS: production Rust modules are <= {MAX_LINES} lines (tests exempt), numbered test fragments are absent, include! targets resolve, and module regressions are guarded.")