diff --git a/scripts/autorun.py b/scripts/autorun.py index ca4e18656..bb807d967 100644 --- a/scripts/autorun.py +++ b/scripts/autorun.py @@ -44,7 +44,7 @@ from core.hostguard import ( ) from unattended.breaker import gh_json from unattended.jobs import DEFERRED, OK, load_jobs -from unattended.runner import Context, render_status, run_job, status +from unattended.runner import Context, UnknownJob, describe, find_job, render_status, run_job, status from unattended.timers import install_timers, render_units from unattended.worktree import main_checkout @@ -90,7 +90,13 @@ def main() -> int: ) if args.command == "run": ctx.dry_run, ctx.push = args.dry_run, args.push - result, detail = run_job(ctx, args.job) + try: + if args.dry_run: + print("\n".join(describe(find_job(ctx, args.job), ctx))) + result, detail = run_job(ctx, args.job) + except UnknownJob as exc: + print(exc, file=sys.stderr) + return 1 print(f"{args.job}: {result} ({detail})") return EXIT_CODES.get(result, 1) if args.command == "status": diff --git a/scripts/unattended/runner.py b/scripts/unattended/runner.py index cf053dda5..8f146176c 100644 --- a/scripts/unattended/runner.py +++ b/scripts/unattended/runner.py @@ -444,13 +444,22 @@ def describe(job: Job, ctx: Context) -> list[str]: return lines -def run_job(ctx: Context, name: str) -> tuple[str, str]: - """Run one job and record it, returning (result, detail).""" +class UnknownJob(LookupError): + """A job name that automation/jobs.yml does not declare.""" + + +def find_job(ctx: Context, name: str) -> Job: + """The job of that name, or UnknownJob naming the declared ones.""" job = ctx.jobs.get(name) if job is None: - raise SystemExit(f"unknown job {name!r}: {', '.join(sorted(ctx.jobs))}") + raise UnknownJob(f"unknown job {name!r}: {', '.join(sorted(ctx.jobs))}") + return job + + +def run_job(ctx: Context, name: str) -> tuple[str, str]: + """Run one job and record it, returning (result, detail).""" + job = find_job(ctx, name) if ctx.dry_run: - print("\n".join(describe(job, ctx))) return OK, "dry run" state = load_state(ctx.state_path) runner = Runner(ctx, job, state) diff --git a/tests/test_autorun.py b/tests/test_autorun.py index 332c254df..a188f1d2a 100644 --- a/tests/test_autorun.py +++ b/tests/test_autorun.py @@ -9,8 +9,6 @@ from __future__ import annotations import tests -import contextlib -import io import json import os import shutil @@ -195,6 +193,13 @@ class Guards(Scratch): self.assertIn("GB free", detail) self.assertFalse((ctx.repo / "ran").exists()) + def test_an_unknown_job_raises_and_records_nothing(self): + ctx = self.context({"x": {"worktree": False, "min_free_gb": 0, "steps": []}}) + with self.assertRaises(runner.UnknownJob) as caught: + runner.run_job(ctx, "y") + self.assertEqual(str(caught.exception), "unknown job 'y': x") + self.assertFalse(ctx.state_path.exists()) + def test_a_failure_counts_and_a_success_resets(self): ctx = self.context({ "bad": {"worktree": False, "min_free_gb": 0, "steps": [[PY, "-c", "raise SystemExit(3)"]]}, @@ -517,8 +522,7 @@ class Commits(Origin): jobs=unattended_jobs.parse_jobs({"jobs": {"n": {"commits": [{ "subject": "chore: regenerate collection records", "paths": ["database.json", "install/*.json"]}]}}})) - with contextlib.redirect_stdout(io.StringIO()): - self.assertEqual(runner.run_job(ctx, "n"), (unattended_jobs.OK, "dry run")) + self.assertEqual(runner.run_job(ctx, "n"), (unattended_jobs.OK, "dry run")) lines = runner.describe(ctx.jobs["n"], ctx) would = sorted(line.split("would commit ")[1] for line in lines if "would commit" in line) self.assertEqual(would, ["database.json", "install/b.json"])