93 lines
4.4 KiB
Python
Executable File
93 lines
4.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Fail when a workspace package is not covered by a CI step.
|
|
|
|
`scripts/ci-coverage.json` assigns every workspace member exactly one level:
|
|
|
|
tested built and `cargo test`ed (directly or through check-local.sh) by a CI job
|
|
typechecked `cargo check`ed only (the crate cannot run on the CI host), with the reason in "notes"
|
|
exempt not covered, with the reason; each entry is an explicit, reviewed decision
|
|
|
|
The script reads the real member and default-member lists from `cargo metadata --locked`, so a crate added to the
|
|
workspace, or to default-members, without a line in the JSON fails the CI job instead of silently going unbuilt.
|
|
It also looks for evidence that the claimed coverage exists: for every `tested` or `typechecked` package the
|
|
workflows or check-local.sh must name it (`-p NAME`, `--manifest-path DIR/Cargo.toml`, or the strict_crates list).
|
|
|
|
Usage: scripts/check-ci-coverage.py [--root DIR] (no model, GPU or network access; needs cargo and the lockfile)
|
|
"""
|
|
import argparse
|
|
import json
|
|
import pathlib
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--root", default=str(pathlib.Path(__file__).resolve().parent.parent))
|
|
root = pathlib.Path(parser.parse_args().root).resolve()
|
|
|
|
metadata = json.loads(
|
|
subprocess.run(
|
|
["cargo", "metadata", "--locked", "--no-deps", "--format-version", "1"],
|
|
cwd=root, check=True, capture_output=True, text=True,
|
|
).stdout
|
|
)
|
|
packages = {p["id"]: p for p in metadata["packages"]}
|
|
members = {packages[i]["name"]: pathlib.Path(packages[i]["manifest_path"]).parent.relative_to(root).as_posix()
|
|
for i in metadata["workspace_members"]}
|
|
defaults = {packages[i]["name"] for i in metadata["workspace_default_members"]}
|
|
|
|
coverage = json.loads((root / "scripts" / "ci-coverage.json").read_text(encoding="utf-8"))
|
|
level = {}
|
|
errors = []
|
|
for name in coverage["tested"]:
|
|
level.setdefault(name, []).append("tested")
|
|
for name in coverage["typechecked"]:
|
|
level.setdefault(name, []).append("typechecked")
|
|
for name in coverage["exempt"]:
|
|
level.setdefault(name, []).append("exempt")
|
|
|
|
for name in sorted(members):
|
|
got = level.get(name, [])
|
|
if not got:
|
|
where = "default-member" if name in defaults else "member"
|
|
errors.append(f"{name}: workspace {where} with no CI coverage entry in scripts/ci-coverage.json")
|
|
elif len(got) > 1:
|
|
errors.append(f"{name}: listed more than once ({', '.join(got)})")
|
|
for name in sorted(level):
|
|
if name not in members:
|
|
errors.append(f"{name}: listed in scripts/ci-coverage.json but not a workspace member (stale entry)")
|
|
for name, reason in coverage["exempt"].items():
|
|
if not reason.strip():
|
|
errors.append(f"{name}: exempt without a reason")
|
|
for name in sorted(defaults & {n for n, v in level.items() if v == ["exempt"]}):
|
|
errors.append(f"{name}: a default-member (built by a plain `cargo build`) is exempt from CI; "
|
|
"give it a build/test step or take it out of default-members")
|
|
|
|
corpus = ""
|
|
for path in [*sorted((root / ".github" / "workflows").glob("*.yml")), root / "scripts" / "check-local.sh"]:
|
|
corpus += path.read_text(encoding="utf-8") + "\n"
|
|
strict = re.search(r"strict_crates=\(([^)]*)\)", corpus)
|
|
strict_names = set(strict.group(1).split()) if strict else set()
|
|
for name in sorted(n for n, v in level.items() if v and v[0] in ("tested", "typechecked") and n in members):
|
|
directory = members[name]
|
|
evidence = (
|
|
re.search(rf"-p\s+{re.escape(name)}(?![\w-])", corpus)
|
|
or re.search(rf"--manifest-path\s+{re.escape(directory)}/Cargo\.toml", corpus)
|
|
or name in strict_names
|
|
)
|
|
if not evidence:
|
|
errors.append(f"{name}: claimed {level[name][0]} but no workflow or check-local.sh step names it "
|
|
f"(-p {name} / --manifest-path {directory}/Cargo.toml)")
|
|
|
|
print(f"workspace members: {len(members)}; default-members: {len(defaults)}; "
|
|
f"tested {len(coverage['tested'])}, typechecked {len(coverage['typechecked'])}, exempt {len(coverage['exempt'])}")
|
|
for line in errors:
|
|
print(f"UNCOVERED: {line}", file=sys.stderr)
|
|
return 1 if errors else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|