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