Skip to main content

testing_conventions/
coverage.rs

1//! Coverage rule (Python — issue #26; TypeScript — issue #31; Rust — issue #37; exemptions — issue #32).
2//!
3//! Enforces the README's Coverage rule: a library's unit suite must meet the
4//! configured floor, with test files excluded from the denominator. This module
5//! is the deterministic core — given a parsed coverage report and the thresholds
6//! from config, an `evaluate` function decides pass/fail. Producing the report
7//! (shelling out to the language's coverage tool) is a thin layer on top, kept
8//! separate so the guarantee is testable without that toolchain installed.
9//!
10//! Python (#26) uses coverage.py: a single total, branch coverage on. Given a
11//! [`CoverageReport`] and [`Thresholds`], [`evaluate`] decides pass/fail, and
12//! [`measure`] shells out to `coverage`. TypeScript (#31) is the twin: vitest
13//! reports four independent metrics (lines / branches / functions / statements),
14//! so it carries its own [`TypeScriptThresholds`], [`VitestReport`], and
15//! [`evaluate_typescript`] / [`measure_typescript`] pair — sharing only the
16//! [`Outcome`] type. Its subprocess layer shells out to `vitest`. Rust (#37) is
17//! the third twin: `cargo llvm-cov` reports regions/lines (branch coverage is
18//! experimental), so it carries [`RustThresholds`], [`LlvmCovReport`], and
19//! [`evaluate_rust`] / [`measure_rust`]; its subprocess layer shells out to
20//! `cargo llvm-cov`.
21//!
22//! Files exempted from coverage in config (issue #32) are omitted from the
23//! denominator alongside the test files; the caller resolves them
24//! ([`crate::config::resolve_exempt`]) and passes their paths to [`measure`] /
25//! [`measure_typescript`] / [`measure_rust`].
26
27use std::collections::{BTreeMap, BTreeSet};
28use std::path::{Path, PathBuf};
29use std::process::Command;
30use std::sync::atomic::{AtomicU64, Ordering};
31
32use anyhow::{bail, Context, Result};
33use serde::Deserialize;
34
35/// Always omitted from the coverage denominator: colocated unit tests are the
36/// suite, never a subject of it.
37const TEST_OMIT: &str = "*_test.py";
38
39/// Also always omitted: `conftest.py` holds pytest fixtures (test support), never
40/// a coverage subject. `*conftest.py` matches it at any depth, mirroring the
41/// `*_test.py` glob. (#112)
42const SUPPORT_OMIT: &str = "*conftest.py";
43
44/// The coverage floor to enforce, from a `[<language>].coverage` table.
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub struct Thresholds {
47    /// Minimum total coverage percent the unit suite must meet.
48    pub fail_under: u8,
49    /// Whether branch coverage must be measured (and folded into the total).
50    pub branch: bool,
51}
52
53/// A coverage.py JSON report (`coverage json`), pared to what the checks need:
54/// the `totals` (the floor) and the per-file `files` block (patch
55/// coverage, #132). Unmodeled fields (metadata, per-function/class data) are
56/// ignored.
57#[derive(Debug, Clone, Deserialize)]
58pub struct CoverageReport {
59    pub totals: Totals,
60    /// Per-file line/branch detail, keyed by the path coverage.py reports
61    /// (relative to the measured root). Additive: `#[serde(default)]`, so a report
62    /// parsed for the floor alone (the inline tests) needs no `files`.
63    #[serde(default)]
64    pub files: BTreeMap<String, FileCoverage>,
65}
66
67/// Per-file coverage detail from a coverage.py report (one `files` entry) — what
68/// patch coverage (#132) reads to decide whether a changed line is covered.
69/// Unmodeled fields (the summary, per-function/class data) are ignored.
70#[derive(Debug, Clone, Default, Deserialize)]
71pub struct FileCoverage {
72    /// Executable lines the suite ran.
73    #[serde(default)]
74    pub executed_lines: Vec<u64>,
75    /// Executable lines the suite never ran — an uncovered changed line is one of
76    /// these.
77    #[serde(default)]
78    pub missing_lines: Vec<u64>,
79    /// Lines excluded from coverage (e.g. `# pragma: no cover`); never a miss.
80    #[serde(default)]
81    pub excluded_lines: Vec<u64>,
82    /// `[source_line, dest_line]` pairs for branches the suite never took; `dest`
83    /// may be negative (a function / loop exit). Only the source line matters to
84    /// patch coverage. Empty when branch coverage was off.
85    #[serde(default)]
86    pub missing_branches: Vec<Vec<i64>>,
87    /// `[source_line, dest_line]` pairs for branches the suite DID take (coverage.py
88    /// emits these alongside `missing_branches` under `--branch`). The diff-scoped
89    /// floor (#162) counts an arc toward changed-line branch coverage when its source
90    /// line is in the diff; with `missing_branches` it gives branch coverage over the
91    /// changed lines. Empty when branch coverage was off.
92    #[serde(default)]
93    pub executed_branches: Vec<Vec<i64>>,
94}
95
96/// The `totals` block of a coverage.py report.
97#[derive(Debug, Clone, Deserialize)]
98pub struct Totals {
99    /// Total covered percent — line coverage, plus branch when measured.
100    pub percent_covered: f64,
101    /// Branches measured; `0` when branch coverage was not enabled.
102    #[serde(default)]
103    pub num_branches: u64,
104}
105
106/// The result of checking a report against the thresholds.
107#[derive(Debug, Clone, PartialEq)]
108pub enum Outcome {
109    /// The floor is met.
110    Pass,
111    /// The floor is not met; the message explains why (actual vs. required).
112    Fail(String),
113}
114
115/// Parse a coverage.py JSON report (the output of `coverage json`).
116pub fn parse_report(json: &str) -> Result<CoverageReport> {
117    serde_json::from_str(json).context("parsing coverage.py JSON report")
118}
119
120/// Decide whether `report` meets `thresholds`.
121///
122/// Fails when total coverage is below `fail_under`, or when branch coverage was
123/// required but the report measured no branches (a misconfigured run).
124pub fn evaluate(report: &CoverageReport, thresholds: Thresholds) -> Outcome {
125    if thresholds.branch && report.totals.num_branches == 0 {
126        return Outcome::Fail(
127            "branch coverage is required but the report measured no branches".to_string(),
128        );
129    }
130    let actual = report.totals.percent_covered;
131    let required = f64::from(thresholds.fail_under);
132    // A hair of tolerance so a report that rounds to the floor (e.g. 99.999…%
133    // for a 100% target) isn't failed by float noise.
134    if actual + 1e-9 >= required {
135        Outcome::Pass
136    } else {
137        Outcome::Fail(format!(
138            "coverage {actual:.2}% is below the required {}%",
139            thresholds.fail_under
140        ))
141    }
142}
143
144/// Run the unit suite under coverage.py in `root` and check it against
145/// `thresholds`.
146///
147/// Shells out to `coverage run --branch` (omitting `*_test.py` and every path in
148/// `omit` from the denominator) then `coverage json`, and evaluates the report.
149/// `omit` holds the `coverage`-rule exemptions resolved from config, as
150/// `root`-relative paths. The `coverage` CLI — with `pytest` importable — must be
151/// on `PATH`.
152pub fn measure(root: &Path, thresholds: Thresholds, omit: &[String]) -> Result<Outcome> {
153    let report = run_coverage(root, omit, false)?;
154    Ok(evaluate(&report, thresholds))
155}
156
157/// Run the Python unit suite under coverage.py in `root` with **every** source
158/// under `root` measured (`coverage run --source=.`) and return the parsed report
159/// — so an untested source shows in the `files` block as wholly uncovered rather
160/// than vanishing. The per-file detail is what patch coverage (#132) reads; `omit`
161/// is as in [`measure`] (an exempt file stays out of the run, so its changed
162/// lines are lifted).
163pub fn measure_patch_report(root: &Path, omit: &[String]) -> Result<CoverageReport> {
164    run_coverage(root, omit, true)
165}
166
167/// A coverage.py data file under the temp dir — unique per call (so checks
168/// running in parallel don't collide) and removed on drop (so nothing leaks
169/// into the scanned tree).
170struct DataFile(PathBuf);
171
172impl DataFile {
173    fn new() -> Self {
174        static COUNTER: AtomicU64 = AtomicU64::new(0);
175        let name = format!(
176            "testing-conventions-{}-{}.coverage",
177            std::process::id(),
178            COUNTER.fetch_add(1, Ordering::Relaxed),
179        );
180        DataFile(std::env::temp_dir().join(name))
181    }
182}
183
184impl Drop for DataFile {
185    fn drop(&mut self) {
186        let _ = std::fs::remove_file(&self.0);
187    }
188}
189
190/// Run coverage.py over the unit suite in `root` and return the parsed report.
191///
192/// `include_all_sources` adds `--source=.` so coverage measures every source
193/// under `root` — even one no test imports, which then appears in the `files`
194/// block as wholly uncovered. The floor passes `false` (measuring only imported
195/// files, so its total is unchanged); patch coverage passes `true`.
196fn run_coverage(root: &Path, omit: &[String], include_all_sources: bool) -> Result<CoverageReport> {
197    let data = DataFile::new();
198    let omit = build_omit(omit);
199
200    // Branch coverage on; measure the sources in `root` with the test files —
201    // and any `coverage`-waived files — omitted from the denominator. Byte-code
202    // and the pytest cache are suppressed so the scanned tree stays pristine.
203    let mut command = Command::new("coverage");
204    command
205        .current_dir(root)
206        .args(["run", "--branch"])
207        .arg(format!("--omit={omit}"));
208    if include_all_sources {
209        command.arg("--source=.");
210    }
211    let run = command
212        .args(["-m", "pytest", "-q", "-p", "no:cacheprovider", "."])
213        .env("COVERAGE_FILE", &data.0)
214        .env("PYTHONDONTWRITEBYTECODE", "1")
215        .output()
216        .context("running `coverage run -m pytest` (is coverage.py installed?)")?;
217    if !run.status.success() {
218        bail!(
219            "the unit suite did not run cleanly under coverage in `{}`:\n{}{}",
220            root.display(),
221            String::from_utf8_lossy(&run.stdout),
222            String::from_utf8_lossy(&run.stderr),
223        );
224    }
225
226    // Emit the report to stdout and parse it.
227    let json = Command::new("coverage")
228        .current_dir(root)
229        .args(["json", "-o", "-"])
230        .env("COVERAGE_FILE", &data.0)
231        .output()
232        .context("running `coverage json`")?;
233    if !json.status.success() {
234        bail!(
235            "`coverage json` failed:\n{}",
236            String::from_utf8_lossy(&json.stderr),
237        );
238    }
239
240    parse_report(&String::from_utf8_lossy(&json.stdout))
241}
242
243/// The single comma-joined `--omit` value for the coverage run: always the test
244/// glob `*_test.py` and the support glob `*conftest.py`, plus every
245/// `coverage`-exempt path from config. (coverage.py takes one `--omit` — repeated
246/// flags don't accumulate, so the patterns must be joined.) An exempt file leaves
247/// the denominator with its reason recorded in config — an auditable omission, not
248/// a silent ignore-glob.
249fn build_omit(omit: &[String]) -> String {
250    [TEST_OMIT.to_string(), SUPPORT_OMIT.to_string()]
251        .into_iter()
252        .chain(omit.iter().cloned())
253        .collect::<Vec<_>>()
254        .join(",")
255}
256
257// ---------------------------------------------------------------------------
258// TypeScript (vitest) — issue #31.
259//
260// The TypeScript twin of the Python rule above. vitest reports four independent
261// metrics rather than Python's single total-plus-branch, so it carries its own
262// thresholds, report shape, and evaluate/measure pair; only `Outcome` is shared.
263// The split is the same: a pure `evaluate_typescript` over a parsed json-summary
264// report, and a thin `measure_typescript` that shells out to vitest to produce
265// one — so the enforcement core is testable without a Node toolchain.
266// ---------------------------------------------------------------------------
267
268/// What vitest measures: every TypeScript source under the scanned root. The
269/// braces are a vitest (picomatch) glob, expanded by vitest, not the shell.
270const TS_INCLUDE: &str = "**/*.{ts,tsx,mts,cts}";
271/// Always excluded from the denominator: the colocated unit tests are the suite,
272/// never a subject of it (`*.test.*`), and declaration files carry no runtime
273/// code (`*.d.ts` / `*.d.mts` / `*.d.cts`).
274const TS_TEST_EXCLUDE: &str = "**/*.test.*";
275const TS_DECL_EXCLUDE: &str = "**/*.d.{ts,mts,cts}";
276
277/// The four vitest coverage floors, from a `[typescript].coverage` table. Each
278/// is an independent percent the unit suite must meet — vitest measures all four.
279#[derive(Debug, Clone, Copy, PartialEq, Eq)]
280pub struct TypeScriptThresholds {
281    pub lines: u8,
282    pub branches: u8,
283    pub functions: u8,
284    pub statements: u8,
285}
286
287/// A vitest `coverage-summary.json` report, pared to the `total` block the check
288/// needs. Per-file entries and unmodeled fields are ignored.
289#[derive(Debug, Clone, Copy, Deserialize)]
290pub struct VitestReport {
291    pub total: VitestTotals,
292}
293
294/// The `total` block of a vitest json-summary report — the four metrics this
295/// rule enforces. vitest also emits `branchesTrue`, which the check ignores.
296#[derive(Debug, Clone, Copy, Deserialize)]
297pub struct VitestTotals {
298    pub lines: VitestMetric,
299    pub branches: VitestMetric,
300    pub functions: VitestMetric,
301    pub statements: VitestMetric,
302}
303
304/// One metric's totals from a vitest json-summary block, pared to what the check
305/// needs: the covered percent and the denominator size.
306#[derive(Debug, Clone, Copy, Deserialize)]
307pub struct VitestMetric {
308    /// Percent covered — `None` when nothing was measured, which vitest writes as
309    /// the string `"Unknown"` (and `total` is then `0`).
310    #[serde(deserialize_with = "deserialize_pct")]
311    pub pct: Option<f64>,
312    /// Size of the denominator (statements/branches/functions/lines counted).
313    pub total: u64,
314}
315
316/// Deserialize a json-summary `pct`: a number for a measured metric (vitest
317/// emits whole percents as JSON integers and fractional ones as floats), or the
318/// string `"Unknown"` (→ `None`) when the denominator is empty.
319fn deserialize_pct<'de, D>(deserializer: D) -> std::result::Result<Option<f64>, D::Error>
320where
321    D: serde::Deserializer<'de>,
322{
323    struct PctVisitor;
324    impl serde::de::Visitor<'_> for PctVisitor {
325        type Value = Option<f64>;
326
327        fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
328            f.write_str("a coverage percent number or the string \"Unknown\"")
329        }
330
331        fn visit_f64<E>(self, value: f64) -> std::result::Result<Self::Value, E> {
332            Ok(Some(value))
333        }
334
335        // serde_json hands a whole-number percent (e.g. `100`) to `visit_u64`;
336        // percents are never negative, so `visit_i64` is not needed.
337        fn visit_u64<E>(self, value: u64) -> std::result::Result<Self::Value, E> {
338            Ok(Some(value as f64))
339        }
340
341        // Any non-numeric percent (vitest writes the literal "Unknown") means the
342        // metric had nothing to measure.
343        fn visit_str<E>(self, _value: &str) -> std::result::Result<Self::Value, E> {
344            Ok(None)
345        }
346    }
347    deserializer.deserialize_any(PctVisitor)
348}
349
350/// Parse a vitest json-summary report (`coverage-summary.json`).
351pub fn parse_vitest_report(json: &str) -> Result<VitestReport> {
352    serde_json::from_str(json).context("parsing vitest coverage-summary JSON report")
353}
354
355/// Decide whether `report` meets every threshold in `thresholds`.
356///
357/// Fails when the run measured no code at all (an empty line denominator — a
358/// wrong path, or a suite that touched nothing — is never a silent pass),
359/// otherwise checks each of the four metrics and fails listing every one below
360/// its floor. A metric whose denominator is empty *amid* a non-empty run (e.g.
361/// branch-free code measured alongside real code) has nothing to miss and is
362/// vacuously satisfied.
363pub fn evaluate_typescript(report: &VitestReport, thresholds: TypeScriptThresholds) -> Outcome {
364    let total = &report.total;
365    // Vacuous-run guard: every source file has lines, so a zero line-denominator
366    // means nothing was measured — a misconfigured run (wrong path, or every file
367    // excluded), failed rather than passed on an empty measurement.
368    if total.lines.total == 0 {
369        return Outcome::Fail(
370            "the unit suite measured no code — check the path and that the suite runs".to_string(),
371        );
372    }
373    let checks = [
374        ("lines", total.lines, thresholds.lines),
375        ("branches", total.branches, thresholds.branches),
376        ("functions", total.functions, thresholds.functions),
377        ("statements", total.statements, thresholds.statements),
378    ];
379    let mut shortfalls = Vec::new();
380    for (name, metric, required) in checks {
381        // A metric with an empty denominator (e.g. branch-free code) has nothing
382        // to cover and is vacuously full; a measured one compares its percent.
383        let actual = metric.pct.unwrap_or(100.0);
384        // A hair of tolerance so a percent that rounds to the floor isn't failed
385        // by float noise (matches the Python path).
386        if actual + 1e-9 < f64::from(required) {
387            shortfalls.push(format!("{name} {actual:.2}% < {required}%"));
388        }
389    }
390    if shortfalls.is_empty() {
391        Outcome::Pass
392    } else {
393        Outcome::Fail(format!(
394            "coverage below thresholds: {}",
395            shortfalls.join(", ")
396        ))
397    }
398}
399
400/// Run the unit suite under vitest coverage in `root` and check it against
401/// `thresholds`.
402///
403/// Shells out to `npx vitest run` with v8 coverage and the json-summary reporter,
404/// excluding `*.test.*`, declaration files, and every path in `exclude` from the
405/// denominator, then evaluates the report. `exclude` holds the `coverage`-rule
406/// exemptions resolved from config, as `root`-relative paths. `npx` resolves the
407/// project-local `vitest`, so it and `@vitest/coverage-v8` must be installed
408/// under `root`.
409pub fn measure_typescript(
410    root: &Path,
411    thresholds: TypeScriptThresholds,
412    exclude: &[String],
413) -> Result<Outcome> {
414    let report = run_vitest(root, exclude)?;
415    Ok(evaluate_typescript(&report, thresholds))
416}
417
418/// A vitest reports directory under the temp dir — unique per call (so checks
419/// running in parallel don't collide) and removed on drop (so the report never
420/// leaks into the scanned tree). vitest writes `coverage-summary.json` here.
421struct ReportDir(PathBuf);
422
423impl ReportDir {
424    fn new() -> Self {
425        static COUNTER: AtomicU64 = AtomicU64::new(0);
426        let name = format!(
427            "testing-conventions-vitest-{}-{}",
428            std::process::id(),
429            COUNTER.fetch_add(1, Ordering::Relaxed),
430        );
431        ReportDir(std::env::temp_dir().join(name))
432    }
433}
434
435impl Drop for ReportDir {
436    fn drop(&mut self) {
437        let _ = std::fs::remove_dir_all(&self.0);
438    }
439}
440
441/// Run vitest over the unit suite in `root` and return the parsed floor report.
442fn run_vitest(root: &Path, exclude: &[String]) -> Result<VitestReport> {
443    let json = run_vitest_coverage(root, exclude, "json-summary", "coverage-summary.json")?;
444    parse_vitest_report(&json)
445}
446
447/// Run vitest coverage over the unit suite in `root` and return the raw contents
448/// of the `report_file` the `reporter` wrote. Shared by the floor (#31, the
449/// `json-summary` → `coverage-summary.json` pair) and patch coverage (#135, the
450/// detailed `json` → `coverage-final.json` Istanbul pair) — the two differ only in
451/// the reporter and how they parse it.
452///
453/// v8 coverage is written to an out-of-tree temp dir so the scanned tree stays
454/// pristine. `include` scopes measurement to the sources under `root`; the test
455/// glob, declaration files, and the config `exclude` paths are excluded from the
456/// denominator. `all=true` counts source files the suite never imported, so an
457/// untested file is measured (lowering the floor / showing as uncovered) rather
458/// than vanishing. `--no-cache` keeps vitest from writing a cache into the tree.
459fn run_vitest_coverage(
460    root: &Path,
461    exclude: &[String],
462    reporter: &str,
463    report_file: &str,
464) -> Result<String> {
465    let reports = ReportDir::new();
466
467    let mut command = Command::new("npx");
468    command
469        .current_dir(root)
470        .args(["--yes", "vitest", "run", "--no-cache"])
471        .args(["--coverage.enabled", "--coverage.provider=v8"])
472        .arg(format!("--coverage.reporter={reporter}"))
473        .arg("--coverage.all=true")
474        .arg(format!(
475            "--coverage.reportsDirectory={}",
476            reports.0.display()
477        ))
478        .arg(format!("--coverage.include={TS_INCLUDE}"))
479        .arg(format!("--coverage.exclude={TS_TEST_EXCLUDE}"))
480        .arg(format!("--coverage.exclude={TS_DECL_EXCLUDE}"));
481    for path in exclude {
482        command.arg(format!("--coverage.exclude={path}"));
483    }
484    // CI=1 keeps vitest non-interactive (no watch prompt, plain output).
485    let run = command.env("CI", "1").output().context(
486        "running `npx vitest run --coverage` (are vitest and @vitest/coverage-v8 installed?)",
487    )?;
488    if !run.status.success() {
489        bail!(
490            "the unit suite did not run cleanly under vitest in `{}`:\n{}{}",
491            root.display(),
492            String::from_utf8_lossy(&run.stdout),
493            String::from_utf8_lossy(&run.stderr),
494        );
495    }
496
497    let path = reports.0.join(report_file);
498    std::fs::read_to_string(&path).with_context(|| {
499        format!(
500            "reading vitest coverage report `{}` (did the run produce a {reporter} report?)",
501            path.display()
502        )
503    })
504}
505
506// ---------------------------------------------------------------------------
507// TypeScript diff-scoped coverage detail — issues #135, #162.
508//
509// What the diff-scoped floor (`crate::patch_coverage::measure_typescript`) reads:
510// per-file coverage detail for the four vitest metrics. vitest's `json-summary`
511// gives only per-file totals, so this measures with the detailed `json` (Istanbul
512// `coverage-final.json`) reporter and reduces each file to the per-statement /
513// per-branch-arm / per-function `(line, covered)` counts the floor's ratio needs.
514// ---------------------------------------------------------------------------
515
516/// One file's entry in a vitest v8 `coverage-final.json` (Istanbul) report, pared
517/// to what patch coverage reads: the statement / branch / function maps and their
518/// hit counts. Unmodeled fields (`path`, per-node metadata) are ignored.
519#[derive(Debug, Clone, Deserialize)]
520struct IstanbulFile {
521    /// Statement id → source span. A statement whose hit count in `s` is `0` was
522    /// never executed, so its lines are uncovered.
523    #[serde(rename = "statementMap", default)]
524    statement_map: BTreeMap<String, IstanbulSpan>,
525    /// Statement id → execution count.
526    #[serde(default)]
527    s: BTreeMap<String, u64>,
528    /// Branch id → branch location. A branch with a `0` among its `b` counts had a
529    /// path the suite never took, so its source line is uncovered.
530    #[serde(rename = "branchMap", default)]
531    branch_map: BTreeMap<String, IstanbulBranch>,
532    /// Branch id → per-arm execution counts (one count per branch arm).
533    #[serde(default)]
534    b: BTreeMap<String, Vec<u64>>,
535    /// Function id → declaration location. A function whose hit count in `f` is `0`
536    /// was never called. The diff-scoped floor (#162) reads this via
537    /// [`istanbul_patch_detail`].
538    #[serde(rename = "fnMap", default)]
539    fn_map: BTreeMap<String, IstanbulFn>,
540    /// Function id → execution count.
541    #[serde(default)]
542    f: BTreeMap<String, u64>,
543}
544
545/// A source span — only the 1-based line numbers matter to patch coverage.
546#[derive(Debug, Clone, Deserialize)]
547struct IstanbulSpan {
548    start: IstanbulPos,
549    end: IstanbulPos,
550}
551
552/// A position in a source span; the `column` is ignored.
553#[derive(Debug, Clone, Deserialize)]
554struct IstanbulPos {
555    line: u64,
556}
557
558/// A branch entry — only its location (whose start line is the branch's source
559/// line) matters; the `type` and per-path `locations` are ignored.
560#[derive(Debug, Clone, Deserialize)]
561struct IstanbulBranch {
562    loc: IstanbulSpan,
563}
564
565/// A function entry — only its declaration's start line (the function's source
566/// line) matters; the `name`, `loc`, and top-level `line` are ignored. vitest's
567/// v8 export shapes this as `{"name":.., "decl":{"start":{"line":N,..},..}, ..}`.
568#[derive(Debug, Clone, Deserialize)]
569struct IstanbulFn {
570    decl: IstanbulSpan,
571}
572
573/// Per-file coverage detail from a vitest v8 `coverage-final.json` (Istanbul)
574/// report — the counts the diff-scoped floor (#162) needs. Each entry carries the
575/// Istanbul maps reduced to `(line, …, covered)` tuples, so the pure
576/// [`crate::patch_coverage::evaluate_patch_typescript`] can restrict each of the
577/// four metrics to the changed lines.
578#[derive(Debug, Clone, Default)]
579pub struct TsPatchCoverage {
580    /// One per `statementMap` entry: `(start_line, end_line, covered)` — `covered`
581    /// is `s[id] > 0`. A statement counts toward the diff when any line it spans is
582    /// a changed line.
583    pub statements: Vec<(u64, u64, bool)>,
584    /// One per branch **arm**: `(source_line, covered)` — `source_line` is the
585    /// branch's `loc.start.line` (shared by every arm) and `covered` is that arm's
586    /// count `> 0`. An arm counts toward the diff when its source line is changed.
587    pub branch_arms: Vec<(u64, bool)>,
588    /// One per `fnMap` entry: `(decl_line, covered)` — `decl_line` is `decl.start.line`
589    /// and `covered` is `f[id] > 0`. A function counts toward the diff when its
590    /// declaration line is changed.
591    pub functions: Vec<(u64, bool)>,
592}
593
594/// Run the TypeScript unit suite under vitest in `root` and return the per-file
595/// coverage detail for the four metrics — keyed by the absolute path vitest
596/// reports, the caller re-keying to `root`-relative to match the diff. Reads the
597/// Istanbul report for the diff-scoped floor (#162): the per-statement /
598/// per-branch-arm / per-function `(line, covered)` detail the floor's ratio needs.
599/// `exclude` is the `coverage`-rule exemptions,
600/// dropped from the run so an exempt file's changed lines are lifted. `npx`
601/// resolves the project-local `vitest`, so it and `@vitest/coverage-v8` must be
602/// installed under `root`.
603pub fn measure_patch_typescript_detail(
604    root: &Path,
605    exclude: &[String],
606) -> Result<BTreeMap<String, TsPatchCoverage>> {
607    let json = run_vitest_coverage(root, exclude, "json", "coverage-final.json")?;
608    istanbul_patch_detail(&json)
609}
610
611/// Pure: per-file [`TsPatchCoverage`] from a vitest v8 `coverage-final.json`
612/// (Istanbul) report. Keyed by the path vitest reports (absolute). A file present
613/// but with no statements/branches/functions maps to an empty `TsPatchCoverage`.
614fn istanbul_patch_detail(json: &str) -> Result<BTreeMap<String, TsPatchCoverage>> {
615    let files: BTreeMap<String, IstanbulFile> = serde_json::from_str(json)
616        .context("parsing vitest coverage-final (Istanbul) JSON report")?;
617    let mut out = BTreeMap::new();
618    for (path, file) in files {
619        let mut detail = TsPatchCoverage::default();
620        // Each statement → (start, end, covered): covered when its count is > 0.
621        for (id, span) in &file.statement_map {
622            let covered = file.s.get(id).is_some_and(|&count| count > 0);
623            detail
624                .statements
625                .push((span.start.line, span.end.line, covered));
626        }
627        // Each branch arm → (source_line, covered): the branch's location start line
628        // (shared by every arm) with that arm's count > 0. v8 may model a branch as
629        // a single arm (a `[count]` array) or several (`[arm0, arm1, …]`); one tuple
630        // per arm either way.
631        for (id, branch) in &file.branch_map {
632            let line = branch.loc.start.line;
633            if let Some(counts) = file.b.get(id) {
634                for &count in counts {
635                    detail.branch_arms.push((line, count > 0));
636                }
637            }
638        }
639        // Each function → (decl_line, covered): the declaration's start line with
640        // its call count > 0.
641        for (id, function) in &file.fn_map {
642            let covered = file.f.get(id).is_some_and(|&count| count > 0);
643            detail.functions.push((function.decl.start.line, covered));
644        }
645        out.insert(path, detail);
646    }
647    Ok(out)
648}
649
650// ---------------------------------------------------------------------------
651// Rust (cargo llvm-cov) — issue #37.
652//
653// The Rust twin of the rules above. `cargo llvm-cov` reports LLVM source-based
654// coverage as regions + lines (branch coverage is still experimental), so the
655// Rust rule carries its own thresholds and `measure_rust` entry point; only the
656// `Outcome` type is shared. Mirroring the Python/TypeScript split, a pure
657// `evaluate_rust` over a parsed llvm-cov export and the thin subprocess layer
658// that produces one land with the implementation (#37).
659// ---------------------------------------------------------------------------
660
661/// The two `cargo llvm-cov` coverage floors, from a `[rust].coverage` table.
662/// Branch coverage is still experimental, so only regions and lines are enforced.
663#[derive(Debug, Clone, Copy, PartialEq, Eq)]
664pub struct RustThresholds {
665    pub regions: u8,
666    pub lines: u8,
667}
668
669/// A `cargo llvm-cov --json` export (LLVM's `llvm.coverage.json.export`), pared to
670/// the totals the check needs. A single run produces one `data` entry; unmodeled
671/// fields (per-file/per-function detail, `type`, `version`) are ignored.
672#[derive(Debug, Clone, Deserialize)]
673pub struct LlvmCovReport {
674    pub data: Vec<LlvmCovData>,
675}
676
677/// One export entry — only its `totals` are needed (`--summary-only` omits the
678/// per-file and per-function detail).
679#[derive(Debug, Clone, Copy, Deserialize)]
680pub struct LlvmCovData {
681    pub totals: LlvmCovTotals,
682}
683
684/// The `totals` block of an llvm-cov export — the two metrics this rule enforces.
685/// llvm-cov also reports `functions`, `instantiations`, and (experimental)
686/// `branches`, which the check ignores.
687#[derive(Debug, Clone, Copy, Deserialize)]
688pub struct LlvmCovTotals {
689    pub regions: LlvmCovMetric,
690    pub lines: LlvmCovMetric,
691}
692
693/// One metric's totals from an llvm-cov export, pared to what the check needs: the
694/// denominator size and the covered percent.
695#[derive(Debug, Clone, Copy, Deserialize)]
696pub struct LlvmCovMetric {
697    /// Size of the denominator (regions or lines counted).
698    pub count: u64,
699    /// How many were covered.
700    pub covered: u64,
701    /// Covered percent — llvm-cov computes `100 * covered / count`.
702    pub percent: f64,
703}
704
705/// Parse a `cargo llvm-cov --json` export.
706pub fn parse_llvm_cov_report(json: &str) -> Result<LlvmCovReport> {
707    serde_json::from_str(json).context("parsing cargo llvm-cov JSON report")
708}
709
710/// Decide whether `report` meets both thresholds.
711///
712/// Fails when the run measured no regions at all (an empty denominator — a wrong
713/// path, or a crate that compiled nothing — is never a silent pass), otherwise
714/// checks regions and lines and fails listing each below its floor.
715pub fn evaluate_rust(report: &LlvmCovReport, thresholds: RustThresholds) -> Outcome {
716    let Some(totals) = report.data.first().map(|entry| &entry.totals) else {
717        return Outcome::Fail("the cargo llvm-cov report contained no data".to_string());
718    };
719    // Vacuous-run guard: every compiled crate has regions, so a zero region
720    // denominator means nothing was measured — failed rather than passed on an
721    // empty measurement (mirrors the TypeScript path).
722    if totals.regions.count == 0 {
723        return Outcome::Fail(
724            "the unit suite measured no code — check the path and that the suite runs".to_string(),
725        );
726    }
727    let checks = [
728        ("regions", totals.regions.percent, thresholds.regions),
729        ("lines", totals.lines.percent, thresholds.lines),
730    ];
731    let mut shortfalls = Vec::new();
732    for (name, actual, required) in checks {
733        // A hair of tolerance so a percent that rounds to the floor isn't failed by
734        // float noise (matches the Python / TypeScript paths).
735        if actual + 1e-9 < f64::from(required) {
736            shortfalls.push(format!("{name} {actual:.2}% < {required}%"));
737        }
738    }
739    if shortfalls.is_empty() {
740        Outcome::Pass
741    } else {
742        Outcome::Fail(format!(
743            "coverage below thresholds: {}",
744            shortfalls.join(", ")
745        ))
746    }
747}
748
749/// Run the unit suite under `cargo llvm-cov` in `root` and check it against
750/// `thresholds`.
751///
752/// Shells out to `cargo llvm-cov --json --summary-only`, omitting every path in
753/// `ignore` from the denominator (a single `--ignore-filename-regex`), then
754/// evaluates the export. `ignore` holds the `coverage`-rule exemptions resolved
755/// from config, as `root`-relative paths. `cargo-llvm-cov` must be installed.
756pub fn measure_rust(root: &Path, thresholds: RustThresholds, ignore: &[String]) -> Result<Outcome> {
757    let report = run_llvm_cov(root, ignore)?;
758    Ok(evaluate_rust(&report, thresholds))
759}
760
761/// A `cargo llvm-cov` target directory under the temp dir — unique per call (so
762/// checks running in parallel don't collide) and removed on drop (so the build
763/// never leaks into the scanned tree). Passed to the run as `CARGO_TARGET_DIR`.
764struct TargetDir(PathBuf);
765
766impl TargetDir {
767    fn new() -> Self {
768        static COUNTER: AtomicU64 = AtomicU64::new(0);
769        let name = format!(
770            "testing-conventions-llvm-cov-{}-{}",
771            std::process::id(),
772            COUNTER.fetch_add(1, Ordering::Relaxed),
773        );
774        TargetDir(std::env::temp_dir().join(name))
775    }
776}
777
778impl Drop for TargetDir {
779    fn drop(&mut self) {
780        let _ = std::fs::remove_dir_all(&self.0);
781    }
782}
783
784/// Run cargo llvm-cov over the unit suite in `root` and return the parsed
785/// `--summary-only` export — the totals the floor checks.
786fn run_llvm_cov(root: &Path, ignore: &[String]) -> Result<LlvmCovReport> {
787    parse_llvm_cov_report(&run_cargo_llvm_cov(
788        root,
789        ignore,
790        &["--json", "--summary-only"],
791    )?)
792}
793
794/// Run `cargo llvm-cov` over the unit suite in `root` with the given coverage
795/// `format` args (`["--json", "--summary-only"]` for the whole-tree floor's totals,
796/// `["--json"]` for the diff-scoped floor's per-region detail) and return its
797/// stdout. Shared by the whole-tree floor (#37) and the diff-scoped floor (#162).
798///
799/// The build goes to an out-of-tree target dir (via `CARGO_TARGET_DIR`) so the
800/// scanned crate stays pristine; the `coverage`-rule exemptions become one
801/// `--ignore-filename-regex`; and the outer run's instrumentation env is stripped
802/// for nested-run hygiene (the loop below explains why).
803fn run_cargo_llvm_cov(root: &Path, ignore: &[String], format: &[&str]) -> Result<String> {
804    let target = TargetDir::new();
805
806    let mut command = Command::new("cargo");
807    command
808        .current_dir(root)
809        .arg("llvm-cov")
810        .args(format)
811        .env("CARGO_TARGET_DIR", &target.0);
812    if let Some(regex) = ignore_filename_regex(ignore) {
813        command.arg("--ignore-filename-regex").arg(regex);
814    }
815    // Nested-run hygiene: when this check itself runs under `cargo llvm-cov` (the
816    // package's own coverage job), the outer run exports its instrumentation state
817    // into our environment — the coverage flags and profile path, and (because
818    // cargo-llvm-cov drives instrumentation through a rustc wrapper) a
819    // `RUSTC_WRAPPER` pointing back at `cargo-llvm-cov`. Inherited, that wrapper
820    // makes the inner run re-enter cargo-llvm-cov on every rustc invocation and
821    // never finish — it hangs compiling the scanned crate until the runner is
822    // OOM-killed. Strip the lot so the inner run instruments from a clean slate.
823    for var in [
824        "RUSTFLAGS",
825        "CARGO_ENCODED_RUSTFLAGS",
826        "RUSTDOCFLAGS",
827        "CARGO_ENCODED_RUSTDOCFLAGS",
828        "LLVM_PROFILE_FILE",
829        "CARGO_LLVM_COV",
830        "CARGO_LLVM_COV_SHOW_ENV",
831        "CARGO_LLVM_COV_TARGET_DIR",
832        "CARGO_LLVM_COV_BUILD_DIR",
833        "RUSTC_WRAPPER",
834        "RUSTC_WORKSPACE_WRAPPER",
835        "__CARGO_LLVM_COV_RUSTC_WRAPPER",
836        "__CARGO_LLVM_COV_RUSTC_WRAPPER_RUSTFLAGS",
837        "__CARGO_LLVM_COV_RUSTC_WRAPPER_CRATE_NAMES",
838    ] {
839        command.env_remove(var);
840    }
841    let output = command
842        .output()
843        .context("running `cargo llvm-cov` (is cargo-llvm-cov installed?)")?;
844    if !output.status.success() {
845        bail!(
846            "the unit suite did not run cleanly under cargo llvm-cov in `{}`:\n{}{}",
847            root.display(),
848            String::from_utf8_lossy(&output.stdout),
849            String::from_utf8_lossy(&output.stderr),
850        );
851    }
852    Ok(String::from_utf8_lossy(&output.stdout).into_owned())
853}
854
855/// Per-file region detail from a `cargo llvm-cov --json` export — the per-region
856/// counts the diff-scoped floor (#162) needs. Each entry carries one
857/// `(start_line, end_line, covered)` tuple per code region, so the pure
858/// [`crate::patch_coverage::evaluate_patch_rust`] can restrict both the regions and
859/// lines metrics to the changed lines.
860#[derive(Debug, Clone, Default)]
861pub struct RustPatchCoverage {
862    /// One per code region (a `kind == 0` region of the LLVM export):
863    /// `(start_line, end_line, covered)` — `covered` is the region's
864    /// `executionCount > 0`. A region counts toward the diff when any line it spans
865    /// is a changed line.
866    pub regions: Vec<(u64, u64, bool)>,
867}
868
869/// A full `cargo llvm-cov --json` export (LLVM's `llvm.coverage.json.export`),
870/// modeling the per-function region detail the diff-scoped floor needs — separate
871/// from [`LlvmCovReport`], which keeps only the `totals` the whole-tree floor
872/// reads. A single run produces one `data` entry; unmodeled fields (`totals`,
873/// `type`, `version`) are ignored.
874#[derive(Debug, Clone, Deserialize)]
875struct LlvmCovExport {
876    data: Vec<LlvmCovExportData>,
877}
878
879/// One export entry — its per-function `functions` block carries the regions (the
880/// `--summary-only` runs that feed [`LlvmCovReport`] omit it), and its `files` block
881/// names the measured files. `--ignore-filename-regex` drops an exempt file from
882/// `files` but *not* from `functions` (the regions array is unfiltered), so the
883/// `files` list is the allowlist [`llvm_cov_patch_detail`] restricts the regions to.
884#[derive(Debug, Clone, Deserialize)]
885struct LlvmCovExportData {
886    files: Vec<LlvmCovExportFile>,
887    functions: Vec<LlvmCovFunction>,
888}
889
890/// One measured file in the export's `files` block — only its `filename` (the
891/// absolute path) is needed, to build the not-ignored allowlist. The per-file
892/// `segments` / `summary` detail is ignored (the regions come from `functions`).
893#[derive(Debug, Clone, Deserialize)]
894struct LlvmCovExportFile {
895    filename: String,
896}
897
898/// One function's coverage in the export: the source files it spans (`filenames`,
899/// indexed by a region's `fileID`) and its regions. Each region is a flat array
900/// `[lineStart, colStart, lineEnd, colEnd, executionCount, fileID, expandedFileID,
901/// kind]`; the fields are read positionally in [`llvm_cov_patch_detail`].
902#[derive(Debug, Clone, Deserialize)]
903struct LlvmCovFunction {
904    filenames: Vec<String>,
905    regions: Vec<Vec<i64>>,
906}
907
908/// Run the Rust unit suite under `cargo llvm-cov` in `root` and return the per-file
909/// region detail — keyed by the absolute path llvm-cov reports, the caller re-keying
910/// to `root`-relative to match the diff. Reads the full `--json` export for the
911/// diff-scoped floor (#162): the per-region `(line, covered)` detail the floor's
912/// regions metric needs. `ignore` is the `coverage`-rule exemptions, dropped
913/// from the run so an exempt file's changed lines are lifted. `cargo-llvm-cov` must
914/// be installed.
915pub fn measure_patch_rust_detail(
916    root: &Path,
917    ignore: &[String],
918) -> Result<BTreeMap<String, RustPatchCoverage>> {
919    llvm_cov_patch_detail(&run_cargo_llvm_cov(root, ignore, &["--json"])?)
920}
921
922/// Pure: per-file [`RustPatchCoverage`] from a `cargo llvm-cov --json` export.
923/// Keyed by the path llvm-cov reports (absolute). Walks every function's regions;
924/// for each region:
925///   - **skips** any region whose file is not in the export's `files` allowlist —
926///     `--ignore-filename-regex` drops an exempt file from `files` (and the totals)
927///     but leaves it in the unfiltered `functions` regions, so honoring the
928///     exemption means intersecting with `files`. A run with nothing exempt lists
929///     every measured file, so this is a no-op there.
930///   - **skips** any region whose `kind` (index 7) is not `0` — only `kind == 0`
931///     code regions count toward coverage (gap / expansion / skipped / branch
932///     regions carry no line-coverage signal). The kept count (with nothing
933///     ignored) matches the `totals.regions.count` a `--summary-only` run reports.
934///   - reads `start_line = region[0]`, `end_line = region[2]`,
935///     `covered = region[4] > 0`, and the file `filenames[region[5]]` (the
936///     `fileID`), pushing `(start_line, end_line, covered)` under that file.
937///
938/// A region array with fewer than 8 elements (malformed — never seen from
939/// llvm-cov) is skipped rather than panicking on an index, as is one whose `fileID`
940/// is out of range for its `filenames`.
941fn llvm_cov_patch_detail(json: &str) -> Result<BTreeMap<String, RustPatchCoverage>> {
942    let export: LlvmCovExport =
943        serde_json::from_str(json).context("parsing cargo llvm-cov JSON export")?;
944    let mut out: BTreeMap<String, RustPatchCoverage> = BTreeMap::new();
945    for data in &export.data {
946        // The `files` block honors `--ignore-filename-regex`; the `functions` regions
947        // do not, so restrict to the measured (not-ignored) files.
948        let measured: BTreeSet<&str> = data.files.iter().map(|f| f.filename.as_str()).collect();
949        for function in &data.functions {
950            for region in &function.regions {
951                // A code region carries eight fields; anything shorter is malformed
952                // (never emitted by llvm-cov) and skipped rather than indexed.
953                if region.len() < 8 {
954                    continue;
955                }
956                // Only `kind == 0` (a code region) contributes to line coverage;
957                // gap (1) / expansion (2) / skipped / branch regions are ignored.
958                if region[7] != 0 {
959                    continue;
960                }
961                let file_id = region[5];
962                let Ok(file_id) = usize::try_from(file_id) else {
963                    continue;
964                };
965                let Some(file) = function.filenames.get(file_id) else {
966                    continue;
967                };
968                // Skip a file the run ignored (absent from `files`) so a `coverage`
969                // exemption drops its regions, lifting its changed lines.
970                if !measured.contains(file.as_str()) {
971                    continue;
972                }
973                let start = region[0].max(0) as u64;
974                let end = region[2].max(0) as u64;
975                let covered = region[4] > 0;
976                out.entry(file.clone())
977                    .or_default()
978                    .regions
979                    .push((start, end, covered));
980            }
981        }
982    }
983    Ok(out)
984}
985
986/// The single `--ignore-filename-regex` value for the run, or `None` when nothing
987/// is exempt. `cargo llvm-cov` takes one regex, so the `coverage`-exempt paths are
988/// each regex-escaped (matched literally, not as a pattern) and joined with `|`. An
989/// exempt file leaves the denominator with its reason recorded in config — an
990/// auditable omission, not a silent ignore-glob.
991fn ignore_filename_regex(ignore: &[String]) -> Option<String> {
992    if ignore.is_empty() {
993        return None;
994    }
995    Some(
996        ignore
997            .iter()
998            .map(|path| regex_escape(path))
999            .collect::<Vec<_>>()
1000            .join("|"),
1001    )
1002}
1003
1004/// Escape the regex metacharacters in `s` so it matches literally — an exempt path
1005/// carries `.` (and may carry other metacharacters) that must not read as regex.
1006fn regex_escape(s: &str) -> String {
1007    const META: &str = r"\.+*?()|[]{}^$";
1008    let mut out = String::with_capacity(s.len());
1009    for c in s.chars() {
1010        if META.contains(c) {
1011            out.push('\\');
1012        }
1013        out.push(c);
1014    }
1015    out
1016}
1017
1018#[cfg(test)]
1019mod tests {
1020    use super::*;
1021
1022    fn report(percent_covered: f64, num_branches: u64) -> CoverageReport {
1023        CoverageReport {
1024            totals: Totals {
1025                percent_covered,
1026                num_branches,
1027            },
1028            files: BTreeMap::new(),
1029        }
1030    }
1031
1032    #[test]
1033    fn passes_when_total_meets_the_floor() {
1034        assert_eq!(
1035            evaluate(
1036                &report(100.0, 12),
1037                Thresholds {
1038                    fail_under: 100,
1039                    branch: true
1040                }
1041            ),
1042            Outcome::Pass
1043        );
1044    }
1045
1046    #[test]
1047    fn fails_when_total_is_below_the_floor() {
1048        assert!(matches!(
1049            evaluate(
1050                &report(80.0, 12),
1051                Thresholds {
1052                    fail_under: 100,
1053                    branch: true
1054                }
1055            ),
1056            Outcome::Fail(_)
1057        ));
1058    }
1059
1060    #[test]
1061    fn fails_when_branch_required_but_unmeasured() {
1062        // branch=true but the report measured no branches → a misconfigured run.
1063        assert!(matches!(
1064            evaluate(
1065                &report(100.0, 0),
1066                Thresholds {
1067                    fail_under: 90,
1068                    branch: true
1069                }
1070            ),
1071            Outcome::Fail(_)
1072        ));
1073    }
1074
1075    #[test]
1076    fn parses_a_coverage_py_report() {
1077        let json = r#"{"totals":{"percent_covered":91.5,"num_branches":8,"covered_lines":91}}"#;
1078        let report = parse_report(json).expect("valid coverage.py json");
1079        assert_eq!(report.totals.percent_covered, 91.5);
1080        assert_eq!(report.totals.num_branches, 8);
1081    }
1082
1083    #[test]
1084    fn parses_the_per_file_block_for_patch_coverage() {
1085        // A realistic `coverage json` shape: a `files` map carrying the per-file
1086        // missing lines and `[src, dst]` branch pairs patch coverage (#132) reads.
1087        let json = r#"{
1088            "files": {
1089                "widget.py": {
1090                    "executed_lines": [1, 2, 3, 4, 6],
1091                    "summary": {"percent_covered": 85.0},
1092                    "missing_lines": [5],
1093                    "excluded_lines": [],
1094                    "missing_branches": [[4, 5]]
1095                }
1096            },
1097            "totals": {"percent_covered": 85.0, "num_branches": 4}
1098        }"#;
1099        let report = parse_report(json).expect("valid coverage.py json with files");
1100        let widget = report.files.get("widget.py").expect("widget.py is present");
1101        assert_eq!(widget.missing_lines, vec![5]);
1102        assert_eq!(widget.missing_branches, vec![vec![4, 5]]);
1103        // The floor still reads totals from the same report.
1104        assert_eq!(report.totals.percent_covered, 85.0);
1105    }
1106
1107    #[test]
1108    fn a_report_without_a_files_block_parses_with_an_empty_map() {
1109        // The floor path parses totals only; `files` defaults to empty.
1110        let report = parse_report(r#"{"totals":{"percent_covered":100.0,"num_branches":2}}"#)
1111            .expect("valid coverage.py json");
1112        assert!(report.files.is_empty());
1113    }
1114
1115    #[test]
1116    fn omit_is_the_test_and_support_globs_when_nothing_is_exempt() {
1117        assert_eq!(build_omit(&[]), "*_test.py,*conftest.py");
1118    }
1119
1120    #[test]
1121    fn omit_folds_in_the_exempt_paths_after_the_test_glob() {
1122        // The caller passes already-resolved, sorted, `root`-relative paths.
1123        let exempt = vec!["pkg/gen.py".to_string(), "shim.py".to_string()];
1124        assert_eq!(
1125            build_omit(&exempt),
1126            "*_test.py,*conftest.py,pkg/gen.py,shim.py"
1127        );
1128    }
1129
1130    // --- TypeScript (vitest) — issue #31 ---
1131
1132    fn metric(pct: f64) -> VitestMetric {
1133        VitestMetric {
1134            pct: Some(pct),
1135            total: 10,
1136        }
1137    }
1138
1139    fn ts_report(lines: f64, branches: f64, functions: f64, statements: f64) -> VitestReport {
1140        VitestReport {
1141            total: VitestTotals {
1142                lines: metric(lines),
1143                branches: metric(branches),
1144                functions: metric(functions),
1145                statements: metric(statements),
1146            },
1147        }
1148    }
1149
1150    const TS_FULL: TypeScriptThresholds = TypeScriptThresholds {
1151        lines: 100,
1152        branches: 100,
1153        functions: 100,
1154        statements: 100,
1155    };
1156    const TS_MID: TypeScriptThresholds = TypeScriptThresholds {
1157        lines: 80,
1158        branches: 75,
1159        functions: 80,
1160        statements: 80,
1161    };
1162
1163    #[test]
1164    fn typescript_passes_when_every_metric_meets_its_floor() {
1165        assert_eq!(
1166            evaluate_typescript(&ts_report(100.0, 100.0, 100.0, 100.0), TS_FULL),
1167            Outcome::Pass
1168        );
1169    }
1170
1171    #[test]
1172    fn typescript_fails_on_the_one_metric_below_its_floor() {
1173        // 100% lines but only 66.66% branches (the `below` fixture's shape): the
1174        // branch floor catches what line coverage misses — and only `branches` is
1175        // named as a shortfall, not the metrics that met their floor.
1176        let outcome = evaluate_typescript(&ts_report(100.0, 66.66, 100.0, 100.0), TS_MID);
1177        assert!(
1178            matches!(&outcome, Outcome::Fail(message) if message.contains("branches") && !message.contains("lines")),
1179            "got: {outcome:?}"
1180        );
1181    }
1182
1183    #[test]
1184    fn typescript_fail_message_names_every_metric_below() {
1185        let outcome = evaluate_typescript(&ts_report(70.0, 70.0, 70.0, 70.0), TS_MID);
1186        assert!(
1187            matches!(&outcome, Outcome::Fail(message)
1188                if message.contains("lines")
1189                    && message.contains("branches")
1190                    && message.contains("functions")
1191                    && message.contains("statements")),
1192            "got: {outcome:?}"
1193        );
1194    }
1195
1196    #[test]
1197    fn typescript_tolerates_float_noise_at_the_floor() {
1198        // A percent a hair under the floor from rounding still passes.
1199        assert_eq!(
1200            evaluate_typescript(&ts_report(99.999_999_999, 100.0, 100.0, 100.0), TS_FULL),
1201            Outcome::Pass
1202        );
1203    }
1204
1205    #[test]
1206    fn typescript_empty_denominator_metric_is_vacuously_satisfied() {
1207        // Branch-free code measured alongside real code: branches has nothing to
1208        // cover (pct "Unknown") but lines/etc. are real and pass → overall pass.
1209        let report = VitestReport {
1210            total: VitestTotals {
1211                lines: metric(100.0),
1212                branches: VitestMetric {
1213                    pct: None,
1214                    total: 0,
1215                },
1216                functions: metric(100.0),
1217                statements: metric(100.0),
1218            },
1219        };
1220        assert_eq!(evaluate_typescript(&report, TS_FULL), Outcome::Pass);
1221    }
1222
1223    #[test]
1224    fn typescript_fails_a_vacuous_run_that_measured_no_code() {
1225        // No lines in the denominator (everything excluded, or a wrong path): a
1226        // vacuous run is a failure, never a silent pass.
1227        let nothing = VitestMetric {
1228            pct: None,
1229            total: 0,
1230        };
1231        let report = VitestReport {
1232            total: VitestTotals {
1233                lines: nothing,
1234                branches: nothing,
1235                functions: nothing,
1236                statements: nothing,
1237            },
1238        };
1239        let outcome = evaluate_typescript(&report, TS_MID);
1240        assert!(
1241            matches!(&outcome, Outcome::Fail(message) if message.contains("measured no code")),
1242            "got: {outcome:?}"
1243        );
1244    }
1245
1246    #[test]
1247    fn parses_a_vitest_summary_report() {
1248        // A realistic `coverage-summary.json`: the four metrics plus the
1249        // `branchesTrue` block and a per-file entry the check ignores.
1250        let json = r#"{
1251            "total": {
1252                "lines": {"total": 5, "covered": 4, "skipped": 0, "pct": 80},
1253                "statements": {"total": 5, "covered": 4, "skipped": 0, "pct": 80},
1254                "functions": {"total": 2, "covered": 2, "skipped": 0, "pct": 100},
1255                "branches": {"total": 3, "covered": 2, "skipped": 0, "pct": 66.66},
1256                "branchesTrue": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"}
1257            },
1258            "/abs/widget.ts": {
1259                "lines": {"total": 5, "covered": 4, "skipped": 0, "pct": 80}
1260            }
1261        }"#;
1262        let report = parse_vitest_report(json).expect("valid vitest json-summary");
1263        // A whole-number percent (`visit_u64`) and a fractional one (`visit_f64`).
1264        assert_eq!(report.total.lines.pct, Some(80.0));
1265        assert_eq!(report.total.branches.pct, Some(66.66));
1266        assert_eq!(report.total.functions.total, 2);
1267    }
1268
1269    #[test]
1270    fn parses_an_unknown_pct_as_unmeasured() {
1271        let json = r#"{"total": {
1272            "lines": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"},
1273            "statements": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"},
1274            "functions": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"},
1275            "branches": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"}
1276        }}"#;
1277        let report = parse_vitest_report(json).expect("valid vitest json-summary");
1278        assert_eq!(report.total.lines.pct, None);
1279        assert_eq!(report.total.lines.total, 0);
1280    }
1281
1282    #[test]
1283    fn a_pct_that_is_neither_number_nor_string_is_a_parse_error() {
1284        // vitest only ever writes a number or "Unknown"; anything else (here a
1285        // bool) is a malformed report, surfaced as an error rather than guessed.
1286        let json = r#"{"total":{
1287            "lines": {"total": 1, "covered": 1, "skipped": 0, "pct": true},
1288            "statements": {"total": 1, "covered": 1, "skipped": 0, "pct": 100},
1289            "functions": {"total": 1, "covered": 1, "skipped": 0, "pct": 100},
1290            "branches": {"total": 1, "covered": 1, "skipped": 0, "pct": 100}
1291        }}"#;
1292        assert!(parse_vitest_report(json).is_err());
1293    }
1294
1295    // --- Rust (cargo llvm-cov) — issue #37 ---
1296
1297    fn rust_metric(percent: f64) -> LlvmCovMetric {
1298        LlvmCovMetric {
1299            count: 10,
1300            covered: 10,
1301            percent,
1302        }
1303    }
1304
1305    fn rust_report(regions: f64, lines: f64) -> LlvmCovReport {
1306        LlvmCovReport {
1307            data: vec![LlvmCovData {
1308                totals: LlvmCovTotals {
1309                    regions: rust_metric(regions),
1310                    lines: rust_metric(lines),
1311                },
1312            }],
1313        }
1314    }
1315
1316    const RUST_FULL: RustThresholds = RustThresholds {
1317        regions: 100,
1318        lines: 100,
1319    };
1320    const RUST_MID: RustThresholds = RustThresholds {
1321        regions: 80,
1322        lines: 85,
1323    };
1324
1325    #[test]
1326    fn rust_passes_when_both_metrics_meet_their_floor() {
1327        assert_eq!(
1328            evaluate_rust(&rust_report(100.0, 100.0), RUST_FULL),
1329            Outcome::Pass
1330        );
1331    }
1332
1333    #[test]
1334    fn rust_fails_on_the_one_metric_below_its_floor() {
1335        // 100% lines but only 70% regions: the regions floor catches what line
1336        // coverage misses — and only `regions` is named, not the metric that met
1337        // its floor.
1338        let outcome = evaluate_rust(&rust_report(70.0, 100.0), RUST_MID);
1339        assert!(
1340            matches!(&outcome, Outcome::Fail(message) if message.contains("regions") && !message.contains("lines")),
1341            "got: {outcome:?}"
1342        );
1343    }
1344
1345    #[test]
1346    fn rust_fail_message_names_every_metric_below() {
1347        let outcome = evaluate_rust(&rust_report(50.0, 50.0), RUST_MID);
1348        assert!(
1349            matches!(&outcome, Outcome::Fail(message)
1350                if message.contains("regions") && message.contains("lines")),
1351            "got: {outcome:?}"
1352        );
1353    }
1354
1355    #[test]
1356    fn rust_tolerates_float_noise_at_the_floor() {
1357        // A percent a hair under the floor from rounding still passes.
1358        assert_eq!(
1359            evaluate_rust(&rust_report(99.999_999_999, 100.0), RUST_FULL),
1360            Outcome::Pass
1361        );
1362    }
1363
1364    #[test]
1365    fn rust_fails_a_vacuous_run_that_measured_no_code() {
1366        // No regions in the denominator (a wrong path, or a crate that compiled
1367        // nothing): a vacuous run is a failure, never a silent pass.
1368        let nothing = LlvmCovMetric {
1369            count: 0,
1370            covered: 0,
1371            percent: 0.0,
1372        };
1373        let report = LlvmCovReport {
1374            data: vec![LlvmCovData {
1375                totals: LlvmCovTotals {
1376                    regions: nothing,
1377                    lines: nothing,
1378                },
1379            }],
1380        };
1381        let outcome = evaluate_rust(&report, RUST_MID);
1382        assert!(
1383            matches!(&outcome, Outcome::Fail(message) if message.contains("measured no code")),
1384            "got: {outcome:?}"
1385        );
1386    }
1387
1388    #[test]
1389    fn rust_fails_an_export_with_no_data() {
1390        // `cargo llvm-cov` always emits one `data` entry; an empty array is a
1391        // malformed run, failed rather than treated as a pass.
1392        let report = LlvmCovReport { data: vec![] };
1393        assert!(matches!(evaluate_rust(&report, RUST_MID), Outcome::Fail(_)));
1394    }
1395
1396    #[test]
1397    fn parses_a_cargo_llvm_cov_report() {
1398        // A realistic `--json --summary-only` export: regions/lines (enforced) plus
1399        // the functions block and the `type`/`version` the check ignores.
1400        let json = r#"{
1401            "data": [{"totals": {
1402                "regions": {"count": 12, "covered": 9, "notcovered": 3, "percent": 75.0},
1403                "lines": {"count": 20, "covered": 18, "percent": 90.0},
1404                "functions": {"count": 3, "covered": 3, "percent": 100.0}
1405            }}],
1406            "type": "llvm.coverage.json.export",
1407            "version": "2.0.1"
1408        }"#;
1409        let report = parse_llvm_cov_report(json).expect("valid llvm-cov json");
1410        assert_eq!(report.data[0].totals.regions.percent, 75.0);
1411        assert_eq!(report.data[0].totals.lines.count, 20);
1412    }
1413
1414    // --- Rust diff-scoped region detail (`cargo llvm-cov --json`) — issue #162 ---
1415
1416    #[test]
1417    fn llvm_cov_patch_detail_reads_code_regions_per_file() {
1418        // A realistic full `--json` export: one function spanning two regions on
1419        // `/abs/grade.rs` — line 6 covered (execCount 1), line 10 the uncovered
1420        // `else` arm (execCount 0). Both are `kind == 0` code regions, indexed back
1421        // to `filenames[0]`.
1422        let json = r#"{
1423            "data": [{
1424                "files": [{"filename": "/abs/grade.rs"}],
1425                "functions": [{
1426                    "filenames": ["/abs/grade.rs"],
1427                    "regions": [
1428                        [6, 5, 6, 26, 1, 0, 0, 0],
1429                        [10, 9, 10, 17, 0, 0, 0, 0]
1430                    ]
1431                }],
1432                "totals": {}
1433            }],
1434            "type": "llvm.coverage.json.export",
1435            "version": "3.0.1"
1436        }"#;
1437        let out = llvm_cov_patch_detail(json).expect("valid llvm-cov export");
1438        assert_eq!(
1439            out["/abs/grade.rs"].regions,
1440            vec![(6, 6, true), (10, 10, false)]
1441        );
1442    }
1443
1444    #[test]
1445    fn llvm_cov_patch_detail_skips_non_code_regions() {
1446        // Only `kind == 0` counts: a gap region (kind 1) and an expansion region
1447        // (kind 2) on the same function are ignored, leaving just the one code region.
1448        let json = r#"{
1449            "data": [{
1450                "files": [{"filename": "/abs/a.rs"}],
1451                "functions": [{
1452                    "filenames": ["/abs/a.rs"],
1453                    "regions": [
1454                        [1, 1, 1, 10, 2, 0, 0, 0],
1455                        [2, 1, 2, 10, 0, 0, 0, 1],
1456                        [3, 1, 3, 10, 0, 0, 0, 2]
1457                    ]
1458                }]
1459            }]
1460        }"#;
1461        let out = llvm_cov_patch_detail(json).expect("valid llvm-cov export");
1462        assert_eq!(out["/abs/a.rs"].regions, vec![(1, 1, true)]);
1463    }
1464
1465    #[test]
1466    fn llvm_cov_patch_detail_groups_regions_by_filename_id() {
1467        // A region's `fileID` (index 5) selects its file from the function's
1468        // `filenames`; two regions under the same function land in different files.
1469        let json = r#"{
1470            "data": [{
1471                "files": [{"filename": "/abs/a.rs"}, {"filename": "/abs/b.rs"}],
1472                "functions": [{
1473                    "filenames": ["/abs/a.rs", "/abs/b.rs"],
1474                    "regions": [
1475                        [1, 1, 1, 5, 1, 0, 0, 0],
1476                        [9, 1, 9, 5, 0, 1, 1, 0]
1477                    ]
1478                }]
1479            }]
1480        }"#;
1481        let out = llvm_cov_patch_detail(json).expect("valid llvm-cov export");
1482        assert_eq!(out["/abs/a.rs"].regions, vec![(1, 1, true)]);
1483        assert_eq!(out["/abs/b.rs"].regions, vec![(9, 9, false)]);
1484    }
1485
1486    #[test]
1487    fn llvm_cov_patch_detail_skips_a_malformed_short_region() {
1488        // A region array shorter than the eight fields (never seen from llvm-cov) is
1489        // skipped rather than panicking on an index; the well-formed one survives.
1490        let json = r#"{
1491            "data": [{
1492                "files": [{"filename": "/abs/a.rs"}],
1493                "functions": [{
1494                    "filenames": ["/abs/a.rs"],
1495                    "regions": [
1496                        [4, 1, 4],
1497                        [5, 1, 5, 9, 1, 0, 0, 0]
1498                    ]
1499                }]
1500            }]
1501        }"#;
1502        let out = llvm_cov_patch_detail(json).expect("valid llvm-cov export");
1503        assert_eq!(out["/abs/a.rs"].regions, vec![(5, 5, true)]);
1504    }
1505
1506    #[test]
1507    fn llvm_cov_patch_detail_spans_a_multiline_region() {
1508        // A region spanning lines 3–5 keeps both endpoints, so a changed line
1509        // anywhere in 3..=5 can count it.
1510        let json = r#"{
1511            "data": [{
1512                "files": [{"filename": "/abs/a.rs"}],
1513                "functions": [{
1514                    "filenames": ["/abs/a.rs"],
1515                    "regions": [[3, 5, 5, 6, 0, 0, 0, 0]]
1516                }]
1517            }]
1518        }"#;
1519        let out = llvm_cov_patch_detail(json).expect("valid llvm-cov export");
1520        assert_eq!(out["/abs/a.rs"].regions, vec![(3, 5, false)]);
1521    }
1522
1523    #[test]
1524    fn llvm_cov_patch_detail_drops_a_file_absent_from_the_files_allowlist() {
1525        // `--ignore-filename-regex` drops an exempt file from `files` but leaves its
1526        // regions in `functions`; restricting to the `files` allowlist lifts them, so
1527        // the ignored file contributes nothing while the kept file still does.
1528        let json = r#"{
1529            "data": [{
1530                "files": [{"filename": "/abs/kept.rs"}],
1531                "functions": [{
1532                    "filenames": ["/abs/kept.rs", "/abs/ignored.rs"],
1533                    "regions": [
1534                        [1, 1, 1, 9, 1, 0, 0, 0],
1535                        [2, 1, 2, 9, 0, 1, 0, 0]
1536                    ]
1537                }]
1538            }]
1539        }"#;
1540        let out = llvm_cov_patch_detail(json).expect("valid llvm-cov export");
1541        assert_eq!(out["/abs/kept.rs"].regions, vec![(1, 1, true)]);
1542        assert!(!out.contains_key("/abs/ignored.rs"));
1543    }
1544
1545    #[test]
1546    fn llvm_cov_patch_detail_malformed_json_is_an_error() {
1547        assert!(llvm_cov_patch_detail("{ not json").is_err());
1548    }
1549
1550    #[test]
1551    fn rust_ignore_regex_is_none_when_nothing_is_exempt() {
1552        assert_eq!(ignore_filename_regex(&[]), None);
1553    }
1554
1555    #[test]
1556    fn rust_ignore_regex_escapes_and_joins_exempt_paths() {
1557        // The caller passes already-resolved, `root`-relative paths; each is
1558        // regex-escaped (the `.` becomes `\.`) and joined into one alternation.
1559        let exempt = vec!["src/shim.rs".to_string(), "src/gen.rs".to_string()];
1560        assert_eq!(
1561            ignore_filename_regex(&exempt).as_deref(),
1562            Some(r"src/shim\.rs|src/gen\.rs")
1563        );
1564    }
1565}