87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
"""Reproducible route-vs-locust coverage check. Fails CI on gaps.
|
|||
|
|
|
||
|
|
Usage: python3 example/check_locust_coverage.py
|
||
|
|
Exit 0 = full coverage. Exit 1 = missing routes printed.
|
||
|
|
"""
|
||
|
|
import re
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
HERE = Path(__file__).resolve().parent
|
||
|
|
APP = HERE / "app"
|
||
|
|
LOCUST = HERE / "locustfile.py"
|
||
|
|
|
||
|
|
FILLS = {"name": "locust", "id": "1", "status": "404", "tenant": "acme"}
|
||
|
|
|
||
|
|
|
||
|
|
def fill(route: str) -> str:
|
||
|
|
out = route
|
||
|
|
for k, v in FILLS.items():
|
||
|
|
out = out.replace("{" + k + "}", v)
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
def collect_routes() -> set[str]:
|
||
|
|
routes: set[str] = set()
|
||
|
|
# 1. Index features (authoritative user-facing list)
|
||
|
|
out = subprocess.run(
|
||
|
|
["grep", "-rhoE", r'add_feature\("[^"]+", "/[^"]+"\)', str(APP)],
|
||
|
|
capture_output=True, text=True, check=True,
|
||
|
|
).stdout
|
||
|
|
for m in re.findall(r'"/[^"]+"\)', out):
|
||
|
|
routes.add(m.strip('")'))
|
||
|
|
# 2. Direct router registrations
|
||
|
|
out = subprocess.run(
|
||
|
|
["grep", "-rhoE", r'\.(get|post|put|delete|patch|options|head)\("/[^"]+"', str(APP)],
|
||
|
|
capture_output=True, text=True, check=True,
|
||
|
|
).stdout
|
||
|
|
for m in re.findall(r'"/[^"]+"', out):
|
||
|
|
routes.add(m.strip('"'))
|
||
|
|
# 3. Prefix-mounted equivalents (see RoutingDemo.cpp / App.cpp)
|
||
|
|
routes |= {"/views/greet", "/routers/nested/deep",
|
||
|
|
"/app1/app1/app1/whoami"}
|
||
|
|
return routes
|
||
|
|
|
||
|
|
|
||
|
|
def collect_locust_urls() -> tuple[set[str], str]:
|
||
|
|
src = LOCUST.read_text()
|
||
|
|
urls = set(re.findall(r'"(/[^\"]*)"', src))
|
||
|
|
methods = set(re.findall(r"self\.client\.(get|post|put|options|delete|patch|head)\(", src))
|
||
|
|
return urls, methods
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> int:
|
||
|
|
routes = collect_routes()
|
||
|
|
urls, methods = collect_locust_urls()
|
||
|
|
missing: list[str] = []
|
||
|
|
for r in sorted(routes):
|
||
|
|
c = fill(r)
|
||
|
|
ok = (
|
||
|
|
c in urls
|
||
|
|
or "/routing" + c in urls
|
||
|
|
or (r == "/static/{name}" and "/static/app.css" in urls)
|
||
|
|
or (r == "/embedded/{name}" and "/embedded/test.txt" in urls)
|
||
|
|
or (r == "/di/resolve/{name}" and "/di/resolve/testsvc" in urls)
|
||
|
|
or (r == "/views/greet" and ("/greet" in urls or "/routing/views/greet" in urls))
|
||
|
|
or (r == "/routers/nested/deep" and ("/deep" in urls or "/routing/routers/nested/deep" in urls))
|
||
|
|
)
|
||
|
|
if not ok:
|
||
|
|
missing.append(f"{r} -> {c}")
|
||
|
|
required_methods = {"get", "post", "put", "options"}
|
||
|
|
missing_methods = required_methods - methods
|
||
|
|
print(f"routes={len(routes)} urls={len(urls)} methods={sorted(methods)}")
|
||
|
|
if missing_methods:
|
||
|
|
print("MISSING METHODS:", sorted(missing_methods))
|
||
|
|
for m in missing:
|
||
|
|
print("MISS", m)
|
||
|
|
if missing or missing_methods:
|
||
|
|
print(f"FAIL: {len(missing)} missing routes, {len(missing_methods)} missing methods")
|
||
|
|
return 1
|
||
|
|
print(f"OK: {len(routes)}/{len(routes)} routes covered, methods {sorted(methods)}")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|