|
| 1 | +""" |
| 2 | +Run the workspace smoke test suite. |
| 3 | +
|
| 4 | +Reads `smoke_tests.txt` from the workspace root and `config/build/env_vars.yaml` |
| 5 | +for per-script env var overrides, then runs each listed script with the |
| 6 | +appropriate environment. Continues through failures and exits non-zero |
| 7 | +if any script failed. |
| 8 | +
|
| 9 | +Mirrors the logic of the `/smoke-test` skill so CI and local runs stay |
| 10 | +in sync. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +import time |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import yaml |
| 22 | + |
| 23 | + |
| 24 | +WORKSPACE = Path(__file__).resolve().parents[2] |
| 25 | +SMOKE_FILE = WORKSPACE / "smoke_tests.txt" |
| 26 | +ENV_VARS_FILE = WORKSPACE / "config" / "build" / "env_vars.yaml" |
| 27 | +SCRIPTS_DIR = WORKSPACE / "scripts" |
| 28 | + |
| 29 | + |
| 30 | +def load_smoke_scripts() -> list[str]: |
| 31 | + scripts: list[str] = [] |
| 32 | + for line in SMOKE_FILE.read_text().splitlines(): |
| 33 | + line = line.strip() |
| 34 | + if not line or line.startswith("#"): |
| 35 | + continue |
| 36 | + scripts.append(line) |
| 37 | + return scripts |
| 38 | + |
| 39 | + |
| 40 | +def load_env_config() -> dict: |
| 41 | + if not ENV_VARS_FILE.exists(): |
| 42 | + return {"defaults": {}, "overrides": []} |
| 43 | + return yaml.safe_load(ENV_VARS_FILE.read_text()) or {} |
| 44 | + |
| 45 | + |
| 46 | +def pattern_matches(pattern: str, script_path: str) -> bool: |
| 47 | + if "/" in pattern: |
| 48 | + return pattern in script_path |
| 49 | + return Path(script_path).stem == pattern |
| 50 | + |
| 51 | + |
| 52 | +def build_env(script_rel: str, cfg: dict) -> dict: |
| 53 | + env = os.environ.copy() |
| 54 | + defaults = cfg.get("defaults") or {} |
| 55 | + env.update({k: str(v) for k, v in defaults.items()}) |
| 56 | + for override in cfg.get("overrides") or []: |
| 57 | + if pattern_matches(override["pattern"], script_rel): |
| 58 | + for key in override.get("unset", []): |
| 59 | + env.pop(key, None) |
| 60 | + for key, val in (override.get("set") or {}).items(): |
| 61 | + env[key] = str(val) |
| 62 | + return env |
| 63 | + |
| 64 | + |
| 65 | +def run_one(script_rel: str, cfg: dict) -> tuple[str, int, float, str]: |
| 66 | + env = build_env(script_rel, cfg) |
| 67 | + script_path = SCRIPTS_DIR / script_rel |
| 68 | + t0 = time.time() |
| 69 | + result = subprocess.run( |
| 70 | + [sys.executable, str(script_path)], |
| 71 | + cwd=str(WORKSPACE), |
| 72 | + env=env, |
| 73 | + capture_output=True, |
| 74 | + text=True, |
| 75 | + ) |
| 76 | + elapsed = time.time() - t0 |
| 77 | + output = result.stdout + result.stderr |
| 78 | + return script_rel, result.returncode, elapsed, output |
| 79 | + |
| 80 | + |
| 81 | +def main() -> int: |
| 82 | + if not SMOKE_FILE.exists(): |
| 83 | + print(f"ERROR: no smoke_tests.txt at {SMOKE_FILE}", file=sys.stderr) |
| 84 | + return 1 |
| 85 | + scripts = load_smoke_scripts() |
| 86 | + if not scripts: |
| 87 | + print("No smoke test scripts listed.") |
| 88 | + return 0 |
| 89 | + cfg = load_env_config() |
| 90 | + |
| 91 | + print(f"Running {len(scripts)} smoke test script(s) from {SMOKE_FILE.name}\n") |
| 92 | + failures: list[tuple[str, int, str]] = [] |
| 93 | + for script_rel in scripts: |
| 94 | + print(f"::group::{script_rel}") |
| 95 | + name, rc, elapsed, output = run_one(script_rel, cfg) |
| 96 | + print(output, end="") |
| 97 | + status = "PASS" if rc == 0 else f"FAIL (exit {rc})" |
| 98 | + print(f"\n[{status}] {name} — {elapsed:.1f}s") |
| 99 | + print("::endgroup::") |
| 100 | + if rc != 0: |
| 101 | + failures.append((name, rc, output)) |
| 102 | + |
| 103 | + total = len(scripts) |
| 104 | + passed = total - len(failures) |
| 105 | + print(f"\n=== Smoke test summary: {passed}/{total} passed ===") |
| 106 | + for name, rc, _ in failures: |
| 107 | + print(f" FAIL {name} (exit {rc})") |
| 108 | + return 0 if not failures else 1 |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + sys.exit(main()) |
0 commit comments