Skip to main content

amont_runtime/
registry.rs

1//! The hook registry — one table, one signature.
2//!
3//! Before this, dispatch was a 20-arm `match` in main.rs and handlers had four
4//! different signatures (`run(&args)`, `run(&hook, &args)`, `argo_lint(&args)`,
5//! …). Two costs, one of them real:
6//!
7//!   - adding a hook meant touching a match arm, a module, and remembering
8//!     which signature that one used;
9//!   - the hook NAME was written twice — in the arm and as the shim's filename
10//!     — with nothing checking they agree. A shim the binary does not recognise
11//!     exits 2 and blocks the commit; a handler with no shim is dead code. The
12//!     consistency test below turns that pairing into something enforced.
13
14use std::ffi::OsString;
15use std::path::Path;
16
17use crate::check::{Builtin, Check, Fix, GitState, Outcome, Scope, Severity, Stage, Verdict};
18use crate::pushrefs::PushRefs;
19use crate::{dispatch, hooks};
20
21/// Everything a hook is given. One shape for all of them, so a handler that
22/// needs the invoked name (ban-terms excludes its own source by it) or the
23/// hooks directory (the dispatchers glob it) does not need its own signature.
24pub struct Ctx<'a> {
25    /// The hook name as invoked.
26    pub name: &'a str,
27    /// Arguments git passed the hook.
28    pub args: &'a [OsString],
29    /// Directory the shim lives in. Only foreign sub-hooks are found here now;
30    /// our own checks are functions in this binary.
31    pub hooks_dir: &'a Path,
32    /// The pre-push ref list, read from stdin at most once and lent to every
33    /// check that asks. See `pushrefs`.
34    pub push: &'a PushRefs,
35    /// What this repository's manifest declares — parsed and trust-gated once
36    /// by the entrypoint, lent to everything downstream. The same shape as
37    /// `push`: read once, owned high, borrowed everywhere.
38    pub manifest: &'a crate::manifest::Manifest,
39}
40
41pub type HookFn = fn(&Ctx) -> Verdict;
42
43/// name → handler. The single place a hook is registered.
44/// The five hook names git itself invokes. Everything else is a `Check`.
45pub const ENTRYPOINTS: &[(&str, HookFn)] = &[
46    ("pre-commit", dispatch::pre_commit),
47    ("pre-push", dispatch::pre_push),
48    ("commit-msg", |ctx| hooks::commit_msg::run(ctx.args)),
49    ("prepare-commit-msg", |ctx| {
50        hooks::prepare_commit_msg::run(ctx.args)
51    }),
52    ("post-commit", |ctx| hooks::post_commit::run(ctx)),
53];
54
55/// Every check, in the order its stage runs them.
56///
57/// ONE declaration each: name, stage, scope and function together. This
58/// replaced `REGISTRY` plus `PRE_COMMIT_CHECKS` plus `PRE_PUSH_CHECKS` plus the
59/// fleet crate's `LANGUAGES` — four tables keyed by the same string, kept in
60/// step by reconciliation tests that are now unnecessary rather than passing.
61///
62/// pre-push order is the cost order: refuse a forbidden push before validating
63/// a name, and validate everything structural before paying for a test suite.
64/// Operations during which a content check cannot say anything useful: half the
65/// tree is somebody else's work, and you cannot fix it from inside the
66/// operation anyway.
67///
68/// NOT applied to `merge-conflict` or `ban-terms`. Those are exactly the checks
69/// you want during a resolution commit — leaving a conflict marker in the
70/// commit that RESOLVES a merge is the bug, and importing a banned term from
71/// the other branch is the other one. The old behaviour skipped the whole
72/// pre-commit stage during a cherry-pick, which silenced both.
73const MID_OPERATION: &[GitState] = &[
74    GitState::Merge,
75    GitState::Rebase,
76    GitState::CherryPick,
77    GitState::Revert,
78];
79
80pub const CHECKS: &[Builtin] = &[
81    // ---- pre-commit ----
82    Builtin {
83        name: "pre-commit-argo-lint",
84        stage: Stage::PreCommit,
85        scope: Scope::new(
86            hooks::k8s::EXTS,
87            &["kustomization.yaml", "kustomization.yml"],
88        )
89        .not_during(MID_OPERATION),
90        severity: Severity::Block,
91        fix: Fix::None,
92        run: |ctx| hooks::k8s::argo_lint(ctx.args),
93    },
94    Builtin {
95        name: "pre-commit-ban-terms",
96        stage: Stage::PreCommit,
97        scope: Scope::files(&[".js", ".jsx", ".ts", ".tsx", ".vue"]),
98        severity: Severity::Block,
99        fix: Fix::None,
100        run: |ctx| hooks::ban_terms::run(ctx.name, ctx.args),
101    },
102    // The push-time contract, said at the first commit — when renaming the
103    // branch costs one command and zero rework. Same short name as the
104    // pre-push check on purpose: `hook.skip branch-pattern` silences the
105    // rule, not one of its two voices.
106    Builtin {
107        name: "pre-commit-branch-pattern",
108        stage: Stage::PreCommit,
109        scope: Scope::ALWAYS,
110        severity: Severity::Warn,
111        fix: Fix::None,
112        run: |_ctx| hooks::branch_pattern::early(),
113    },
114    Builtin {
115        name: "pre-commit-cargo-fmt",
116        stage: Stage::PreCommit,
117        scope: Scope::new(hooks::rust_tools::EXTS, &["Cargo.toml"]).not_during(MID_OPERATION),
118        severity: Severity::Block,
119        fix: Fix::Rewrite,
120        run: |ctx| hooks::rust_tools::fmt(ctx.args),
121    },
122    Builtin {
123        name: "pre-commit-clippy",
124        stage: Stage::PreCommit,
125        scope: Scope::new(hooks::rust_tools::EXTS, &["Cargo.toml"]).not_during(MID_OPERATION),
126        severity: Severity::Block,
127        fix: Fix::None,
128        run: |ctx| hooks::rust_tools::clippy(ctx.args),
129    },
130    Builtin {
131        name: "pre-commit-kube-linter",
132        stage: Stage::PreCommit,
133        scope: Scope::new(
134            hooks::k8s::EXTS,
135            &[".kube-linter*.yaml", ".kube-linter*.yml"],
136        )
137        .not_during(MID_OPERATION),
138        severity: Severity::Block,
139        fix: Fix::None,
140        run: |ctx| hooks::k8s::kube_linter(ctx.args),
141    },
142    Builtin {
143        name: "pre-commit-kubeconform",
144        stage: Stage::PreCommit,
145        scope: Scope::new(
146            hooks::k8s::EXTS,
147            &["kustomization.yaml", "kustomization.yml"],
148        )
149        .not_during(MID_OPERATION),
150        severity: Severity::Block,
151        fix: Fix::None,
152        run: |ctx| hooks::k8s::kubeconform(ctx.args),
153    },
154    Builtin {
155        name: "pre-commit-lint-js",
156        stage: Stage::PreCommit,
157        scope: Scope::new(hooks::lint_js::EXTS, &["package.json"]).not_during(MID_OPERATION),
158        severity: Severity::Block,
159        fix: Fix::None,
160        run: |ctx| hooks::lint_js::run(ctx.args),
161    },
162    Builtin {
163        name: "pre-commit-lint-json-yaml",
164        stage: Stage::PreCommit,
165        scope: Scope::files(hooks::lint_json_yaml::EXTS).not_during(MID_OPERATION),
166        severity: Severity::Block,
167        fix: Fix::None,
168        run: |ctx| hooks::lint_json_yaml::run(ctx.args),
169    },
170    Builtin {
171        name: "pre-commit-merge-conflict",
172        stage: Stage::PreCommit,
173        scope: Scope::ALWAYS,
174        severity: Severity::Block,
175        fix: Fix::None,
176        run: |ctx| hooks::merge_conflict::run(ctx.name, ctx.args),
177    },
178    Builtin {
179        name: "pre-commit-package-lock",
180        stage: Stage::PreCommit,
181        scope: Scope::new(&[], &["package.json"]),
182        severity: Severity::Block,
183        fix: Fix::None,
184        run: |ctx| hooks::package_lock::run(ctx.args),
185    },
186    Builtin {
187        name: "pre-commit-prettier",
188        stage: Stage::PreCommit,
189        scope: Scope::new(
190            &[],
191            &[
192                ".prettierrc",
193                ".prettierrc.json",
194                ".prettierrc.yml",
195                ".prettierrc.yaml",
196                ".prettierrc.js",
197                "prettier.config.js",
198            ],
199        )
200        .not_during(MID_OPERATION),
201        severity: Severity::Block,
202        fix: Fix::Rewrite,
203        run: |ctx| hooks::prettier::run(ctx.args),
204    },
205    Builtin {
206        name: "pre-commit-pyright",
207        stage: Stage::PreCommit,
208        scope: Scope::new(
209            hooks::python_tools::EXTS,
210            &[
211                "pyrightconfig.json",
212                "pyrightconfig.jsonc",
213                "pyproject.toml",
214            ],
215        )
216        .not_during(MID_OPERATION),
217        severity: Severity::Block,
218        fix: Fix::None,
219        run: |ctx| hooks::python_tools::pyright(ctx.args),
220    },
221    Builtin {
222        name: "pre-commit-ruff",
223        stage: Stage::PreCommit,
224        scope: Scope::new(
225            hooks::python_tools::EXTS,
226            &["ruff.toml", ".ruff.toml", "pyproject.toml"],
227        )
228        .not_during(MID_OPERATION),
229        severity: Severity::Block,
230        fix: Fix::Rewrite,
231        run: |ctx| hooks::python_tools::ruff(ctx.args),
232    },
233    Builtin {
234        name: "pre-commit-usual-name",
235        stage: Stage::PreCommit,
236        scope: Scope::ALWAYS,
237        severity: Severity::Block,
238        fix: Fix::None,
239        run: |ctx| hooks::usual_name::run(ctx.args),
240    },
241    Builtin {
242        name: "pre-commit-yamllint",
243        stage: Stage::PreCommit,
244        scope: Scope::new(
245            hooks::yamllint::EXTS,
246            &[".yamllint.yaml", ".yamllint.yml", ".yamllint"],
247        )
248        .not_during(MID_OPERATION),
249        severity: Severity::Block,
250        fix: Fix::None,
251        run: |ctx| hooks::yamllint::run(ctx.args),
252    },
253    // ---- pre-push, cheapest and most decisive first ----
254    Builtin {
255        name: "pre-push-branch-protect",
256        stage: Stage::PrePush,
257        scope: Scope::ALWAYS,
258        severity: Severity::Block,
259        fix: Fix::None,
260        run: |ctx| hooks::branch_protect::run(ctx.push.get()),
261    },
262    Builtin {
263        name: "pre-push-branch-pattern",
264        stage: Stage::PrePush,
265        scope: Scope::ALWAYS,
266        severity: Severity::Block,
267        fix: Fix::None,
268        run: |ctx| hooks::branch_pattern::run(ctx.push.get(), ctx.args),
269    },
270    Builtin {
271        name: "pre-push-pull-rebase",
272        stage: Stage::PrePush,
273        scope: Scope::ALWAYS.not_during(&[GitState::Rebase, GitState::Merge]),
274        severity: Severity::Block,
275        fix: Fix::None,
276        run: |ctx| hooks::pull_rebase::run(ctx.args),
277    },
278    Builtin {
279        name: "pre-push-run-tests-js",
280        stage: Stage::PrePush,
281        scope: Scope::new(hooks::run_tests::JS_EXTS, &["package.json"])
282            .not_during(&[GitState::Bisect, GitState::Rebase]),
283        severity: Severity::Block,
284        fix: Fix::None,
285        run: |ctx| hooks::run_tests::run(ctx.push.get(), &ctx.manifest.externals),
286    },
287    Builtin {
288        name: "pre-push-cargo-test",
289        stage: Stage::PrePush,
290        scope: Scope::new(hooks::rust_tools::EXTS, &["Cargo.toml"])
291            .not_during(&[GitState::Bisect, GitState::Rebase]),
292        severity: Severity::Block,
293        fix: Fix::None,
294        run: |ctx| hooks::rust_tools::test(ctx.push.get()),
295    },
296];
297
298/// A check's severity, after any per-repository override.
299///
300/// `git config amont.severity.<check> warn` downgrades a blocking check to a
301/// warning. Unlike `hook.skip` it keeps the signal: the check still runs and
302/// still reports, it just stops failing the commit.
303pub fn severity_of(check: &dyn Check) -> Severity {
304    effective_override(None, check.name()).unwrap_or_else(|| check.severity())
305}
306
307/// The config key a severity override lives under.
308pub fn severity_key(check: &str) -> String {
309    format!("amont.severity.{check}")
310}
311
312/// Every severity override visible here, resolved the way `--get` resolves it.
313///
314/// ONE subprocess for the whole stage, and — more importantly — a VALUE. The
315/// dispatcher used to call `severity_of` inside the loop that classifies
316/// outcomes, which put a `git` spawn in the middle of a fold and made the fold
317/// impossible to test without a repository.
318///
319/// `--get-regexp` emits entries in precedence order, so folding with overwrite
320/// lands on the same answer `--get` gives. That equivalence is not assumed:
321/// `the_batch_agrees_with_the_authority` pins it against `effective_override`.
322#[derive(Debug, Default, Clone)]
323pub struct Overrides(std::collections::BTreeMap<String, Severity>);
324
325impl Overrides {
326    pub fn read() -> Overrides {
327        Overrides::from_config(crate::git::stdout(&[
328            "config",
329            "--get-regexp",
330            r"^amont\.severity\.",
331        ]))
332    }
333
334    /// Built from `--get-regexp`-shaped text (`amont.severity.<check>
335    /// <value>` per line, in git's own precedence order — system, then
336    /// global, then local, then includes). `pub` so a reader that has
337    /// already fetched those lines for its own reasons (the fleet dashboard
338    /// needs the origin of each, which `Overrides` does not track) can still
339    /// ask this — the one place that must not get precedence wrong — rather
340    /// than re-deriving it from the lines by hand.
341    pub fn from_config(out: Option<String>) -> Overrides {
342        let mut map = std::collections::BTreeMap::new();
343        for line in out.as_deref().unwrap_or_default().lines() {
344            let Some((key, value)) = line.split_once(' ') else {
345                continue;
346            };
347            let Some(check) = key.strip_prefix("amont.severity.") else {
348                continue;
349            };
350            // Later entries overwrite earlier ones — git's own precedence,
351            // observed rather than reimplemented.
352            match Severity::parse(value.trim()) {
353                Some(s) => {
354                    map.insert(check.to_string(), s);
355                }
356                // An unrecognised value is not an override at all, and must not
357                // shadow a valid earlier one either.
358                None => {
359                    map.remove(check);
360                }
361            }
362        }
363        Overrides(map)
364    }
365
366    /// The configured key that applies to `check`, and what it says.
367    ///
368    /// Several keys can name one check — `pre-commit` and `clippy` and
369    /// `pre-commit-clippy` all reach `pre-commit-clippy`. The most specific
370    /// wins, which is the rule anybody would guess and the only one that lets
371    /// you downgrade a whole trigger and then exempt one check from it.
372    pub fn applied_to(&self, check: &str) -> Option<(&str, Severity)> {
373        self.0
374            .iter()
375            .filter_map(|(pattern, severity)| {
376                crate::names_check(check, pattern).map(|m| (m, pattern.as_str(), *severity))
377            })
378            .max_by_key(|(m, _, _)| *m)
379            .map(|(_, pattern, severity)| (pattern, severity))
380    }
381
382    /// The severity to apply to `check`, override or declared.
383    pub fn of(&self, check: &dyn Check) -> Severity {
384        self.applied_to(check.name())
385            .map(|(_, severity)| severity)
386            .unwrap_or_else(|| check.severity())
387    }
388}
389
390#[cfg(test)]
391mod precedence {
392    use super::{Overrides, Severity};
393
394    fn overrides(lines: &[&str]) -> Overrides {
395        let text = lines
396            .iter()
397            .map(|l| format!("amont.severity.{l}\n"))
398            .collect::<String>();
399        Overrides::from_config(Some(text))
400    }
401
402    /// The whole reason triggers and short names are allowed as keys: downgrade
403    /// a trigger wholesale, then say something different about one check. If the
404    /// broader key won instead, the exemption would be unwritable.
405    #[test]
406    fn the_more_specific_key_wins() {
407        let both = overrides(&["pre-commit warn", "pre-commit-clippy block"]);
408        assert_eq!(
409            both.applied_to("pre-commit-clippy"),
410            Some(("pre-commit-clippy", Severity::Block)),
411            "a full id beats its trigger"
412        );
413        assert_eq!(
414            both.applied_to("pre-commit-shellcheck"),
415            Some(("pre-commit", Severity::Warn)),
416            "and the trigger still governs every check it did not exempt"
417        );
418    }
419
420    /// Full id > short name > trigger, all three at once, so the ordering is
421    /// pinned end to end rather than one pair at a time.
422    #[test]
423    fn the_three_ways_to_name_a_check_are_ranked() {
424        let all = overrides(&["pre-commit warn", "clippy block", "pre-commit-clippy warn"]);
425        assert_eq!(
426            all.applied_to("pre-commit-clippy"),
427            Some(("pre-commit-clippy", Severity::Warn))
428        );
429
430        let no_full = overrides(&["pre-commit warn", "clippy block"]);
431        assert_eq!(
432            no_full.applied_to("pre-commit-clippy"),
433            Some(("clippy", Severity::Block)),
434            "a short name beats a trigger"
435        );
436    }
437
438    /// A key naming nothing must not become the answer by being the only one
439    /// there — that is how a repo believes it downgraded a check it never named.
440    #[test]
441    fn a_key_that_names_no_check_applies_to_nothing() {
442        let typo = overrides(&["clipy warn", "e warn", " warn"]);
443        assert_eq!(typo.applied_to("pre-commit-clippy"), None);
444    }
445}
446
447/// The override git would actually apply for `check`, or `None` if there is
448/// none (or the value is not one this understands).
449///
450/// `--get`, NOT `--get-regexp`: git returns the LAST value, so a local `block`
451/// beats a global `warn`. A reader that listed every entry instead and treated
452/// each as authoritative would report a downgrade that the dispatcher does not
453/// apply — which is exactly what the dashboard used to do.
454///
455/// `repo` is `None` for the current directory, which is where a hook runs.
456pub fn effective_override(repo: Option<&Path>, check: &str) -> Option<Severity> {
457    overrides_in(repo).applied_to(check).map(|(_, s)| s)
458}
459
460/// Which configured KEY applies to `check` here, if any.
461///
462/// The dashboard needs the key rather than the value: with three ways to name a
463/// check, "which of these lines is the one doing something" is the question a
464/// reader actually has.
465pub fn effective_key(repo: Option<&Path>, check: &str) -> Option<String> {
466    overrides_in(repo)
467        .applied_to(check)
468        .map(|(pattern, _)| pattern.to_string())
469}
470
471fn overrides_in(repo: Option<&Path>) -> Overrides {
472    let args = ["config", "--get-regexp", r"^amont\.severity\."];
473    Overrides::from_config(match repo {
474        None => crate::git::stdout(&args),
475        Some(dir) => crate::git::stdout_in(dir, &args),
476    })
477}
478
479/// Built-in checks for one stage, in declared order.
480pub fn stage_checks(stage: Stage) -> impl Iterator<Item = &'static Builtin> {
481    CHECKS.iter().filter(move |check| check.stage == stage)
482}
483
484/// Every check for one stage — built-ins first, then whatever this repository
485/// declares in `amont.conf`.
486///
487/// The order is not negotiable and not configurable. A third-party command must
488/// not be able to delay `pre-push-branch-protect`, and appending is the only
489/// arrangement in which it cannot.
490pub fn all_stage_checks<'a>(
491    stage: Stage,
492    manifest: &'a crate::manifest::Manifest,
493) -> Vec<&'a dyn Check> {
494    let mut out: Vec<&'a dyn Check> = stage_checks(stage)
495        .map(|check| check as &dyn Check)
496        .collect();
497    out.extend(
498        manifest
499            .externals
500            .iter()
501            .filter(|external| external.stage == stage)
502            .map(|external| external as &dyn Check),
503    );
504    out
505}
506
507pub fn lookup(name: &str, manifest: &crate::manifest::Manifest) -> Option<HookFn> {
508    if let Some((_, f)) = ENTRYPOINTS.iter().find(|(n, _)| *n == name) {
509        return Some(*f);
510    }
511    // A check invoked directly by name — how the tests drive individual checks,
512    // and how `amont <check>` works from a shell. Its Outcome collapses to an
513    // exit code here, honouring severity, so a `warn` check invoked directly
514    // reports without failing exactly as it does inside a dispatcher. The
515    // closure re-resolves through the Ctx it is handed — a plain fn pointer
516    // captures nothing, and the manifest travels ON the Ctx.
517    if CHECKS.iter().any(|check| check.name == name)
518        || manifest
519            .externals
520            .iter()
521            .any(|external| external.id == name)
522    {
523        return Some(|ctx: &Ctx| {
524            let check = one_named(ctx.name, ctx.manifest).expect("checked above");
525            Verdict::blocking(matches!(
526                (check.run(ctx), severity_of(check)),
527                (Outcome::Failed, Severity::Block)
528            ))
529        });
530    }
531    None
532}
533
534/// One check by name, whichever kind it is. Built-ins are searched first, which
535/// costs nothing because `manifest::parse` refuses a name a built-in already
536/// holds — the two guards together mean neither kind can shadow the other.
537pub fn one_named<'a>(name: &str, manifest: &'a crate::manifest::Manifest) -> Option<&'a dyn Check> {
538    if let Some(builtin) = CHECKS.iter().find(|check| check.name == name) {
539        return Some(builtin);
540    }
541    manifest
542        .externals
543        .iter()
544        .find(|external| external.id == name)
545        .map(|external| external as &dyn Check)
546}
547
548#[cfg(test)]
549mod tests {
550    use super::{lookup, Overrides, Severity, Stage, CHECKS, ENTRYPOINTS};
551    use std::collections::BTreeSet;
552
553    /// The batch reader must land on the same answer as the authority.
554    ///
555    /// `Overrides` folds `--get-regexp` output with overwrite; `effective_override`
556    /// asks `--get`. They agree only because git emits entries in precedence
557    /// order — an assumption, so it is asserted against real git rather than
558    /// trusted.
559    #[test]
560    fn the_batch_agrees_with_the_authority() {
561        let d = std::env::temp_dir().join(format!("ov-{}", std::process::id()));
562        let _ = std::fs::remove_dir_all(&d);
563        std::fs::create_dir_all(&d).unwrap();
564        let git = |args: &[&str]| {
565            std::process::Command::new("git")
566                .args(args)
567                .current_dir(&d)
568                .output()
569                .expect("git");
570        };
571        git(&["init", "-q", "--template=", "."]);
572        let key = "amont.severity.pre-commit-merge-conflict";
573        git(&["config", "--add", key, "warn"]);
574        git(&["config", "--add", key, "block"]);
575
576        let raw = std::process::Command::new("git")
577            .args(["config", "--get-regexp", r"^amont\.severity\."])
578            .current_dir(&d)
579            .output()
580            .expect("git");
581        let batch = Overrides::from_config(Some(
582            String::from_utf8_lossy(&raw.stdout).trim().to_string(),
583        ));
584        let authority =
585            crate::git::stdout_in(&d, &["config", "--get", key]).and_then(|v| Severity::parse(&v));
586        let _ = std::fs::remove_dir_all(&d);
587
588        assert_eq!(
589            authority,
590            Some(Severity::Block),
591            "git applies the last entry"
592        );
593        assert_eq!(
594            batch.0.get("pre-commit-merge-conflict").copied(),
595            authority,
596            "the batch reader disagreed with `--get`"
597        );
598    }
599
600    /// An unrecognised value is not an override, and must not shadow a valid
601    /// earlier one either — a typo would otherwise silently restore the
602    /// declared severity in a way nobody could see.
603    #[test]
604    fn an_unrecognised_value_clears_rather_than_overrides() {
605        let o = Overrides::from_config(Some(
606            "amont.severity.a warn\namont.severity.a advisory\namont.severity.b warn".to_string(),
607        ));
608        assert_eq!(o.0.get("a"), None, "a typo must not leave `warn` standing");
609        assert_eq!(o.0.get("b").copied(), Some(Severity::Warn));
610    }
611
612    #[test]
613    fn names_are_unique_across_entrypoints_and_checks() {
614        let mut seen = BTreeSet::new();
615        for n in ENTRYPOINTS
616            .iter()
617            .map(|(n, _)| *n)
618            .chain(CHECKS.iter().map(|check| check.name))
619        {
620            assert!(seen.insert(n), "duplicate registration: {n}");
621        }
622    }
623
624    /// Only FIVE files ship, and they are exactly the hook names git invokes.
625    #[test]
626    fn the_shipped_shims_are_exactly_the_git_invoked_hooks() {
627        let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../../templates/hooks");
628        let mut shipped: Vec<String> = std::fs::read_dir(dir)
629            .expect("templates/hooks")
630            .flatten()
631            .map(|entry| entry.file_name().to_string_lossy().into_owned())
632            .collect();
633        shipped.sort();
634        assert_eq!(
635            shipped,
636            vec![
637                "commit-msg",
638                "post-commit",
639                "pre-commit",
640                "pre-push",
641                "prepare-commit-msg"
642            ]
643        );
644        let none = crate::manifest::Manifest::default();
645        for name in &shipped {
646            assert!(
647                lookup(name, &none).is_some(),
648                "shipped shim {name:?} has no handler"
649            );
650        }
651    }
652
653    /// Every check is reachable by name, which is how a shell — and the tests —
654    /// invoke one directly.
655    #[test]
656    fn every_check_is_reachable_by_name() {
657        let none = crate::manifest::Manifest::default();
658        for check in CHECKS {
659            assert!(
660                lookup(check.name, &none).is_some(),
661                "{} not reachable",
662                check.name
663            );
664        }
665        assert!(lookup("pre-commit-not-a-check", &none).is_none());
666    }
667
668    /// pre-push is serial and fail-fast, so declaration order IS cost order.
669    #[test]
670    fn pre_push_runs_cheapest_first() {
671        let order: Vec<&str> = super::stage_checks(Stage::PrePush)
672            .map(|check| check.name)
673            .collect();
674        assert_eq!(
675            order,
676            vec![
677                "pre-push-branch-protect",
678                "pre-push-branch-pattern",
679                "pre-push-pull-rebase",
680                "pre-push-run-tests-js",
681                "pre-push-cargo-test",
682            ]
683        );
684    }
685
686    /// What a check actually looks at, as opposed to what it DECLARES.
687    ///
688    /// `All` is a check that reads every staged path regardless of the
689    /// extensions in its scope (ban-terms greps them all); `Exts(e)` is a check
690    /// that filters by suffix, and `e` is the list it filters WITH.
691    enum Consumes {
692        All,
693        Exts(&'static [&'static str]),
694    }
695
696    /// Every check, and the file set it consumes. The table exists so a NEW
697    /// check cannot be added without somebody stating the answer.
698    ///
699    /// The incident: `pre-commit-lint-json-yaml` declared
700    /// `[".json", ".yaml", ".yml"]` in the registry while `lint_json_yaml::run`
701    /// asked `staged_files` for `[".yaml"]`. `amont list` reported the check
702    /// as covering `.yml`, the fleet dashboard agreed, and a staged, broken
703    /// `x.yml` returned `Outcome::Passed` with no output whatsoever. Nothing
704    /// connected the two lists, so nothing could notice.
705    ///
706    /// Each entry now names the module constant the check itself filters with,
707    /// which is the same constant the registry declares its scope from — so
708    /// this table cannot silently agree with a stale copy.
709    const CONSUMED: &[(&str, Consumes)] = &[
710        (
711            "pre-commit-argo-lint",
712            Consumes::Exts(crate::hooks::k8s::EXTS),
713        ),
714        // Declares JS extensions so the dashboard can say what it is FOR, but
715        // greps every staged path — a banned term in a `.md` is still a banned
716        // term. This is why the assertion below is a SUBSET check.
717        ("pre-commit-ban-terms", Consumes::All),
718        (
719            "pre-commit-cargo-fmt",
720            Consumes::Exts(crate::hooks::rust_tools::EXTS),
721        ),
722        (
723            "pre-commit-clippy",
724            Consumes::Exts(crate::hooks::rust_tools::RUST_PATHS),
725        ),
726        (
727            "pre-commit-kube-linter",
728            Consumes::Exts(crate::hooks::k8s::EXTS),
729        ),
730        (
731            "pre-commit-kubeconform",
732            Consumes::Exts(crate::hooks::k8s::EXTS),
733        ),
734        (
735            "pre-commit-lint-js",
736            Consumes::Exts(crate::hooks::lint_js::EXTS),
737        ),
738        (
739            "pre-commit-lint-json-yaml",
740            Consumes::Exts(crate::hooks::lint_json_yaml::EXTS),
741        ),
742        ("pre-commit-merge-conflict", Consumes::All),
743        ("pre-commit-package-lock", Consumes::All),
744        // Declares `files: &[]` — it is opt-in by CONFIG, not by file type —
745        // while consuming seventeen extensions. The other reason the assertion
746        // is a subset check rather than an equality.
747        (
748            "pre-commit-prettier",
749            Consumes::Exts(crate::hooks::prettier::EXTS),
750        ),
751        (
752            "pre-commit-pyright",
753            Consumes::Exts(crate::hooks::python_tools::EXTS),
754        ),
755        (
756            "pre-commit-ruff",
757            Consumes::Exts(crate::hooks::python_tools::EXTS),
758        ),
759        ("pre-commit-usual-name", Consumes::All),
760        (
761            "pre-commit-yamllint",
762            Consumes::Exts(crate::hooks::yamllint::EXTS),
763        ),
764        ("pre-push-branch-protect", Consumes::All),
765        ("pre-push-branch-pattern", Consumes::All),
766        ("pre-push-pull-rebase", Consumes::All),
767        (
768            "pre-push-run-tests-js",
769            Consumes::Exts(crate::hooks::run_tests::JS_EXTS),
770        ),
771        (
772            "pre-push-cargo-test",
773            Consumes::Exts(crate::hooks::rust_tools::RUST_PATHS),
774        ),
775    ];
776
777    /// A declared scope must never promise more than the check consumes.
778    ///
779    /// Two halves, and the first is the one that earns its keep: a check with a
780    /// non-empty `scope.files` that nobody has entered in `CONSUMED` fails the
781    /// build, so adding a check forces somebody to state what it actually
782    /// reads. The second half then pins that `scope.files ⊆ consumed`.
783    ///
784    /// SUBSET, not equality, deliberately: `ban-terms` declares JS extensions
785    /// and greps every staged path, and `prettier` declares `files: &[]` while
786    /// consuming seventeen extensions. Equality would forbid both.
787    #[test]
788    fn no_check_declares_a_file_type_it_does_not_consume() {
789        for (name, _) in CONSUMED {
790            assert!(
791                CHECKS.iter().any(|check| check.name == *name),
792                "CONSUMED names {name:?}, which is not a check"
793            );
794        }
795        for check in CHECKS {
796            let entry = CONSUMED.iter().find(|(name, _)| *name == check.name);
797            if check.scope.files.is_empty() && entry.is_none() {
798                continue;
799            }
800            let Some((_, consumes)) = entry else {
801                panic!(
802                    "{} declares scope.files {:?} but is missing from CONSUMED — \
803                     say what it actually reads",
804                    check.name, check.scope.files
805                );
806            };
807            let Consumes::Exts(consumed) = consumes else {
808                continue; // `All` consumes everything, so any declaration fits
809            };
810            for ext in check.scope.files {
811                assert!(
812                    consumed.contains(ext),
813                    "{} declares {ext:?} in its scope but never asks for it — \
814                     `amont list` would report a coverage the check does not have",
815                    check.name
816                );
817            }
818        }
819    }
820
821    /// Which checks contain code that repairs and re-stages, stated by hand.
822    ///
823    /// `pre-commit-cargo-fmt` and `pre-commit-ruff` both declared
824    /// `Fix::Rewrite` with NO fixing code anywhere — only `prettier.rs` and
825    /// `manifest.rs` ever called `restage`/`fixing_enabled`. `amont list
826    /// --json` reported `"fix":"rewrite"` for them regardless, and `agents_md`
827    /// explicitly directs agents to trust that JSON, so an agent would set
828    /// `amont.fix true` and wait for a repair that could never arrive.
829    const HAS_FIXING_CODE: &[(&str, bool)] = &[
830        ("pre-commit-argo-lint", false),
831        ("pre-commit-ban-terms", false),
832        ("pre-commit-branch-pattern", false),
833        ("pre-commit-cargo-fmt", true),
834        ("pre-commit-clippy", false),
835        ("pre-commit-kube-linter", false),
836        ("pre-commit-kubeconform", false),
837        ("pre-commit-lint-js", false),
838        ("pre-commit-lint-json-yaml", false),
839        ("pre-commit-merge-conflict", false),
840        ("pre-commit-package-lock", false),
841        ("pre-commit-prettier", true),
842        ("pre-commit-pyright", false),
843        ("pre-commit-ruff", true),
844        ("pre-commit-usual-name", false),
845        ("pre-commit-yamllint", false),
846        ("pre-push-branch-protect", false),
847        ("pre-push-branch-pattern", false),
848        ("pre-push-pull-rebase", false),
849        ("pre-push-run-tests-js", false),
850        ("pre-push-cargo-test", false),
851    ];
852
853    /// A `Fix::Rewrite` declaration is a PROMISE, and the set of checks that
854    /// keep it must equal the set that make it.
855    #[test]
856    fn every_rewrite_declaration_has_a_fixer() {
857        let declared: BTreeSet<&str> = CHECKS
858            .iter()
859            .filter(|check| check.fix == super::Fix::Rewrite)
860            .map(|check| check.name)
861            .collect();
862        let implemented: BTreeSet<&str> = HAS_FIXING_CODE
863            .iter()
864            .filter(|(_, has)| *has)
865            .map(|(name, _)| *name)
866            .collect();
867        assert_eq!(
868            declared, implemented,
869            "a check declaring Fix::Rewrite with no fixer lies to `amont list --json`, \
870             and a check with a fixer that does not declare it can never be reached"
871        );
872
873        // …and the table must cover every check, so a new one cannot be added
874        // without somebody answering the question.
875        let listed: BTreeSet<&str> = HAS_FIXING_CODE.iter().map(|(name, _)| *name).collect();
876        let all: BTreeSet<&str> = CHECKS.iter().map(|check| check.name).collect();
877        assert_eq!(listed, all, "HAS_FIXING_CODE does not cover CHECKS");
878    }
879
880    /// The reconciliation tests that used to live here are gone, and that is
881    /// the point of the refactor: there is no second table to disagree with.
882    #[test]
883    fn every_check_declares_a_stage_and_a_scope() {
884        assert_eq!(CHECKS.len(), 21);
885        let pre_commit = super::stage_checks(Stage::PreCommit).count();
886        let pre_push = super::stage_checks(Stage::PrePush).count();
887        assert_eq!(
888            pre_commit + pre_push,
889            CHECKS.len(),
890            "every check has a stage"
891        );
892    }
893}