Skip to main content

testing_conventions/
lib.rs

1pub mod co_change;
2pub mod colocated_test;
3pub mod config;
4pub mod coverage;
5pub mod e2e;
6pub mod isolation;
7pub mod lint;
8pub mod mutation;
9pub mod packaging;
10pub mod patch_coverage;
11pub mod ts;
12pub mod violation;
13pub mod workflow;
14
15use std::path::{Path, PathBuf};
16
17use clap::{CommandFactory, Parser, Subcommand};
18
19#[derive(Parser, Debug)]
20#[command(
21    name = "testing-conventions",
22    version,
23    about = "Enforce testing conventions in libraries (Python, TypeScript, and Rust).",
24    long_about = None,
25)]
26pub struct Cli {
27    #[command(subcommand)]
28    command: Option<Command>,
29}
30
31#[derive(Subcommand, Debug)]
32enum Command {
33    /// Check the repository against its testing-conventions config.
34    Check,
35    /// Unit-test conventions.
36    Unit {
37        #[command(subcommand)]
38        rule: UnitRule,
39    },
40    /// Integration-test conventions.
41    Integration {
42        #[command(subcommand)]
43        rule: IntegrationRule,
44    },
45    /// Packaging conventions: test files must not ship in the built artifact.
46    Packaging {
47        /// Root of the built artifact to inspect (e.g. an unpacked wheel or `dist/`).
48        path: PathBuf,
49        /// Language convention to enforce (required).
50        #[arg(long, value_enum)]
51        language: colocated_test::Language,
52    },
53    /// Workflow guard (private — hidden from `--help`, #191): every `testing-conventions`
54    /// invocation in a CI workflow must name a subcommand this binary still exposes
55    /// (guards the `@v0` path, #92). Run from our own CI, not a documented consumer command;
56    /// it stays in the binary because the guard needs the in-process command tree.
57    #[command(hide = true)]
58    Workflow {
59        /// Workflow file (or a directory of them) to scan.
60        path: PathBuf,
61    },
62    /// End-to-end-test conventions.
63    E2e {
64        #[command(subcommand)]
65        command: E2eCommand,
66    },
67}
68
69/// Rules enforced on the unit-test suite (the README's "Unit" taxonomy).
70#[derive(Subcommand, Debug)]
71enum UnitRule {
72    /// Check that every source file has a colocated, matching-named unit test
73    /// (tree-wide presence). With `--base`, additionally run the commit-scoped
74    /// `co-change` check over `<base>...HEAD` (#33): a modified or deleted source
75    /// whose colocated test is not in the diff fails. Presence always runs;
76    /// `--base` *adds* the diff-scoped check.
77    ColocatedTest {
78        /// Directory to scan recursively.
79        path: PathBuf,
80        /// Language convention to enforce (required).
81        #[arg(long, value_enum)]
82        language: colocated_test::Language,
83        /// Opt-in commit-scoped co-change check (#33): diff `<base>...HEAD` and
84        /// also flag a modified or deleted source whose colocated test didn't
85        /// co-change. Absent means presence-only — there is no default. Python /
86        /// TypeScript only: `--base --language rust` is rejected (inline
87        /// `#[cfg(test)]` units have no sibling test to go stale).
88        #[arg(long)]
89        base: Option<String>,
90        /// testing-conventions config file providing the `exempt` list. Optional:
91        /// if the file is absent, no files are exempt.
92        #[arg(long, default_value = "testing-conventions.toml")]
93        config: PathBuf,
94    },
95    /// Check that the unit suite meets the configured coverage floor. With
96    /// `--base`, the same configured floor is measured over the `<base>...HEAD`
97    /// diff (the changed lines) instead of the whole tree (#162) — a changed line
98    /// below the floor fails, no matter how small the diff.
99    Coverage {
100        /// Directory whose unit suite is run and measured.
101        path: PathBuf,
102        /// Language convention to enforce (required).
103        #[arg(long, value_enum)]
104        language: colocated_test::Language,
105        /// Opt-in diff-scoped coverage (#162): diff `<base>...HEAD` and measure the
106        /// configured floor over only the changed lines, instead of the whole tree.
107        /// Absent means whole-tree — there is no default. This is the patch-scoped
108        /// check the old `unit patch-coverage` command did, re-homed onto the floor
109        /// it shares.
110        #[arg(long)]
111        base: Option<String>,
112        /// testing-conventions config file with the coverage thresholds and
113        /// `exempt` list. Optional: if the file — or its `[<language>].coverage`
114        /// table — is absent, the language's sane default floor is used and
115        /// nothing is exempt.
116        #[arg(long, default_value = "testing-conventions.toml")]
117        config: PathBuf,
118    },
119    /// Lint unit test files for isolation: mock every collaborator (Python, TypeScript, Rust).
120    Lint {
121        /// Crate root / source dir to scan recursively.
122        path: PathBuf,
123        /// Language convention to enforce (required).
124        #[arg(long, value_enum)]
125        language: isolation::Language,
126        /// testing-conventions config file providing the `exempt` list (waivers).
127        /// Optional: if the file is absent, nothing is waived.
128        #[arg(long, default_value = "testing-conventions.toml")]
129        config: PathBuf,
130    },
131    /// Run mutation testing over the unit suite and fail on any surviving mutant not
132    /// lifted by a `mutation` exemption — the rung above coverage (#201). The gate is
133    /// on by default (no report-only mode). Rust only for now, and not yet wired into
134    /// the reusable workflow (parity pending, #199).
135    Mutation {
136        /// Crate whose unit suite is mutated.
137        path: PathBuf,
138        /// Language convention to enforce (required). `rust` only for now.
139        #[arg(long, value_enum)]
140        language: colocated_test::Language,
141        /// Opt-in diff-scoping (#201): restrict to mutants on lines a `<base>...HEAD`
142        /// diff added or modified, via cargo-mutants' `--in-diff`. Absent means the
143        /// whole crate (slower).
144        #[arg(long)]
145        base: Option<String>,
146        /// testing-conventions config file providing the `exempt` list. Optional:
147        /// absent means nothing is exempt (every survivor must be killed).
148        #[arg(long, default_value = "testing-conventions.toml")]
149        config: PathBuf,
150    },
151}
152
153/// Languages the integration-test lints support — its own set (Python,
154/// TypeScript, Rust), distinct from the file-pairing `colocated_test::Language`,
155/// so adding Rust here doesn't touch the colocated-test/coverage rules.
156#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
157pub enum IntegrationLintLanguage {
158    /// Python test files (`*_test.py`, `test_*.py`, `conftest.py`).
159    #[value(name = "python")]
160    Python,
161    /// TypeScript test files (`*.test.{ts,tsx,mts,cts}`).
162    #[value(name = "typescript")]
163    TypeScript,
164    /// Rust integration crates under `tests/`.
165    #[value(name = "rust")]
166    Rust,
167}
168
169/// Lints enforced on integration tests (mocking mechanism & style, and more to
170/// come). The README's "Integration" taxonomy.
171#[derive(Subcommand, Debug)]
172enum IntegrationRule {
173    /// Lint integration test files for mocking mechanism & style (Python, TypeScript, Rust).
174    Lint {
175        /// Directory to scan recursively for test files.
176        path: PathBuf,
177        /// Language convention to enforce (required).
178        #[arg(long, value_enum)]
179        language: IntegrationLintLanguage,
180        /// testing-conventions config file providing the `exempt` list (waivers).
181        /// Optional: if the file is absent, nothing is waived.
182        #[arg(long, default_value = "testing-conventions.toml")]
183        config: PathBuf,
184    },
185}
186
187/// E2E attestation commands (#17): record a local e2e run and (later, #68)
188/// verify in CI that the latest code commit is attested.
189#[derive(Subcommand, Debug)]
190enum E2eCommand {
191    /// Run the e2e suite and write a committed attestation naming the current commit.
192    Attest {
193        /// The e2e command to run (e.g. `pnpm run e2e`), executed via the shell.
194        command: String,
195    },
196    /// Verify the committed attestation names the latest code commit (the CI gate).
197    Verify,
198}
199
200pub fn run<I, T>(args: I) -> anyhow::Result<i32>
201where
202    I: IntoIterator<Item = T>,
203    T: Into<std::ffi::OsString> + Clone,
204{
205    let cli = Cli::try_parse_from(args)?;
206    match cli.command {
207        // The config-driven `check` umbrella isn't wired yet; the scaffold
208        // proves the wiring while individual rules land under their test-kind
209        // group (e.g. `unit colocated-test`).
210        Some(Command::Check) | None => Ok(0),
211        Some(Command::Unit { rule }) => match rule {
212            UnitRule::ColocatedTest {
213                path,
214                language,
215                base,
216                config,
217            } => run_unit_colocated_test(&path, language, base.as_deref(), &config),
218            UnitRule::Coverage {
219                path,
220                language,
221                base,
222                config,
223            } => run_unit_coverage(&path, language, base.as_deref(), &config),
224            UnitRule::Lint {
225                path,
226                language,
227                config,
228            } => run_unit_lint(&path, language, &config),
229            UnitRule::Mutation {
230                path,
231                language,
232                base,
233                config,
234            } => run_unit_mutation(&path, language, base.as_deref(), &config),
235        },
236        Some(Command::Integration { rule }) => match rule {
237            IntegrationRule::Lint {
238                path,
239                language,
240                config,
241            } => run_integration_lint(&path, language, &config),
242        },
243        Some(Command::Packaging { path, language }) => run_packaging(&path, language),
244        Some(Command::Workflow { path }) => run_workflow(&path),
245        Some(Command::E2e { command }) => match command {
246            E2eCommand::Attest { command } => run_e2e_attest(&command),
247            E2eCommand::Verify => run_e2e_verify(),
248        },
249    }
250}
251
252/// The binary's own clap command tree — the source of truth for which subcommands
253/// it exposes. The `workflow` guard (#92) checks a workflow's invocations against
254/// it, so a renamed or removed subcommand is caught the moment they diverge.
255pub fn command() -> clap::Command {
256    Cli::command()
257}
258
259/// Run the unit colocated-test check over `root` for `language`. Always runs the
260/// tree-wide **presence** check (every source file has its colocated test; Rust:
261/// an inline `#[cfg(test)]` module). When `base` is `Some`, *additionally* runs the
262/// commit-scoped **co-change** check (#33) over `<base>...HEAD` — a modified or
263/// deleted source whose colocated test didn't co-change — and the run fails if
264/// either check does. Returns `0` only when both pass.
265///
266/// Presence loads the `colocated-test`-rule exemptions and co-change the
267/// `co-change`-rule exemptions from the config at `config_path` (no config file →
268/// no exemptions). `--base` rejects `--language rust`: Rust units are inline
269/// `#[cfg(test)]` in the same file, so a sibling test can't go stale (presence,
270/// without `--base`, still supports Rust).
271fn run_unit_colocated_test(
272    root: &Path,
273    language: colocated_test::Language,
274    base: Option<&str>,
275    config_path: &Path,
276) -> anyhow::Result<i32> {
277    // `--base` carries the co-change check, which rejects Rust the same way the
278    // standalone `unit co-change` did — before any work, so the message matches.
279    if base.is_some() && language == colocated_test::Language::Rust {
280        anyhow::bail!(
281            "`unit colocated-test --base` supports `--language python` / `typescript`; Rust \
282             units are inline `#[cfg(test)]` in the same file, so a sibling test can't go stale"
283        );
284    }
285    let presence_clean = report_colocated_presence(root, language, config_path)?;
286    let co_change_clean = match base {
287        Some(base) => report_co_change(root, base, language, config_path)?,
288        None => true,
289    };
290    Ok(if presence_clean && co_change_clean {
291        0
292    } else {
293        1
294    })
295}
296
297/// The tree-wide colocated-test **presence** check: every source file under `root`
298/// has its colocated unit test (Rust: an inline `#[cfg(test)]` module). Prints each
299/// orphan to stderr and returns `Ok(false)` when any are found, `Ok(true)` when the
300/// tree is clean. The `colocated-test`-rule exemptions from the config at
301/// `config_path` lift a file (no config file → nothing exempt).
302fn report_colocated_presence(
303    root: &Path,
304    language: colocated_test::Language,
305    config_path: &Path,
306) -> anyhow::Result<bool> {
307    let exempt = colocated_test_exemptions(root, language, config_path)?;
308    let orphans = match language {
309        // Rust units are inline `#[cfg(test)]` modules, so "colocated" means a test
310        // module in the same file, not a sibling file (#40).
311        colocated_test::Language::Rust => colocated_test::missing_inline_tests(root, &exempt)?,
312        _ => colocated_test::missing_unit_tests(root, language, &exempt)?,
313    };
314    if orphans.is_empty() {
315        return Ok(true);
316    }
317    let (label, summary) = match language {
318        colocated_test::Language::Rust => (
319            "missing inline `#[cfg(test)]` tests",
320            "source file(s) with testable code but no inline `#[cfg(test)]` module \
321             (add an inline test module, or an `exempt` entry with a reason)",
322        ),
323        _ => (
324            "missing colocated unit test",
325            "source file(s) missing a colocated unit test \
326             (add a colocated test, or an `exempt` entry with a reason)",
327        ),
328    };
329    for orphan in &orphans {
330        eprintln!("{label}: {}", orphan.display());
331    }
332    eprintln!("error: {} {summary}", orphans.len());
333    Ok(false)
334}
335
336/// The `colocated-test`-rule exempt paths for `language`, resolved (and validated)
337/// from the config at `config_path`. A missing config file means no exemptions —
338/// the check still runs, just with nothing exempted.
339fn colocated_test_exemptions(
340    root: &Path,
341    language: colocated_test::Language,
342    config_path: &Path,
343) -> anyhow::Result<std::collections::BTreeSet<String>> {
344    if !config_path.exists() {
345        return Ok(std::collections::BTreeSet::new());
346    }
347    let config = config::load_config(config_path)?;
348    config::resolve_exempt(
349        root,
350        config.exemptions(language),
351        config::Rule::ColocatedTest,
352    )
353}
354
355/// The commit-scoped **co-change** check (#33) over `root`, diffing `<base>...HEAD`:
356/// every modified or deleted source whose colocated test didn't co-change. Prints
357/// each stale source to stderr and returns `Ok(false)` when any are found,
358/// `Ok(true)` when clean.
359///
360/// Loads the `co-change`-rule exemptions from the config at `config_path` (no
361/// config file → no exemptions); an exempt source needn't co-change. The caller
362/// rejects `--language rust` before this runs: Rust units are inline `#[cfg(test)]`
363/// in the same file, so a sibling test can't go stale.
364fn report_co_change(
365    root: &Path,
366    base: &str,
367    language: colocated_test::Language,
368    config_path: &Path,
369) -> anyhow::Result<bool> {
370    let exempt = co_change_exemptions(root, language, config_path)?;
371    let stale = co_change::stale_sources(root, base, language, &exempt)?;
372    if stale.is_empty() {
373        return Ok(true);
374    }
375    for source in &stale {
376        eprintln!(
377            "source changed without its colocated test: {}",
378            source.display()
379        );
380    }
381    eprintln!(
382        "error: {} source file(s) changed without their colocated test co-changing \
383         (update the test, or add an `exempt` entry with a reason)",
384        stale.len()
385    );
386    Ok(false)
387}
388
389/// The `co-change`-rule exempt paths for `language`, resolved (and validated)
390/// from the config at `config_path`. A missing config file means no exemptions —
391/// every changed source must co-change its test.
392fn co_change_exemptions(
393    root: &Path,
394    language: colocated_test::Language,
395    config_path: &Path,
396) -> anyhow::Result<std::collections::BTreeSet<String>> {
397    if !config_path.exists() {
398        return Ok(std::collections::BTreeSet::new());
399    }
400    let config = config::load_config(config_path)?;
401    config::resolve_exempt(root, config.exemptions(language), config::Rule::CoChange)
402}
403
404/// Split a resolved exempt-scope map (#226) into the whole-file paths and the
405/// line-scoped sets — the two shapes the `coverage` / `mutation` measure functions
406/// take. A [`config::LineScope::WholeFile`] becomes a path in the first vec; a
407/// [`config::LineScope::Lines`] a `path → lines` entry in the second map.
408fn split_scopes(
409    scopes: std::collections::BTreeMap<String, config::LineScope>,
410) -> (
411    Vec<String>,
412    std::collections::BTreeMap<String, std::collections::BTreeSet<u32>>,
413) {
414    let mut whole_file = Vec::new();
415    let mut line_scoped = std::collections::BTreeMap::new();
416    for (path, scope) in scopes {
417        match scope {
418            config::LineScope::WholeFile => whole_file.push(path),
419            config::LineScope::Lines(lines) => {
420                line_scoped.insert(path, lines);
421            }
422        }
423    }
424    (whole_file, line_scoped)
425}
426
427/// Run the unit-test coverage check over `root` for `language`, enforcing the
428/// floor from the config at `config_path`. Returns `0` when the floor is met,
429/// `1` otherwise.
430///
431/// With `base` set, the same configured floor is measured over the
432/// `<base>...HEAD` diff (the changed lines) rather than the whole tree (#162),
433/// via the diff-scoped [`patch_coverage::measure`] / `measure_typescript` /
434/// `measure_rust`; without it, the whole-tree [`coverage::measure`] family runs.
435///
436/// Coverage is zero-config by default for Python and TypeScript (#80): a missing
437/// config file — or a config with no `[<language>].coverage` table — falls back to
438/// the language's sane default floor ([`config::PythonCoverage::default`] /
439/// [`config::TypeScriptCoverage::default`]), the same way `unit colocated-test` and
440/// `integration lint` treat an absent config as "nothing exempt". A present
441/// `coverage` table overrides the default; `coverage`-rule exemptions still apply.
442/// Rust (#206) is zero-config too: a missing `[rust].coverage` table falls back to
443/// [`config::RustCoverage::default`] (`lines = 100`, `regions` opt-in, no branch).
444fn run_unit_coverage(
445    root: &Path,
446    language: colocated_test::Language,
447    base: Option<&str>,
448    config_path: &Path,
449) -> anyhow::Result<i32> {
450    let config = if config_path.exists() {
451        config::load_config(config_path)?
452    } else {
453        config::Config::default()
454    };
455    let outcome = match language {
456        colocated_test::Language::Python => {
457            let python = config.python.unwrap_or_default();
458            let coverage = python.coverage.unwrap_or_default();
459            let thresholds = coverage::Thresholds {
460                fail_under: coverage.fail_under,
461                branch: coverage.branch,
462            };
463            let (omit, exempt_lines) = split_scopes(config::resolve_exempt_scoped(
464                root,
465                &python.exempt,
466                config::Rule::Coverage,
467            )?);
468            match base {
469                Some(base) => {
470                    patch_coverage::measure(root, base, thresholds, &omit, &exempt_lines)?
471                }
472                None if exempt_lines.is_empty() => coverage::measure(root, thresholds, &omit)?,
473                None => {
474                    patch_coverage::measure_line_exempt(root, thresholds, &omit, &exempt_lines)?
475                }
476            }
477        }
478        colocated_test::Language::TypeScript => {
479            let typescript = config.typescript.unwrap_or_default();
480            let coverage = typescript.coverage.unwrap_or_default();
481            let thresholds = coverage::TypeScriptThresholds {
482                lines: coverage.lines,
483                branches: coverage.branches,
484                functions: coverage.functions,
485                statements: coverage.statements,
486            };
487            let (exclude, exempt_lines) = split_scopes(config::resolve_exempt_scoped(
488                root,
489                &typescript.exempt,
490                config::Rule::Coverage,
491            )?);
492            match base {
493                Some(base) => patch_coverage::measure_typescript(
494                    root,
495                    base,
496                    thresholds,
497                    &exclude,
498                    &exempt_lines,
499                )?,
500                None if exempt_lines.is_empty() => {
501                    coverage::measure_typescript(root, thresholds, &exclude)?
502                }
503                None => patch_coverage::measure_line_exempt_typescript(
504                    root,
505                    thresholds,
506                    &exclude,
507                    &exempt_lines,
508                )?,
509            }
510        }
511        colocated_test::Language::Rust => {
512            let rust = config.rust.unwrap_or_default();
513            // Zero-config (#206): a missing `[rust].coverage` table falls back to the
514            // default Rust floor (`lines = 100`, with `regions` opt-in and no branch
515            // component) — matching Python/TypeScript — rather than erroring for a
516            // required table. A present table overrides it.
517            let coverage = rust.coverage.unwrap_or_default();
518            let thresholds = coverage::RustThresholds {
519                regions: coverage.regions,
520                lines: coverage.lines,
521            };
522            let (ignore, exempt_lines) = split_scopes(config::resolve_exempt_scoped(
523                root,
524                &rust.exempt,
525                config::Rule::Coverage,
526            )?);
527            match base {
528                Some(base) => {
529                    patch_coverage::measure_rust(root, base, thresholds, &ignore, &exempt_lines)?
530                }
531                None if exempt_lines.is_empty() => {
532                    coverage::measure_rust(root, thresholds, &ignore)?
533                }
534                None => patch_coverage::measure_line_exempt_rust(
535                    root,
536                    thresholds,
537                    &ignore,
538                    &exempt_lines,
539                )?,
540            }
541        }
542    };
543    match outcome {
544        coverage::Outcome::Pass => Ok(0),
545        coverage::Outcome::Fail(reason) => {
546            eprintln!("error: coverage check failed — {reason}");
547            Ok(1)
548        }
549    }
550}
551
552/// Run `unit mutation` over `root` (#201, #202): run the per-language engine and fail
553/// on any surviving mutant not lifted by a `mutation` exemption.
554///
555/// The gate is **on by default and binary** — "no *unexplained* surviving mutant":
556/// every survivor must be killed with an assertion, or lifted by a reason-required
557/// `[[<language>.exempt]] rules = ["mutation"]` for an equivalent / deliberately-defensive
558/// mutation. There is no percentage floor (equivalent mutants make one unreachable)
559/// and no report-only mode — the only loosening is a reasoned, per-file exemption.
560/// All three languages are wired: Rust (cargo-mutants), TypeScript (Stryker), and
561/// Python (cosmic-ray). `--base` scopes the run to the diff.
562fn run_unit_mutation(
563    root: &Path,
564    language: colocated_test::Language,
565    base: Option<&str>,
566    config_path: &Path,
567) -> anyhow::Result<i32> {
568    let config = if config_path.exists() {
569        config::load_config(config_path)?
570    } else {
571        config::Config::default()
572    };
573    let survivors = match language {
574        colocated_test::Language::Rust => {
575            let rust = config.rust.unwrap_or_default();
576            let (exempt, exempt_lines) = split_scopes(config::resolve_exempt_scoped(
577                root,
578                &rust.exempt,
579                config::Rule::Mutation,
580            )?);
581            mutation::measure_rust(root, &exempt, &exempt_lines, base)?
582        }
583        colocated_test::Language::TypeScript => {
584            let typescript = config.typescript.unwrap_or_default();
585            let (exempt, exempt_lines) = split_scopes(config::resolve_exempt_scoped(
586                root,
587                &typescript.exempt,
588                config::Rule::Mutation,
589            )?);
590            mutation::measure_typescript(root, &exempt, &exempt_lines, base)?
591        }
592        colocated_test::Language::Python => {
593            let python = config.python.unwrap_or_default();
594            let (exempt, exempt_lines) = split_scopes(config::resolve_exempt_scoped(
595                root,
596                &python.exempt,
597                config::Rule::Mutation,
598            )?);
599            mutation::measure_python(root, &exempt, &exempt_lines, base)?
600        }
601    };
602    if survivors.is_empty() {
603        println!("unit mutation: no surviving mutants — every mutation was caught");
604        return Ok(0);
605    }
606
607    eprintln!(
608        "error: {} unexplained surviving mutant(s) — kill each with an assertion, or lift an \
609         equivalent/defensive one with a reason-required `[[<language>.exempt]] rules = [\"mutation\"]`:",
610        survivors.len()
611    );
612    for survivor in &survivors {
613        eprintln!(
614            "  {}:{}: {}",
615            survivor.file, survivor.line, survivor.description
616        );
617    }
618    Ok(1)
619}
620
621/// Run the `unit lint` check over `root` for `language` — the unit-suite
622/// isolation lints (`unmocked-collaborator`, `untyped-mock`, `no-out-of-module-call`,
623/// `no-out-of-module-import`) — printing each violation to stderr as
624/// `path:line: rule — message` and returning `1` when any are found, `0` otherwise.
625fn run_unit_lint(
626    root: &Path,
627    language: isolation::Language,
628    config_path: &Path,
629) -> anyhow::Result<i32> {
630    let (raw, select): (Vec<lint::Violation>, ExemptSelect) = match language {
631        isolation::Language::Rust => (isolation::find_violations(root)?, |c| c.rust_exemptions()),
632        isolation::Language::TypeScript => (ts::find_unit_violations(root)?, |c| {
633            c.exemptions(colocated_test::Language::TypeScript)
634        }),
635        isolation::Language::Python => (lint::find_unit_isolation_violations(root)?, |c| {
636            c.exemptions(colocated_test::Language::Python)
637        }),
638    };
639    let violations = apply_waivers(raw, root, config_path, select)?;
640    if violations.is_empty() {
641        return Ok(0);
642    }
643    for v in &violations {
644        eprintln!(
645            "{}:{}: {} — {}",
646            v.file.display(),
647            v.line,
648            v.rule,
649            v.message
650        );
651    }
652    eprintln!("error: {} isolation violation(s)", violations.len());
653    Ok(1)
654}
655
656/// Run the integration-test lints over `root` for `language`, printing each
657/// violation to stderr as `path:line: rule — message` and returning `1` when any
658/// are found, `0` otherwise.
659fn run_integration_lint(
660    root: &Path,
661    language: IntegrationLintLanguage,
662    config_path: &Path,
663) -> anyhow::Result<i32> {
664    let (raw, select): (Vec<lint::Violation>, ExemptSelect) = match language {
665        IntegrationLintLanguage::Python => (lint::find_violations(root)?, |c| {
666            c.exemptions(colocated_test::Language::Python)
667        }),
668        IntegrationLintLanguage::TypeScript => (ts::find_integration_violations(root)?, |c| {
669            c.exemptions(colocated_test::Language::TypeScript)
670        }),
671        IntegrationLintLanguage::Rust => (isolation::find_integration_violations(root)?, |c| {
672            c.rust_exemptions()
673        }),
674    };
675    let violations = apply_waivers(raw, root, config_path, select)?;
676    if violations.is_empty() {
677        return Ok(0);
678    }
679    for v in &violations {
680        eprintln!(
681            "{}:{}: {} — {}",
682            v.file.display(),
683            v.line,
684            v.rule,
685            v.message
686        );
687    }
688    eprintln!("error: {} lint violation(s)", violations.len());
689    Ok(1)
690}
691
692/// Selects a language's `[[<lang>.exempt]]` table from a loaded config — the one
693/// varying piece between the `unit lint` and `integration lint` waiver paths.
694type ExemptSelect = fn(&config::Config) -> &[config::Exemption];
695
696/// Drop the violations waived by the config's `exempt` list (#32/#102). A
697/// violation is waived when its `rule` is a known [`config::Rule`] and its
698/// `root`-relative path is exempt for that rule. `exemptions` selects the
699/// language's `[[<lang>.exempt]]` table from the loaded config. A missing config
700/// file waives nothing; a reason-less or stale entry errors (via `load_config` /
701/// `resolve_exempt`), so the escape hatch can't silently rot.
702fn apply_waivers(
703    violations: Vec<lint::Violation>,
704    root: &Path,
705    config_path: &Path,
706    exemptions: ExemptSelect,
707) -> anyhow::Result<Vec<lint::Violation>> {
708    use std::collections::hash_map::Entry;
709
710    if !config_path.exists() {
711        return Ok(violations);
712    }
713    let config = config::load_config(config_path)?;
714    let exempt = exemptions(&config);
715    // Resolve each rule's exempt set once (and surface a stale entry as an error).
716    let mut resolved: std::collections::HashMap<config::Rule, std::collections::BTreeSet<String>> =
717        std::collections::HashMap::new();
718    let mut kept = Vec::new();
719    for violation in violations {
720        let waived = match config::Rule::from_id(violation.rule) {
721            Some(rule) => {
722                let exempt_paths = match resolved.entry(rule) {
723                    Entry::Occupied(entry) => entry.into_mut(),
724                    Entry::Vacant(entry) => {
725                        entry.insert(config::resolve_exempt(root, exempt, rule)?)
726                    }
727                };
728                violation
729                    .file
730                    .strip_prefix(root)
731                    .ok()
732                    .map(|rel| rel.to_string_lossy().replace('\\', "/"))
733                    .is_some_and(|rel| exempt_paths.contains(&rel))
734            }
735            None => false,
736        };
737        if !waived {
738            kept.push(violation);
739        }
740    }
741    Ok(kept)
742}
743
744/// Run the packaging check: inspect the built artifact at `artifact` for test
745/// files that must not ship (README "Packaging"), per `language`'s test-file
746/// globs.
747///
748/// `artifact` is either an already-unpacked directory or a packed artifact the
749/// rule unpacks itself — a Python wheel (`.whl`) today; the TypeScript (#73) and
750/// Rust (#74) archives follow. Returns `0` when no test file is present, `1`
751/// otherwise (after printing each offending path, relative to the artifact root).
752fn run_packaging(artifact: &Path, language: colocated_test::Language) -> anyhow::Result<i32> {
753    let globs = match language {
754        colocated_test::Language::Python => vec!["*_test.py".to_string()],
755        colocated_test::Language::TypeScript => vec!["*.test.*".to_string()],
756        // `#[cfg(test)]` units compile out for free; the only thing to keep out of
757        // the `.crate` source tarball is the crate-root integration `tests/` dir.
758        colocated_test::Language::Rust => vec!["tests/".to_string()],
759    };
760    let offenders = packaging::inspect(artifact, &globs)?;
761    if offenders.is_empty() {
762        return Ok(0);
763    }
764    for offender in &offenders {
765        eprintln!("test file in built artifact: {}", offender.display());
766    }
767    eprintln!(
768        "error: {} test file(s) present in the built artifact \
769         (they must be excluded from packaging)",
770        offenders.len()
771    );
772    Ok(1)
773}
774
775/// Run the workflow guard over `path` (a workflow file or directory): flag every
776/// `testing-conventions` invocation that names a subcommand this binary no longer
777/// exposes, printing each as `path:line: rule — message` and returning `1` when any
778/// are found, `0` otherwise.
779fn run_workflow(path: &Path) -> anyhow::Result<i32> {
780    let violations = workflow::check(path, &command())?;
781    if violations.is_empty() {
782        return Ok(0);
783    }
784    for v in &violations {
785        eprintln!(
786            "{}:{}: {} — {}",
787            v.file.display(),
788            v.line,
789            v.rule,
790            v.message
791        );
792    }
793    eprintln!(
794        "error: {} workflow invocation(s) name a subcommand this binary no longer exposes",
795        violations.len()
796    );
797    Ok(1)
798}
799
800/// Run `command` as an e2e suite and write a committed attestation naming the
801/// current commit (#67). Force-runs: the attestation is written regardless of
802/// the command's exit code, so this exits `0` once the attestation is recorded.
803fn run_e2e_attest(command: &str) -> anyhow::Result<i32> {
804    let repo = std::env::current_dir()?;
805    let attestation = e2e::attest(&repo, command)?;
806    println!(
807        "e2e attestation recorded for commit {} (command exited {})",
808        attestation.commit, attestation.exit_code
809    );
810    Ok(0)
811}
812
813/// Verify the committed e2e attestation names the latest code commit (#68) — the
814/// CI side of the nudge. Exits `0` when fresh; otherwise prints the actionable
815/// hint and exits `1`. Never runs e2e, never judges the recorded run.
816fn run_e2e_verify() -> anyhow::Result<i32> {
817    let repo = std::env::current_dir()?;
818    match e2e::verify(&repo)? {
819        e2e::Verification::Fresh => Ok(0),
820        e2e::Verification::Missing => {
821            eprintln!(
822                "e2e attestation missing — run `testing-conventions e2e attest '<your e2e command>'`"
823            );
824            Ok(1)
825        }
826        e2e::Verification::Stale { attested, latest } => {
827            eprintln!(
828                "e2e attestation out of date: attested {}, latest code commit {} — \
829                 run `testing-conventions e2e attest '<your e2e command>'`",
830                &attested[..attested.len().min(7)],
831                &latest[..latest.len().min(7)]
832            );
833            Ok(1)
834        }
835    }
836}
837
838#[cfg(test)]
839mod tests {
840    use super::*;
841
842    #[test]
843    fn no_args_returns_ok_zero() {
844        assert_eq!(run(["testing-conventions"]).unwrap(), 0);
845    }
846
847    #[test]
848    fn check_returns_ok_zero() {
849        assert_eq!(run(["testing-conventions", "check"]).unwrap(), 0);
850    }
851
852    #[test]
853    fn unknown_flag_errors() {
854        assert!(run(["testing-conventions", "--bogus"]).is_err());
855    }
856
857    #[test]
858    fn help_flag_returns_clap_display_help() {
859        let err = run(["testing-conventions", "--help"]).expect_err("--help should bubble");
860        let clap_err = err
861            .downcast_ref::<clap::Error>()
862            .expect("error should be a clap::Error");
863        assert_eq!(clap_err.kind(), clap::error::ErrorKind::DisplayHelp);
864    }
865
866    #[test]
867    fn version_flag_returns_clap_display_version() {
868        let err = run(["testing-conventions", "--version"]).expect_err("--version should bubble");
869        let clap_err = err
870            .downcast_ref::<clap::Error>()
871            .expect("error should be a clap::Error");
872        assert_eq!(clap_err.kind(), clap::error::ErrorKind::DisplayVersion);
873    }
874}