1use std::collections::{BTreeMap, BTreeSet};
6use std::path::{Path, PathBuf};
7use std::process::Command;
8use std::sync::atomic::{AtomicU64, Ordering};
9
10use anyhow::{bail, Context, Result};
11use serde::Deserialize;
12
13const TEST_OMIT: &str = "*_test.py";
15
16const SUPPORT_OMIT: &str = "*conftest.py";
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct Thresholds {
22 pub fail_under: u8,
24 pub branch: bool,
26}
27
28#[derive(Debug, Clone, Deserialize)]
31pub struct CoverageReport {
32 pub totals: Totals,
33 #[serde(default)]
36 pub files: BTreeMap<String, FileCoverage>,
37}
38
39#[derive(Debug, Clone, Default, Deserialize)]
42pub struct FileCoverage {
43 #[serde(default)]
45 pub executed_lines: Vec<u64>,
46 #[serde(default)]
48 pub missing_lines: Vec<u64>,
49 #[serde(default)]
51 pub excluded_lines: Vec<u64>,
52 #[serde(default)]
55 pub missing_branches: Vec<Vec<i64>>,
56 #[serde(default)]
59 pub executed_branches: Vec<Vec<i64>>,
60}
61
62#[derive(Debug, Clone, Deserialize)]
64pub struct Totals {
65 pub percent_covered: f64,
67 #[serde(default)]
69 pub num_branches: u64,
70}
71
72#[derive(Debug, Clone, PartialEq)]
74pub enum Outcome {
75 Pass,
76 Fail(String),
78}
79
80pub fn parse_report(json: &str) -> Result<CoverageReport> {
82 serde_json::from_str(json).context("parsing coverage.py JSON report")
83}
84
85pub fn evaluate(report: &CoverageReport, thresholds: Thresholds) -> Outcome {
89 let actual = report.totals.percent_covered;
90 let required = f64::from(thresholds.fail_under);
91 if actual + 1e-9 >= required {
93 Outcome::Pass
94 } else {
95 Outcome::Fail(format!(
96 "coverage {actual:.2}% is below the required {}%",
97 thresholds.fail_under
98 ))
99 }
100}
101
102pub fn measure(root: &Path, thresholds: Thresholds, omit: &[String]) -> Result<Outcome> {
106 let report = run_coverage(root, omit)?;
107 Ok(evaluate(&report, thresholds))
108}
109
110pub fn measure_report(root: &Path, omit: &[String]) -> Result<CoverageReport> {
113 run_coverage(root, omit)
114}
115
116struct DataFile(PathBuf);
119
120impl DataFile {
121 fn new() -> Self {
122 static COUNTER: AtomicU64 = AtomicU64::new(0);
123 let name = format!(
124 "testing-conventions-{}-{}.coverage",
125 std::process::id(),
126 COUNTER.fetch_add(1, Ordering::Relaxed),
127 );
128 DataFile(std::env::temp_dir().join(name))
129 }
130}
131
132impl Drop for DataFile {
133 fn drop(&mut self) {
134 let _ = std::fs::remove_file(&self.0);
135 }
136}
137
138fn run_coverage(root: &Path, omit: &[String]) -> Result<CoverageReport> {
142 let data = DataFile::new();
143 let omit = build_omit(omit);
144
145 let mut command = Command::new("coverage");
147 command
148 .current_dir(root)
149 .args(["run", "--branch", "--source=."])
150 .arg(format!("--omit={omit}"));
151 let run = command
152 .args([
153 "-m",
154 "pytest",
155 "-q",
156 "-p",
157 "no:cacheprovider",
158 "--ignore=tests",
159 ".",
160 ])
161 .env("COVERAGE_FILE", &data.0)
162 .env("PYTHONDONTWRITEBYTECODE", "1")
163 .output()
164 .context("running `coverage run -m pytest` (is coverage.py installed?)")?;
165 if !run.status.success() {
166 bail!(
167 "the unit suite did not run cleanly under coverage in `{}`:\n{}{}",
168 root.display(),
169 String::from_utf8_lossy(&run.stdout),
170 String::from_utf8_lossy(&run.stderr),
171 );
172 }
173
174 let json = Command::new("coverage")
175 .current_dir(root)
176 .args(["json", "-o", "-"])
177 .env("COVERAGE_FILE", &data.0)
178 .output()
179 .context("running `coverage json`")?;
180 if !json.status.success() {
181 bail!(
182 "`coverage json` failed:\n{}",
183 String::from_utf8_lossy(&json.stderr),
184 );
185 }
186
187 parse_report(&String::from_utf8_lossy(&json.stdout))
188}
189
190fn build_omit(omit: &[String]) -> String {
194 [TEST_OMIT.to_string(), SUPPORT_OMIT.to_string()]
195 .into_iter()
196 .chain(omit.iter().cloned())
197 .collect::<Vec<_>>()
198 .join(",")
199}
200
201const TS_INCLUDE: &str = "**/*.{ts,tsx,mts,cts}";
204
205fn vitest_default_excludes(root: &Path) -> Result<Vec<String>> {
209 let run = Command::new("node")
210 .current_dir(root)
211 .args([
212 "-e",
213 "process.stdout.write(JSON.stringify(require('vitest/config').coverageConfigDefaults.exclude))",
214 ])
215 .output()
216 .context("resolving vitest's default coverage excludes via node")?;
217 if !run.status.success() {
218 bail!(
219 "could not resolve vitest's default coverage excludes in `{}`. The check runs the \
220 project's own vitest via `npx --no-install` and never downloads it, so `vitest` \
221 must be installed in the project. node output:\n{}{}",
222 root.display(),
223 String::from_utf8_lossy(&run.stdout),
224 String::from_utf8_lossy(&run.stderr),
225 );
226 }
227 parse_default_excludes(&run.stdout)
228}
229
230fn parse_default_excludes(stdout: &[u8]) -> Result<Vec<String>> {
232 let excludes: Vec<String> = serde_json::from_slice(stdout).with_context(|| {
233 format!(
234 "vitest's default coverage excludes were not a JSON string array — got: {}",
235 String::from_utf8_lossy(stdout)
236 )
237 })?;
238 Ok(excludes.into_iter().filter(|p| !p.contains('\0')).collect())
241}
242
243#[derive(Debug, Clone, Copy, PartialEq, Eq)]
245pub struct TypeScriptThresholds {
246 pub lines: u8,
247 pub branches: u8,
248 pub functions: u8,
249 pub statements: u8,
250}
251
252#[derive(Debug, Clone, Copy, Deserialize)]
254pub struct VitestReport {
255 pub total: VitestTotals,
256}
257
258#[derive(Debug, Clone, Copy, Deserialize)]
260pub struct VitestTotals {
261 pub lines: VitestMetric,
262 pub branches: VitestMetric,
263 pub functions: VitestMetric,
264 pub statements: VitestMetric,
265}
266
267#[derive(Debug, Clone, Copy, Deserialize)]
269pub struct VitestMetric {
270 #[serde(deserialize_with = "deserialize_pct")]
273 pub pct: Option<f64>,
274 pub total: u64,
276}
277
278fn deserialize_pct<'de, D>(deserializer: D) -> std::result::Result<Option<f64>, D::Error>
281where
282 D: serde::Deserializer<'de>,
283{
284 struct PctVisitor;
285 impl serde::de::Visitor<'_> for PctVisitor {
286 type Value = Option<f64>;
287
288 fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
289 f.write_str("a coverage percent number or the string \"Unknown\"")
290 }
291
292 fn visit_f64<E>(self, value: f64) -> std::result::Result<Self::Value, E> {
293 Ok(Some(value))
294 }
295
296 fn visit_u64<E>(self, value: u64) -> std::result::Result<Self::Value, E> {
298 Ok(Some(value as f64))
299 }
300
301 fn visit_str<E>(self, _value: &str) -> std::result::Result<Self::Value, E> {
303 Ok(None)
304 }
305 }
306 deserializer.deserialize_any(PctVisitor)
307}
308
309pub fn parse_vitest_report(json: &str) -> Result<VitestReport> {
311 serde_json::from_str(json).context("parsing vitest coverage-summary JSON report")
312}
313
314pub fn evaluate_typescript(report: &VitestReport, thresholds: TypeScriptThresholds) -> Outcome {
318 let total = &report.total;
319 if total.lines.total == 0 {
321 return Outcome::Fail(
322 "the unit suite measured no code — check the path and that the suite runs".to_string(),
323 );
324 }
325 let checks = [
326 ("lines", total.lines, thresholds.lines),
327 ("branches", total.branches, thresholds.branches),
328 ("functions", total.functions, thresholds.functions),
329 ("statements", total.statements, thresholds.statements),
330 ];
331 let mut shortfalls = Vec::new();
332 for (name, metric, required) in checks {
333 let actual = metric.pct.unwrap_or(100.0);
335 if actual + 1e-9 < f64::from(required) {
337 shortfalls.push(format!("{name} {actual:.2}% < {required}%"));
338 }
339 }
340 if shortfalls.is_empty() {
341 Outcome::Pass
342 } else {
343 Outcome::Fail(format!(
344 "coverage below thresholds: {}",
345 shortfalls.join(", ")
346 ))
347 }
348}
349
350pub fn measure_typescript(
354 root: &Path,
355 thresholds: TypeScriptThresholds,
356 exclude: &[String],
357) -> Result<Outcome> {
358 let report = run_vitest(root, exclude)?;
359 Ok(evaluate_typescript(&report, thresholds))
360}
361
362struct ReportDir(PathBuf);
365
366impl ReportDir {
367 fn new() -> Self {
368 static COUNTER: AtomicU64 = AtomicU64::new(0);
369 let name = format!(
370 "testing-conventions-vitest-{}-{}",
371 std::process::id(),
372 COUNTER.fetch_add(1, Ordering::Relaxed),
373 );
374 ReportDir(std::env::temp_dir().join(name))
375 }
376}
377
378impl Drop for ReportDir {
379 fn drop(&mut self) {
380 let _ = std::fs::remove_dir_all(&self.0);
381 }
382}
383
384fn run_vitest(root: &Path, exclude: &[String]) -> Result<VitestReport> {
386 let json = run_vitest_coverage(root, exclude, "json-summary", "coverage-summary.json")?;
387 parse_vitest_report(&json)
388}
389
390fn run_vitest_coverage(
394 root: &Path,
395 exclude: &[String],
396 reporter: &str,
397 report_file: &str,
398) -> Result<String> {
399 let reports = ReportDir::new();
400
401 let mut command = Command::new("npx");
402 command
403 .current_dir(root)
404 .args(["--no-install", "vitest", "run", "--no-cache"])
407 .args(["--coverage.enabled", "--coverage.provider=v8"])
408 .arg(format!("--coverage.reporter={reporter}"))
409 .arg("--coverage.all=true")
410 .arg(format!(
411 "--coverage.reportsDirectory={}",
412 reports.0.display()
413 ))
414 .arg(format!("--coverage.include={TS_INCLUDE}"))
415 .args([
418 "--coverage.thresholds.lines=0",
419 "--coverage.thresholds.branches=0",
420 "--coverage.thresholds.functions=0",
421 "--coverage.thresholds.statements=0",
422 "--coverage.thresholds.autoUpdate=false",
423 ]);
424 for path in vitest_default_excludes(root)?.iter().chain(exclude) {
425 command.arg(format!("--coverage.exclude={path}"));
426 }
427 let run = command
429 .env("CI", "1")
430 .output()
431 .context("running `npx --no-install vitest run --coverage`")?;
432 if !run.status.success() {
433 bail!(
434 "the unit suite did not run cleanly under vitest in `{}`. The check runs the \
435 project's own vitest via `npx --no-install` and never downloads it, so `vitest` \
436 and `@vitest/coverage-v8` must be installed in the project. vitest output:\n{}{}",
437 root.display(),
438 String::from_utf8_lossy(&run.stdout),
439 String::from_utf8_lossy(&run.stderr),
440 );
441 }
442
443 read_vitest_report(&reports.0.join(report_file), reporter)
444}
445
446fn read_vitest_report(path: &Path, reporter: &str) -> Result<String> {
448 std::fs::read_to_string(path).with_context(|| {
449 format!(
450 "reading vitest coverage report `{}` (did the run produce a {reporter} report?)",
451 path.display()
452 )
453 })
454}
455
456#[derive(Debug, Clone, Deserialize)]
459struct IstanbulFile {
460 #[serde(rename = "statementMap", default)]
462 statement_map: BTreeMap<String, IstanbulSpan>,
463 #[serde(default)]
465 s: BTreeMap<String, u64>,
466 #[serde(rename = "branchMap", default)]
468 branch_map: BTreeMap<String, IstanbulBranch>,
469 #[serde(default)]
471 b: BTreeMap<String, Vec<u64>>,
472 #[serde(rename = "fnMap", default)]
474 fn_map: BTreeMap<String, IstanbulFn>,
475 #[serde(default)]
477 f: BTreeMap<String, u64>,
478}
479
480#[derive(Debug, Clone, Deserialize)]
482struct IstanbulSpan {
483 start: IstanbulPos,
484 end: IstanbulPos,
485}
486
487#[derive(Debug, Clone, Deserialize)]
489struct IstanbulPos {
490 line: u64,
491}
492
493#[derive(Debug, Clone, Deserialize)]
495struct IstanbulBranch {
496 loc: IstanbulSpan,
497}
498
499#[derive(Debug, Clone, Deserialize)]
502struct IstanbulFn {
503 decl: IstanbulSpan,
504}
505
506#[derive(Debug, Clone, Default)]
509pub struct TsPatchCoverage {
510 pub statements: Vec<(u64, u64, bool)>,
513 pub branch_arms: Vec<(u64, bool)>,
516 pub functions: Vec<(u64, bool)>,
519}
520
521pub fn measure_patch_typescript_detail(
525 root: &Path,
526 exclude: &[String],
527) -> Result<BTreeMap<String, TsPatchCoverage>> {
528 let json = run_vitest_coverage(root, exclude, "json", "coverage-final.json")?;
529 istanbul_patch_detail(&json)
530}
531
532fn istanbul_patch_detail(json: &str) -> Result<BTreeMap<String, TsPatchCoverage>> {
535 let files: BTreeMap<String, IstanbulFile> = serde_json::from_str(json)
536 .context("parsing vitest coverage-final (Istanbul) JSON report")?;
537 let mut out = BTreeMap::new();
538 for (path, file) in files {
539 let mut detail = TsPatchCoverage::default();
540 for (id, span) in &file.statement_map {
541 let covered = file.s.get(id).is_some_and(|&count| count > 0);
542 detail
543 .statements
544 .push((span.start.line, span.end.line, covered));
545 }
546 for (id, branch) in &file.branch_map {
549 let line = branch.loc.start.line;
550 if let Some(counts) = file.b.get(id) {
551 for &count in counts {
552 detail.branch_arms.push((line, count > 0));
553 }
554 }
555 }
556 for (id, function) in &file.fn_map {
557 let covered = file.f.get(id).is_some_and(|&count| count > 0);
558 detail.functions.push((function.decl.start.line, covered));
559 }
560 out.insert(path, detail);
561 }
562 Ok(out)
563}
564
565#[derive(Debug, Clone, Copy, PartialEq, Eq)]
569pub struct RustThresholds {
570 pub regions: Option<u8>,
571 pub lines: u8,
572 pub functions: Option<u8>,
573 pub branch: Option<u8>,
574}
575
576#[derive(Debug, Clone, Deserialize)]
579pub struct LlvmCovReport {
580 pub data: Vec<LlvmCovData>,
581}
582
583#[derive(Debug, Clone, Copy, Deserialize)]
585pub struct LlvmCovData {
586 pub totals: LlvmCovTotals,
587}
588
589#[derive(Debug, Clone, Copy, Default, Deserialize)]
592pub struct LlvmCovTotals {
593 pub regions: LlvmCovMetric,
594 pub lines: LlvmCovMetric,
595 pub functions: LlvmCovMetric,
596 #[serde(default)]
597 pub branches: Option<LlvmCovMetric>,
598}
599
600#[derive(Debug, Clone, Copy, Default, Deserialize)]
602pub struct LlvmCovMetric {
603 pub count: u64,
605 pub covered: u64,
606 pub percent: f64,
607}
608
609pub fn parse_llvm_cov_report(json: &str) -> Result<LlvmCovReport> {
611 serde_json::from_str(json).context("parsing cargo llvm-cov JSON report")
612}
613
614pub fn evaluate_rust(report: &LlvmCovReport, thresholds: RustThresholds) -> Outcome {
617 let Some(totals) = report.data.first().map(|entry| &entry.totals) else {
618 return Outcome::Fail("the cargo llvm-cov report contained no data".to_string());
619 };
620 if totals.regions.count == 0 {
622 return Outcome::Fail(
623 "the unit suite measured no code — check the path and that the suite runs".to_string(),
624 );
625 }
626 let mut checks: Vec<(&str, f64, u8)> = Vec::new();
628 if let Some(regions) = thresholds.regions {
629 checks.push(("regions", totals.regions.percent, regions));
630 }
631 checks.push(("lines", totals.lines.percent, thresholds.lines));
632 if let Some(functions) = thresholds.functions {
633 checks.push(("functions", totals.functions.percent, functions));
634 }
635 if let Some(branch) = thresholds.branch {
636 if let Some(branches) = totals.branches.filter(|metric| metric.count > 0) {
639 checks.push(("branches", branches.percent, branch));
640 }
641 }
642 let mut shortfalls = Vec::new();
643 for (name, actual, required) in checks {
644 if actual + 1e-9 < f64::from(required) {
646 shortfalls.push(format!("{name} {actual:.2}% < {required}%"));
647 }
648 }
649 if shortfalls.is_empty() {
650 Outcome::Pass
651 } else {
652 Outcome::Fail(format!(
653 "coverage below thresholds: {}",
654 shortfalls.join(", ")
655 ))
656 }
657}
658
659pub fn measure_rust(
663 root: &Path,
664 thresholds: RustThresholds,
665 ignore: &[String],
666 features: &[String],
667) -> Result<Outcome> {
668 let report = run_llvm_cov(root, ignore, features, thresholds.branch.is_some())?;
669 Ok(evaluate_rust(&report, thresholds))
670}
671
672struct TargetDir(PathBuf);
675
676impl TargetDir {
677 fn new() -> Self {
678 static COUNTER: AtomicU64 = AtomicU64::new(0);
679 let name = format!(
680 "testing-conventions-llvm-cov-{}-{}",
681 std::process::id(),
682 COUNTER.fetch_add(1, Ordering::Relaxed),
683 );
684 TargetDir(std::env::temp_dir().join(name))
685 }
686}
687
688impl Drop for TargetDir {
689 fn drop(&mut self) {
690 let _ = std::fs::remove_dir_all(&self.0);
691 }
692}
693
694fn run_llvm_cov(
699 root: &Path,
700 ignore: &[String],
701 features: &[String],
702 branch: bool,
703) -> Result<LlvmCovReport> {
704 let (json, lcov) = run_cargo_llvm_cov_json_and_lcov(root, ignore, features, branch)?;
705 let hidden = hidden_lines_by_file(&json)?;
706 let mut report = llvm_cov_totals_less_hidden(&parse_llvm_cov_export(&json)?, &hidden);
707 let lines = lcov_lines_metric(&lcov, &hidden);
708 for data in &mut report.data {
709 data.totals.lines = lines;
710 }
711 Ok(report)
712}
713
714fn lcov_lines(lcov: &str) -> BTreeMap<String, BTreeMap<u32, bool>> {
716 let mut out: BTreeMap<String, BTreeMap<u32, bool>> = BTreeMap::new();
717 let mut file: Option<&str> = None;
718 for record in lcov.lines() {
719 if let Some(name) = record.strip_prefix("SF:") {
720 file = Some(name);
721 } else if let Some((number, count)) =
722 record.strip_prefix("DA:").and_then(|da| da.split_once(','))
723 {
724 let (Some(file), Ok(number), Ok(count)) =
725 (file, number.parse::<u32>(), count.trim().parse::<u64>())
726 else {
727 continue;
728 };
729 *out.entry(file.to_string())
730 .or_default()
731 .entry(number)
732 .or_default() |= count > 0;
733 }
734 }
735 out
736}
737
738fn lcov_lines_metric(lcov: &str, hidden: &BTreeMap<String, BTreeSet<u32>>) -> LlvmCovMetric {
747 let mut tally = Tally::default();
748 for (file, lines) in lcov_lines(lcov) {
749 let gated = hidden.get(&file);
750 for (number, covered) in lines {
751 if gated.is_some_and(|gated| gated.contains(&number)) {
752 continue;
753 }
754 tally.count += 1;
755 tally.covered += u64::from(covered);
756 }
757 }
758 as_metric(tally.count, tally.covered)
759}
760
761fn hidden_lines_by_file(json: &str) -> Result<BTreeMap<String, BTreeSet<u32>>> {
769 let export = parse_llvm_cov_export(json)?;
770 let mut out = BTreeMap::new();
771 for data in &export.data {
772 for file in &data.files {
773 let Ok(source) = std::fs::read_to_string(&file.filename) else {
774 continue;
775 };
776 let lines = crate::isolation::lines_hidden_from_tests(&source);
777 if !lines.is_empty() {
778 out.insert(file.filename.clone(), lines);
779 }
780 }
781 }
782 Ok(out)
783}
784
785#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
787struct Tally {
788 count: u64,
789 covered: u64,
790}
791
792impl Tally {
793 fn merge(&mut self, other: Tally) {
796 self.count = self.count.max(other.count);
797 self.covered = self.covered.max(other.covered);
798 }
799
800 fn add(&mut self, other: Tally) {
801 self.count += other.count;
802 self.covered += other.covered;
803 }
804}
805
806#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
808struct HiddenTotals {
809 regions: Tally,
810 lines: Tally,
811 functions: Tally,
812 branches: Tally,
813}
814
815impl HiddenTotals {
816 fn merge(&mut self, other: HiddenTotals) {
817 self.regions.merge(other.regions);
818 self.lines.merge(other.lines);
819 self.functions.merge(other.functions);
820 self.branches.merge(other.branches);
821 }
822
823 fn add(&mut self, other: HiddenTotals) {
824 self.regions.add(other.regions);
825 self.lines.add(other.lines);
826 self.functions.add(other.functions);
827 self.branches.add(other.branches);
828 }
829}
830
831fn llvm_cov_totals_less_hidden(
833 export: &LlvmCovExport,
834 hidden: &BTreeMap<String, BTreeSet<u32>>,
835) -> LlvmCovReport {
836 LlvmCovReport {
837 data: export
838 .data
839 .iter()
840 .map(|data| LlvmCovData {
841 totals: totals_less_hidden(data, hidden),
842 })
843 .collect(),
844 }
845}
846
847fn totals_less_hidden(
849 data: &LlvmCovExportData,
850 hidden: &BTreeMap<String, BTreeSet<u32>>,
851) -> LlvmCovTotals {
852 let delta = hidden_totals(data, hidden);
853 LlvmCovTotals {
854 regions: metric_less(data.totals.regions, delta.regions),
855 lines: metric_less(data.totals.lines, delta.lines),
856 functions: metric_less(data.totals.functions, delta.functions),
857 branches: data
858 .totals
859 .branches
860 .map(|metric| metric_less(metric, delta.branches)),
861 }
862}
863
864fn metric_less(metric: LlvmCovMetric, delta: Tally) -> LlvmCovMetric {
866 let count = metric.count.saturating_sub(delta.count);
867 as_metric(
868 count,
869 metric.covered.saturating_sub(delta.covered).min(count),
870 )
871}
872
873fn as_metric(count: u64, covered: u64) -> LlvmCovMetric {
876 let percent = if count == 0 {
877 100.0
878 } else {
879 covered as f64 * 100.0 / count as f64
880 };
881 LlvmCovMetric {
882 count,
883 covered,
884 percent,
885 }
886}
887
888fn hidden_totals(
892 data: &LlvmCovExportData,
893 hidden: &BTreeMap<String, BTreeSet<u32>>,
894) -> HiddenTotals {
895 let measured: BTreeSet<&str> = data.files.iter().map(|f| f.filename.as_str()).collect();
896 let mut groups: BTreeMap<(String, i64, i64), HiddenTotals> = BTreeMap::new();
897 for function in &data.functions {
898 let Some((file, line, column)) = function_start(function) else {
899 continue;
900 };
901 if !measured.contains(file.as_str()) {
902 continue;
903 }
904 let Some(lines) = hidden.get(&file) else {
905 continue;
906 };
907 if !lines.contains(&(line.max(0) as u32)) {
908 continue;
909 }
910 groups
911 .entry((file, line, column))
912 .or_default()
913 .merge(function_totals(function, lines));
914 }
915 groups.values().fold(HiddenTotals::default(), |mut acc, g| {
916 acc.add(*g);
917 acc
918 })
919}
920
921fn function_start(function: &LlvmCovFunction) -> Option<(String, i64, i64)> {
924 let region = function.regions.first()?;
925 if region.len() < 8 {
926 return None;
927 }
928 let file = function.filenames.get(usize::try_from(region[5]).ok()?)?;
929 Some((file.clone(), region[0], region[1]))
930}
931
932fn function_totals(function: &LlvmCovFunction, hidden: &BTreeSet<u32>) -> HiddenTotals {
935 let code: Vec<&Vec<i64>> = function
936 .regions
937 .iter()
938 .filter(|region| region.len() >= 8 && region[7] == 0)
939 .collect();
940 let mut lines: BTreeMap<u32, bool> = BTreeMap::new();
941 for region in &code {
942 for line in region[0].max(0) as u32..=region[2].max(0) as u32 {
943 if hidden.contains(&line) {
944 *lines.entry(line).or_default() |= region[4] > 0;
945 }
946 }
947 }
948 HiddenTotals {
949 regions: Tally {
950 count: code.len() as u64,
951 covered: code.iter().filter(|region| region[4] > 0).count() as u64,
952 },
953 lines: Tally {
954 count: lines.len() as u64,
955 covered: lines.values().filter(|covered| **covered).count() as u64,
956 },
957 functions: Tally {
958 count: 1,
959 covered: u64::from(function.count > 0),
960 },
961 branches: branch_tally(&function.branches),
962 }
963}
964
965fn branch_tally(branches: &[Vec<i64>]) -> Tally {
967 let mut tally = Tally::default();
968 for branch in branches.iter().filter(|branch| branch.len() >= 9) {
969 tally.count += 2;
970 tally.covered += u64::from(branch[4] > 0) + u64::from(branch[5] > 0);
971 }
972 tally
973}
974
975fn run_cargo_llvm_cov(
979 root: &Path,
980 ignore: &[String],
981 format: &[&str],
982 features: &[String],
983 branch: bool,
984) -> Result<String> {
985 run_in(&TargetDir::new(), root, ignore, format, features, branch)
986}
987
988fn run_cargo_llvm_cov_json_and_lcov(
992 root: &Path,
993 ignore: &[String],
994 features: &[String],
995 branch: bool,
996) -> Result<(String, String)> {
997 let target = TargetDir::new();
998 let json = run_in(&target, root, ignore, &["--json"], features, branch)?;
999 let lcov = report_in(&target, root, ignore, &["--lcov"])?;
1000 Ok((json, lcov))
1001}
1002
1003fn report_in(
1006 target: &TargetDir,
1007 root: &Path,
1008 ignore: &[String],
1009 format: &[&str],
1010) -> Result<String> {
1011 let mut command = Command::new("cargo");
1012 command
1013 .current_dir(root)
1014 .arg("llvm-cov")
1015 .arg("report")
1016 .args(format)
1017 .env("CARGO_TARGET_DIR", &target.0);
1018 if let Some(regex) = ignore_filename_regex(root, ignore) {
1019 command.arg("--ignore-filename-regex").arg(regex);
1020 }
1021 scrub_outer_llvm_cov(&mut command);
1022 let output = command
1023 .output()
1024 .context("running `cargo llvm-cov report` (is cargo-llvm-cov installed?)")?;
1025 llvm_cov_stdout(
1026 output.status.success(),
1027 &output.stdout,
1028 &output.stderr,
1029 &format!(
1030 "cargo llvm-cov could not re-export the profile it just wrote in `{}`:",
1031 root.display()
1032 ),
1033 )
1034}
1035
1036fn llvm_cov_stdout(success: bool, stdout: &[u8], stderr: &[u8], failure: &str) -> Result<String> {
1038 if !success {
1039 bail!(
1040 "{failure}\n{}{}",
1041 String::from_utf8_lossy(stdout),
1042 String::from_utf8_lossy(stderr)
1043 );
1044 }
1045 Ok(String::from_utf8_lossy(stdout).into_owned())
1046}
1047
1048fn run_in(
1049 target: &TargetDir,
1050 root: &Path,
1051 ignore: &[String],
1052 format: &[&str],
1053 features: &[String],
1054 branch: bool,
1055) -> Result<String> {
1056 let mut command = Command::new("cargo");
1057 command
1058 .current_dir(root)
1059 .arg("llvm-cov")
1060 .arg("--lib")
1064 .arg("--bins")
1065 .args(format)
1066 .env("CARGO_TARGET_DIR", &target.0);
1067 if !features.is_empty() {
1068 command.arg("--features").arg(features.join(","));
1069 }
1070 if branch {
1071 command.arg("--branch");
1073 }
1074 if let Some(regex) = ignore_filename_regex(root, ignore) {
1075 command.arg("--ignore-filename-regex").arg(regex);
1076 }
1077 scrub_outer_llvm_cov(&mut command);
1078 let output = command
1079 .output()
1080 .context("running `cargo llvm-cov` (is cargo-llvm-cov installed?)")?;
1081 let hint = if branch {
1082 "\n(the [rust].coverage `branch` floor runs with --branch, which requires a \
1083 nightly toolchain — pin one in the crate's rust-toolchain.toml with \
1084 llvm-tools-preview, or set a rustup directory override)"
1085 } else {
1086 ""
1087 };
1088 llvm_cov_stdout(
1089 output.status.success(),
1090 &output.stdout,
1091 &output.stderr,
1092 &format!(
1093 "the unit suite did not run cleanly under cargo llvm-cov in `{}`:{hint}",
1094 root.display()
1095 ),
1096 )
1097}
1098
1099fn scrub_outer_llvm_cov(command: &mut Command) {
1105 for var in [
1106 "RUSTFLAGS",
1107 "CARGO_ENCODED_RUSTFLAGS",
1108 "RUSTDOCFLAGS",
1109 "CARGO_ENCODED_RUSTDOCFLAGS",
1110 "LLVM_PROFILE_FILE",
1111 "CARGO_LLVM_COV",
1112 "CARGO_LLVM_COV_SHOW_ENV",
1113 "CARGO_LLVM_COV_TARGET_DIR",
1114 "CARGO_LLVM_COV_BUILD_DIR",
1115 "RUSTC_WRAPPER",
1116 "RUSTC_WORKSPACE_WRAPPER",
1117 "__CARGO_LLVM_COV_RUSTC_WRAPPER",
1118 "__CARGO_LLVM_COV_RUSTC_WRAPPER_RUSTFLAGS",
1119 "__CARGO_LLVM_COV_RUSTC_WRAPPER_CRATE_NAMES",
1120 "RUSTUP_TOOLCHAIN",
1124 "CARGO",
1125 "RUSTC",
1126 ] {
1127 command.env_remove(var);
1128 }
1129}
1130
1131#[derive(Debug, Clone, Default)]
1134pub struct RustPatchCoverage {
1135 pub regions: Vec<(u64, u64, bool)>,
1138}
1139
1140#[derive(Debug, Clone, Deserialize)]
1143struct LlvmCovExport {
1144 data: Vec<LlvmCovExportData>,
1145}
1146
1147#[derive(Debug, Clone, Deserialize)]
1151struct LlvmCovExportData {
1152 files: Vec<LlvmCovExportFile>,
1153 functions: Vec<LlvmCovFunction>,
1154 #[serde(default)]
1157 totals: LlvmCovTotals,
1158}
1159
1160#[derive(Debug, Clone, Deserialize)]
1163struct LlvmCovExportFile {
1164 filename: String,
1165}
1166
1167#[derive(Debug, Clone, Deserialize)]
1172struct LlvmCovFunction {
1173 filenames: Vec<String>,
1174 regions: Vec<Vec<i64>>,
1175 #[serde(default)]
1177 count: u64,
1178 #[serde(default)]
1179 branches: Vec<Vec<i64>>,
1180}
1181
1182pub fn measure_patch_rust_detail(
1186 root: &Path,
1187 ignore: &[String],
1188 features: &[String],
1189) -> Result<BTreeMap<String, RustPatchCoverage>> {
1190 let json = run_cargo_llvm_cov(root, ignore, &["--json"], features, false)?;
1192 let hidden = hidden_lines_by_file(&json)?;
1193 llvm_cov_patch_detail(&json, &hidden)
1194}
1195
1196fn parse_llvm_cov_export(json: &str) -> Result<LlvmCovExport> {
1198 serde_json::from_str(json).context("parsing cargo llvm-cov JSON export")
1199}
1200
1201fn llvm_cov_patch_detail(
1206 json: &str,
1207 hidden: &BTreeMap<String, BTreeSet<u32>>,
1208) -> Result<BTreeMap<String, RustPatchCoverage>> {
1209 let export = parse_llvm_cov_export(json)?;
1210 let mut out: BTreeMap<String, RustPatchCoverage> = BTreeMap::new();
1211 for data in &export.data {
1212 let measured: BTreeSet<&str> = data.files.iter().map(|f| f.filename.as_str()).collect();
1213 for function in &data.functions {
1214 for region in &function.regions {
1215 if region.len() < 8 {
1216 continue;
1217 }
1218 if region[7] != 0 {
1220 continue;
1221 }
1222 let file_id = region[5];
1223 let Ok(file_id) = usize::try_from(file_id) else {
1224 continue;
1225 };
1226 let Some(file) = function.filenames.get(file_id) else {
1227 continue;
1228 };
1229 if !measured.contains(file.as_str()) {
1231 continue;
1232 }
1233 let start = region[0].max(0) as u64;
1234 let end = region[2].max(0) as u64;
1235 if hidden
1238 .get(file)
1239 .is_some_and(|lines| lines.contains(&(start as u32)))
1240 {
1241 continue;
1242 }
1243 let covered = region[4] > 0;
1244 out.entry(file.clone())
1245 .or_default()
1246 .regions
1247 .push((start, end, covered));
1248 }
1249 }
1250 }
1251 Ok(out)
1252}
1253
1254fn ignore_filename_regex(root: &Path, ignore: &[String]) -> Option<String> {
1258 if ignore.is_empty() {
1259 return None;
1260 }
1261 Some(
1262 ignore
1263 .iter()
1264 .map(|rel| {
1265 let full = root.join(rel);
1268 let full = full.canonicalize().unwrap_or(full);
1269 format!("{}$", regex_escape(&full.to_string_lossy()))
1270 })
1271 .collect::<Vec<_>>()
1272 .join("|"),
1273 )
1274}
1275
1276fn regex_escape(s: &str) -> String {
1278 const META: &str = r"\.+*?()|[]{}^$";
1279 let mut out = String::with_capacity(s.len());
1280 for c in s.chars() {
1281 if META.contains(c) {
1282 out.push('\\');
1283 }
1284 out.push(c);
1285 }
1286 out
1287}
1288
1289#[cfg(test)]
1290mod tests {
1291 use super::*;
1292
1293 fn report(percent_covered: f64, num_branches: u64) -> CoverageReport {
1294 CoverageReport {
1295 totals: Totals {
1296 percent_covered,
1297 num_branches,
1298 },
1299 files: BTreeMap::new(),
1300 }
1301 }
1302
1303 #[test]
1304 fn passes_when_total_meets_the_floor() {
1305 assert_eq!(
1306 evaluate(
1307 &report(100.0, 12),
1308 Thresholds {
1309 fail_under: 100,
1310 branch: true
1311 }
1312 ),
1313 Outcome::Pass
1314 );
1315 }
1316
1317 #[test]
1318 fn fails_when_total_is_below_the_floor() {
1319 assert!(matches!(
1320 evaluate(
1321 &report(80.0, 12),
1322 Thresholds {
1323 fail_under: 100,
1324 branch: true
1325 }
1326 ),
1327 Outcome::Fail(_)
1328 ));
1329 }
1330
1331 #[test]
1332 fn passes_when_branch_required_and_none_are_measured() {
1333 assert_eq!(
1334 evaluate(
1335 &report(100.0, 0),
1336 Thresholds {
1337 fail_under: 100,
1338 branch: true
1339 }
1340 ),
1341 Outcome::Pass
1342 );
1343 }
1344
1345 #[test]
1346 fn parses_a_coverage_py_report() {
1347 let json = r#"{"totals":{"percent_covered":91.5,"num_branches":8,"covered_lines":91}}"#;
1348 let report = parse_report(json).expect("valid coverage.py json");
1349 assert_eq!(report.totals.percent_covered, 91.5);
1350 assert_eq!(report.totals.num_branches, 8);
1351 }
1352
1353 #[test]
1354 fn parses_the_per_file_block_for_patch_coverage() {
1355 let json = r#"{
1356 "files": {
1357 "widget.py": {
1358 "executed_lines": [1, 2, 3, 4, 6],
1359 "summary": {"percent_covered": 85.0},
1360 "missing_lines": [5],
1361 "excluded_lines": [],
1362 "missing_branches": [[4, 5]]
1363 }
1364 },
1365 "totals": {"percent_covered": 85.0, "num_branches": 4}
1366 }"#;
1367 let report = parse_report(json).expect("valid coverage.py json with files");
1368 let widget = report.files.get("widget.py").expect("widget.py is present");
1369 assert_eq!(widget.missing_lines, vec![5]);
1370 assert_eq!(widget.missing_branches, vec![vec![4, 5]]);
1371 assert_eq!(report.totals.percent_covered, 85.0);
1372 }
1373
1374 #[test]
1375 fn a_report_without_a_files_block_parses_with_an_empty_map() {
1376 let report = parse_report(r#"{"totals":{"percent_covered":100.0,"num_branches":2}}"#)
1377 .expect("valid coverage.py json");
1378 assert!(report.files.is_empty());
1379 }
1380
1381 #[test]
1382 fn omit_is_the_test_and_support_globs_when_nothing_is_exempt() {
1383 assert_eq!(build_omit(&[]), "*_test.py,*conftest.py");
1384 }
1385
1386 #[test]
1387 fn omit_folds_in_the_exempt_paths_after_the_test_glob() {
1388 let exempt = vec!["pkg/gen.py".to_string(), "shim.py".to_string()];
1389 assert_eq!(
1390 build_omit(&exempt),
1391 "*_test.py,*conftest.py,pkg/gen.py,shim.py"
1392 );
1393 }
1394
1395 fn metric(pct: f64) -> VitestMetric {
1396 VitestMetric {
1397 pct: Some(pct),
1398 total: 10,
1399 }
1400 }
1401
1402 fn ts_report(lines: f64, branches: f64, functions: f64, statements: f64) -> VitestReport {
1403 VitestReport {
1404 total: VitestTotals {
1405 lines: metric(lines),
1406 branches: metric(branches),
1407 functions: metric(functions),
1408 statements: metric(statements),
1409 },
1410 }
1411 }
1412
1413 const TS_FULL: TypeScriptThresholds = TypeScriptThresholds {
1414 lines: 100,
1415 branches: 100,
1416 functions: 100,
1417 statements: 100,
1418 };
1419 const TS_MID: TypeScriptThresholds = TypeScriptThresholds {
1420 lines: 80,
1421 branches: 75,
1422 functions: 80,
1423 statements: 80,
1424 };
1425
1426 #[test]
1427 fn typescript_passes_when_every_metric_meets_its_floor() {
1428 assert_eq!(
1429 evaluate_typescript(&ts_report(100.0, 100.0, 100.0, 100.0), TS_FULL),
1430 Outcome::Pass
1431 );
1432 }
1433
1434 #[test]
1435 fn typescript_fails_on_the_one_metric_below_its_floor() {
1436 let outcome = evaluate_typescript(&ts_report(100.0, 66.66, 100.0, 100.0), TS_MID);
1437 assert!(
1438 matches!(&outcome, Outcome::Fail(message) if message.contains("branches") && !message.contains("lines")),
1439 "got: {outcome:?}"
1440 );
1441 }
1442
1443 #[test]
1444 fn typescript_fail_message_names_every_metric_below() {
1445 let outcome = evaluate_typescript(&ts_report(70.0, 70.0, 70.0, 70.0), TS_MID);
1446 assert!(
1447 matches!(&outcome, Outcome::Fail(message)
1448 if message.contains("lines")
1449 && message.contains("branches")
1450 && message.contains("functions")
1451 && message.contains("statements")),
1452 "got: {outcome:?}"
1453 );
1454 }
1455
1456 #[test]
1457 fn typescript_tolerates_float_noise_at_the_floor() {
1458 assert_eq!(
1459 evaluate_typescript(&ts_report(99.999_999_999, 100.0, 100.0, 100.0), TS_FULL),
1460 Outcome::Pass
1461 );
1462 }
1463
1464 #[test]
1465 fn typescript_empty_denominator_metric_is_vacuously_satisfied() {
1466 let report = VitestReport {
1467 total: VitestTotals {
1468 lines: metric(100.0),
1469 branches: VitestMetric {
1470 pct: None,
1471 total: 0,
1472 },
1473 functions: metric(100.0),
1474 statements: metric(100.0),
1475 },
1476 };
1477 assert_eq!(evaluate_typescript(&report, TS_FULL), Outcome::Pass);
1478 }
1479
1480 #[test]
1481 fn typescript_fails_a_vacuous_run_that_measured_no_code() {
1482 let nothing = VitestMetric {
1483 pct: None,
1484 total: 0,
1485 };
1486 let report = VitestReport {
1487 total: VitestTotals {
1488 lines: nothing,
1489 branches: nothing,
1490 functions: nothing,
1491 statements: nothing,
1492 },
1493 };
1494 let outcome = evaluate_typescript(&report, TS_MID);
1495 assert!(
1496 matches!(&outcome, Outcome::Fail(message) if message.contains("measured no code")),
1497 "got: {outcome:?}"
1498 );
1499 }
1500
1501 #[test]
1502 fn parses_a_vitest_summary_report() {
1503 let json = r#"{
1504 "total": {
1505 "lines": {"total": 5, "covered": 4, "skipped": 0, "pct": 80},
1506 "statements": {"total": 5, "covered": 4, "skipped": 0, "pct": 80},
1507 "functions": {"total": 2, "covered": 2, "skipped": 0, "pct": 100},
1508 "branches": {"total": 3, "covered": 2, "skipped": 0, "pct": 66.66},
1509 "branchesTrue": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"}
1510 },
1511 "/abs/widget.ts": {
1512 "lines": {"total": 5, "covered": 4, "skipped": 0, "pct": 80}
1513 }
1514 }"#;
1515 let report = parse_vitest_report(json).expect("valid vitest json-summary");
1516 assert_eq!(report.total.lines.pct, Some(80.0));
1518 assert_eq!(report.total.branches.pct, Some(66.66));
1519 assert_eq!(report.total.functions.total, 2);
1520 }
1521
1522 #[test]
1523 fn parses_an_unknown_pct_as_unmeasured() {
1524 let json = r#"{"total": {
1525 "lines": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"},
1526 "statements": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"},
1527 "functions": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"},
1528 "branches": {"total": 0, "covered": 0, "skipped": 0, "pct": "Unknown"}
1529 }}"#;
1530 let report = parse_vitest_report(json).expect("valid vitest json-summary");
1531 assert_eq!(report.total.lines.pct, None);
1532 assert_eq!(report.total.lines.total, 0);
1533 }
1534
1535 #[test]
1536 fn a_pct_that_is_neither_number_nor_string_is_a_parse_error() {
1537 let json = r#"{"total":{
1538 "lines": {"total": 1, "covered": 1, "skipped": 0, "pct": true},
1539 "statements": {"total": 1, "covered": 1, "skipped": 0, "pct": 100},
1540 "functions": {"total": 1, "covered": 1, "skipped": 0, "pct": 100},
1541 "branches": {"total": 1, "covered": 1, "skipped": 0, "pct": 100}
1542 }}"#;
1543 assert!(parse_vitest_report(json).is_err());
1544 }
1545
1546 fn rust_metric(percent: f64) -> LlvmCovMetric {
1547 LlvmCovMetric {
1548 count: 10,
1549 covered: 10,
1550 percent,
1551 }
1552 }
1553
1554 fn rust_report(regions: f64, lines: f64) -> LlvmCovReport {
1555 LlvmCovReport {
1556 data: vec![LlvmCovData {
1557 totals: LlvmCovTotals {
1558 regions: rust_metric(regions),
1559 lines: rust_metric(lines),
1560 functions: rust_metric(lines),
1561 branches: None,
1562 },
1563 }],
1564 }
1565 }
1566
1567 fn rust_report_full(
1570 regions: f64,
1571 lines: f64,
1572 functions: f64,
1573 branches: (u64, f64),
1574 ) -> LlvmCovReport {
1575 let (count, percent) = branches;
1576 LlvmCovReport {
1577 data: vec![LlvmCovData {
1578 totals: LlvmCovTotals {
1579 regions: rust_metric(regions),
1580 lines: rust_metric(lines),
1581 functions: rust_metric(functions),
1582 branches: Some(LlvmCovMetric {
1583 count,
1584 covered: count,
1585 percent,
1586 }),
1587 },
1588 }],
1589 }
1590 }
1591
1592 const RUST_FULL: RustThresholds = RustThresholds {
1593 regions: Some(100),
1594 lines: 100,
1595 functions: None,
1596 branch: None,
1597 };
1598 const RUST_MID: RustThresholds = RustThresholds {
1599 regions: Some(80),
1600 lines: 85,
1601 functions: None,
1602 branch: None,
1603 };
1604
1605 #[test]
1606 fn rust_functions_floor_fails_below_and_passes_at_its_bar() {
1607 let report = rust_report_full(100.0, 100.0, 66.67, (0, 0.0));
1608 let floor = |functions| RustThresholds {
1609 regions: None,
1610 lines: 50,
1611 functions: Some(functions),
1612 branch: None,
1613 };
1614 assert!(matches!(
1615 evaluate_rust(&report, floor(100)),
1616 Outcome::Fail(message) if message.contains("functions")
1617 ));
1618 assert_eq!(evaluate_rust(&report, floor(60)), Outcome::Pass);
1619 }
1620
1621 #[test]
1622 fn rust_branch_floor_fails_below_and_passes_at_its_bar() {
1623 let report = rust_report_full(100.0, 100.0, 100.0, (2, 50.0));
1624 let floor = |branch| RustThresholds {
1625 regions: None,
1626 lines: 50,
1627 functions: None,
1628 branch: Some(branch),
1629 };
1630 assert!(matches!(
1631 evaluate_rust(&report, floor(100)),
1632 Outcome::Fail(message) if message.contains("branches")
1633 ));
1634 assert_eq!(evaluate_rust(&report, floor(50)), Outcome::Pass);
1635 }
1636
1637 #[test]
1638 fn rust_a_branchless_crate_clears_any_branch_floor_vacuously() {
1639 let report = rust_report_full(100.0, 100.0, 100.0, (0, 0.0));
1640 let floor = RustThresholds {
1641 regions: None,
1642 lines: 50,
1643 functions: None,
1644 branch: Some(100),
1645 };
1646 assert_eq!(evaluate_rust(&report, floor), Outcome::Pass);
1647 }
1648
1649 #[test]
1650 fn rust_passes_when_both_metrics_meet_their_floor() {
1651 assert_eq!(
1652 evaluate_rust(&rust_report(100.0, 100.0), RUST_FULL),
1653 Outcome::Pass
1654 );
1655 }
1656
1657 #[test]
1658 fn rust_fails_on_the_one_metric_below_its_floor() {
1659 let outcome = evaluate_rust(&rust_report(70.0, 100.0), RUST_MID);
1660 assert!(
1661 matches!(&outcome, Outcome::Fail(message) if message.contains("regions") && !message.contains("lines")),
1662 "got: {outcome:?}"
1663 );
1664 }
1665
1666 #[test]
1667 fn rust_fail_message_names_every_metric_below() {
1668 let outcome = evaluate_rust(&rust_report(50.0, 50.0), RUST_MID);
1669 assert!(
1670 matches!(&outcome, Outcome::Fail(message)
1671 if message.contains("regions") && message.contains("lines")),
1672 "got: {outcome:?}"
1673 );
1674 }
1675
1676 #[test]
1677 fn rust_skips_the_region_check_when_regions_is_opt_out() {
1678 let thresholds = RustThresholds {
1679 regions: None,
1680 lines: 100,
1681 functions: None,
1682 branch: None,
1683 };
1684 assert_eq!(
1685 evaluate_rust(&rust_report(40.0, 100.0), thresholds),
1686 Outcome::Pass
1687 );
1688 }
1689
1690 #[test]
1691 fn rust_still_fails_lines_with_regions_opt_out() {
1692 let thresholds = RustThresholds {
1693 regions: None,
1694 lines: 100,
1695 functions: None,
1696 branch: None,
1697 };
1698 let outcome = evaluate_rust(&rust_report(100.0, 80.0), thresholds);
1699 assert!(
1700 matches!(&outcome, Outcome::Fail(message)
1701 if message.contains("lines") && !message.contains("regions")),
1702 "got: {outcome:?}"
1703 );
1704 }
1705
1706 #[test]
1707 fn rust_tolerates_float_noise_at_the_floor() {
1708 assert_eq!(
1709 evaluate_rust(&rust_report(99.999_999_999, 100.0), RUST_FULL),
1710 Outcome::Pass
1711 );
1712 }
1713
1714 #[test]
1715 fn rust_fails_a_vacuous_run_that_measured_no_code() {
1716 let nothing = LlvmCovMetric {
1717 count: 0,
1718 covered: 0,
1719 percent: 0.0,
1720 };
1721 let report = LlvmCovReport {
1722 data: vec![LlvmCovData {
1723 totals: LlvmCovTotals {
1724 regions: nothing,
1725 lines: nothing,
1726 functions: nothing,
1727 branches: None,
1728 },
1729 }],
1730 };
1731 let outcome = evaluate_rust(&report, RUST_MID);
1732 assert!(
1733 matches!(&outcome, Outcome::Fail(message) if message.contains("measured no code")),
1734 "got: {outcome:?}"
1735 );
1736 }
1737
1738 #[test]
1739 fn rust_fails_an_export_with_no_data() {
1740 let report = LlvmCovReport { data: vec![] };
1741 assert!(matches!(evaluate_rust(&report, RUST_MID), Outcome::Fail(_)));
1742 }
1743
1744 #[test]
1745 fn parses_a_cargo_llvm_cov_report() {
1746 let json = r#"{
1747 "data": [{"totals": {
1748 "regions": {"count": 12, "covered": 9, "notcovered": 3, "percent": 75.0},
1749 "lines": {"count": 20, "covered": 18, "percent": 90.0},
1750 "functions": {"count": 3, "covered": 3, "percent": 100.0}
1751 }}],
1752 "type": "llvm.coverage.json.export",
1753 "version": "2.0.1"
1754 }"#;
1755 let report = parse_llvm_cov_report(json).expect("valid llvm-cov json");
1756 assert_eq!(report.data[0].totals.regions.percent, 75.0);
1757 assert_eq!(report.data[0].totals.lines.count, 20);
1758 }
1759
1760 fn patch_detail(json: &str) -> BTreeMap<String, RustPatchCoverage> {
1762 llvm_cov_patch_detail(json, &BTreeMap::new()).expect("valid llvm-cov export")
1763 }
1764
1765 #[test]
1766 fn llvm_cov_patch_detail_reads_code_regions_per_file() {
1767 let json = r#"{
1768 "data": [{
1769 "files": [{"filename": "/abs/grade.rs"}],
1770 "functions": [{
1771 "filenames": ["/abs/grade.rs"],
1772 "regions": [
1773 [6, 5, 6, 26, 1, 0, 0, 0],
1774 [10, 9, 10, 17, 0, 0, 0, 0]
1775 ]
1776 }]
1777 }],
1778 "type": "llvm.coverage.json.export",
1779 "version": "3.0.1"
1780 }"#;
1781 let out = patch_detail(json);
1782 assert_eq!(
1783 out["/abs/grade.rs"].regions,
1784 vec![(6, 6, true), (10, 10, false)]
1785 );
1786 }
1787
1788 #[test]
1789 fn llvm_cov_patch_detail_skips_non_code_regions() {
1790 let json = r#"{
1791 "data": [{
1792 "files": [{"filename": "/abs/a.rs"}],
1793 "functions": [{
1794 "filenames": ["/abs/a.rs"],
1795 "regions": [
1796 [1, 1, 1, 10, 2, 0, 0, 0],
1797 [2, 1, 2, 10, 0, 0, 0, 1],
1798 [3, 1, 3, 10, 0, 0, 0, 2]
1799 ]
1800 }]
1801 }]
1802 }"#;
1803 let out = patch_detail(json);
1804 assert_eq!(out["/abs/a.rs"].regions, vec![(1, 1, true)]);
1805 }
1806
1807 #[test]
1808 fn llvm_cov_patch_detail_groups_regions_by_filename_id() {
1809 let json = r#"{
1810 "data": [{
1811 "files": [{"filename": "/abs/a.rs"}, {"filename": "/abs/b.rs"}],
1812 "functions": [{
1813 "filenames": ["/abs/a.rs", "/abs/b.rs"],
1814 "regions": [
1815 [1, 1, 1, 5, 1, 0, 0, 0],
1816 [9, 1, 9, 5, 0, 1, 1, 0]
1817 ]
1818 }]
1819 }]
1820 }"#;
1821 let out = patch_detail(json);
1822 assert_eq!(out["/abs/a.rs"].regions, vec![(1, 1, true)]);
1823 assert_eq!(out["/abs/b.rs"].regions, vec![(9, 9, false)]);
1824 }
1825
1826 #[test]
1827 fn llvm_cov_patch_detail_skips_a_malformed_short_region() {
1828 let json = r#"{
1829 "data": [{
1830 "files": [{"filename": "/abs/a.rs"}],
1831 "functions": [{
1832 "filenames": ["/abs/a.rs"],
1833 "regions": [
1834 [4, 1, 4],
1835 [5, 1, 5, 9, 1, 0, 0, 0]
1836 ]
1837 }]
1838 }]
1839 }"#;
1840 let out = patch_detail(json);
1841 assert_eq!(out["/abs/a.rs"].regions, vec![(5, 5, true)]);
1842 }
1843
1844 #[test]
1845 fn llvm_cov_patch_detail_spans_a_multiline_region() {
1846 let json = r#"{
1847 "data": [{
1848 "files": [{"filename": "/abs/a.rs"}],
1849 "functions": [{
1850 "filenames": ["/abs/a.rs"],
1851 "regions": [[3, 5, 5, 6, 0, 0, 0, 0]]
1852 }]
1853 }]
1854 }"#;
1855 let out = patch_detail(json);
1856 assert_eq!(out["/abs/a.rs"].regions, vec![(3, 5, false)]);
1857 }
1858
1859 #[test]
1860 fn llvm_cov_patch_detail_drops_a_file_absent_from_the_files_allowlist() {
1861 let json = r#"{
1862 "data": [{
1863 "files": [{"filename": "/abs/kept.rs"}],
1864 "functions": [{
1865 "filenames": ["/abs/kept.rs", "/abs/ignored.rs"],
1866 "regions": [
1867 [1, 1, 1, 9, 1, 0, 0, 0],
1868 [2, 1, 2, 9, 0, 1, 0, 0]
1869 ]
1870 }]
1871 }]
1872 }"#;
1873 let out = patch_detail(json);
1874 assert_eq!(out["/abs/kept.rs"].regions, vec![(1, 1, true)]);
1875 assert!(!out.contains_key("/abs/ignored.rs"));
1876 }
1877
1878 #[test]
1879 fn llvm_cov_patch_detail_malformed_json_is_an_error() {
1880 assert!(llvm_cov_patch_detail("{ not json", &BTreeMap::new()).is_err());
1881 }
1882
1883 #[test]
1884 fn llvm_cov_patch_detail_skips_a_negative_file_id() {
1885 let json = r#"{
1886 "data": [{
1887 "files": [{"filename": "/abs/a.rs"}],
1888 "functions": [{
1889 "filenames": ["/abs/a.rs"],
1890 "regions": [[1, 1, 1, 5, 1, -1, 0, 0]]
1891 }]
1892 }]
1893 }"#;
1894 let out = patch_detail(json);
1895 assert!(out.is_empty(), "got: {out:?}");
1896 }
1897
1898 #[test]
1899 fn llvm_cov_patch_detail_skips_an_out_of_range_file_id() {
1900 let json = r#"{
1901 "data": [{
1902 "files": [{"filename": "/abs/a.rs"}],
1903 "functions": [{
1904 "filenames": ["/abs/a.rs"],
1905 "regions": [[1, 1, 1, 5, 1, 7, 0, 0]]
1906 }]
1907 }]
1908 }"#;
1909 let out = patch_detail(json);
1910 assert!(out.is_empty(), "got: {out:?}");
1911 }
1912
1913 #[test]
1914 fn istanbul_patch_detail_reads_statements_arms_and_functions() {
1915 let json = r#"{
1916 "/abs/a.ts": {
1917 "statementMap": {"0": {"start": {"line": 1}, "end": {"line": 2}}},
1918 "s": {"0": 1},
1919 "branchMap": {"0": {"loc": {"start": {"line": 3}, "end": {"line": 3}}}},
1920 "b": {"0": [1, 0]},
1921 "fnMap": {"0": {"decl": {"start": {"line": 7}, "end": {"line": 7}}}},
1922 "f": {"0": 0}
1923 }
1924 }"#;
1925 let out = istanbul_patch_detail(json).expect("valid Istanbul report");
1926 let detail = &out["/abs/a.ts"];
1927 assert_eq!(detail.statements, vec![(1, 2, true)]);
1928 assert_eq!(detail.branch_arms, vec![(3, true), (3, false)]);
1929 assert_eq!(detail.functions, vec![(7, false)]);
1930 }
1931
1932 #[test]
1933 fn istanbul_patch_detail_keeps_a_branch_without_counts() {
1934 let json = r#"{
1935 "/abs/a.ts": {
1936 "statementMap": {},
1937 "s": {},
1938 "branchMap": {"0": {"loc": {"start": {"line": 3}, "end": {"line": 3}}}},
1939 "b": {},
1940 "fnMap": {},
1941 "f": {}
1942 }
1943 }"#;
1944 let out = istanbul_patch_detail(json).expect("valid Istanbul report");
1945 assert!(out["/abs/a.ts"].branch_arms.is_empty(), "got: {out:?}");
1946 }
1947
1948 #[test]
1949 fn default_excludes_that_are_not_json_name_the_output() {
1950 let err = parse_default_excludes(b"vitest warmed up first").unwrap_err();
1951 let msg = format!("{err:#}");
1952 assert!(msg.contains("not a JSON string array"), "got: {msg}");
1953 assert!(msg.contains("vitest warmed up first"), "got: {msg}");
1954 }
1955
1956 #[test]
1957 fn default_excludes_drop_a_nul_bearing_pattern() {
1958 let parsed = parse_default_excludes(br#"["**/dist/**", "**/\u0000*"]"#).unwrap();
1959 assert_eq!(parsed, vec!["**/dist/**".to_string()]);
1960 }
1961
1962 #[test]
1963 fn a_missing_vitest_report_names_the_reporter() {
1964 let path = std::env::temp_dir().join("tc-no-such-report/coverage-final.json");
1965 let err = read_vitest_report(&path, "json").unwrap_err();
1966 assert!(format!("{err:#}").contains("json report"), "got: {err:#}");
1967 }
1968
1969 #[test]
1970 fn rust_ignore_regex_is_none_when_nothing_is_exempt() {
1971 assert_eq!(ignore_filename_regex(Path::new("/repo"), &[]), None);
1972 }
1973
1974 #[test]
1975 fn rust_ignore_regex_anchors_each_exempt_path_to_its_full_path() {
1976 let exempt = vec!["src/shim.rs".to_string(), "src/gen.rs".to_string()];
1978 assert_eq!(
1979 ignore_filename_regex(Path::new("/repo"), &exempt).as_deref(),
1980 Some(r"/repo/src/shim\.rs$|/repo/src/gen\.rs$")
1981 );
1982 }
1983
1984 fn llvm_would_ignore(regex: &str, filename: &str) -> bool {
1987 regex.split('|').any(|alt| {
1988 let (lit, anchored) = match alt.strip_suffix('$') {
1989 Some(head) => (head, true),
1990 None => (alt, false),
1991 };
1992 let lit = lit.replace('\\', "");
1993 if anchored {
1994 filename.ends_with(&lit)
1995 } else {
1996 filename.contains(&lit)
1997 }
1998 })
1999 }
2000
2001 #[test]
2002 fn llvm_would_ignore_matches_an_unanchored_literal_anywhere() {
2003 assert!(llvm_would_ignore("/repo/src", "/repo/src/a.rs"));
2004 assert!(!llvm_would_ignore("/elsewhere", "/repo/src/a.rs"));
2005 }
2006
2007 #[test]
2008 fn rust_ignore_regex_does_not_over_match_a_member_with_the_same_suffix() {
2009 let regex = ignore_filename_regex(Path::new("/repo"), &["src/a.rs".to_string()]).unwrap();
2010 assert!(
2011 llvm_would_ignore(®ex, "/repo/src/a.rs"),
2012 "the exempted file must still be ignored: {regex}"
2013 );
2014 assert!(
2015 !llvm_would_ignore(®ex, "/repo/member/src/a.rs"),
2016 "`src/a.rs` over-matched `member/src/a.rs`: {regex}"
2017 );
2018 assert!(
2019 !llvm_would_ignore(®ex, "/repo/src/xsrc/a.rs"),
2020 "`src/a.rs` over-matched `src/xsrc/a.rs`: {regex}"
2021 );
2022 }
2023
2024 const GATED_EXPORT: &str = r#"{
2027 "data": [{
2028 "files": [{"filename": "/abs/entrypoint.rs"}],
2029 "functions": [
2030 {"name": "report", "count": 1, "filenames": ["/abs/entrypoint.rs"], "branches": [],
2031 "regions": [[11, 1, 11, 37, 1, 0, 0, 0], [12, 5, 12, 19, 1, 0, 0, 0],
2032 [12, 20, 12, 30, 1, 0, 0, 0], [13, 1, 13, 2, 1, 0, 0, 0]]},
2033 {"name": "main", "count": 0, "filenames": ["/abs/entrypoint.rs"], "branches": [],
2034 "regions": [[6, 1, 6, 26, 0, 0, 0, 0], [7, 5, 7, 11, 0, 0, 0, 0],
2035 [7, 12, 7, 46, 0, 0, 0, 0], [8, 1, 8, 2, 0, 0, 0, 0]]},
2036 {"name": "report", "count": 0, "filenames": ["/abs/entrypoint.rs"], "branches": [],
2037 "regions": [[11, 1, 11, 37, 0, 0, 0, 0], [12, 5, 12, 19, 0, 0, 0, 0],
2038 [12, 20, 12, 30, 0, 0, 0, 0], [13, 1, 13, 2, 0, 0, 0, 0]]}
2039 ],
2040 "totals": {
2041 "regions": {"count": 13, "covered": 9, "percent": 69.23},
2042 "lines": {"count": 9, "covered": 6, "percent": 66.67},
2043 "functions": {"count": 3, "covered": 2, "percent": 66.67}
2044 }
2045 }]
2046 }"#;
2047
2048 fn gated_main() -> BTreeMap<String, BTreeSet<u32>> {
2050 BTreeMap::from([(
2051 "/abs/entrypoint.rs".to_string(),
2052 BTreeSet::from([5, 6, 7, 8]),
2053 )])
2054 }
2055
2056 fn totals_less(json: &str, hidden: &BTreeMap<String, BTreeSet<u32>>) -> LlvmCovTotals {
2058 let export = parse_llvm_cov_export(json).expect("valid llvm-cov export");
2059 llvm_cov_totals_less_hidden(&export, hidden).data[0].totals
2060 }
2061
2062 #[test]
2063 fn a_gated_entry_point_leaves_every_ratio_full() {
2064 let totals = totals_less(GATED_EXPORT, &gated_main());
2065 assert_eq!((totals.regions.count, totals.regions.covered), (9, 9));
2066 assert_eq!((totals.lines.count, totals.lines.covered), (6, 6));
2067 assert_eq!((totals.functions.count, totals.functions.covered), (2, 2));
2068 assert_eq!(totals.regions.percent, 100.0);
2069 }
2070
2071 #[test]
2072 fn an_export_with_nothing_gated_keeps_its_totals() {
2073 let totals = totals_less(GATED_EXPORT, &BTreeMap::new());
2074 assert_eq!((totals.regions.count, totals.regions.covered), (13, 9));
2075 assert_eq!((totals.lines.count, totals.lines.covered), (9, 6));
2076 assert_eq!((totals.functions.count, totals.functions.covered), (3, 2));
2077 }
2078
2079 #[test]
2080 fn two_copies_of_one_gated_item_subtract_once() {
2081 let json = GATED_EXPORT.replace(
2082 r#"{"name": "report", "count": 1"#,
2083 r#"{"name": "main", "count": 0, "filenames": ["/abs/entrypoint.rs"], "branches": [],
2084 "regions": [[6, 1, 6, 26, 0, 0, 0, 0], [7, 5, 7, 11, 0, 0, 0, 0],
2085 [7, 12, 7, 46, 0, 0, 0, 0], [8, 1, 8, 2, 0, 0, 0, 0]]},
2086 {"name": "report", "count": 1"#,
2087 );
2088 let totals = totals_less(&json, &gated_main());
2089 assert_eq!((totals.regions.count, totals.regions.covered), (9, 9));
2090 assert_eq!(totals.functions.count, 2);
2091 }
2092
2093 #[test]
2094 fn a_record_with_no_region_to_place_it_is_not_subtracted() {
2095 let json = r#"{
2096 "data": [{
2097 "files": [{"filename": "/abs/a.rs"}],
2098 "functions": [{"name": "empty", "count": 0, "filenames": ["/abs/a.rs"],
2099 "regions": [], "branches": []}],
2100 "totals": {
2101 "regions": {"count": 2, "covered": 2, "percent": 100.0},
2102 "lines": {"count": 2, "covered": 2, "percent": 100.0},
2103 "functions": {"count": 1, "covered": 1, "percent": 100.0}
2104 }
2105 }]
2106 }"#;
2107 let hidden = BTreeMap::from([("/abs/a.rs".to_string(), BTreeSet::from([1]))]);
2108 assert_eq!(totals_less(json, &hidden).regions.count, 2);
2109 }
2110
2111 #[test]
2112 fn a_record_outside_the_files_allowlist_is_not_subtracted() {
2113 let hidden = BTreeMap::from([("/abs/other.rs".to_string(), BTreeSet::from([6]))]);
2114 let json = GATED_EXPORT.replace("\"/abs/entrypoint.rs\"],", "\"/abs/other.rs\"],");
2115 assert_eq!(totals_less(&json, &hidden).regions.count, 13);
2116 }
2117
2118 #[test]
2119 fn a_gated_branch_drops_both_of_its_arms() {
2120 let json = r#"{
2121 "data": [{
2122 "files": [{"filename": "/abs/a.rs"}],
2123 "functions": [{"name": "main", "count": 0, "filenames": ["/abs/a.rs"],
2124 "regions": [[1, 1, 3, 2, 0, 0, 0, 0]],
2125 "branches": [[2, 9, 2, 14, 0, 0, 0, 0, 4], [2, 9, 2, 14, 0, 0, 0, 0]]}],
2126 "totals": {
2127 "regions": {"count": 5, "covered": 4, "percent": 80.0},
2128 "lines": {"count": 9, "covered": 6, "percent": 66.67},
2129 "functions": {"count": 3, "covered": 2, "percent": 66.67},
2130 "branches": {"count": 6, "covered": 4, "percent": 66.67}
2131 }
2132 }]
2133 }"#;
2134 let hidden = BTreeMap::from([("/abs/a.rs".to_string(), BTreeSet::from([1, 2, 3]))]);
2135 let branches = totals_less(json, &hidden).branches.expect("branch totals");
2136 assert_eq!((branches.count, branches.covered), (4, 4));
2138 }
2139
2140 #[test]
2141 fn a_metric_the_subtraction_empties_reads_full() {
2142 let metric = LlvmCovMetric {
2143 count: 4,
2144 covered: 0,
2145 percent: 0.0,
2146 };
2147 let emptied = metric_less(
2148 metric,
2149 Tally {
2150 count: 9,
2151 covered: 9,
2152 },
2153 );
2154 assert_eq!((emptied.count, emptied.covered), (0, 0));
2155 assert_eq!(emptied.percent, 100.0);
2156 }
2157
2158 const TWO_GROUPS_LCOV: &str = "SF:/abs/a.rs\nDA:1,4\nDA:2,4\nDA:3,0\n\
2161 SF:/abs/a.rs\nDA:1,0\nDA:2,0\nDA:3,7\nend_of_record\n";
2162
2163 #[test]
2164 fn a_line_one_instantiation_ran_counts_once_covered() {
2165 let lines = lcov_lines_metric(TWO_GROUPS_LCOV, &BTreeMap::new());
2166 assert_eq!((lines.count, lines.covered), (3, 3));
2167 assert_eq!(lines.percent, 100.0);
2168 }
2169
2170 #[test]
2171 fn a_gated_line_leaves_the_lcov_denominator() {
2172 let hidden = BTreeMap::from([("/abs/a.rs".to_string(), BTreeSet::from([3]))]);
2173 let lines = lcov_lines_metric(TWO_GROUPS_LCOV, &hidden);
2174 assert_eq!((lines.count, lines.covered), (2, 2));
2175 }
2176
2177 #[test]
2178 fn a_line_no_instantiation_ran_is_still_missing() {
2179 let lcov = "SF:/abs/a.rs\nDA:1,4\nDA:2,0\nend_of_record\n";
2180 let lines = lcov_lines_metric(lcov, &BTreeMap::new());
2181 assert_eq!((lines.count, lines.covered), (2, 1));
2182 }
2183
2184 #[test]
2185 fn records_outside_a_source_file_are_not_lines() {
2186 let lcov = "DA:1,4\nSF:/abs/a.rs\nFN:1,main\nDA:x,4\nDA:2\nDA:3,y\nDA:4,1\nLF:4\n";
2187 assert_eq!(
2188 lcov_lines(lcov),
2189 BTreeMap::from([("/abs/a.rs".to_string(), BTreeMap::from([(4, true)]))]),
2190 "a `DA:` before any `SF:`, and a malformed one after, name no line"
2191 );
2192 }
2193
2194 #[test]
2195 fn a_failed_export_carries_both_streams() {
2196 let err = llvm_cov_stdout(false, b"out\n", b"err\n", "could not re-export:")
2197 .expect_err("a non-zero exit is an error");
2198 assert_eq!(format!("{err}"), "could not re-export:\nout\nerr\n");
2199 }
2200
2201 #[test]
2202 fn an_export_measuring_nothing_reads_full() {
2203 let lines = lcov_lines_metric("", &BTreeMap::new());
2204 assert_eq!((lines.count, lines.covered), (0, 0));
2205 assert_eq!(lines.percent, 100.0);
2206 }
2207
2208 #[test]
2209 fn a_function_record_without_a_usable_first_region_has_no_group() {
2210 let short = LlvmCovFunction {
2211 filenames: vec!["/abs/a.rs".to_string()],
2212 regions: vec![vec![1, 1, 1, 2]],
2213 count: 0,
2214 branches: Vec::new(),
2215 };
2216 let unnamed = LlvmCovFunction {
2217 filenames: Vec::new(),
2218 regions: vec![vec![1, 1, 1, 2, 0, 7, 0, 0]],
2219 count: 0,
2220 branches: Vec::new(),
2221 };
2222 let empty = LlvmCovFunction {
2223 filenames: vec!["/abs/a.rs".to_string()],
2224 regions: Vec::new(),
2225 count: 0,
2226 branches: Vec::new(),
2227 };
2228 assert_eq!(function_start(&short), None);
2229 assert_eq!(function_start(&unnamed), None);
2230 assert_eq!(function_start(&empty), None);
2231 }
2232
2233 #[test]
2234 fn a_gated_region_drops_out_of_the_changed_line_detail() {
2235 let hidden =
2236 BTreeMap::from([("/abs/entrypoint.rs".to_string(), BTreeSet::from([6, 7, 8]))]);
2237 let detail = llvm_cov_patch_detail(GATED_EXPORT, &hidden).expect("valid export");
2238 assert_eq!(
2239 detail["/abs/entrypoint.rs"]
2240 .regions
2241 .iter()
2242 .map(|(start, _, _)| *start)
2243 .collect::<BTreeSet<_>>(),
2244 BTreeSet::from([11, 12, 13])
2245 );
2246 }
2247
2248 #[test]
2249 fn hidden_lines_come_from_the_sources_the_export_measured() {
2250 let dir = std::env::temp_dir().join(format!("tc-cov-hidden-{}", std::process::id()));
2251 std::fs::create_dir_all(&dir).unwrap();
2252 let gated = dir.join("gated.rs");
2253 std::fs::write(&gated, "#[cfg(not(test))]\nfn main() {}\n").unwrap();
2254 let json = format!(
2255 r#"{{"data": [{{"files": [{{"filename": "{}"}}, {{"filename": "{}"}}],
2256 "functions": []}}]}}"#,
2257 gated.display(),
2258 dir.join("absent.rs").display(),
2259 );
2260 let hidden = hidden_lines_by_file(&json).expect("valid export");
2261 std::fs::remove_dir_all(&dir).ok();
2262 assert_eq!(
2263 hidden,
2264 BTreeMap::from([(gated.display().to_string(), BTreeSet::from([1, 2]))]),
2265 "an unreadable source hides nothing"
2266 );
2267 }
2268}