|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Run every Validate check locally in one command (#364). No Blender needed. |
| 3 | +
|
| 4 | + pip install -r requirements-dev.txt |
| 5 | + python tests/run_all.py # all checks, summary at the end |
| 6 | + python tests/run_all.py -k gallery # only checks whose name contains "gallery" |
| 7 | +
|
| 8 | +It runs every tests/check_*.py and tests/test_*.py, the smoke-harness tests, |
| 9 | +the generator --check modes, and the Python heredoc checks that live inline in |
| 10 | +.github/workflows/validate.yml (extracted from the workflow at run time, so |
| 11 | +there is one copy of each). It does not run Blender, `gh`, or the site build. |
| 12 | +Exit 0 only if everything passed. |
| 13 | +""" |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | +import tempfile |
| 20 | +import textwrap |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +ROOT = Path(__file__).resolve().parent.parent |
| 24 | + |
| 25 | + |
| 26 | +def inline_checks() -> list[tuple[str, str]]: |
| 27 | + """(name, python source) for each `python3 << 'PYEOF'` step in validate.yml.""" |
| 28 | + import yaml |
| 29 | + |
| 30 | + wf = yaml.safe_load((ROOT / ".github" / "workflows" / "validate.yml").read_text(encoding="utf-8")) |
| 31 | + out = [] |
| 32 | + for job in wf["jobs"].values(): |
| 33 | + for step in job.get("steps", []): |
| 34 | + run = step.get("run") or "" |
| 35 | + if "<< 'PYEOF'" in run: |
| 36 | + body = run.split("<< 'PYEOF'\n", 1)[1].rsplit("PYEOF", 1)[0] |
| 37 | + out.append((f"validate.yml: {step.get('name', '?')}", textwrap.dedent(body))) |
| 38 | + return out |
| 39 | + |
| 40 | + |
| 41 | +GALLERY_DRIFT = """ |
| 42 | +import subprocess, sys |
| 43 | +subprocess.run([sys.executable, "scripts/build_gallery.py"], check=True, capture_output=True) |
| 44 | +diff = subprocess.run(["git", "status", "--porcelain", "docs/gallery"], capture_output=True, text=True).stdout |
| 45 | +if diff.strip(): |
| 46 | + sys.exit("docs/gallery/ is stale; commit the regenerated pages: " + diff) |
| 47 | +""" |
| 48 | + |
| 49 | +# check_site_links reads the built landing page, which is not committed. |
| 50 | +SITE_BUILD = """ |
| 51 | +import subprocess, sys |
| 52 | +subprocess.run([sys.executable, "scripts/site/build_site.py", "--repo-root", ".", "--out", "docs"], |
| 53 | + check=True) |
| 54 | +""" |
| 55 | + |
| 56 | + |
| 57 | +def commands() -> list[tuple[str, list[str] | str]]: |
| 58 | + py = sys.executable |
| 59 | + cmds: list[tuple[str, list[str] | str]] = [ |
| 60 | + ("build landing page (for check_site_links)", SITE_BUILD), |
| 61 | + ("committed gallery matches its generator", GALLERY_DRIFT), |
| 62 | + ] |
| 63 | + for path in sorted((ROOT / "tests").glob("check_*.py")): |
| 64 | + cmds.append((path.name, [py, str(path)])) |
| 65 | + for path in [*sorted((ROOT / "tests").glob("test_*.py")), ROOT / "tests" / "smoke" / "test_harness.py"]: |
| 66 | + cmds.append((path.name, [py, str(path)])) |
| 67 | + cmds.append(("build_claude_rules --check", [py, str(ROOT / "scripts" / "build_claude_rules.py"), "--check"])) |
| 68 | + cmds.append(("build_plugin_dist --check", [py, str(ROOT / "scripts" / "build_plugin_dist.py"), "--check"])) |
| 69 | + cmds += inline_checks() |
| 70 | + return cmds |
| 71 | + |
| 72 | + |
| 73 | +def run(name: str, cmd: list[str] | str) -> bool: |
| 74 | + if isinstance(cmd, str): # inline heredoc source |
| 75 | + with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False, encoding="utf-8") as fh: |
| 76 | + fh.write(cmd) |
| 77 | + argv = [sys.executable, fh.name] |
| 78 | + else: |
| 79 | + argv = cmd |
| 80 | + proc = subprocess.run(argv, cwd=ROOT, capture_output=True, text=True, |
| 81 | + encoding="utf-8", errors="replace") |
| 82 | + ok = proc.returncode == 0 |
| 83 | + print(f"{'PASS' if ok else 'FAIL'} {name}", flush=True) |
| 84 | + if not ok: |
| 85 | + tail = (proc.stdout + proc.stderr).strip().splitlines()[-15:] |
| 86 | + print("\n".join(" " + line for line in tail), flush=True) |
| 87 | + return ok |
| 88 | + |
| 89 | + |
| 90 | +def main(argv=None) -> int: |
| 91 | + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 92 | + ap.add_argument("-k", default="", help="only run checks whose name contains this") |
| 93 | + args = ap.parse_args(argv) |
| 94 | + todo = [(n, c) for n, c in commands() if args.k.lower() in n.lower()] |
| 95 | + failed = [n for n, c in todo if not run(n, c)] |
| 96 | + print(f"\n{len(todo) - len(failed)} passed, {len(failed)} failed") |
| 97 | + return 1 if failed else 0 |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + sys.exit(main()) |
0 commit comments