lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

coverage.rs (222779B)


      1 #![forbid(unsafe_code)]
      2 
      3 use std::ffi::OsString;
      4 use std::fs;
      5 use std::path::Component;
      6 use std::path::Path;
      7 use std::path::PathBuf;
      8 use std::process::Command;
      9 use std::{collections::BTreeMap, collections::BTreeSet, io::Write};
     10 
     11 use serde::Deserialize;
     12 use serde::Serialize;
     13 
     14 #[derive(Debug, Clone)]
     15 pub struct CoverageSummary {
     16     pub functions_percent: f64,
     17     pub summary_lines_percent: f64,
     18     pub summary_regions_percent: f64,
     19     normalized_executable_lines: Option<CoverageCount>,
     20     normalized_branches: Option<CoverageCount>,
     21 }
     22 
     23 #[derive(Debug, Clone, Copy)]
     24 struct DetailedCoverageSummary {
     25     functions_percent: f64,
     26     regions_percent: f64,
     27     executable_lines: CoverageCount,
     28     branches: CoverageCount,
     29 }
     30 
     31 #[derive(Debug, Clone, Copy)]
     32 pub enum ExecutableSource {
     33     Da,
     34     LfLh,
     35 }
     36 
     37 #[derive(Debug, Clone)]
     38 pub struct LcovCoverage {
     39     pub executable_total: u64,
     40     pub executable_covered: u64,
     41     pub executable_percent: f64,
     42     pub executable_source: ExecutableSource,
     43     pub branch_total: u64,
     44     pub branch_covered: u64,
     45     pub branches_available: bool,
     46     pub branch_percent: Option<f64>,
     47 }
     48 
     49 #[derive(Debug, Clone, Copy)]
     50 pub struct CoverageThresholds {
     51     pub fail_under_exec_lines: f64,
     52     pub fail_under_functions: f64,
     53     pub fail_under_regions: f64,
     54     pub fail_under_branches: f64,
     55     pub require_branches: bool,
     56 }
     57 
     58 #[derive(Debug, Clone)]
     59 pub struct CoverageGateResult {
     60     pub pass: bool,
     61     pub fail_reasons: Vec<String>,
     62 }
     63 
     64 #[derive(Debug, Serialize, Deserialize)]
     65 struct CoverageGateReport {
     66     scope: String,
     67     thresholds: CoverageGateReportThresholds,
     68     measured: CoverageGateReportMeasured,
     69     counts: CoverageGateReportCounts,
     70     result: CoverageGateReportResult,
     71 }
     72 
     73 #[derive(Debug, Serialize, Deserialize)]
     74 struct CoverageGateReportThresholds {
     75     executable_lines: f64,
     76     functions: f64,
     77     regions: f64,
     78     branches: f64,
     79     branches_required: bool,
     80 }
     81 
     82 #[derive(Debug, Serialize, Deserialize)]
     83 struct CoverageGateReportMeasured {
     84     executable_lines_percent: f64,
     85     executable_lines_source: String,
     86     functions_percent: f64,
     87     branches_percent: Option<f64>,
     88     branches_available: bool,
     89     summary_lines_percent: f64,
     90     summary_regions_percent: f64,
     91 }
     92 
     93 #[derive(Debug, Serialize, Deserialize)]
     94 struct CoverageGateReportCounts {
     95     executable_lines: CoverageCount,
     96     branches: CoverageCount,
     97 }
     98 
     99 #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
    100 struct CoverageCount {
    101     covered: u64,
    102     total: u64,
    103 }
    104 
    105 #[derive(Debug, Serialize, Deserialize)]
    106 struct CoverageGateReportResult {
    107     pass: bool,
    108     fail_reasons: Vec<String>,
    109 }
    110 
    111 #[derive(Debug, Deserialize)]
    112 struct LlvmCovSummaryRoot {
    113     data: Vec<LlvmCovSummaryData>,
    114 }
    115 
    116 #[derive(Debug, Deserialize)]
    117 struct LlvmCovSummaryData {
    118     totals: LlvmCovSummaryTotals,
    119 }
    120 
    121 #[derive(Debug, Deserialize)]
    122 struct LlvmCovSummaryTotals {
    123     functions: LlvmCovSummaryMetric,
    124     lines: LlvmCovSummaryMetric,
    125     regions: LlvmCovSummaryMetric,
    126 }
    127 
    128 #[derive(Debug, Deserialize)]
    129 struct LlvmCovSummaryMetric {
    130     percent: f64,
    131 }
    132 
    133 #[derive(Debug, Deserialize)]
    134 struct LlvmCovDetailsRoot {
    135     data: Vec<LlvmCovDetailsData>,
    136 }
    137 
    138 #[derive(Debug, Deserialize)]
    139 struct LlvmCovDetailsData {
    140     #[serde(default)]
    141     functions: Vec<LlvmCovFunction>,
    142 }
    143 
    144 #[derive(Debug, Deserialize)]
    145 struct LlvmCovFunction {
    146     count: u64,
    147     #[serde(default)]
    148     filenames: Vec<String>,
    149     #[serde(default)]
    150     regions: Vec<[u64; 8]>,
    151     #[serde(default)]
    152     branches: Vec<[u64; 9]>,
    153 }
    154 
    155 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
    156 struct FunctionCoverageKey {
    157     filename: String,
    158     definition: RegionCoverageKey,
    159 }
    160 
    161 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
    162 struct RegionCoverageKey {
    163     line_start: u64,
    164     column_start: u64,
    165     line_end: u64,
    166     column_end: u64,
    167     kind: u64,
    168 }
    169 
    170 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
    171 struct BranchCoverageKey {
    172     line_start: u64,
    173     column_start: u64,
    174     line_end: u64,
    175     column_end: u64,
    176     kind: u64,
    177 }
    178 
    179 #[derive(Debug)]
    180 struct CoverageSource {
    181     raw: String,
    182     cfg_test_lines: Vec<bool>,
    183     coverage_off_lines: Vec<bool>,
    184 }
    185 
    186 type CoverageSourceCache = BTreeMap<String, Option<CoverageSource>>;
    187 
    188 impl CoverageSource {
    189     fn new(raw: String) -> Self {
    190         let cfg_test_lines = cfg_test_source_lines(&raw);
    191         let coverage_off_lines = coverage_off_source_lines(&raw);
    192         Self {
    193             raw,
    194             cfg_test_lines,
    195             coverage_off_lines,
    196         }
    197     }
    198 
    199     fn is_cfg_test_line(&self, line_number: u64) -> bool {
    200         line_number
    201             .checked_sub(1)
    202             .and_then(|index| self.cfg_test_lines.get(index as usize))
    203             .copied()
    204             .unwrap_or(false)
    205     }
    206 
    207     fn is_coverage_off_line(&self, line_number: u64) -> bool {
    208         line_number
    209             .checked_sub(1)
    210             .and_then(|index| self.coverage_off_lines.get(index as usize))
    211             .copied()
    212             .unwrap_or(false)
    213     }
    214 
    215     fn is_ignorable_line(&self, line_number: u64) -> bool {
    216         self.is_cfg_test_line(line_number) || self.is_coverage_off_line(line_number)
    217     }
    218 }
    219 
    220 #[derive(Debug, Deserialize)]
    221 #[serde(deny_unknown_fields)]
    222 pub(crate) struct CoveragePolicyFile {
    223     gate: CoveragePolicyGate,
    224     required: CoverageRequiredList,
    225     #[serde(default)]
    226     overrides: BTreeMap<String, CoveragePolicyOverride>,
    227 }
    228 
    229 #[derive(Debug, Deserialize)]
    230 #[serde(deny_unknown_fields)]
    231 pub(crate) struct CoveragePolicyGate {
    232     fail_under_exec_lines: f64,
    233     fail_under_functions: f64,
    234     fail_under_regions: f64,
    235     fail_under_branches: f64,
    236     require_branches: bool,
    237 }
    238 
    239 #[derive(Debug, Deserialize)]
    240 #[serde(deny_unknown_fields)]
    241 pub(crate) struct CoverageRequiredList {
    242     crates: Vec<String>,
    243 }
    244 
    245 #[derive(Debug, Deserialize)]
    246 #[serde(deny_unknown_fields)]
    247 pub(crate) struct CoveragePolicyOverride {
    248     fail_under_exec_lines: Option<f64>,
    249     fail_under_functions: Option<f64>,
    250     fail_under_regions: Option<f64>,
    251     fail_under_branches: Option<f64>,
    252     require_branches: Option<bool>,
    253     temporary: bool,
    254     reason: String,
    255 }
    256 
    257 #[derive(Debug, Deserialize)]
    258 struct WorkspaceManifest {
    259     workspace: WorkspaceMembers,
    260 }
    261 
    262 #[derive(Debug, Deserialize)]
    263 struct WorkspaceMembers {
    264     members: Vec<String>,
    265 }
    266 
    267 #[derive(Debug, Deserialize)]
    268 struct PackageManifest {
    269     package: PackageSection,
    270 }
    271 
    272 #[derive(Debug, Deserialize)]
    273 struct PackageSection {
    274     name: String,
    275 }
    276 
    277 #[derive(Debug, Deserialize, Default)]
    278 struct CoverageProfilesFile {
    279     #[serde(default)]
    280     profiles: CoverageProfilesSection,
    281 }
    282 
    283 #[derive(Debug, Deserialize, Default)]
    284 struct CoverageProfilesSection {
    285     #[serde(default)]
    286     default: CoverageProfileRaw,
    287     #[serde(default)]
    288     crates: BTreeMap<String, CoverageProfileRaw>,
    289 }
    290 
    291 #[derive(Debug, Deserialize, Default, Clone)]
    292 struct CoverageProfileRaw {
    293     no_default_features: Option<bool>,
    294     features: Option<Vec<String>>,
    295     test_threads: Option<u32>,
    296     test_packages: Option<Vec<String>>,
    297     test_manifest: Option<String>,
    298 }
    299 
    300 #[derive(Debug, Clone)]
    301 struct CoverageProfile {
    302     no_default_features: bool,
    303     features: Vec<String>,
    304     test_threads: Option<u32>,
    305     test_packages: Vec<String>,
    306     test_manifest: Option<String>,
    307 }
    308 
    309 #[cfg_attr(not(test), allow(dead_code))]
    310 pub fn read_summary(path: &Path) -> Result<CoverageSummary, String> {
    311     read_summary_for_scope(path, None)
    312 }
    313 
    314 fn read_summary_for_scope(path: &Path, scope: Option<&str>) -> Result<CoverageSummary, String> {
    315     let raw = match fs::read_to_string(path) {
    316         Ok(raw) => raw,
    317         Err(err) => return Err(format!("failed to read summary {}: {err}", path.display())),
    318     };
    319     let parsed: LlvmCovSummaryRoot = match serde_json::from_str(&raw) {
    320         Ok(parsed) => parsed,
    321         Err(err) => return Err(format!("failed to parse summary {}: {err}", path.display())),
    322     };
    323     let totals = match parsed.data.first() {
    324         Some(entry) => &entry.totals,
    325         None => return Err(format!("summary data is empty in {}", path.display())),
    326     };
    327 
    328     let mut summary = CoverageSummary {
    329         functions_percent: totals.functions.percent,
    330         summary_lines_percent: totals.lines.percent,
    331         summary_regions_percent: totals.regions.percent,
    332         normalized_executable_lines: None,
    333         normalized_branches: None,
    334     };
    335 
    336     let details_path = coverage_details_path(path);
    337     if details_path.exists() {
    338         let normalized = read_detailed_summary(&details_path, scope)?;
    339         summary.functions_percent = normalized.functions_percent;
    340         summary.summary_regions_percent = normalized.regions_percent;
    341         summary.normalized_executable_lines = Some(normalized.executable_lines);
    342         summary.normalized_branches = Some(normalized.branches);
    343     }
    344 
    345     Ok(summary)
    346 }
    347 
    348 fn coverage_details_path(summary_path: &Path) -> PathBuf {
    349     summary_path
    350         .parent()
    351         .unwrap_or_else(|| Path::new("."))
    352         .join("coverage-details.json")
    353 }
    354 
    355 fn read_detailed_summary(
    356     path: &Path,
    357     scope: Option<&str>,
    358 ) -> Result<DetailedCoverageSummary, String> {
    359     let raw = match fs::read_to_string(path) {
    360         Ok(raw) => raw,
    361         Err(err) => {
    362             return Err(format!(
    363                 "failed to read coverage details {}: {err}",
    364                 path.display()
    365             ));
    366         }
    367     };
    368     let parsed: LlvmCovDetailsRoot = match serde_json::from_str(&raw) {
    369         Ok(parsed) => parsed,
    370         Err(err) => {
    371             return Err(format!(
    372                 "failed to parse coverage details {}: {err}",
    373                 path.display()
    374             ));
    375         }
    376     };
    377     let Some(entry) = parsed.data.first() else {
    378         return Err(format!(
    379             "coverage details data is empty in {}",
    380             path.display()
    381         ));
    382     };
    383 
    384     let mut functions_by_key: BTreeMap<FunctionCoverageKey, Vec<&LlvmCovFunction>> =
    385         BTreeMap::new();
    386     for function in &entry.functions {
    387         if function.filenames.is_empty() || function.regions.is_empty() {
    388             continue;
    389         }
    390         let region = function.regions[0];
    391         let Some(filename) = region_filename(function, &region) else {
    392             continue;
    393         };
    394         let key = FunctionCoverageKey {
    395             filename: filename.to_owned(),
    396             definition: RegionCoverageKey {
    397                 line_start: region[0],
    398                 column_start: region[1],
    399                 line_end: region[2],
    400                 column_end: region[3],
    401                 kind: region[7],
    402             },
    403         };
    404         functions_by_key.entry(key).or_default().push(function);
    405     }
    406 
    407     if functions_by_key.is_empty() {
    408         return Err(format!(
    409             "coverage details functions are empty in {}",
    410             path.display()
    411         ));
    412     }
    413 
    414     let mut all_regions = BTreeMap::<(String, RegionCoverageKey), bool>::new();
    415     let mut functions_total = 0_u64;
    416     let mut functions_covered = 0_u64;
    417     let mut executable_lines = BTreeMap::<(String, u64), bool>::new();
    418     let mut branches = BTreeMap::<(String, BranchCoverageKey), (bool, bool)>::new();
    419     let mut source_cache = CoverageSourceCache::new();
    420     let scope_filter = scope.map(scope_path_fragment);
    421     for variants in functions_by_key.values() {
    422         if let Some(scope_filter) = scope_filter.as_deref()
    423             && !variants.iter().any(|function| {
    424                 function
    425                     .filenames
    426                     .iter()
    427                     .any(|filename| coverage_filename_matches_scope(filename, scope_filter))
    428             })
    429         {
    430             continue;
    431         }
    432         let primary_definition = variants.iter().find_map(|function| {
    433             let region = function.regions.first()?;
    434             let filename = region_filename(function, region)?;
    435             if scope_filter
    436                 .as_deref()
    437                 .is_none_or(|scope_filter| coverage_filename_matches_scope(filename, scope_filter))
    438             {
    439                 Some((filename, region[0]))
    440             } else {
    441                 None
    442             }
    443         });
    444         if primary_definition.is_some_and(|(filename, _)| {
    445             is_ignorable_detail_function(filename, variants, &mut source_cache)
    446         }) {
    447             continue;
    448         }
    449         if primary_definition.is_some_and(|(filename, line)| {
    450             is_authored_function_line(filename, line, &mut source_cache)
    451         }) {
    452             functions_total = functions_total.saturating_add(1);
    453             if variants.iter().any(|function| function.count > 0) {
    454                 functions_covered = functions_covered.saturating_add(1);
    455             }
    456         }
    457         let mut group_regions: BTreeMap<(String, RegionCoverageKey), bool> = BTreeMap::new();
    458         for function in variants {
    459             for region in &function.regions {
    460                 let Some(filename) = region_filename(function, region) else {
    461                     continue;
    462                 };
    463                 if scope_filter.as_deref().is_some_and(|scope_filter| {
    464                     !coverage_filename_matches_scope(filename, scope_filter)
    465                 }) {
    466                     continue;
    467                 }
    468                 let key = RegionCoverageKey {
    469                     line_start: region[0],
    470                     column_start: region[1],
    471                     line_end: region[2],
    472                     column_end: region[3],
    473                     kind: region[7],
    474                 };
    475                 let covered = region[4] > 0;
    476                 group_regions
    477                     .entry((filename.to_owned(), key))
    478                     .and_modify(|existing| *existing |= covered)
    479                     .or_insert(covered);
    480             }
    481         }
    482         for ((filename, region), covered) in group_regions {
    483             if !covered && is_ignorable_synthetic_region(&filename, &region, &mut source_cache) {
    484                 continue;
    485             }
    486             all_regions
    487                 .entry((filename.clone(), region.clone()))
    488                 .and_modify(|existing| *existing |= covered)
    489                 .or_insert(covered);
    490             if region.kind == 0 {
    491                 for line in region.line_start..=region.line_end {
    492                     if !is_ignorable_lcov_source_line(&filename, line, &mut source_cache) {
    493                         executable_lines
    494                             .entry((filename.clone(), line))
    495                             .and_modify(|existing| *existing |= covered)
    496                             .or_insert(covered);
    497                     }
    498                 }
    499             }
    500         }
    501         for function in variants {
    502             for branch in &function.branches {
    503                 let Some(filename) = branch_filename(function, branch) else {
    504                     continue;
    505                 };
    506                 if scope_filter.as_deref().is_some_and(|scope_filter| {
    507                     !coverage_filename_matches_scope(filename, scope_filter)
    508                 }) {
    509                     continue;
    510                 }
    511                 let key = BranchCoverageKey {
    512                     line_start: branch[0],
    513                     column_start: branch[1],
    514                     line_end: branch[2],
    515                     column_end: branch[3],
    516                     kind: branch[8],
    517                 };
    518                 if is_ignorable_synthetic_branch(filename, &key, &mut source_cache) {
    519                     continue;
    520                 }
    521                 let true_covered = branch[4] > 0;
    522                 let false_covered = branch[5] > 0;
    523                 branches
    524                     .entry((filename.to_owned(), key))
    525                     .and_modify(|covered| {
    526                         covered.0 |= true_covered;
    527                         covered.1 |= false_covered;
    528                     })
    529                     .or_insert((true_covered, false_covered));
    530             }
    531         }
    532     }
    533 
    534     let executable_lines_total = executable_lines.len() as u64;
    535     let executable_lines_covered = executable_lines
    536         .values()
    537         .filter(|covered| **covered)
    538         .count() as u64;
    539     let branches_total = (branches.len() * 2) as u64;
    540     let branches_covered = branches
    541         .values()
    542         .map(|covered| u64::from(covered.0) + u64::from(covered.1))
    543         .sum();
    544     let regions_total = all_regions.len() as u64;
    545     let regions_covered = all_regions.values().filter(|covered| **covered).count() as u64;
    546 
    547     Ok(DetailedCoverageSummary {
    548         functions_percent: percentage(functions_covered, functions_total),
    549         regions_percent: percentage(regions_covered, regions_total),
    550         executable_lines: CoverageCount {
    551             covered: executable_lines_covered,
    552             total: executable_lines_total,
    553         },
    554         branches: CoverageCount {
    555             covered: branches_covered,
    556             total: branches_total,
    557         },
    558     })
    559 }
    560 
    561 fn region_filename<'a>(function: &'a LlvmCovFunction, region: &[u64; 8]) -> Option<&'a str> {
    562     function
    563         .filenames
    564         .get(region[5] as usize)
    565         .or_else(|| function.filenames.first())
    566         .map(String::as_str)
    567 }
    568 
    569 fn branch_filename<'a>(function: &'a LlvmCovFunction, branch: &[u64; 9]) -> Option<&'a str> {
    570     function
    571         .filenames
    572         .get(branch[6] as usize)
    573         .or_else(|| function.filenames.first())
    574         .map(String::as_str)
    575 }
    576 
    577 fn is_authored_function_line(
    578     filename: &str,
    579     line: u64,
    580     source_cache: &mut CoverageSourceCache,
    581 ) -> bool {
    582     let source = source_cache
    583         .entry(filename.to_string())
    584         .or_insert_with(|| fs::read_to_string(filename).ok().map(CoverageSource::new));
    585     source
    586         .as_ref()
    587         .and_then(|source| source.raw.lines().nth(line.saturating_sub(1) as usize))
    588         .is_some_and(|source_line| source_line.contains("fn "))
    589 }
    590 
    591 fn is_ignorable_synthetic_branch(
    592     filename: &str,
    593     branch: &BranchCoverageKey,
    594     source_cache: &mut CoverageSourceCache,
    595 ) -> bool {
    596     is_ignorable_synthetic_region(
    597         filename,
    598         &RegionCoverageKey {
    599             line_start: branch.line_start,
    600             column_start: branch.column_start,
    601             line_end: branch.line_end,
    602             column_end: branch.column_end,
    603             kind: branch.kind,
    604         },
    605         source_cache,
    606     )
    607 }
    608 
    609 fn is_ignorable_detail_function(
    610     filename: &str,
    611     variants: &[&LlvmCovFunction],
    612     source_cache: &mut CoverageSourceCache,
    613 ) -> bool {
    614     let source = source_cache
    615         .entry(filename.to_string())
    616         .or_insert_with(|| fs::read_to_string(filename).ok().map(CoverageSource::new));
    617     let Some(source) = source.as_ref() else {
    618         return false;
    619     };
    620     variants.iter().all(|function| {
    621         function
    622             .regions
    623             .first()
    624             .is_some_and(|region| source.is_ignorable_line(region[0]))
    625     })
    626 }
    627 
    628 fn scope_path_fragment(scope: &str) -> String {
    629     if scope == "xtask" {
    630         return "/tools/xtask/src/".to_owned();
    631     }
    632     let crate_dir = scope.strip_prefix("radroots_").unwrap_or(scope);
    633     format!("/crates/{crate_dir}/src/")
    634 }
    635 
    636 fn coverage_filename_matches_scope(filename: &str, scope_filter: &str) -> bool {
    637     let mut normalized = PathBuf::new();
    638     for component in Path::new(filename).components() {
    639         match component {
    640             Component::CurDir => {}
    641             Component::ParentDir => {
    642                 normalized.pop();
    643             }
    644             component => normalized.push(component.as_os_str()),
    645         }
    646     }
    647     normalized.to_string_lossy().contains(scope_filter)
    648 }
    649 
    650 fn percentage(covered: u64, total: u64) -> f64 {
    651     if total == 0 {
    652         100.0
    653     } else {
    654         (covered as f64 / total as f64) * 100.0
    655     }
    656 }
    657 
    658 fn is_ignorable_synthetic_region(
    659     filename: &str,
    660     region: &RegionCoverageKey,
    661     source_cache: &mut CoverageSourceCache,
    662 ) -> bool {
    663     let source = source_cache
    664         .entry(filename.to_string())
    665         .or_insert_with(|| fs::read_to_string(filename).ok().map(CoverageSource::new));
    666     let Some(source) = source.as_ref() else {
    667         return false;
    668     };
    669     if source.is_ignorable_line(region.line_start) {
    670         return true;
    671     }
    672     if region.line_start != region.line_end {
    673         return false;
    674     }
    675     let Some(line) = source
    676         .raw
    677         .lines()
    678         .nth(region.line_start.saturating_sub(1) as usize)
    679     else {
    680         return false;
    681     };
    682     let start = region.column_start.saturating_sub(1) as usize;
    683     let end = region.column_end.saturating_sub(1) as usize;
    684     let slice = line.get(start..end);
    685     if region.column_end == region.column_start + 1 && slice == Some("?") {
    686         return true;
    687     }
    688     if line.contains("unreachable!()") {
    689         return true;
    690     }
    691     if line.contains("assert!(matches!(") && slice == Some("matches!") {
    692         return true;
    693     }
    694 
    695     filename.ends_with("/tests.rs")
    696         && line.contains("panic!(\"unexpected")
    697         && matches!(slice, Some("other") | Some("panic!"))
    698 }
    699 
    700 fn is_ignorable_lcov_source_line(
    701     filename: &str,
    702     line_number: u64,
    703     source_cache: &mut CoverageSourceCache,
    704 ) -> bool {
    705     let source = source_cache
    706         .entry(filename.to_string())
    707         .or_insert_with(|| fs::read_to_string(filename).ok().map(CoverageSource::new));
    708     let Some(source) = source.as_ref() else {
    709         return false;
    710     };
    711     if source.is_ignorable_line(line_number) {
    712         return true;
    713     }
    714     let Some(line) = source
    715         .raw
    716         .lines()
    717         .nth(line_number.saturating_sub(1) as usize)
    718     else {
    719         return false;
    720     };
    721     let trimmed = line.trim();
    722     matches!(
    723         trimmed,
    724         ")?" | ")?;"
    725             | ")?)"
    726             | ")?, "
    727             | ")?,"
    728             | "})?"
    729             | "})?;"
    730             | "})?,"
    731             | "])?;"
    732             | "}"
    733             | "},"
    734             | "};"
    735     ) || line.contains("unreachable!()")
    736         || line.contains("panic!(\"expected")
    737         || line.contains("panic!(\"unexpected")
    738 }
    739 
    740 #[cfg_attr(not(test), allow(dead_code))]
    741 fn is_cfg_test_source_line(source: &str, line_number: u64) -> bool {
    742     line_number
    743         .checked_sub(1)
    744         .and_then(|index| cfg_test_source_lines(source).get(index as usize).copied())
    745         .unwrap_or(false)
    746 }
    747 
    748 fn cfg_test_source_lines(source: &str) -> Vec<bool> {
    749     let mut pending_cfg_test = false;
    750     let mut test_depth: Option<i64> = None;
    751     let mut lines = Vec::with_capacity(source.lines().count());
    752     for (line, delta) in source.lines().zip(source_brace_deltas(source)) {
    753         let trimmed = line.trim();
    754         let mut in_test = test_depth.is_some();
    755         if test_depth.is_none()
    756             && (trimmed.starts_with("#[cfg(test)]") || trimmed.starts_with("#[cfg(all(test,"))
    757         {
    758             pending_cfg_test = true;
    759             in_test = true;
    760         } else if test_depth.is_none() && pending_cfg_test {
    761             in_test = true;
    762             let is_item_content =
    763                 !trimmed.is_empty() && !trimmed.starts_with("//") && !trimmed.starts_with("#[");
    764             if is_item_content {
    765                 if delta > 0 {
    766                     test_depth = Some(0);
    767                     pending_cfg_test = false;
    768                 } else if trimmed.contains('{') || trimmed.ends_with(';') {
    769                     pending_cfg_test = false;
    770                 }
    771             }
    772         }
    773         lines.push(in_test);
    774         if let Some(depth) = test_depth.as_mut() {
    775             *depth += delta;
    776             if *depth <= 0 {
    777                 test_depth = None;
    778             }
    779         }
    780     }
    781     lines
    782 }
    783 
    784 fn coverage_off_source_lines(source: &str) -> Vec<bool> {
    785     let mut pending_coverage_off = false;
    786     let mut coverage_off_depth: Option<i64> = None;
    787     let mut lines = Vec::with_capacity(source.lines().count());
    788     for (line, delta) in source.lines().zip(source_brace_deltas(source)) {
    789         let trimmed = line.trim();
    790         let mut excluded = coverage_off_depth.is_some();
    791         if coverage_off_depth.is_none()
    792             && trimmed.contains("cfg_attr(coverage_nightly, coverage(off))")
    793         {
    794             pending_coverage_off = true;
    795             excluded = true;
    796         } else if coverage_off_depth.is_none() && pending_coverage_off {
    797             excluded = true;
    798             let is_item_content =
    799                 !trimmed.is_empty() && !trimmed.starts_with("//") && !trimmed.starts_with("#[");
    800             if is_item_content {
    801                 if delta > 0 {
    802                     coverage_off_depth = Some(0);
    803                     pending_coverage_off = false;
    804                 } else if trimmed.contains('{') || trimmed.ends_with(';') {
    805                     pending_coverage_off = false;
    806                 }
    807             }
    808         }
    809         lines.push(excluded);
    810         if let Some(depth) = coverage_off_depth.as_mut() {
    811             *depth += delta;
    812             if *depth <= 0 {
    813                 coverage_off_depth = None;
    814             }
    815         }
    816     }
    817     lines
    818 }
    819 
    820 #[derive(Clone, Copy, Debug, Default)]
    821 enum RustLexicalState {
    822     #[default]
    823     Normal,
    824     String {
    825         escaped: bool,
    826     },
    827     RawString {
    828         hashes: usize,
    829     },
    830     BlockComment {
    831         depth: usize,
    832     },
    833 }
    834 
    835 fn source_brace_deltas(source: &str) -> impl Iterator<Item = i64> + '_ {
    836     let mut state = RustLexicalState::Normal;
    837     source
    838         .lines()
    839         .map(move |line| rust_line_brace_delta(line, &mut state))
    840 }
    841 
    842 fn rust_line_brace_delta(line: &str, state: &mut RustLexicalState) -> i64 {
    843     let bytes = line.as_bytes();
    844     let mut index = 0;
    845     let mut delta = 0;
    846     while index < bytes.len() {
    847         match *state {
    848             RustLexicalState::Normal => match bytes[index] {
    849                 b'/' if bytes.get(index + 1) == Some(&b'/') => break,
    850                 b'/' if bytes.get(index + 1) == Some(&b'*') => {
    851                     *state = RustLexicalState::BlockComment { depth: 1 };
    852                     index += 2;
    853                 }
    854                 b'r' => {
    855                     if let Some((hashes, content_start)) = raw_string_start(bytes, index) {
    856                         *state = RustLexicalState::RawString { hashes };
    857                         index = content_start;
    858                     } else {
    859                         index += 1;
    860                     }
    861                 }
    862                 b'"' => {
    863                     *state = RustLexicalState::String { escaped: false };
    864                     index += 1;
    865                 }
    866                 b'\'' => {
    867                     index = char_literal_end(line, index).unwrap_or(index + 1);
    868                 }
    869                 b'{' => {
    870                     delta += 1;
    871                     index += 1;
    872                 }
    873                 b'}' => {
    874                     delta -= 1;
    875                     index += 1;
    876                 }
    877                 _ => index += 1,
    878             },
    879             RustLexicalState::String { escaped } => {
    880                 if escaped {
    881                     *state = RustLexicalState::String { escaped: false };
    882                 } else if bytes[index] == b'\\' {
    883                     *state = RustLexicalState::String { escaped: true };
    884                 } else if bytes[index] == b'"' {
    885                     *state = RustLexicalState::Normal;
    886                 }
    887                 index += 1;
    888             }
    889             RustLexicalState::RawString { hashes } => {
    890                 if bytes[index] == b'"'
    891                     && bytes
    892                         .get(index + 1..index + 1 + hashes)
    893                         .is_some_and(|suffix| suffix.iter().all(|byte| *byte == b'#'))
    894                 {
    895                     *state = RustLexicalState::Normal;
    896                     index += 1 + hashes;
    897                 } else {
    898                     index += 1;
    899                 }
    900             }
    901             RustLexicalState::BlockComment { depth } => {
    902                 if bytes[index] == b'/' && bytes.get(index + 1) == Some(&b'*') {
    903                     *state = RustLexicalState::BlockComment { depth: depth + 1 };
    904                     index += 2;
    905                 } else if bytes[index] == b'*' && bytes.get(index + 1) == Some(&b'/') {
    906                     *state = if depth == 1 {
    907                         RustLexicalState::Normal
    908                     } else {
    909                         RustLexicalState::BlockComment { depth: depth - 1 }
    910                     };
    911                     index += 2;
    912                 } else {
    913                     index += 1;
    914                 }
    915             }
    916         }
    917     }
    918     delta
    919 }
    920 
    921 fn raw_string_start(bytes: &[u8], start: usize) -> Option<(usize, usize)> {
    922     let mut index = start.checked_add(1)?;
    923     while bytes.get(index) == Some(&b'#') {
    924         index += 1;
    925     }
    926     (bytes.get(index) == Some(&b'"')).then_some((index - start - 1, index + 1))
    927 }
    928 
    929 fn char_literal_end(line: &str, start: usize) -> Option<usize> {
    930     let remainder = line.get(start + 1..)?;
    931     let mut chars = remainder.char_indices();
    932     let (_, first) = chars.next()?;
    933     let content_len = if first == '\\' {
    934         let (escape_index, escape) = chars.next()?;
    935         if escape == 'u' && remainder.as_bytes().get(escape_index + 1) == Some(&b'{') {
    936             let close = remainder.get(escape_index + 2..)?.find('}')?;
    937             escape_index + 2 + close + 1
    938         } else {
    939             escape_index + escape.len_utf8()
    940         }
    941     } else {
    942         first.len_utf8()
    943     };
    944     let closing = start + 1 + content_len;
    945     (line.as_bytes().get(closing) == Some(&b'\'')).then_some(closing + 1)
    946 }
    947 
    948 impl CoveragePolicyFile {
    949     pub(crate) fn thresholds(&self) -> CoverageThresholds {
    950         CoverageThresholds {
    951             fail_under_exec_lines: self.gate.fail_under_exec_lines,
    952             fail_under_functions: self.gate.fail_under_functions,
    953             fail_under_regions: self.gate.fail_under_regions,
    954             fail_under_branches: self.gate.fail_under_branches,
    955             require_branches: self.gate.require_branches,
    956         }
    957     }
    958 
    959     pub(crate) fn thresholds_for_scope(&self, scope: &str) -> CoverageThresholds {
    960         let base = self.thresholds();
    961         let Some(override_policy) = self.overrides.get(scope) else {
    962             return base;
    963         };
    964         CoverageThresholds {
    965             fail_under_exec_lines: override_policy
    966                 .fail_under_exec_lines
    967                 .unwrap_or(base.fail_under_exec_lines),
    968             fail_under_functions: override_policy
    969                 .fail_under_functions
    970                 .unwrap_or(base.fail_under_functions),
    971             fail_under_regions: override_policy
    972                 .fail_under_regions
    973                 .unwrap_or(base.fail_under_regions),
    974             fail_under_branches: override_policy
    975                 .fail_under_branches
    976                 .unwrap_or(base.fail_under_branches),
    977             require_branches: override_policy
    978                 .require_branches
    979                 .unwrap_or(base.require_branches),
    980         }
    981     }
    982 
    983     pub(crate) fn required_crates(&self) -> Result<Vec<String>, String> {
    984         if self.required.crates.is_empty() {
    985             return Err("coverage required crates list must not be empty".to_string());
    986         }
    987         let mut seen = BTreeSet::new();
    988         for crate_name in &self.required.crates {
    989             if crate_name.trim().is_empty() {
    990                 return Err(
    991                     "coverage required crates list includes an empty crate name".to_string()
    992                 );
    993             }
    994             if !seen.insert(crate_name.clone()) {
    995                 return Err(format!(
    996                     "coverage required crates list includes duplicate crate {crate_name}"
    997                 ));
    998             }
    999         }
   1000         Ok(self.required.crates.clone())
   1001     }
   1002 
   1003     fn validate_overrides(&self) -> Result<(), String> {
   1004         let required_crates = self.required_crates()?;
   1005         let required_set: BTreeSet<_> = required_crates.into_iter().collect();
   1006         let base = self.thresholds();
   1007         for (crate_name, override_policy) in &self.overrides {
   1008             if !required_set.contains(crate_name) {
   1009                 return Err(format!(
   1010                     "coverage override {crate_name} must target a required crate"
   1011                 ));
   1012             }
   1013             if !override_policy.temporary {
   1014                 return Err(format!(
   1015                     "coverage override {crate_name} must set temporary = true"
   1016                 ));
   1017             }
   1018             if override_policy.reason.trim().is_empty() {
   1019                 return Err(format!(
   1020                     "coverage override {crate_name} must include a non-empty reason"
   1021                 ));
   1022             }
   1023             validate_override_threshold(
   1024                 crate_name,
   1025                 "fail_under_exec_lines",
   1026                 override_policy.fail_under_exec_lines,
   1027                 base.fail_under_exec_lines,
   1028             )?;
   1029             validate_override_threshold(
   1030                 crate_name,
   1031                 "fail_under_functions",
   1032                 override_policy.fail_under_functions,
   1033                 base.fail_under_functions,
   1034             )?;
   1035             validate_override_threshold(
   1036                 crate_name,
   1037                 "fail_under_regions",
   1038                 override_policy.fail_under_regions,
   1039                 base.fail_under_regions,
   1040             )?;
   1041             validate_override_threshold(
   1042                 crate_name,
   1043                 "fail_under_branches",
   1044                 override_policy.fail_under_branches,
   1045                 base.fail_under_branches,
   1046             )?;
   1047             if override_policy.require_branches == Some(true) && !base.require_branches {
   1048                 return Err(format!(
   1049                     "coverage override {crate_name} require_branches cannot be stricter than the global gate"
   1050                 ));
   1051             }
   1052         }
   1053         Ok(())
   1054     }
   1055 
   1056     pub(crate) fn required_crate_entries(&self) -> &[String] {
   1057         &self.required.crates
   1058     }
   1059 }
   1060 
   1061 fn validate_override_threshold(
   1062     crate_name: &str,
   1063     label: &str,
   1064     value: Option<f64>,
   1065     global: f64,
   1066 ) -> Result<(), String> {
   1067     let Some(value) = value else {
   1068         return Ok(());
   1069     };
   1070     if !value.is_finite() {
   1071         return Err(format!(
   1072             "coverage override {crate_name} {label} must be finite"
   1073         ));
   1074     }
   1075     if !(0.0..=100.0).contains(&value) {
   1076         return Err(format!(
   1077             "coverage override {crate_name} {label} must be within 0..=100"
   1078         ));
   1079     }
   1080     if value > global {
   1081         return Err(format!(
   1082             "coverage override {crate_name} {label} must not exceed the global gate"
   1083         ));
   1084     }
   1085     Ok(())
   1086 }
   1087 
   1088 pub(crate) fn coverage_policy_path(root: &Path) -> PathBuf {
   1089     root.join("contracts").join("coverage.toml")
   1090 }
   1091 
   1092 pub(crate) fn read_coverage_policy(path: &Path) -> Result<CoveragePolicyFile, String> {
   1093     let raw = match fs::read_to_string(path) {
   1094         Ok(raw) => raw,
   1095         Err(err) => {
   1096             return Err(format!(
   1097                 "failed to read coverage policy {}: {err}",
   1098                 path.display()
   1099             ));
   1100         }
   1101     };
   1102     let parsed: CoveragePolicyFile = match toml::from_str(&raw) {
   1103         Ok(parsed) => parsed,
   1104         Err(err) => {
   1105             return Err(format!(
   1106                 "failed to parse coverage policy {}: {err}",
   1107                 path.display()
   1108             ));
   1109         }
   1110     };
   1111     let thresholds = parsed.thresholds();
   1112     for (label, value) in [
   1113         ("fail_under_exec_lines", thresholds.fail_under_exec_lines),
   1114         ("fail_under_functions", thresholds.fail_under_functions),
   1115         ("fail_under_regions", thresholds.fail_under_regions),
   1116         ("fail_under_branches", thresholds.fail_under_branches),
   1117     ] {
   1118         if !value.is_finite() {
   1119             return Err(format!("coverage policy {label} must be finite"));
   1120         }
   1121         if !(0.0..=100.0).contains(&value) {
   1122             return Err(format!("coverage policy {label} must be within 0..=100"));
   1123         }
   1124     }
   1125     parsed.required_crates()?;
   1126     parsed.validate_overrides()?;
   1127     Ok(parsed)
   1128 }
   1129 
   1130 fn read_required_crates(path: &Path) -> Result<Vec<String>, String> {
   1131     read_coverage_policy(path)?.required_crates()
   1132 }
   1133 
   1134 #[cfg(test)]
   1135 fn read_workspace_crates(workspace_root: &Path) -> Result<Vec<String>, String> {
   1136     let packages = read_workspace_packages(workspace_root)?;
   1137     Ok(packages.into_iter().map(|(name, _)| name).collect())
   1138 }
   1139 
   1140 fn read_workspace_packages(workspace_root: &Path) -> Result<Vec<(String, PathBuf)>, String> {
   1141     let workspace_manifest = parse_toml::<WorkspaceManifest>(&workspace_root.join("Cargo.toml"))?;
   1142     if workspace_manifest.workspace.members.is_empty() {
   1143         return Err("workspace members list must not be empty".to_string());
   1144     }
   1145     let mut packages = Vec::with_capacity(workspace_manifest.workspace.members.len());
   1146     let mut seen = BTreeSet::new();
   1147     for member in workspace_manifest.workspace.members {
   1148         let package_manifest =
   1149             parse_toml::<PackageManifest>(&workspace_root.join(&member).join("Cargo.toml"))?;
   1150         let package_name = package_manifest.package.name;
   1151         if package_name.trim().is_empty() {
   1152             return Err("workspace includes an empty package name".to_string());
   1153         }
   1154         if !seen.insert(package_name.clone()) {
   1155             return Err(format!(
   1156                 "workspace includes duplicate package name {}",
   1157                 package_name
   1158             ));
   1159         }
   1160         packages.push((package_name, PathBuf::from(member)));
   1161     }
   1162     Ok(packages)
   1163 }
   1164 
   1165 fn parse_toml<T: for<'de> Deserialize<'de>>(path: &Path) -> Result<T, String> {
   1166     let raw = match fs::read_to_string(path) {
   1167         Ok(raw) => raw,
   1168         Err(err) => return Err(format!("failed to read {}: {err}", path.display())),
   1169     };
   1170     match toml::from_str::<T>(&raw) {
   1171         Ok(parsed) => Ok(parsed),
   1172         Err(err) => Err(format!("failed to parse {}: {err}", path.display())),
   1173     }
   1174 }
   1175 
   1176 fn merge_coverage_profile(
   1177     base: CoverageProfileRaw,
   1178     overlay: CoverageProfileRaw,
   1179 ) -> CoverageProfile {
   1180     CoverageProfile {
   1181         no_default_features: overlay
   1182             .no_default_features
   1183             .unwrap_or(base.no_default_features.unwrap_or(false)),
   1184         features: overlay
   1185             .features
   1186             .unwrap_or_else(|| base.features.unwrap_or_default()),
   1187         test_threads: overlay.test_threads.or(base.test_threads),
   1188         test_packages: overlay
   1189             .test_packages
   1190             .unwrap_or_else(|| base.test_packages.unwrap_or_default()),
   1191         test_manifest: overlay.test_manifest.or(base.test_manifest),
   1192     }
   1193 }
   1194 
   1195 fn read_coverage_profile(
   1196     workspace_root: &Path,
   1197     crate_name: &str,
   1198 ) -> Result<CoverageProfile, String> {
   1199     let path = workspace_root
   1200         .join("contracts")
   1201         .join("coverage-profiles.toml");
   1202     if !path.exists() {
   1203         return Ok(CoverageProfile {
   1204             no_default_features: false,
   1205             features: Vec::new(),
   1206             test_threads: None,
   1207             test_packages: Vec::new(),
   1208             test_manifest: None,
   1209         });
   1210     }
   1211     let parsed = parse_toml::<CoverageProfilesFile>(&path)?;
   1212     let base = parsed.profiles.default;
   1213     let overlay = parsed
   1214         .profiles
   1215         .crates
   1216         .get(crate_name)
   1217         .cloned()
   1218         .unwrap_or_default();
   1219     let resolved = merge_coverage_profile(base, overlay);
   1220     if resolved
   1221         .features
   1222         .iter()
   1223         .any(|feature| feature.trim().is_empty())
   1224     {
   1225         return Err(format!(
   1226             "coverage profile for {crate_name} includes an empty feature value"
   1227         ));
   1228     }
   1229     if resolved.test_threads == Some(0) {
   1230         return Err(format!(
   1231             "coverage profile for {crate_name} must set test_threads > 0"
   1232         ));
   1233     }
   1234     let fixture = coverage_fixture_manifest(
   1235         workspace_root,
   1236         crate_name,
   1237         resolved.test_manifest.as_deref(),
   1238     )?;
   1239     let workspace_packages = if let Some(manifest) = fixture {
   1240         let parsed: PackageManifest = parse_toml(&manifest)?;
   1241         BTreeSet::from([parsed.package.name])
   1242     } else if resolved.test_packages.is_empty() {
   1243         BTreeSet::new()
   1244     } else {
   1245         read_workspace_packages(workspace_root)?
   1246             .into_iter()
   1247             .map(|(name, _)| name)
   1248             .collect::<BTreeSet<_>>()
   1249     };
   1250     let mut seen_test_packages = BTreeSet::new();
   1251     for package in &resolved.test_packages {
   1252         if package.trim().is_empty() {
   1253             return Err(format!(
   1254                 "coverage profile for {crate_name} includes an empty test package"
   1255             ));
   1256         }
   1257         if package == crate_name {
   1258             return Err(format!(
   1259                 "coverage profile for {crate_name} repeats the target as a test package"
   1260             ));
   1261         }
   1262         if !workspace_packages.contains(package) {
   1263             return Err(format!(
   1264                 "coverage profile for {crate_name} references unknown test package {package}"
   1265             ));
   1266         }
   1267         if !seen_test_packages.insert(package) {
   1268             return Err(format!(
   1269                 "coverage profile for {crate_name} repeats test package {package}"
   1270             ));
   1271         }
   1272     }
   1273     Ok(resolved)
   1274 }
   1275 
   1276 fn coverage_fixture_manifest(
   1277     root: &Path,
   1278     crate_name: &str,
   1279     relative: Option<&str>,
   1280 ) -> Result<Option<PathBuf>, String> {
   1281     let Some(relative) = relative else {
   1282         return Ok(None);
   1283     };
   1284     let package = read_workspace_packages(root)?
   1285         .into_iter()
   1286         .find(|(name, _)| name == crate_name)
   1287         .ok_or_else(|| format!("coverage fixture target {crate_name} is not a workspace package"))?
   1288         .1;
   1289     let path = Path::new(relative);
   1290     if !path
   1291         .components()
   1292         .all(|component| matches!(component, std::path::Component::Normal(_)))
   1293         || path.file_name().is_none_or(|name| name != "Cargo.toml")
   1294         || !path.starts_with(package.join("tests"))
   1295     {
   1296         return Err(
   1297             "coverage fixture manifest must be below the target package tests directory".to_owned(),
   1298         );
   1299     }
   1300     let canonical_root = root
   1301         .canonicalize()
   1302         .map_err(|error| format!("resolve coverage root: {error}"))?;
   1303     let manifest = root
   1304         .join(path)
   1305         .canonicalize()
   1306         .map_err(|error| format!("resolve coverage fixture: {error}"))?;
   1307     if manifest != canonical_root.join(path) || !manifest.is_file() {
   1308         return Err(
   1309             "coverage fixture manifest must be a regular non-symlink source path".to_owned(),
   1310         );
   1311     }
   1312     Ok(Some(manifest))
   1313 }
   1314 
   1315 pub fn read_lcov(path: &Path) -> Result<LcovCoverage, String> {
   1316     let raw = match fs::read_to_string(path) {
   1317         Ok(raw) => raw,
   1318         Err(err) => return Err(format!("failed to read lcov {}: {err}", path.display())),
   1319     };
   1320 
   1321     let mut current_filename: Option<String> = None;
   1322     let mut source_cache = CoverageSourceCache::new();
   1323     let mut da_total: u64 = 0;
   1324     let mut da_covered: u64 = 0;
   1325     let mut executable_total: u64 = 0;
   1326     let mut executable_covered: u64 = 0;
   1327     let mut branch_total_lcov: u64 = 0;
   1328     let mut branch_covered_lcov: u64 = 0;
   1329     let mut branch_total_brda: u64 = 0;
   1330     let mut branch_covered_brda: u64 = 0;
   1331 
   1332     for line in raw.lines() {
   1333         if let Some(filename) = line.strip_prefix("SF:") {
   1334             current_filename = Some(filename.to_string());
   1335             continue;
   1336         }
   1337         if let Some(value) = line.strip_prefix("DA:") {
   1338             let Some((line_number, hit)) = value.split_once(',') else {
   1339                 return Err(format!("invalid DA record in {}", path.display()));
   1340             };
   1341             let line_number = match line_number.parse::<u64>() {
   1342                 Ok(line_number) => line_number,
   1343                 Err(err) => {
   1344                     return Err(format!(
   1345                         "invalid DA line number `{line_number}` in {}: {err}",
   1346                         path.display()
   1347                     ));
   1348                 }
   1349             };
   1350             let hit_count: u64 = match hit.parse() {
   1351                 Ok(hit_count) => hit_count,
   1352                 Err(err) => {
   1353                     return Err(format!(
   1354                         "invalid DA hit count `{hit}` in {}: {err}",
   1355                         path.display()
   1356                     ));
   1357                 }
   1358             };
   1359             if current_filename.as_deref().is_some_and(|filename| {
   1360                 is_ignorable_lcov_source_line(filename, line_number, &mut source_cache)
   1361             }) {
   1362                 continue;
   1363             }
   1364             da_total = da_total.saturating_add(1);
   1365             if hit_count > 0 {
   1366                 da_covered = da_covered.saturating_add(1);
   1367             }
   1368             continue;
   1369         }
   1370         if let Some(value) = line.strip_prefix("LF:") {
   1371             let parsed: u64 = match value.parse() {
   1372                 Ok(parsed) => parsed,
   1373                 Err(err) => {
   1374                     return Err(format!(
   1375                         "invalid LF value `{value}` in {}: {err}",
   1376                         path.display()
   1377                     ));
   1378                 }
   1379             };
   1380             executable_total = executable_total.saturating_add(parsed);
   1381             continue;
   1382         }
   1383         if let Some(value) = line.strip_prefix("LH:") {
   1384             let parsed: u64 = match value.parse() {
   1385                 Ok(parsed) => parsed,
   1386                 Err(err) => {
   1387                     return Err(format!(
   1388                         "invalid LH value `{value}` in {}: {err}",
   1389                         path.display()
   1390                     ));
   1391                 }
   1392             };
   1393             executable_covered = executable_covered.saturating_add(parsed);
   1394             continue;
   1395         }
   1396         if let Some(value) = line.strip_prefix("BRF:") {
   1397             let parsed: u64 = match value.parse() {
   1398                 Ok(parsed) => parsed,
   1399                 Err(err) => {
   1400                     return Err(format!(
   1401                         "invalid BRF value `{value}` in {}: {err}",
   1402                         path.display()
   1403                     ));
   1404                 }
   1405             };
   1406             branch_total_lcov = branch_total_lcov.saturating_add(parsed);
   1407             continue;
   1408         }
   1409         if let Some(value) = line.strip_prefix("BRH:") {
   1410             let parsed: u64 = match value.parse() {
   1411                 Ok(parsed) => parsed,
   1412                 Err(err) => {
   1413                     return Err(format!(
   1414                         "invalid BRH value `{value}` in {}: {err}",
   1415                         path.display()
   1416                     ));
   1417                 }
   1418             };
   1419             branch_covered_lcov = branch_covered_lcov.saturating_add(parsed);
   1420             continue;
   1421         }
   1422         if let Some(value) = line.strip_prefix("BRDA:") {
   1423             let fields = value.split(',').collect::<Vec<_>>();
   1424             if fields.len() != 4 {
   1425                 return Err(format!("invalid BRDA record in {}", path.display()));
   1426             }
   1427             let line_number = match fields[0].parse::<u64>() {
   1428                 Ok(line_number) => line_number,
   1429                 Err(err) => {
   1430                     return Err(format!(
   1431                         "invalid BRDA line number `{}` in {}: {err}",
   1432                         fields[0],
   1433                         path.display()
   1434                     ));
   1435                 }
   1436             };
   1437             if current_filename.as_deref().is_some_and(|filename| {
   1438                 is_ignorable_lcov_source_line(filename, line_number, &mut source_cache)
   1439             }) {
   1440                 continue;
   1441             }
   1442             let taken = fields[3];
   1443             if taken == "-" {
   1444                 continue;
   1445             }
   1446             let hit_count: u64 = match taken.parse() {
   1447                 Ok(hit_count) => hit_count,
   1448                 Err(err) => {
   1449                     return Err(format!(
   1450                         "invalid BRDA taken count `{taken}` in {}: {err}",
   1451                         path.display()
   1452                     ));
   1453                 }
   1454             };
   1455             branch_total_brda = branch_total_brda.saturating_add(1);
   1456             if hit_count > 0 {
   1457                 branch_covered_brda = branch_covered_brda.saturating_add(1);
   1458             }
   1459         }
   1460     }
   1461 
   1462     let mut executable_source = ExecutableSource::Da;
   1463     let mut executable_percent = 100.0_f64;
   1464 
   1465     if da_total > 0 {
   1466         executable_total = da_total;
   1467         executable_covered = da_covered;
   1468         executable_percent = (da_covered as f64 / da_total as f64) * 100.0_f64;
   1469     } else if executable_total > 0 {
   1470         executable_source = ExecutableSource::LfLh;
   1471         executable_percent = (executable_covered as f64 / executable_total as f64) * 100.0_f64;
   1472     }
   1473 
   1474     let (branch_total, branch_covered) = if branch_total_brda > 0 {
   1475         (branch_total_brda, branch_covered_brda)
   1476     } else {
   1477         (branch_total_lcov, branch_covered_lcov)
   1478     };
   1479     let branches_available = branch_total > 0;
   1480     let branch_percent = if branches_available {
   1481         Some((branch_covered as f64 / branch_total as f64) * 100.0_f64)
   1482     } else {
   1483         None
   1484     };
   1485 
   1486     Ok(LcovCoverage {
   1487         executable_total,
   1488         executable_covered,
   1489         executable_percent,
   1490         executable_source,
   1491         branch_total,
   1492         branch_covered,
   1493         branches_available,
   1494         branch_percent,
   1495     })
   1496 }
   1497 
   1498 pub fn evaluate_gate(
   1499     summary: &CoverageSummary,
   1500     lcov: &LcovCoverage,
   1501     thresholds: CoverageThresholds,
   1502 ) -> CoverageGateResult {
   1503     let exec_ok = lcov.executable_percent >= thresholds.fail_under_exec_lines;
   1504     let functions_ok = summary.functions_percent >= thresholds.fail_under_functions;
   1505     let regions_ok = summary.summary_regions_percent >= thresholds.fail_under_regions;
   1506     let branch_presence_ok = !thresholds.require_branches || lcov.branches_available;
   1507     let branch_ok = lcov
   1508         .branch_percent
   1509         .is_none_or(|branch_percent| branch_percent >= thresholds.fail_under_branches);
   1510 
   1511     let pass = [
   1512         exec_ok,
   1513         functions_ok,
   1514         regions_ok,
   1515         branch_presence_ok,
   1516         branch_ok,
   1517     ]
   1518     .into_iter()
   1519     .all(|flag| flag);
   1520     let mut fail_reasons: Vec<String> = Vec::new();
   1521 
   1522     if !exec_ok {
   1523         fail_reasons.push(format!(
   1524             "executable_lines={:.6} < {:.6}",
   1525             lcov.executable_percent, thresholds.fail_under_exec_lines
   1526         ));
   1527     }
   1528 
   1529     if !functions_ok {
   1530         fail_reasons.push(format!(
   1531             "functions={:.6} < {:.6}",
   1532             summary.functions_percent, thresholds.fail_under_functions
   1533         ));
   1534     }
   1535 
   1536     if !regions_ok {
   1537         fail_reasons.push(format!(
   1538             "regions={:.6} < {:.6}",
   1539             summary.summary_regions_percent, thresholds.fail_under_regions
   1540         ));
   1541     }
   1542 
   1543     if thresholds.require_branches && !lcov.branches_available {
   1544         fail_reasons.push("branches=unavailable".to_string());
   1545     }
   1546 
   1547     if lcov.branches_available && !branch_ok {
   1548         fail_reasons.push(format!(
   1549             "branches={:.6} < {:.6}",
   1550             lcov.branch_percent.unwrap_or(0.0),
   1551             thresholds.fail_under_branches
   1552         ));
   1553     }
   1554 
   1555     CoverageGateResult { pass, fail_reasons }
   1556 }
   1557 
   1558 fn executable_source_label(source: ExecutableSource) -> &'static str {
   1559     match source {
   1560         ExecutableSource::Da => "da",
   1561         ExecutableSource::LfLh => "lf_lh",
   1562     }
   1563 }
   1564 
   1565 fn parse_string_arg(args: &[String], name: &str) -> Result<String, String> {
   1566     let flag = format!("--{name}");
   1567     let mut index = 0usize;
   1568     while index < args.len() {
   1569         if args[index] == flag {
   1570             let Some(value) = args.get(index + 1) else {
   1571                 return Err(format!("missing value for --{name}"));
   1572             };
   1573             return Ok(value.clone());
   1574         }
   1575         index += 1;
   1576     }
   1577     Err(format!("missing --{name}"))
   1578 }
   1579 
   1580 fn parse_optional_string_arg(args: &[String], name: &str) -> Option<String> {
   1581     let flag = format!("--{name}");
   1582     let mut index = 0usize;
   1583     while index < args.len() {
   1584         if args[index] == flag {
   1585             return args.get(index + 1).cloned();
   1586         }
   1587         index += 1;
   1588     }
   1589     None
   1590 }
   1591 
   1592 fn parse_optional_f64_arg(args: &[String], name: &str) -> Result<Option<f64>, String> {
   1593     if let Some(raw) = parse_optional_string_arg(args, name) {
   1594         let parsed = raw
   1595             .parse::<f64>()
   1596             .map_err(|err| format!("invalid --{name} value `{raw}`: {err}"))?;
   1597         if !parsed.is_finite() {
   1598             return Err(format!("invalid --{name} value `{raw}`: must be finite"));
   1599         }
   1600         return Ok(Some(parsed));
   1601     }
   1602     Ok(None)
   1603 }
   1604 
   1605 #[cfg_attr(not(test), allow(dead_code))]
   1606 fn parse_f64_arg(args: &[String], name: &str, default: f64) -> Result<f64, String> {
   1607     if let Some(raw) = parse_optional_string_arg(args, name) {
   1608         return raw
   1609             .parse::<f64>()
   1610             .map_err(|err| format!("invalid --{name} value `{raw}`: {err}"));
   1611     }
   1612     Ok(default)
   1613 }
   1614 
   1615 fn parse_optional_u32_arg(args: &[String], name: &str) -> Result<Option<u32>, String> {
   1616     if let Some(raw) = parse_optional_string_arg(args, name) {
   1617         let parsed = raw
   1618             .parse::<u32>()
   1619             .map_err(|err| format!("invalid --{name} value `{raw}`: {err}"))?;
   1620         return Ok(Some(parsed));
   1621     }
   1622     Ok(None)
   1623 }
   1624 
   1625 fn parse_bool_flag(args: &[String], name: &str) -> bool {
   1626     let flag = format!("--{name}");
   1627     args.iter().any(|arg| arg == &flag)
   1628 }
   1629 
   1630 fn has_flag(args: &[String], name: &str) -> bool {
   1631     let flag = format!("--{name}");
   1632     args.iter().any(|arg| arg == &flag)
   1633 }
   1634 
   1635 fn workspace_root_with_override(override_root: Option<&str>) -> PathBuf {
   1636     if let Some(raw) = override_root {
   1637         let trimmed = raw.trim();
   1638         if !trimmed.is_empty() {
   1639             return PathBuf::from(trimmed);
   1640         }
   1641     }
   1642     let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
   1643     let crates_dir = manifest_dir.parent().unwrap_or(manifest_dir);
   1644     let root = crates_dir.parent().unwrap_or(crates_dir);
   1645     root.to_path_buf()
   1646 }
   1647 
   1648 fn workspace_root() -> PathBuf {
   1649     let override_root = std::env::var("RADROOTS_WORKSPACE_ROOT").ok();
   1650     workspace_root_with_override(override_root.as_deref())
   1651 }
   1652 
   1653 fn run_command(mut command: Command, name: &str) -> Result<(), String> {
   1654     let status = match command.status() {
   1655         Ok(status) => status,
   1656         Err(err) => return Err(format!("failed to run {name}: {err}")),
   1657     };
   1658     if !status.success() {
   1659         return Err(format!("{name} failed with status {status}"));
   1660     }
   1661     Ok(())
   1662 }
   1663 
   1664 fn apply_coverage_profile_flags(command: &mut Command, profile: &CoverageProfile) {
   1665     if profile.no_default_features {
   1666         command.arg("--no-default-features");
   1667     }
   1668     if !profile.features.is_empty() {
   1669         command.arg("--features").arg(profile.features.join(","));
   1670     }
   1671 }
   1672 
   1673 fn prepend_toolchain_bin_to_path(
   1674     toolchain_bin: &Path,
   1675     existing_path: Option<OsString>,
   1676 ) -> OsString {
   1677     match existing_path {
   1678         Some(existing) => std::env::join_paths(
   1679             std::iter::once(toolchain_bin.to_path_buf()).chain(std::env::split_paths(&existing)),
   1680         )
   1681         .expect("joining PATH entries for coverage toolchain should succeed"),
   1682         None => OsString::from(toolchain_bin),
   1683     }
   1684 }
   1685 
   1686 fn configure_coverage_toolchain_env(command: &mut Command, toolchain_bin: &Path) {
   1687     let joined_path = prepend_toolchain_bin_to_path(toolchain_bin, std::env::var_os("PATH"));
   1688     command.env("PATH", joined_path);
   1689 
   1690     for (env_name, binary_name) in [
   1691         ("RUSTC", "rustc"),
   1692         ("RUSTDOC", "rustdoc"),
   1693         ("LLVM_COV", "llvm-cov"),
   1694         ("LLVM_PROFDATA", "llvm-profdata"),
   1695     ] {
   1696         let binary_path = toolchain_bin.join(binary_name);
   1697         if binary_path.exists() {
   1698             command.env(env_name, binary_path);
   1699         }
   1700     }
   1701 }
   1702 
   1703 fn coverage_cargo_command_with_override(override_binary: Option<&str>) -> Command {
   1704     if let Some(binary) = override_binary {
   1705         let mut cmd = Command::new(binary);
   1706         if let Some(toolchain_bin) = Path::new(binary).parent() {
   1707             configure_coverage_toolchain_env(&mut cmd, toolchain_bin);
   1708         }
   1709         return cmd;
   1710     }
   1711 
   1712     let mut cmd = Command::new("rustup");
   1713     cmd.arg("run").arg("nightly").arg("cargo");
   1714     cmd
   1715 }
   1716 
   1717 fn normalized_coverage_cargo_override(raw: Option<String>) -> Option<String> {
   1718     raw.map(|raw| raw.trim().to_string())
   1719         .filter(|raw| !raw.is_empty())
   1720 }
   1721 
   1722 fn coverage_cargo_command() -> Command {
   1723     let override_binary =
   1724         normalized_coverage_cargo_override(std::env::var("RADROOTS_COVERAGE_CARGO").ok());
   1725     coverage_cargo_command_with_override(override_binary.as_deref())
   1726 }
   1727 
   1728 fn coverage_llvm_cov_command() -> Command {
   1729     let mut cmd = coverage_cargo_command();
   1730     cmd.arg("llvm-cov");
   1731     cmd
   1732 }
   1733 
   1734 const COVERAGE_EXTERNAL_IGNORE_FILENAME_REGEX: &str =
   1735     r"(/\.cargo/registry/|/lib/rustlib/src/rust/)";
   1736 
   1737 fn escape_regex_literal(raw: &str) -> String {
   1738     let mut escaped = String::with_capacity(raw.len());
   1739     for ch in raw.chars() {
   1740         match ch {
   1741             '\\' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' => {
   1742                 escaped.push('\\');
   1743                 escaped.push(ch);
   1744             }
   1745             _ => escaped.push(ch),
   1746         }
   1747     }
   1748     escaped
   1749 }
   1750 
   1751 fn coverage_ignore_filename_regex(
   1752     workspace_root: &Path,
   1753     crate_name: &str,
   1754 ) -> Result<String, String> {
   1755     let mut patterns = vec![COVERAGE_EXTERNAL_IGNORE_FILENAME_REGEX.to_string()];
   1756     let mut found_target = false;
   1757 
   1758     for (package_name, member_path) in read_workspace_packages(workspace_root)? {
   1759         let absolute_member = workspace_root.join(member_path);
   1760         if package_name == crate_name {
   1761             found_target = true;
   1762             patterns.push(format!(
   1763                 "^{}/([^/]+/)*tests/",
   1764                 escape_regex_literal(&absolute_member.display().to_string())
   1765             ));
   1766             continue;
   1767         }
   1768         patterns.push(format!(
   1769             "^{}/",
   1770             escape_regex_literal(&absolute_member.display().to_string())
   1771         ));
   1772     }
   1773 
   1774     if !found_target {
   1775         return Err(format!(
   1776             "workspace coverage filters could not resolve crate directory for {crate_name}"
   1777         ));
   1778     }
   1779 
   1780     Ok(format!("({})", patterns.join("|")))
   1781 }
   1782 
   1783 fn apply_coverage_report_filters(command: &mut Command, ignore_regex: &str) {
   1784     command.arg("--ignore-filename-regex").arg(ignore_regex);
   1785 }
   1786 
   1787 fn run_crate_with_runner_at_root(
   1788     args: &[String],
   1789     workspace_root: &Path,
   1790     runner: &mut dyn FnMut(Command, &str) -> Result<(), String>,
   1791 ) -> Result<(), String> {
   1792     let crate_name = parse_string_arg(args, "crate")?;
   1793     let profile = read_coverage_profile(workspace_root, &crate_name)?;
   1794     let fixture = coverage_fixture_manifest(
   1795         workspace_root,
   1796         &crate_name,
   1797         profile.test_manifest.as_deref(),
   1798     )?;
   1799     let test_root = fixture
   1800         .as_deref()
   1801         .and_then(Path::parent)
   1802         .unwrap_or(workspace_root);
   1803     let out_dir = if let Some(raw) = parse_optional_string_arg(args, "out") {
   1804         PathBuf::from(raw)
   1805     } else {
   1806         workspace_root
   1807             .join("target")
   1808             .join("coverage")
   1809             .join(crate_name.replace('-', "_"))
   1810     };
   1811     let test_threads = parse_optional_u32_arg(args, "test-threads")?
   1812         .or(profile.test_threads)
   1813         .unwrap_or(1);
   1814     let ignore_regex = coverage_ignore_filename_regex(workspace_root, &crate_name)?;
   1815 
   1816     if let Err(err) = fs::create_dir_all(&out_dir) {
   1817         return Err(format!("failed to create {}: {err}", out_dir.display()));
   1818     }
   1819 
   1820     runner(
   1821         {
   1822             let mut cmd = coverage_llvm_cov_command();
   1823             // Package cleanup leaves orphan executables under build output
   1824             // directories. LLVM also reads those coverage maps, so an earlier
   1825             // source revision can contaminate the current report even when its
   1826             // profile has been removed. Reset the owned coverage build tree.
   1827             cmd.arg("clean").current_dir(workspace_root);
   1828             cmd
   1829         },
   1830         "cargo llvm-cov clean",
   1831     )?;
   1832 
   1833     runner(
   1834         {
   1835             let mut cmd = coverage_llvm_cov_command();
   1836             cmd.arg("-p").arg(&crate_name);
   1837             if fixture.is_some() {
   1838                 cmd.arg("--dep-coverage").arg(&crate_name);
   1839             }
   1840             for package in &profile.test_packages {
   1841                 cmd.arg("-p").arg(package);
   1842             }
   1843             apply_coverage_profile_flags(&mut cmd, &profile);
   1844             cmd.arg("--no-report")
   1845                 .arg("--branch")
   1846                 .arg("--")
   1847                 .arg(format!("--test-threads={test_threads}"))
   1848                 .current_dir(test_root);
   1849             cmd
   1850         },
   1851         "cargo llvm-cov --no-report",
   1852     )?;
   1853 
   1854     let summary_path = out_dir.join("coverage-summary.json");
   1855     runner(
   1856         {
   1857             let mut cmd = coverage_llvm_cov_command();
   1858             cmd.arg("report").arg("-p").arg(&crate_name);
   1859             apply_coverage_report_filters(&mut cmd, &ignore_regex);
   1860             cmd.arg("--json")
   1861                 .arg("--summary-only")
   1862                 .arg("--branch")
   1863                 .arg("--output-path")
   1864                 .arg(&summary_path)
   1865                 .current_dir(workspace_root);
   1866             cmd
   1867         },
   1868         "cargo llvm-cov report --json --summary-only",
   1869     )?;
   1870 
   1871     let details_path = out_dir.join("coverage-details.json");
   1872     runner(
   1873         {
   1874             let mut cmd = coverage_llvm_cov_command();
   1875             cmd.arg("report").arg("-p").arg(&crate_name);
   1876             apply_coverage_report_filters(&mut cmd, &ignore_regex);
   1877             cmd.arg("--json")
   1878                 .arg("--branch")
   1879                 .arg("--output-path")
   1880                 .arg(&details_path)
   1881                 .current_dir(workspace_root);
   1882             cmd
   1883         },
   1884         "cargo llvm-cov report --json",
   1885     )?;
   1886 
   1887     let lcov_path = out_dir.join("coverage-lcov.info");
   1888     runner(
   1889         {
   1890             let mut cmd = coverage_llvm_cov_command();
   1891             cmd.arg("report").arg("-p").arg(&crate_name);
   1892             apply_coverage_report_filters(&mut cmd, &ignore_regex);
   1893             cmd.arg("--lcov")
   1894                 .arg("--branch")
   1895                 .arg("--output-path")
   1896                 .arg(&lcov_path)
   1897                 .current_dir(workspace_root);
   1898             cmd
   1899         },
   1900         "cargo llvm-cov report --lcov",
   1901     )?;
   1902 
   1903     eprintln!("coverage summary: {}", summary_path.display());
   1904     eprintln!("coverage details: {}", details_path.display());
   1905     eprintln!("coverage lcov: {}", lcov_path.display());
   1906     Ok(())
   1907 }
   1908 
   1909 fn run_crate_with_runner(
   1910     args: &[String],
   1911     runner: &mut dyn FnMut(Command, &str) -> Result<(), String>,
   1912 ) -> Result<(), String> {
   1913     let root = workspace_root();
   1914     run_crate_with_runner_at_root(args, &root, runner)
   1915 }
   1916 
   1917 fn run_crate(args: &[String]) -> Result<(), String> {
   1918     let mut runner = run_command;
   1919     run_crate_with_runner(args, &mut runner)
   1920 }
   1921 
   1922 fn report_gate_with_root(args: &[String], root: &Path) -> Result<(), String> {
   1923     let scope = parse_string_arg(args, "scope")?;
   1924     let summary_path = PathBuf::from(parse_string_arg(args, "summary")?);
   1925     let lcov_path = PathBuf::from(parse_string_arg(args, "lcov")?);
   1926     let out_path = PathBuf::from(parse_string_arg(args, "out")?);
   1927     let policy_gate = parse_bool_flag(args, "policy-gate");
   1928     let explicit_exec = parse_optional_f64_arg(args, "fail-under-exec-lines")?;
   1929     let explicit_functions = parse_optional_f64_arg(args, "fail-under-functions")?;
   1930     let explicit_regions = parse_optional_f64_arg(args, "fail-under-regions")?;
   1931     let explicit_branches = parse_optional_f64_arg(args, "fail-under-branches")?;
   1932     let explicit_require_branches = has_flag(args, "require-branches");
   1933     let any_explicit_threshold = explicit_exec.is_some()
   1934         || explicit_functions.is_some()
   1935         || explicit_regions.is_some()
   1936         || explicit_branches.is_some();
   1937     let thresholds = if policy_gate {
   1938         if any_explicit_threshold || explicit_require_branches {
   1939             return Err(
   1940                 "--policy-gate cannot be combined with explicit threshold or branch flags"
   1941                     .to_string(),
   1942             );
   1943         }
   1944         let policy = read_coverage_policy(&coverage_policy_path(root))?;
   1945         policy.thresholds_for_scope(&scope)
   1946     } else {
   1947         let Some(fail_under_exec_lines) = explicit_exec else {
   1948             return Err(
   1949                 "missing coverage thresholds; pass --policy-gate or explicit --fail-under-* values"
   1950                     .to_string(),
   1951             );
   1952         };
   1953         let Some(fail_under_functions) = explicit_functions else {
   1954             return Err(
   1955                 "missing coverage thresholds; pass --policy-gate or explicit --fail-under-* values"
   1956                     .to_string(),
   1957             );
   1958         };
   1959         let Some(fail_under_regions) = explicit_regions else {
   1960             return Err(
   1961                 "missing coverage thresholds; pass --policy-gate or explicit --fail-under-* values"
   1962                     .to_string(),
   1963             );
   1964         };
   1965         let Some(fail_under_branches) = explicit_branches else {
   1966             return Err(
   1967                 "missing coverage thresholds; pass --policy-gate or explicit --fail-under-* values"
   1968                     .to_string(),
   1969             );
   1970         };
   1971         CoverageThresholds {
   1972             fail_under_exec_lines,
   1973             fail_under_functions,
   1974             fail_under_regions,
   1975             fail_under_branches,
   1976             require_branches: explicit_require_branches,
   1977         }
   1978     };
   1979 
   1980     let mut summary = read_summary_for_scope(&summary_path, Some(&scope))?;
   1981     let mut lcov = read_lcov(&lcov_path)?;
   1982     if let Some(lines) = summary
   1983         .normalized_executable_lines
   1984         .filter(|lines| lines.total > 0)
   1985     {
   1986         lcov.executable_total = lines.total;
   1987         lcov.executable_covered = lines.covered;
   1988         lcov.executable_percent = percentage(lines.covered, lines.total);
   1989         lcov.executable_source = ExecutableSource::Da;
   1990     }
   1991     if let Some(branches) = summary
   1992         .normalized_branches
   1993         .filter(|branches| branches.total > 0)
   1994     {
   1995         lcov.branch_total = branches.total;
   1996         lcov.branch_covered = branches.covered;
   1997         lcov.branches_available = true;
   1998         lcov.branch_percent = Some(percentage(branches.covered, branches.total));
   1999     }
   2000     normalize_summary_for_gate(&scope, &summary_path, &lcov, &mut summary)?;
   2001     let gate = evaluate_gate(&summary, &lcov, thresholds);
   2002 
   2003     let report = CoverageGateReport {
   2004         scope: scope.clone(),
   2005         thresholds: CoverageGateReportThresholds {
   2006             executable_lines: thresholds.fail_under_exec_lines,
   2007             functions: thresholds.fail_under_functions,
   2008             regions: thresholds.fail_under_regions,
   2009             branches: thresholds.fail_under_branches,
   2010             branches_required: thresholds.require_branches,
   2011         },
   2012         measured: CoverageGateReportMeasured {
   2013             executable_lines_percent: lcov.executable_percent,
   2014             executable_lines_source: executable_source_label(lcov.executable_source).to_string(),
   2015             functions_percent: summary.functions_percent,
   2016             branches_percent: lcov.branch_percent,
   2017             branches_available: lcov.branches_available,
   2018             summary_lines_percent: summary.summary_lines_percent,
   2019             summary_regions_percent: summary.summary_regions_percent,
   2020         },
   2021         counts: CoverageGateReportCounts {
   2022             executable_lines: CoverageCount {
   2023                 covered: lcov.executable_covered,
   2024                 total: lcov.executable_total,
   2025             },
   2026             branches: CoverageCount {
   2027                 covered: lcov.branch_covered,
   2028                 total: lcov.branch_total,
   2029             },
   2030         },
   2031         result: CoverageGateReportResult {
   2032             pass: gate.pass,
   2033             fail_reasons: gate.fail_reasons.clone(),
   2034         },
   2035     };
   2036     write_gate_report(&out_path, &report)?;
   2037 
   2038     if lcov.branches_available {
   2039         eprintln!(
   2040             "{} coverage: executable_lines={:.6} functions={:.6} regions={:.6} branches={:.6}",
   2041             scope,
   2042             lcov.executable_percent,
   2043             summary.functions_percent,
   2044             summary.summary_regions_percent,
   2045             lcov.branch_percent.unwrap_or(0.0)
   2046         );
   2047     } else {
   2048         eprintln!(
   2049             "{} coverage: executable_lines={:.6} functions={:.6} regions={:.6} branches=unavailable",
   2050             scope,
   2051             lcov.executable_percent,
   2052             summary.functions_percent,
   2053             summary.summary_regions_percent
   2054         );
   2055     }
   2056 
   2057     eprintln!(
   2058         "{} summary (informational): lines={:.6} regions={:.6}",
   2059         scope, summary.summary_lines_percent, summary.summary_regions_percent
   2060     );
   2061 
   2062     if !gate.pass {
   2063         for reason in &gate.fail_reasons {
   2064             eprintln!("{scope} gate fail: {reason}");
   2065         }
   2066         return Err("coverage gate failed".to_string());
   2067     }
   2068 
   2069     Ok(())
   2070 }
   2071 
   2072 #[cfg_attr(coverage_nightly, coverage(off))]
   2073 fn normalize_summary_for_gate(
   2074     scope: &str,
   2075     summary_path: &Path,
   2076     _lcov: &LcovCoverage,
   2077     summary: &mut CoverageSummary,
   2078 ) -> Result<(), String> {
   2079     let details_path = coverage_details_path(summary_path);
   2080     if !details_path.exists() {
   2081         return Ok(());
   2082     }
   2083 
   2084     let normalized = read_detailed_summary(&details_path, Some(scope))?;
   2085     summary.functions_percent = normalized.functions_percent;
   2086     summary.summary_regions_percent = normalized.regions_percent;
   2087     Ok(())
   2088 }
   2089 
   2090 #[cfg_attr(not(test), allow(dead_code))]
   2091 fn report_gate(args: &[String]) -> Result<(), String> {
   2092     let root = workspace_root();
   2093     report_gate_with_root(args, &root)
   2094 }
   2095 
   2096 fn report_missing_gate_with_root(args: &[String], root: &Path) -> Result<(), String> {
   2097     let scope = parse_string_arg(args, "scope")?;
   2098     let out_path = PathBuf::from(parse_string_arg(args, "out")?);
   2099     let reason = parse_string_arg(args, "reason")?;
   2100     let policy = read_coverage_policy(&coverage_policy_path(root))?;
   2101     let thresholds = policy.thresholds_for_scope(&scope);
   2102 
   2103     let report = CoverageGateReport {
   2104         scope: scope.clone(),
   2105         thresholds: CoverageGateReportThresholds {
   2106             executable_lines: thresholds.fail_under_exec_lines,
   2107             functions: thresholds.fail_under_functions,
   2108             regions: thresholds.fail_under_regions,
   2109             branches: thresholds.fail_under_branches,
   2110             branches_required: thresholds.require_branches,
   2111         },
   2112         measured: CoverageGateReportMeasured {
   2113             executable_lines_percent: 0.0,
   2114             executable_lines_source: executable_source_label(ExecutableSource::Da).to_string(),
   2115             functions_percent: 0.0,
   2116             branches_percent: None,
   2117             branches_available: false,
   2118             summary_lines_percent: 0.0,
   2119             summary_regions_percent: 0.0,
   2120         },
   2121         counts: CoverageGateReportCounts {
   2122             executable_lines: CoverageCount {
   2123                 covered: 0,
   2124                 total: 0,
   2125             },
   2126             branches: CoverageCount {
   2127                 covered: 0,
   2128                 total: 0,
   2129             },
   2130         },
   2131         result: CoverageGateReportResult {
   2132             pass: false,
   2133             fail_reasons: vec![reason.clone()],
   2134         },
   2135     };
   2136     write_gate_report(&out_path, &report)?;
   2137     eprintln!("{scope} gate fail: {reason}");
   2138     Ok(())
   2139 }
   2140 
   2141 fn write_gate_report(out_path: &Path, report: &CoverageGateReport) -> Result<(), String> {
   2142     let json = serde_json::to_string_pretty(report)
   2143         .expect("serializing coverage gate report should succeed");
   2144     if let Err(err) = fs::write(out_path, format!("{json}\n")) {
   2145         return Err(format!("failed to write {}: {err}", out_path.display()));
   2146     }
   2147     Ok(())
   2148 }
   2149 
   2150 fn coverage_report_path(reports_root: &Path, crate_name: &str) -> PathBuf {
   2151     reports_root
   2152         .join(crate_name.replace('-', "_"))
   2153         .join("gate-report.json")
   2154 }
   2155 
   2156 fn read_gate_report(path: &Path) -> Result<CoverageGateReport, String> {
   2157     let raw = match fs::read_to_string(path) {
   2158         Ok(raw) => raw,
   2159         Err(err) => {
   2160             return Err(format!(
   2161                 "failed to read gate report {}: {err}",
   2162                 path.display()
   2163             ));
   2164         }
   2165     };
   2166     match serde_json::from_str::<CoverageGateReport>(&raw) {
   2167         Ok(report) => Ok(report),
   2168         Err(err) => Err(format!(
   2169             "failed to parse gate report {}: {err}",
   2170             path.display()
   2171         )),
   2172     }
   2173 }
   2174 
   2175 fn list_required_crates_with_root(root: &Path, writer: &mut dyn Write) -> Result<(), String> {
   2176     let required_path = coverage_policy_path(root);
   2177     let policy_crates = read_required_crates(&required_path)?;
   2178     let crates = crate::catalog::active_group(root, "coverage_required")?;
   2179     if crates.iter().collect::<BTreeSet<_>>() != policy_crates.iter().collect::<BTreeSet<_>>() {
   2180         return Err("coverage policy required crates drifted from the catalog".to_owned());
   2181     }
   2182     write_crate_names_output(writer, crates, "required crates")
   2183 }
   2184 
   2185 fn list_workspace_crates_with_root(root: &Path, writer: &mut dyn Write) -> Result<(), String> {
   2186     let crates = crate::catalog::active_packages(root)?;
   2187     write_crate_names_output(writer, crates, "workspace crates")
   2188 }
   2189 
   2190 fn write_crate_names_output(
   2191     writer: &mut dyn Write,
   2192     crates: Vec<String>,
   2193     label: &str,
   2194 ) -> Result<(), String> {
   2195     for crate_name in crates {
   2196         if let Err(err) = writeln!(writer, "{crate_name}") {
   2197             return Err(format!("failed to write {label} output: {err}"));
   2198         }
   2199     }
   2200     Ok(())
   2201 }
   2202 
   2203 fn run_with_root(args: &[String], root: &Path) -> Result<(), String> {
   2204     match args.first().map(String::as_str) {
   2205         Some("help") => Ok(()),
   2206         Some("run-crate") => run_crate(&args[1..]),
   2207         Some("report") => report_gate_with_root(&args[1..], root),
   2208         Some("report-missing") => report_missing_gate_with_root(&args[1..], root),
   2209         Some("refresh-summary") => {
   2210             let reports_root = match parse_optional_string_arg(&args[1..], "reports-root") {
   2211                 Some(raw) => PathBuf::from(raw),
   2212                 None => PathBuf::from("target/coverage"),
   2213             };
   2214             let out_path = match parse_optional_string_arg(&args[1..], "out") {
   2215                 Some(raw) => PathBuf::from(raw),
   2216                 None => PathBuf::from("target/coverage/coverage-refresh.tsv"),
   2217             };
   2218             let status_out_path =
   2219                 parse_optional_string_arg(&args[1..], "status-out").map(PathBuf::from);
   2220             let required_crates = read_required_crates(&coverage_policy_path(root))?;
   2221 
   2222             let mut refresh_rows =
   2223                 String::from("crate\tstatus\texec\tfunc\tbranch\tregion\treport\n");
   2224             let mut status_rows = String::from("crate\tstatus\n");
   2225 
   2226             for crate_name in required_crates {
   2227                 let report_path = coverage_report_path(&reports_root, &crate_name);
   2228                 let report = read_gate_report(&report_path)?;
   2229                 let status = if report.result.pass { "pass" } else { "fail" };
   2230                 let branch = report
   2231                     .measured
   2232                     .branches_percent
   2233                     .map(|value| format!("{value:.6}"))
   2234                     .unwrap_or_else(|| "unavailable".to_string());
   2235                 refresh_rows.push_str(&format!(
   2236                     "{}\t{}\t{:.6}\t{:.6}\t{}\t{:.6}\t{}\n",
   2237                     crate_name,
   2238                     status,
   2239                     report.measured.executable_lines_percent,
   2240                     report.measured.functions_percent,
   2241                     branch,
   2242                     report.measured.summary_regions_percent,
   2243                     report_path.display()
   2244                 ));
   2245                 status_rows.push_str(&format!("{}\t{}\n", crate_name, status));
   2246             }
   2247 
   2248             if let Some(parent) = out_path.parent()
   2249                 && !parent.as_os_str().is_empty()
   2250                 && let Err(err) = fs::create_dir_all(parent)
   2251             {
   2252                 return Err(format!("failed to create {}: {err}", parent.display()));
   2253             }
   2254             fs::write(&out_path, refresh_rows)
   2255                 .map_err(|err| format!("failed to write {}: {err}", out_path.display()))?;
   2256 
   2257             if let Some(status_out_path) = status_out_path {
   2258                 if let Some(parent) = status_out_path.parent()
   2259                     && !parent.as_os_str().is_empty()
   2260                 {
   2261                     fs::create_dir_all(parent)
   2262                         .map_err(|err| format!("failed to create {}: {err}", parent.display()))?;
   2263                 }
   2264                 fs::write(&status_out_path, status_rows).map_err(|err| {
   2265                     format!("failed to write {}: {err}", status_out_path.display())
   2266                 })?;
   2267             }
   2268 
   2269             Ok(())
   2270         }
   2271         Some("required-crates") => {
   2272             let mut stdout = std::io::stdout().lock();
   2273             list_required_crates_with_root(root, &mut stdout)
   2274         }
   2275         Some("workspace-crates") => {
   2276             let mut stdout = std::io::stdout().lock();
   2277             list_workspace_crates_with_root(root, &mut stdout)
   2278         }
   2279         Some(_) => Err("unknown coverage subcommand".to_string()),
   2280         None => Err("missing coverage subcommand".to_string()),
   2281     }
   2282 }
   2283 
   2284 pub fn run(args: &[String]) -> Result<(), String> {
   2285     let root = workspace_root();
   2286     run_with_root(args, &root)
   2287 }
   2288 
   2289 #[cfg(test)]
   2290 mod tests {
   2291     use super::*;
   2292     use std::fs;
   2293     use std::io::{self, Write};
   2294     use std::path::Path;
   2295     use std::sync::{Mutex, MutexGuard, OnceLock};
   2296     use std::time::{SystemTime, UNIX_EPOCH};
   2297 
   2298     fn temp_file_path(prefix: &str) -> PathBuf {
   2299         let ns = SystemTime::now()
   2300             .duration_since(UNIX_EPOCH)
   2301             .expect("system time")
   2302             .as_nanos();
   2303         std::env::temp_dir().join(format!("radroots_xtask_coverage_{prefix}_{ns}.tmp"))
   2304     }
   2305 
   2306     fn temp_dir_path(prefix: &str) -> PathBuf {
   2307         let ns = SystemTime::now()
   2308             .duration_since(UNIX_EPOCH)
   2309             .expect("system time")
   2310             .as_nanos();
   2311         std::env::temp_dir().join(format!("radroots_xtask_coverage_{prefix}_{ns}"))
   2312     }
   2313 
   2314     fn write_file(path: &Path, content: &str) {
   2315         let _ = fs::create_dir_all(path.parent().unwrap_or(Path::new("")));
   2316         fs::write(path, content).expect("write file");
   2317     }
   2318 
   2319     fn cwd_lock() -> &'static Mutex<()> {
   2320         static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
   2321         LOCK.get_or_init(|| Mutex::new(()))
   2322     }
   2323 
   2324     fn recover_lock(lock: &'static Mutex<()>) -> MutexGuard<'static, ()> {
   2325         match lock.lock() {
   2326             Ok(guard) => guard,
   2327             Err(poisoned) => poisoned.into_inner(),
   2328         }
   2329     }
   2330 
   2331     fn lock_cwd() -> MutexGuard<'static, ()> {
   2332         recover_lock(cwd_lock())
   2333     }
   2334 
   2335     fn collect_command_envs(cmd: &Command) -> BTreeMap<String, Option<String>> {
   2336         let mut envs = BTreeMap::new();
   2337         for (key, value) in cmd.get_envs() {
   2338             envs.insert(
   2339                 key.to_string_lossy().to_string(),
   2340                 value.map(|raw| raw.to_string_lossy().to_string()),
   2341             );
   2342         }
   2343         envs
   2344     }
   2345 
   2346     fn ok_runner(_cmd: Command, _name: &str) -> Result<(), String> {
   2347         Ok(())
   2348     }
   2349 
   2350     struct FailingWriter;
   2351 
   2352     impl Write for FailingWriter {
   2353         fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
   2354             Err(io::Error::other("forced write failure"))
   2355         }
   2356 
   2357         fn flush(&mut self) -> io::Result<()> {
   2358             Ok(())
   2359         }
   2360     }
   2361 
   2362     #[test]
   2363     fn reads_summary_totals_from_llvm_cov_json() {
   2364         let path = temp_file_path("summary");
   2365         fs::write(
   2366             &path,
   2367             r#"{
   2368   "data": [
   2369     {
   2370       "totals": {
   2371         "functions": {"percent": 91.25},
   2372         "lines": {"percent": 88.5},
   2373         "regions": {"percent": 86.75}
   2374       }
   2375     }
   2376   ]
   2377 }"#,
   2378         )
   2379         .expect("write summary");
   2380 
   2381         let summary = read_summary(&path).expect("parse summary");
   2382         assert_eq!(summary.functions_percent, 91.25);
   2383         assert_eq!(summary.summary_lines_percent, 88.5);
   2384         assert_eq!(summary.summary_regions_percent, 86.75);
   2385 
   2386         fs::remove_file(path).expect("remove summary");
   2387     }
   2388 
   2389     #[test]
   2390     fn read_summary_normalizes_duplicate_generic_detail_records() {
   2391         let root = temp_dir_path("summary_details_normalized");
   2392         let summary_path = root.join("coverage-summary.json");
   2393         write_file(
   2394             &summary_path,
   2395             r#"{
   2396   "data": [
   2397     {
   2398       "totals": {
   2399         "functions": {"percent": 100.0},
   2400         "lines": {"percent": 88.5},
   2401         "regions": {"percent": 22.0}
   2402       }
   2403     }
   2404   ]
   2405 }"#,
   2406         );
   2407         write_file(
   2408             &root.join("coverage-details.json"),
   2409             r#"{
   2410   "data": [
   2411     {
   2412       "functions": [
   2413         {
   2414           "count": 4,
   2415           "filenames": ["/tmp/lib.rs"],
   2416           "regions": [
   2417             [10, 1, 12, 2, 4, 0, 0, 0],
   2418             [13, 1, 13, 8, 4, 0, 0, 0]
   2419           ]
   2420         },
   2421         {
   2422           "count": 0,
   2423           "filenames": ["/tmp/lib.rs"],
   2424           "regions": [
   2425             [10, 1, 12, 2, 0, 0, 0, 0],
   2426             [13, 1, 13, 8, 0, 0, 0, 0]
   2427           ]
   2428         }
   2429       ]
   2430     }
   2431   ]
   2432 }"#,
   2433         );
   2434 
   2435         let summary = read_summary(&summary_path).expect("parse normalized summary");
   2436         assert_eq!(summary.functions_percent, 100.0);
   2437         assert_eq!(summary.summary_lines_percent, 88.5);
   2438         assert_eq!(summary.summary_regions_percent, 100.0);
   2439 
   2440         fs::remove_dir_all(root).expect("remove summary details root");
   2441     }
   2442 
   2443     #[test]
   2444     fn read_summary_normalizes_details_when_aggregate_functions_are_not_perfect() {
   2445         let root = temp_dir_path("summary_details_not_applied");
   2446         let summary_path = root.join("coverage-summary.json");
   2447         write_file(
   2448             &summary_path,
   2449             r#"{
   2450   "data": [
   2451     {
   2452       "totals": {
   2453         "functions": {"percent": 95.0},
   2454         "lines": {"percent": 88.5},
   2455         "regions": {"percent": 22.0}
   2456       }
   2457     }
   2458   ]
   2459 }"#,
   2460         );
   2461         write_file(
   2462             &root.join("coverage-details.json"),
   2463             r#"{
   2464   "data": [
   2465     {
   2466       "functions": [
   2467         {
   2468           "count": 4,
   2469           "filenames": ["/tmp/lib.rs"],
   2470           "regions": [
   2471             [10, 1, 12, 2, 4, 0, 0, 0]
   2472           ]
   2473         }
   2474       ]
   2475     }
   2476   ]
   2477 }"#,
   2478         );
   2479 
   2480         let summary = read_summary(&summary_path).expect("parse normalized summary");
   2481         assert_eq!(summary.functions_percent, 100.0);
   2482         assert_eq!(summary.summary_regions_percent, 100.0);
   2483 
   2484         fs::remove_dir_all(root).expect("remove summary preserve root");
   2485     }
   2486 
   2487     #[test]
   2488     fn read_summary_for_scope_ignores_other_crate_detail_records() {
   2489         let root = temp_dir_path("summary_details_scope_filtered");
   2490         let summary_path = root.join("coverage-summary.json");
   2491         write_file(
   2492             &summary_path,
   2493             r#"{
   2494   "data": [
   2495     {
   2496       "totals": {
   2497         "functions": {"percent": 100.0},
   2498         "lines": {"percent": 88.5},
   2499         "regions": {"percent": 22.0}
   2500       }
   2501     }
   2502   ]
   2503 }"#,
   2504         );
   2505         write_file(
   2506             &root.join("coverage-details.json"),
   2507             r#"{
   2508   "data": [
   2509     {
   2510       "functions": [
   2511         {
   2512           "count": 4,
   2513           "filenames": ["/workspace/crates/a/src/lib.rs"],
   2514           "regions": [
   2515             [10, 1, 12, 2, 4, 0, 0, 0]
   2516           ]
   2517         },
   2518         {
   2519           "count": 9,
   2520           "filenames": ["/workspace/crates/b/src/lib.rs"],
   2521           "regions": [
   2522             [20, 1, 20, 6, 0, 0, 0, 0]
   2523           ]
   2524         },
   2525         {
   2526           "count": 0,
   2527           "filenames": ["/workspace/crates/a/src/adapters/../../tests/unit.rs"],
   2528           "regions": [
   2529             [30, 1, 30, 6, 0, 0, 0, 0]
   2530           ]
   2531         }
   2532       ]
   2533     }
   2534   ]
   2535 }"#,
   2536         );
   2537 
   2538         let summary =
   2539             read_summary_for_scope(&summary_path, Some("radroots_a")).expect("parse scope summary");
   2540         assert_eq!(summary.functions_percent, 100.0);
   2541         assert_eq!(summary.summary_lines_percent, 88.5);
   2542         assert_eq!(summary.summary_regions_percent, 100.0);
   2543 
   2544         fs::remove_dir_all(root).expect("remove summary scope root");
   2545     }
   2546 
   2547     #[test]
   2548     fn coverage_details_path_uses_summary_parent() {
   2549         let summary_path = Path::new("target/coverage/radroots_a/coverage-summary.json");
   2550         assert_eq!(
   2551             coverage_details_path(summary_path),
   2552             Path::new("target/coverage/radroots_a/coverage-details.json")
   2553         );
   2554         assert_eq!(
   2555             coverage_details_path(Path::new("coverage-summary.json")),
   2556             Path::new("coverage-details.json")
   2557         );
   2558     }
   2559 
   2560     #[test]
   2561     fn read_detailed_summary_covers_empty_skip_and_filter_paths() {
   2562         let root = temp_dir_path("details_empty_skip_filter");
   2563         let missing = root.join("missing-details.json");
   2564         let err = read_detailed_summary(&missing, None).expect_err("missing details");
   2565         assert!(err.contains("failed to read coverage details"));
   2566 
   2567         let empty = root.join("empty-details.json");
   2568         write_file(&empty, r#"{"data":[]}"#);
   2569         let err = read_detailed_summary(&empty, None).expect_err("empty details");
   2570         assert!(err.contains("coverage details data is empty"));
   2571 
   2572         let skipped = root.join("skipped-details.json");
   2573         write_file(
   2574             &skipped,
   2575             r#"{
   2576   "data": [
   2577     {
   2578       "functions": [
   2579         {
   2580           "count": 1,
   2581           "filenames": [],
   2582           "regions": [[10, 1, 10, 2, 1, 0, 0, 0]]
   2583         },
   2584         {
   2585           "count": 1,
   2586           "filenames": ["/workspace/crates/a/src/lib.rs"],
   2587           "regions": []
   2588         }
   2589       ]
   2590     }
   2591   ]
   2592 }"#,
   2593         );
   2594         let err = read_detailed_summary(&skipped, None).expect_err("skipped details");
   2595         assert!(err.contains("coverage details functions are empty"));
   2596 
   2597         let filtered = root.join("filtered-details.json");
   2598         write_file(
   2599             &filtered,
   2600             r#"{
   2601   "data": [
   2602     {
   2603       "functions": [
   2604         {
   2605           "count": 0,
   2606           "filenames": ["/workspace/crates/a/src/lib.rs"],
   2607           "regions": [[10, 1, 10, 2, 0, 0, 0, 0]]
   2608         },
   2609         {
   2610           "count": 1,
   2611           "filenames": ["/workspace/crates/b/src/lib.rs"],
   2612           "regions": [[20, 1, 20, 2, 1, 0, 0, 0]]
   2613         }
   2614       ]
   2615     }
   2616   ]
   2617 }"#,
   2618         );
   2619         let summary =
   2620             read_detailed_summary(&filtered, Some("radroots_a")).expect("filtered summary");
   2621         assert_eq!(summary.functions_percent, 100.0);
   2622         assert_eq!(summary.regions_percent, 0.0);
   2623 
   2624         fs::remove_dir_all(root).expect("remove detail edge root");
   2625     }
   2626 
   2627     #[test]
   2628     fn read_detailed_summary_ignores_synthetic_regions_from_source() {
   2629         let root = temp_dir_path("details_synthetic_regions");
   2630         let source_path = root.join("crates").join("a").join("src").join("lib.rs");
   2631         write_file(
   2632             &source_path,
   2633             "pub fn load() { let _value = call()?; }\npub fn ready() { ok(); }\n",
   2634         );
   2635         let details_path = root.join("coverage-details.json");
   2636         let raw = serde_json::json!({
   2637             "data": [
   2638                 {
   2639                     "functions": [
   2640                         {
   2641                             "count": 1,
   2642                             "filenames": [source_path.display().to_string()],
   2643                             "regions": [
   2644                                 [1, 1, 1, 37, 1, 0, 0, 0],
   2645                                 [1, 36, 1, 37, 0, 0, 0, 0],
   2646                                 [2, 1, 2, 24, 1, 0, 0, 0]
   2647                             ]
   2648                         }
   2649                     ]
   2650                 }
   2651             ]
   2652         });
   2653         write_file(&details_path, &raw.to_string());
   2654 
   2655         let summary =
   2656             read_detailed_summary(&details_path, Some("radroots_a")).expect("synthetic summary");
   2657         assert_eq!(summary.functions_percent, 100.0);
   2658         assert_eq!(summary.regions_percent, 100.0);
   2659 
   2660         fs::remove_dir_all(root).expect("remove synthetic details root");
   2661     }
   2662 
   2663     #[test]
   2664     fn read_detailed_summary_ignores_cfg_test_detail_functions() {
   2665         let root = temp_dir_path("details_cfg_test_functions");
   2666         let source_path = root.join("crates").join("a").join("src").join("lib.rs");
   2667         write_file(
   2668             &source_path,
   2669             "#[cfg(test)]\nmod tests {\n    fn helper() {}\n}\n",
   2670         );
   2671         let details_path = root.join("coverage-details.json");
   2672         let raw = serde_json::json!({
   2673             "data": [
   2674                 {
   2675                     "functions": [
   2676                         {
   2677                             "count": 0,
   2678                             "filenames": [source_path.display().to_string()],
   2679                             "regions": [
   2680                                 [3, 5, 3, 19, 0, 0, 0, 0]
   2681                             ]
   2682                         }
   2683                     ]
   2684                 }
   2685             ]
   2686         });
   2687         write_file(&details_path, &raw.to_string());
   2688 
   2689         let summary = read_detailed_summary(&details_path, Some("radroots_a"))
   2690             .expect("cfg-test detail summary");
   2691         assert_eq!(summary.functions_percent, 100.0);
   2692         assert_eq!(summary.regions_percent, 100.0);
   2693 
   2694         fs::remove_dir_all(root).expect("remove cfg-test details root");
   2695     }
   2696 
   2697     #[test]
   2698     fn detailed_xtask_report_measures_tool_source_instead_of_empty_perfect_totals() {
   2699         let root = temp_dir_path("details_xtask_scope");
   2700         let source_path = root.join("tools/xtask/src/main.rs");
   2701         write_file(
   2702             &source_path,
   2703             "fn used(flag: bool) { if flag { work(); } }\nfn missed() { work(); }\n",
   2704         );
   2705         let details = root.join("coverage-details.json");
   2706         let raw = serde_json::json!({"data": [{"functions": [
   2707             {
   2708                 "count": 1,
   2709                 "filenames": [source_path.display().to_string()],
   2710                 "regions": [[1, 1, 1, 41, 1, 0, 0, 0]],
   2711                 "branches": [[1, 25, 1, 29, 1, 0, 0, 0, 0]]
   2712             },
   2713             {
   2714                 "count": 0,
   2715                 "filenames": [source_path.display().to_string()],
   2716                 "regions": [[2, 1, 2, 23, 0, 0, 0, 0]]
   2717             }
   2718         ]}]});
   2719         write_file(&details, &raw.to_string());
   2720         let measured = read_detailed_summary(&details, Some("xtask")).unwrap();
   2721         assert_eq!(measured.functions_percent, 50.0);
   2722         assert_eq!(measured.regions_percent, 50.0);
   2723         assert_eq!(measured.executable_lines.total, 2);
   2724         assert_eq!(measured.executable_lines.covered, 1);
   2725         assert_eq!(measured.branches.total, 2);
   2726         assert_eq!(measured.branches.covered, 1);
   2727         fs::remove_dir_all(root).unwrap();
   2728     }
   2729 
   2730     #[test]
   2731     fn detailed_scope_separates_macro_files_and_preserves_existing_index_fallback() {
   2732         let root = temp_dir_path("details_mixed_source_maps");
   2733         let source = root.join("tools/xtask/src/main.rs");
   2734         let external = root.join("dependency/src/lib.rs");
   2735         write_file(
   2736             &source,
   2737             "fn measured() { work(); }\n#[cfg(test)]\nfn test_only() {}\n#[cfg(test)]\nconst TEST_ONLY: bool = true;\n",
   2738         );
   2739         write_file(&external, "fn external() {}\n");
   2740         let details = root.join("coverage-details.json");
   2741         let raw = serde_json::json!({"data":[{"functions":[
   2742             {"count":1,"filenames":[source,external],
   2743              "regions":[[1,1,1,25,1,0,0,0],[1,1,1,16,1,1,0,0],[1,1,1,2,1,99,0,0],[4,1,4,11,1,0,0,0],[1,1,1,2,1,0,0,1]],
   2744              "branches":[[1,17,1,21,1,1,0,0,0],[1,1,1,2,1,1,1,0,0],[1,1,1,2,1,1,99,0,0],[3,1,3,2,1,1,0,0,0]]},
   2745             {"count":1,"filenames":[source,external],"regions":[[1,1,1,16,1,1,0,0]],"branches":[]},
   2746             {"count":1,"filenames":[source],"regions":[[1,1,1,2,1,99,0,0]],"branches":[]}
   2747         ]}]});
   2748         write_file(&details, &raw.to_string());
   2749         let measured = read_detailed_summary(&details, Some("xtask")).unwrap();
   2750         assert_eq!(measured.functions_percent, 100.0);
   2751         assert_eq!(measured.executable_lines.covered, 1);
   2752         assert_eq!(measured.executable_lines.total, 1);
   2753         assert_eq!(measured.branches.covered, 4);
   2754         assert_eq!(measured.branches.total, 4);
   2755         fs::remove_dir_all(root).unwrap();
   2756     }
   2757 
   2758     #[test]
   2759     fn read_summary_reports_read_and_parse_errors() {
   2760         let missing = temp_file_path("summary_missing");
   2761         let read_err = read_summary(&missing).expect_err("missing summary should fail");
   2762         assert!(read_err.contains("failed to read summary"));
   2763 
   2764         let invalid = temp_file_path("summary_invalid");
   2765         write_file(&invalid, "{not-json");
   2766         let parse_err = read_summary(&invalid).expect_err("invalid summary should fail");
   2767         assert!(parse_err.contains("failed to parse summary"));
   2768         fs::remove_file(invalid).expect("remove invalid summary");
   2769     }
   2770 
   2771     #[test]
   2772     fn read_summary_reports_detail_parse_errors() {
   2773         let root = temp_dir_path("summary_invalid_details");
   2774         let summary_path = root.join("coverage-summary.json");
   2775         write_file(
   2776             &summary_path,
   2777             r#"{
   2778   "data": [
   2779     {
   2780       "totals": {
   2781         "functions": {"percent": 91.25},
   2782         "lines": {"percent": 88.5},
   2783         "regions": {"percent": 86.75}
   2784       }
   2785     }
   2786   ]
   2787 }"#,
   2788         );
   2789         write_file(&root.join("coverage-details.json"), "{not-json");
   2790 
   2791         let err = read_summary(&summary_path).expect_err("invalid details should fail");
   2792         assert!(err.contains("failed to parse coverage details"));
   2793 
   2794         fs::remove_dir_all(root).expect("remove invalid details root");
   2795     }
   2796 
   2797     #[test]
   2798     fn ignorable_question_mark_regions_require_single_char_question_mark() {
   2799         let path = temp_file_path("coverage_question_mark_region");
   2800         write_file(&path, "let value = call()?;\nreturn Err(());\n");
   2801         let mut cache = BTreeMap::new();
   2802 
   2803         let question_mark = RegionCoverageKey {
   2804             line_start: 1,
   2805             column_start: 19,
   2806             line_end: 1,
   2807             column_end: 20,
   2808             kind: 0,
   2809         };
   2810         assert!(is_ignorable_synthetic_region(
   2811             path.to_str().expect("utf-8 path"),
   2812             &question_mark,
   2813             &mut cache,
   2814         ));
   2815 
   2816         let not_question_mark = RegionCoverageKey {
   2817             line_start: 2,
   2818             column_start: 8,
   2819             line_end: 2,
   2820             column_end: 15,
   2821             kind: 0,
   2822         };
   2823         assert!(!is_ignorable_synthetic_region(
   2824             path.to_str().expect("utf-8 path"),
   2825             &not_question_mark,
   2826             &mut cache,
   2827         ));
   2828 
   2829         let single_char_not_question_mark = RegionCoverageKey {
   2830             line_start: 2,
   2831             column_start: 1,
   2832             line_end: 2,
   2833             column_end: 2,
   2834             kind: 0,
   2835         };
   2836         assert!(!is_ignorable_synthetic_region(
   2837             path.to_str().expect("utf-8 path"),
   2838             &single_char_not_question_mark,
   2839             &mut cache,
   2840         ));
   2841 
   2842         fs::remove_file(path).expect("remove question mark source");
   2843     }
   2844 
   2845     #[test]
   2846     fn ignorable_matches_assertion_regions_require_matching_slice() {
   2847         let path = temp_file_path("coverage_matches_assertion_region");
   2848         write_file(
   2849             &path,
   2850             "        assert!(matches!(value, Err(Error::Nope)));\n",
   2851         );
   2852         let mut cache = BTreeMap::new();
   2853 
   2854         let matches_region = RegionCoverageKey {
   2855             line_start: 1,
   2856             column_start: 17,
   2857             line_end: 1,
   2858             column_end: 25,
   2859             kind: 0,
   2860         };
   2861         assert!(is_ignorable_synthetic_region(
   2862             path.to_str().expect("utf-8 path"),
   2863             &matches_region,
   2864             &mut cache,
   2865         ));
   2866 
   2867         let assert_region = RegionCoverageKey {
   2868             line_start: 1,
   2869             column_start: 9,
   2870             line_end: 1,
   2871             column_end: 15,
   2872             kind: 0,
   2873         };
   2874         assert!(!is_ignorable_synthetic_region(
   2875             path.to_str().expect("utf-8 path"),
   2876             &assert_region,
   2877             &mut cache,
   2878         ));
   2879 
   2880         fs::remove_file(path).expect("remove matches assertion source");
   2881     }
   2882 
   2883     #[test]
   2884     fn ignorable_synthetic_regions_cover_cfg_test_and_unreachable_lines() {
   2885         let root = temp_dir_path("coverage_cfg_test_unreachable_regions");
   2886         let cfg_path = root.join("lib.rs");
   2887         write_file(
   2888             &cfg_path,
   2889             "#[cfg(all(test, feature = \"fixtures\"))]\nmod tests {\n    fn helper() {}\n}\npub fn impossible() { unreachable!() }\n",
   2890         );
   2891         let mut cache = BTreeMap::new();
   2892 
   2893         let cfg_region = RegionCoverageKey {
   2894             line_start: 3,
   2895             column_start: 5,
   2896             line_end: 3,
   2897             column_end: 19,
   2898             kind: 0,
   2899         };
   2900         assert!(is_ignorable_synthetic_region(
   2901             cfg_path.to_str().expect("utf-8 path"),
   2902             &cfg_region,
   2903             &mut cache,
   2904         ));
   2905 
   2906         let unreachable_region = RegionCoverageKey {
   2907             line_start: 5,
   2908             column_start: 23,
   2909             line_end: 5,
   2910             column_end: 35,
   2911             kind: 0,
   2912         };
   2913         assert!(is_ignorable_synthetic_region(
   2914             cfg_path.to_str().expect("utf-8 path"),
   2915             &unreachable_region,
   2916             &mut cache,
   2917         ));
   2918 
   2919         fs::remove_dir_all(root).expect("remove cfg-test unreachable root");
   2920     }
   2921 
   2922     #[test]
   2923     fn cfg_test_source_line_covers_pending_non_block_forms() {
   2924         let source = "#[cfg(test)]\nfn helper() {}\n#[cfg(test)]\nmod tests\n{\n}\n";
   2925 
   2926         assert!(is_cfg_test_source_line(source, 2));
   2927         assert!(is_cfg_test_source_line(source, 4));
   2928     }
   2929 
   2930     #[test]
   2931     fn cfg_test_source_line_stops_after_non_block_items_and_accepts_named_modules() {
   2932         let source = "#[cfg(test)]\nuse crate::fixture;\npub fn production() {}\n#[cfg(test)]\nmod migration_framework {\n    fn helper() {}\n}\n";
   2933 
   2934         assert!(is_cfg_test_source_line(source, 2));
   2935         assert!(!is_cfg_test_source_line(source, 3));
   2936         assert!(is_cfg_test_source_line(source, 5));
   2937         assert!(is_cfg_test_source_line(source, 6));
   2938     }
   2939 
   2940     #[test]
   2941     fn cfg_test_source_lines_ignore_braces_inside_rust_lexical_literals() {
   2942         let source = r####"#[cfg(test)]
   2943 mod tests {
   2944     const FORMAT: &str = "{value}";
   2945     const RAW: &str = r###"raw { } text"###;
   2946     const BYTE_RAW: &[u8] = br#"bytes { }"#;
   2947     const OPEN: char = '{';
   2948     const CLOSE: char = '}';
   2949     // comment braces }}}
   2950     /* outer { /* nested } */ still ignored } */
   2951     fn helper() {}
   2952 }
   2953 pub fn production() {}
   2954 "####;
   2955 
   2956         for line in 1..=11 {
   2957             assert!(is_cfg_test_source_line(source, line), "test line {line}");
   2958         }
   2959         assert!(!is_cfg_test_source_line(source, 12));
   2960     }
   2961 
   2962     #[test]
   2963     fn lexical_brace_tracking_keeps_escaped_literals_and_pending_attributes_bounded() {
   2964         for literal in [r"'\n'", r"'\''", r"'\u{7d}'", "'é'"] {
   2965             assert_eq!(
   2966                 char_literal_end(literal, 0),
   2967                 Some(literal.len()),
   2968                 "{literal}"
   2969             );
   2970             assert_eq!(source_brace_deltas(literal).collect::<Vec<_>>(), [0]);
   2971         }
   2972         for literal in ["'", r"'\", r"'\u{7d", r"'\uX'", "'ab'"] {
   2973             assert_eq!(char_literal_end(literal, 0), None, "{literal}");
   2974         }
   2975         let source = r####"fn boundary() {
   2976     let escaped = "\"}\\{";
   2977     let raw = r##"a"#} b"x { c"##;
   2978     let divided = 8 / 2;
   2979     /* / text * /* nested */ } */
   2980 }
   2981 "####;
   2982         assert_eq!(
   2983             source_brace_deltas(source).collect::<Vec<_>>(),
   2984             [1, 0, 0, 0, 0, -1]
   2985         );
   2986         for attribute in [
   2987             "#[cfg(test)]",
   2988             "#[cfg_attr(coverage_nightly, coverage(off))]",
   2989         ] {
   2990             let source = format!(
   2991                 "{attribute}\n\n// reason\n#[allow(dead_code)]\nfn excluded\n() {{}}\nfn measured() {{}}\n"
   2992             );
   2993             let lines = if attribute == "#[cfg(test)]" {
   2994                 cfg_test_source_lines(&source)
   2995             } else {
   2996                 coverage_off_source_lines(&source)
   2997             };
   2998             assert_eq!(lines, [true, true, true, true, true, true, false]);
   2999         }
   3000     }
   3001 
   3002     #[test]
   3003     fn coverage_off_source_lines_cover_only_the_annotated_item() {
   3004         let source = "#[cfg_attr(coverage_nightly, coverage(off))]\npub fn glue(\n    enabled: bool,\n) -> bool {\n    if enabled { true } else { false }\n}\npub fn policy() -> bool { true }\n";
   3005         let lines = coverage_off_source_lines(source);
   3006 
   3007         for line in 1..=6 {
   3008             assert!(lines[line - 1], "annotated item line {line}");
   3009         }
   3010         assert!(!lines[6], "following production item remains measured");
   3011     }
   3012 
   3013     #[test]
   3014     fn coverage_off_source_lines_cover_annotated_non_block_items() {
   3015         let source = "#[cfg_attr(coverage_nightly, coverage(off))]\nconst GENERATED: bool = true;\npub fn policy() -> bool { true }\n";
   3016         let lines = coverage_off_source_lines(source);
   3017 
   3018         assert!(lines[0], "coverage attribute is excluded");
   3019         assert!(lines[1], "annotated non-block item is excluded");
   3020         assert!(!lines[2], "following production item remains measured");
   3021     }
   3022 
   3023     #[test]
   3024     fn coverage_off_source_lines_ignore_literal_and_comment_braces() {
   3025         let source = r####"#[cfg_attr(coverage_nightly, coverage(off))]
   3026 fn excluded() {
   3027     let _ = "}";
   3028     let _ = r###"{ raw }"###;
   3029     /* } */
   3030 }
   3031 fn measured() {}
   3032 "####;
   3033         let lines = coverage_off_source_lines(source);
   3034 
   3035         assert!(lines[..6].iter().all(|excluded| *excluded));
   3036         assert!(!lines[6]);
   3037     }
   3038 
   3039     #[test]
   3040     fn ignorable_unexpected_panic_regions_require_test_fallback_lines() {
   3041         let root = temp_dir_path("coverage_unexpected_panic_region");
   3042         let path = root.join("tests.rs");
   3043         write_file(
   3044             &path,
   3045             "match &err {\n    RuntimeProtectedFileError::Io { .. } => {}\n        other => panic!(\"unexpected io error: {other}\"),\n}\n",
   3046         );
   3047         let mut cache = BTreeMap::new();
   3048 
   3049         let other_region = RegionCoverageKey {
   3050             line_start: 3,
   3051             column_start: 9,
   3052             line_end: 3,
   3053             column_end: 14,
   3054             kind: 0,
   3055         };
   3056         assert!(is_ignorable_synthetic_region(
   3057             path.to_str().expect("utf-8 path"),
   3058             &other_region,
   3059             &mut cache,
   3060         ));
   3061 
   3062         let panic_region = RegionCoverageKey {
   3063             line_start: 3,
   3064             column_start: 18,
   3065             line_end: 3,
   3066             column_end: 24,
   3067             kind: 0,
   3068         };
   3069         assert!(is_ignorable_synthetic_region(
   3070             path.to_str().expect("utf-8 path"),
   3071             &panic_region,
   3072             &mut cache,
   3073         ));
   3074 
   3075         let non_test_path = root.join("source.rs");
   3076         write_file(
   3077             &non_test_path,
   3078             "match &err {\n    RuntimeProtectedFileError::Io { .. } => {}\n        other => panic!(\"unexpected io error: {other}\"),\n}\n",
   3079         );
   3080         assert!(!is_ignorable_synthetic_region(
   3081             non_test_path.to_str().expect("utf-8 path"),
   3082             &other_region,
   3083             &mut cache,
   3084         ));
   3085 
   3086         let multiline = RegionCoverageKey {
   3087             line_start: 1,
   3088             column_start: 1,
   3089             line_end: 2,
   3090             column_end: 1,
   3091             kind: 0,
   3092         };
   3093         assert!(!is_ignorable_synthetic_region(
   3094             path.to_str().expect("utf-8 path"),
   3095             &multiline,
   3096             &mut cache,
   3097         ));
   3098 
   3099         let missing_file = root.join("missing.rs");
   3100         assert!(!is_ignorable_synthetic_region(
   3101             missing_file.to_str().expect("utf-8 path"),
   3102             &other_region,
   3103             &mut cache,
   3104         ));
   3105 
   3106         let out_of_range = RegionCoverageKey {
   3107             line_start: 99,
   3108             column_start: 1,
   3109             line_end: 99,
   3110             column_end: 2,
   3111             kind: 0,
   3112         };
   3113         assert!(!is_ignorable_synthetic_region(
   3114             path.to_str().expect("utf-8 path"),
   3115             &out_of_range,
   3116             &mut cache,
   3117         ));
   3118 
   3119         let non_panic_test_path = root.join("non_panic").join("tests.rs");
   3120         write_file(&non_panic_test_path, "        other => Ok(()),\n");
   3121         let non_panic_other_region = RegionCoverageKey {
   3122             line_start: 1,
   3123             column_start: 9,
   3124             line_end: 1,
   3125             column_end: 14,
   3126             kind: 0,
   3127         };
   3128         assert!(!is_ignorable_synthetic_region(
   3129             non_panic_test_path.to_str().expect("utf-8 path"),
   3130             &non_panic_other_region,
   3131             &mut cache,
   3132         ));
   3133 
   3134         let non_fallback_region = RegionCoverageKey {
   3135             line_start: 3,
   3136             column_start: 39,
   3137             line_end: 3,
   3138             column_end: 44,
   3139             kind: 0,
   3140         };
   3141         assert!(!is_ignorable_synthetic_region(
   3142             path.to_str().expect("utf-8 path"),
   3143             &non_fallback_region,
   3144             &mut cache,
   3145         ));
   3146 
   3147         fs::remove_dir_all(root).expect("remove unexpected panic source");
   3148     }
   3149 
   3150     #[test]
   3151     fn read_coverage_policy_rejects_non_finite_and_out_of_range_thresholds() {
   3152         let non_finite = temp_file_path("coverage_policy_non_finite");
   3153         write_file(
   3154             &non_finite,
   3155             "[gate]\nfail_under_exec_lines = inf\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3156         );
   3157         let non_finite_err =
   3158             read_coverage_policy(&non_finite).expect_err("non-finite threshold should fail");
   3159         assert!(non_finite_err.contains("must be finite"));
   3160         fs::remove_file(non_finite).expect("remove non-finite policy");
   3161 
   3162         let out_of_range = temp_file_path("coverage_policy_out_of_range");
   3163         write_file(
   3164             &out_of_range,
   3165             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 101.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3166         );
   3167         let out_of_range_err =
   3168             read_coverage_policy(&out_of_range).expect_err("out-of-range threshold should fail");
   3169         assert!(out_of_range_err.contains("must be within 0..=100"));
   3170         fs::remove_file(out_of_range).expect("remove out-of-range policy");
   3171     }
   3172 
   3173     #[test]
   3174     fn coverage_policy_resolves_scope_specific_temporary_overrides() {
   3175         let path = temp_file_path("coverage_policy_override_scope");
   3176         write_file(
   3177             &path,
   3178             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\", \"radroots_b\", \"radroots_c\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = 88.5\nfail_under_functions = 77.5\nfail_under_regions = 66.5\nfail_under_branches = 55.5\nrequire_branches = false\ntemporary = true\nreason = \"temporary publish unblocker\"\n\n[overrides.radroots_b]\nrequire_branches = true\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3179         );
   3180         let policy = read_coverage_policy(&path).expect("parse scoped override policy");
   3181         let override_thresholds = policy.thresholds_for_scope("radroots_a");
   3182         assert_eq!(override_thresholds.fail_under_exec_lines, 88.5);
   3183         assert_eq!(override_thresholds.fail_under_functions, 77.5);
   3184         assert_eq!(override_thresholds.fail_under_regions, 66.5);
   3185         assert_eq!(override_thresholds.fail_under_branches, 55.5);
   3186         assert!(!override_thresholds.require_branches);
   3187 
   3188         let branch_override_thresholds = policy.thresholds_for_scope("radroots_b");
   3189         assert_eq!(branch_override_thresholds.fail_under_exec_lines, 100.0);
   3190         assert_eq!(branch_override_thresholds.fail_under_functions, 100.0);
   3191         assert_eq!(branch_override_thresholds.fail_under_regions, 100.0);
   3192         assert_eq!(branch_override_thresholds.fail_under_branches, 100.0);
   3193         assert!(branch_override_thresholds.require_branches);
   3194 
   3195         let default_thresholds = policy.thresholds_for_scope("radroots_c");
   3196         assert_eq!(default_thresholds.fail_under_exec_lines, 100.0);
   3197         assert_eq!(default_thresholds.fail_under_functions, 100.0);
   3198         assert_eq!(default_thresholds.fail_under_regions, 100.0);
   3199         assert_eq!(default_thresholds.fail_under_branches, 100.0);
   3200         assert!(default_thresholds.require_branches);
   3201 
   3202         fs::remove_file(path).expect("remove override scope policy");
   3203     }
   3204 
   3205     #[test]
   3206     fn read_coverage_policy_rejects_invalid_override_shapes() {
   3207         let non_required = temp_file_path("coverage_policy_override_non_required");
   3208         write_file(
   3209             &non_required,
   3210             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_b]\nfail_under_exec_lines = 90.0\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3211         );
   3212         let non_required_err =
   3213             read_coverage_policy(&non_required).expect_err("non-required override should fail");
   3214         assert!(non_required_err.contains("must target a required crate"));
   3215         fs::remove_file(non_required).expect("remove non-required override policy");
   3216 
   3217         let missing_temporary = temp_file_path("coverage_policy_override_missing_temporary");
   3218         write_file(
   3219             &missing_temporary,
   3220             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = 90.0\ntemporary = false\nreason = \"temporary publish unblocker\"\n",
   3221         );
   3222         let missing_temporary_err = read_coverage_policy(&missing_temporary)
   3223             .expect_err("override without temporary=true should fail");
   3224         assert!(missing_temporary_err.contains("temporary = true"));
   3225         fs::remove_file(missing_temporary).expect("remove temporary override policy");
   3226 
   3227         let missing_reason = temp_file_path("coverage_policy_override_missing_reason");
   3228         write_file(
   3229             &missing_reason,
   3230             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = 90.0\ntemporary = true\nreason = \"  \"\n",
   3231         );
   3232         let missing_reason_err =
   3233             read_coverage_policy(&missing_reason).expect_err("blank override reason should fail");
   3234         assert!(missing_reason_err.contains("non-empty reason"));
   3235         fs::remove_file(missing_reason).expect("remove missing reason policy");
   3236 
   3237         let stricter = temp_file_path("coverage_policy_override_stricter");
   3238         write_file(
   3239             &stricter,
   3240             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = 100.1\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3241         );
   3242         let stricter_err =
   3243             read_coverage_policy(&stricter).expect_err("stricter override should fail");
   3244         assert!(stricter_err.contains("must be within 0..=100"));
   3245         fs::remove_file(stricter).expect("remove stricter override policy");
   3246 
   3247         let above_global = temp_file_path("coverage_policy_override_above_global");
   3248         write_file(
   3249             &above_global,
   3250             "[gate]\nfail_under_exec_lines = 98.0\nfail_under_functions = 98.0\nfail_under_regions = 98.0\nfail_under_branches = 98.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = 99.0\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3251         );
   3252         let above_global_err =
   3253             read_coverage_policy(&above_global).expect_err("above-global override should fail");
   3254         assert!(above_global_err.contains("must not exceed the global gate"));
   3255         fs::remove_file(above_global).expect("remove above-global override policy");
   3256 
   3257         let above_global_functions = temp_file_path("coverage_policy_override_above_global_fn");
   3258         write_file(
   3259             &above_global_functions,
   3260             "[gate]\nfail_under_exec_lines = 98.0\nfail_under_functions = 98.0\nfail_under_regions = 98.0\nfail_under_branches = 98.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_functions = 99.0\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3261         );
   3262         let above_global_functions_err = read_coverage_policy(&above_global_functions)
   3263             .expect_err("above-global function override should fail");
   3264         assert!(above_global_functions_err.contains("must not exceed the global gate"));
   3265         fs::remove_file(above_global_functions).expect("remove above-global function policy");
   3266 
   3267         let above_global_regions = temp_file_path("coverage_policy_override_above_global_regions");
   3268         write_file(
   3269             &above_global_regions,
   3270             "[gate]\nfail_under_exec_lines = 98.0\nfail_under_functions = 98.0\nfail_under_regions = 98.0\nfail_under_branches = 98.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_regions = 99.0\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3271         );
   3272         let above_global_regions_err = read_coverage_policy(&above_global_regions)
   3273             .expect_err("above-global region override should fail");
   3274         assert!(above_global_regions_err.contains("must not exceed the global gate"));
   3275         fs::remove_file(above_global_regions).expect("remove above-global region policy");
   3276 
   3277         let above_global_branches =
   3278             temp_file_path("coverage_policy_override_above_global_branches");
   3279         write_file(
   3280             &above_global_branches,
   3281             "[gate]\nfail_under_exec_lines = 98.0\nfail_under_functions = 98.0\nfail_under_regions = 98.0\nfail_under_branches = 98.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_branches = 99.0\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3282         );
   3283         let above_global_branches_err = read_coverage_policy(&above_global_branches)
   3284             .expect_err("above-global branch override should fail");
   3285         assert!(above_global_branches_err.contains("must not exceed the global gate"));
   3286         fs::remove_file(above_global_branches).expect("remove above-global branch policy");
   3287 
   3288         let non_finite_override = temp_file_path("coverage_policy_override_non_finite");
   3289         write_file(
   3290             &non_finite_override,
   3291             "[gate]\nfail_under_exec_lines = 98.0\nfail_under_functions = 98.0\nfail_under_regions = 98.0\nfail_under_branches = 98.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = inf\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3292         );
   3293         let non_finite_override_err = read_coverage_policy(&non_finite_override)
   3294             .expect_err("non-finite override should fail");
   3295         assert!(non_finite_override_err.contains("must be finite"));
   3296         fs::remove_file(non_finite_override).expect("remove non-finite override policy");
   3297 
   3298         let stricter_branch_presence = temp_file_path("coverage_policy_override_branch_required");
   3299         write_file(
   3300             &stricter_branch_presence,
   3301             "[gate]\nfail_under_exec_lines = 98.0\nfail_under_functions = 98.0\nfail_under_regions = 98.0\nfail_under_branches = 98.0\nrequire_branches = false\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nrequire_branches = true\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3302         );
   3303         let branch_presence_err = read_coverage_policy(&stricter_branch_presence)
   3304             .expect_err("stricter branch presence should fail");
   3305         assert!(branch_presence_err.contains("require_branches cannot be stricter"));
   3306         fs::remove_file(stricter_branch_presence).expect("remove branch presence policy");
   3307     }
   3308 
   3309     #[test]
   3310     fn report_missing_gate_uses_policy_thresholds() {
   3311         let root = temp_dir_path("report_missing_gate_root");
   3312         let coverage_dir = root.join("contracts");
   3313         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3314         write_file(
   3315             &coverage_dir.join("coverage.toml"),
   3316             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3317         );
   3318         let out_path = root.join("gate-report.json");
   3319 
   3320         report_missing_gate_with_root(
   3321             &[
   3322                 "--scope".to_string(),
   3323                 "radroots_a_blocking".to_string(),
   3324                 "--out".to_string(),
   3325                 out_path.display().to_string(),
   3326                 "--reason".to_string(),
   3327                 "missing-coverage-artifacts".to_string(),
   3328             ],
   3329             &root,
   3330         )
   3331         .expect("report missing gate");
   3332 
   3333         let report_raw = fs::read_to_string(&out_path).expect("read gate report");
   3334         let report_json: serde_json::Value =
   3335             serde_json::from_str(&report_raw).expect("parse gate report json");
   3336         assert_eq!(
   3337             report_json["thresholds"]["executable_lines"],
   3338             serde_json::json!(100.0)
   3339         );
   3340         assert_eq!(
   3341             report_json["thresholds"]["branches_required"],
   3342             serde_json::json!(true)
   3343         );
   3344         assert_eq!(report_json["result"]["pass"], serde_json::json!(false));
   3345         assert_eq!(
   3346             report_json["result"]["fail_reasons"],
   3347             serde_json::json!(["missing-coverage-artifacts"])
   3348         );
   3349 
   3350         fs::remove_dir_all(root).expect("remove root");
   3351     }
   3352 
   3353     #[test]
   3354     fn report_missing_gate_uses_scope_specific_override_thresholds() {
   3355         let root = temp_dir_path("report_missing_gate_override_root");
   3356         let coverage_dir = root.join("contracts");
   3357         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3358         write_file(
   3359             &coverage_dir.join("coverage.toml"),
   3360             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = 88.5\nfail_under_functions = 77.5\nfail_under_regions = 66.5\nfail_under_branches = 55.5\nrequire_branches = false\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   3361         );
   3362         let out_path = root.join("gate-report.json");
   3363 
   3364         report_missing_gate_with_root(
   3365             &[
   3366                 "--scope".to_string(),
   3367                 "radroots_a".to_string(),
   3368                 "--out".to_string(),
   3369                 out_path.display().to_string(),
   3370                 "--reason".to_string(),
   3371                 "missing-coverage-artifacts".to_string(),
   3372             ],
   3373             &root,
   3374         )
   3375         .expect("report missing gate with override");
   3376 
   3377         let report_raw = fs::read_to_string(&out_path).expect("read gate report");
   3378         let report_json: serde_json::Value =
   3379             serde_json::from_str(&report_raw).expect("parse gate report json");
   3380         assert_eq!(
   3381             report_json["thresholds"]["executable_lines"],
   3382             serde_json::json!(88.5)
   3383         );
   3384         assert_eq!(
   3385             report_json["thresholds"]["functions"],
   3386             serde_json::json!(77.5)
   3387         );
   3388         assert_eq!(
   3389             report_json["thresholds"]["regions"],
   3390             serde_json::json!(66.5)
   3391         );
   3392         assert_eq!(
   3393             report_json["thresholds"]["branches"],
   3394             serde_json::json!(55.5)
   3395         );
   3396         assert_eq!(
   3397             report_json["thresholds"]["branches_required"],
   3398             serde_json::json!(false)
   3399         );
   3400 
   3401         fs::remove_dir_all(root).expect("remove override root");
   3402     }
   3403 
   3404     #[test]
   3405     fn report_missing_gate_reports_argument_policy_and_write_errors() {
   3406         let root = temp_dir_path("report_missing_gate_error_root");
   3407         let missing_scope =
   3408             report_missing_gate_with_root(&[], &root).expect_err("missing scope should fail");
   3409         assert!(missing_scope.contains("missing --scope"));
   3410 
   3411         let missing_out = report_missing_gate_with_root(
   3412             &[
   3413                 "--scope".to_string(),
   3414                 "radroots_a_blocking".to_string(),
   3415                 "--reason".to_string(),
   3416                 "missing-coverage-artifacts".to_string(),
   3417             ],
   3418             &root,
   3419         )
   3420         .expect_err("missing out should fail");
   3421         assert!(missing_out.contains("missing --out"));
   3422 
   3423         let missing_reason = report_missing_gate_with_root(
   3424             &[
   3425                 "--scope".to_string(),
   3426                 "radroots_a_blocking".to_string(),
   3427                 "--out".to_string(),
   3428                 root.join("missing-gate.json").display().to_string(),
   3429             ],
   3430             &root,
   3431         )
   3432         .expect_err("missing reason should fail");
   3433         assert!(missing_reason.contains("missing --reason"));
   3434 
   3435         let policy_err = report_missing_gate_with_root(
   3436             &[
   3437                 "--scope".to_string(),
   3438                 "radroots_a_blocking".to_string(),
   3439                 "--out".to_string(),
   3440                 root.join("missing-gate.json").display().to_string(),
   3441                 "--reason".to_string(),
   3442                 "missing-coverage-artifacts".to_string(),
   3443             ],
   3444             &root,
   3445         )
   3446         .expect_err("missing policy should fail");
   3447         assert!(policy_err.contains("failed to read coverage policy"));
   3448 
   3449         let coverage_dir = root.join("contracts");
   3450         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3451         write_file(
   3452             &coverage_dir.join("coverage.toml"),
   3453             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3454         );
   3455         let out_path = root.join("gate-report.json");
   3456         fs::create_dir_all(&out_path).expect("create blocking output dir");
   3457         let write_err = report_missing_gate_with_root(
   3458             &[
   3459                 "--scope".to_string(),
   3460                 "radroots_a_blocking".to_string(),
   3461                 "--out".to_string(),
   3462                 out_path.display().to_string(),
   3463                 "--reason".to_string(),
   3464                 "missing-coverage-artifacts".to_string(),
   3465             ],
   3466             &root,
   3467         )
   3468         .expect_err("directory output should fail");
   3469         assert!(write_err.contains("failed to write"));
   3470 
   3471         fs::remove_dir_all(root).expect("remove report missing gate error root");
   3472     }
   3473 
   3474     #[test]
   3475     fn refresh_summary_uses_measured_gate_report_values() {
   3476         let root = temp_dir_path("refresh_summary_root");
   3477         let coverage_dir = root.join("contracts");
   3478         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3479         write_file(
   3480             &coverage_dir.join("coverage.toml"),
   3481             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\", \"radroots_b\"]\n",
   3482         );
   3483 
   3484         let reports_root = root.join("target").join("coverage");
   3485         let crate_dir = reports_root.join("radroots_a");
   3486         fs::create_dir_all(&crate_dir).expect("create crate dir");
   3487         write_file(
   3488             &crate_dir.join("gate-report.json"),
   3489             r#"{
   3490   "scope": "radroots_a",
   3491   "thresholds": {
   3492     "executable_lines": 100.0,
   3493     "functions": 100.0,
   3494     "regions": 100.0,
   3495     "branches": 100.0,
   3496     "branches_required": true
   3497   },
   3498   "measured": {
   3499     "executable_lines_percent": 100.0,
   3500     "executable_lines_source": "da",
   3501     "functions_percent": 100.0,
   3502     "branches_percent": 100.0,
   3503     "branches_available": true,
   3504     "summary_lines_percent": 100.0,
   3505     "summary_regions_percent": 97.5
   3506   },
   3507   "counts": {
   3508     "executable_lines": {
   3509       "covered": 4,
   3510       "total": 4
   3511     },
   3512     "branches": {
   3513       "covered": 2,
   3514       "total": 2
   3515     }
   3516   },
   3517   "result": {
   3518     "pass": true,
   3519     "fail_reasons": []
   3520   }
   3521 }"#,
   3522         );
   3523         let no_branch_crate_dir = reports_root.join("radroots_b");
   3524         fs::create_dir_all(&no_branch_crate_dir).expect("create no branch crate dir");
   3525         write_file(
   3526             &no_branch_crate_dir.join("gate-report.json"),
   3527             r#"{
   3528   "scope": "radroots_b",
   3529   "thresholds": {
   3530     "executable_lines": 100.0,
   3531     "functions": 100.0,
   3532     "regions": 100.0,
   3533     "branches": 100.0,
   3534     "branches_required": false
   3535   },
   3536   "measured": {
   3537     "executable_lines_percent": 100.0,
   3538     "executable_lines_source": "da",
   3539     "functions_percent": 100.0,
   3540     "branches_percent": null,
   3541     "branches_available": false,
   3542     "summary_lines_percent": 100.0,
   3543     "summary_regions_percent": 100.0
   3544   },
   3545   "counts": {
   3546     "executable_lines": {
   3547       "covered": 4,
   3548       "total": 4
   3549     },
   3550     "branches": {
   3551       "covered": 0,
   3552       "total": 0
   3553     }
   3554   },
   3555   "result": {
   3556     "pass": true,
   3557     "fail_reasons": []
   3558   }
   3559 }"#,
   3560         );
   3561 
   3562         let refresh_out = reports_root.join("coverage-refresh.tsv");
   3563         let status_out = reports_root.join("coverage-refresh-status.tsv");
   3564         run_with_root(
   3565             &[
   3566                 "refresh-summary".to_string(),
   3567                 "--reports-root".to_string(),
   3568                 reports_root.display().to_string(),
   3569                 "--out".to_string(),
   3570                 refresh_out.display().to_string(),
   3571                 "--status-out".to_string(),
   3572                 status_out.display().to_string(),
   3573             ],
   3574             &root,
   3575         )
   3576         .expect("write refresh summary");
   3577 
   3578         let refresh = fs::read_to_string(&refresh_out).expect("read refresh summary");
   3579         assert!(refresh.contains("crate\tstatus\texec\tfunc\tbranch\tregion\treport"));
   3580         assert!(
   3581             refresh.contains("radroots_a\tpass\t100.000000\t100.000000\t100.000000\t97.500000\t")
   3582         );
   3583         assert!(
   3584             refresh.contains("radroots_b\tpass\t100.000000\t100.000000\tunavailable\t100.000000\t")
   3585         );
   3586 
   3587         let status = fs::read_to_string(&status_out).expect("read status summary");
   3588         assert_eq!(
   3589             status,
   3590             "crate\tstatus\nradroots_a\tpass\nradroots_b\tpass\n"
   3591         );
   3592 
   3593         fs::remove_dir_all(root).expect("remove root");
   3594 
   3595         let defaults_root = temp_dir_path("refresh_summary_defaults_root");
   3596         let defaults_coverage_dir = defaults_root.join("contracts");
   3597         fs::create_dir_all(&defaults_coverage_dir).expect("create defaults coverage dir");
   3598         write_file(
   3599             &defaults_coverage_dir.join("coverage.toml"),
   3600             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3601         );
   3602         write_file(
   3603             &defaults_root
   3604                 .join("target")
   3605                 .join("coverage")
   3606                 .join("radroots_a")
   3607                 .join("gate-report.json"),
   3608             r#"{
   3609   "scope": "radroots_a",
   3610   "thresholds": {
   3611     "executable_lines": 100.0,
   3612     "functions": 100.0,
   3613     "regions": 100.0,
   3614     "branches": 100.0,
   3615     "branches_required": true
   3616   },
   3617   "measured": {
   3618     "executable_lines_percent": 100.0,
   3619     "executable_lines_source": "da",
   3620     "functions_percent": 100.0,
   3621     "branches_percent": 100.0,
   3622     "branches_available": true,
   3623     "summary_lines_percent": 100.0,
   3624     "summary_regions_percent": 100.0
   3625   },
   3626   "counts": {
   3627     "executable_lines": {
   3628       "covered": 4,
   3629       "total": 4
   3630     },
   3631     "branches": {
   3632       "covered": 2,
   3633       "total": 2
   3634     }
   3635   },
   3636   "result": {
   3637     "pass": false,
   3638     "fail_reasons": ["synthetic-fail"]
   3639   }
   3640 }"#,
   3641         );
   3642 
   3643         let _guard = lock_cwd();
   3644         let previous_dir = std::env::current_dir().expect("read current dir");
   3645         std::env::set_current_dir(&defaults_root).expect("set current dir");
   3646         run_with_root(&["refresh-summary".to_string()], &defaults_root)
   3647             .expect("write refresh summary defaults");
   3648         let defaults_refresh = fs::read_to_string(
   3649             defaults_root
   3650                 .join("target")
   3651                 .join("coverage")
   3652                 .join("coverage-refresh.tsv"),
   3653         )
   3654         .expect("read defaults refresh summary");
   3655         assert!(
   3656             defaults_refresh
   3657                 .contains("radroots_a\tfail\t100.000000\t100.000000\t100.000000\t100.000000\t")
   3658         );
   3659 
   3660         let dispatch_root = temp_dir_path("refresh_summary_parentless_root");
   3661         let dispatch_coverage_dir = dispatch_root.join("contracts");
   3662         fs::create_dir_all(&dispatch_coverage_dir).expect("create dispatch coverage dir");
   3663         write_file(
   3664             &dispatch_coverage_dir.join("coverage.toml"),
   3665             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3666         );
   3667         write_file(
   3668             &dispatch_root.join("Cargo.toml"),
   3669             "[workspace]\nmembers = []\nresolver = \"2\"\n",
   3670         );
   3671         write_file(
   3672             &dispatch_root
   3673                 .join("target")
   3674                 .join("coverage")
   3675                 .join("radroots_a")
   3676                 .join("gate-report.json"),
   3677             r#"{
   3678   "scope": "radroots_a",
   3679   "thresholds": {
   3680     "executable_lines": 100.0,
   3681     "functions": 100.0,
   3682     "regions": 100.0,
   3683     "branches": 100.0,
   3684     "branches_required": true
   3685   },
   3686   "measured": {
   3687     "executable_lines_percent": 100.0,
   3688     "executable_lines_source": "da",
   3689     "functions_percent": 100.0,
   3690     "branches_percent": 100.0,
   3691     "branches_available": true,
   3692     "summary_lines_percent": 100.0,
   3693     "summary_regions_percent": 100.0
   3694   },
   3695   "counts": {
   3696     "executable_lines": {
   3697       "covered": 4,
   3698       "total": 4
   3699     },
   3700     "branches": {
   3701       "covered": 2,
   3702       "total": 2
   3703     }
   3704   },
   3705   "result": {
   3706     "pass": true,
   3707     "fail_reasons": []
   3708   }
   3709 }"#,
   3710         );
   3711         std::env::set_current_dir(&dispatch_root).expect("set dispatch current dir");
   3712         run_with_root(
   3713             &[
   3714                 "report-missing".to_string(),
   3715                 "--scope".to_string(),
   3716                 "radroots_a_blocking".to_string(),
   3717                 "--out".to_string(),
   3718                 "missing-gate.json".to_string(),
   3719                 "--reason".to_string(),
   3720                 "missing-coverage-artifacts".to_string(),
   3721             ],
   3722             &dispatch_root,
   3723         )
   3724         .expect("dispatch report-missing");
   3725         run_with_root(
   3726             &[
   3727                 "refresh-summary".to_string(),
   3728                 "--out".to_string(),
   3729                 "coverage-refresh.tsv".to_string(),
   3730                 "--status-out".to_string(),
   3731                 "coverage-refresh-status.tsv".to_string(),
   3732             ],
   3733             &dispatch_root,
   3734         )
   3735         .expect("dispatch refresh-summary");
   3736         std::env::set_current_dir(previous_dir).expect("restore current dir");
   3737 
   3738         assert!(dispatch_root.join("missing-gate.json").exists());
   3739         assert!(dispatch_root.join("coverage-refresh.tsv").exists());
   3740         assert!(dispatch_root.join("coverage-refresh-status.tsv").exists());
   3741 
   3742         fs::remove_dir_all(defaults_root).expect("remove defaults root");
   3743         fs::remove_dir_all(dispatch_root).expect("remove dispatch root");
   3744     }
   3745 
   3746     #[test]
   3747     fn refresh_summary_rejects_empty_output_paths() {
   3748         let root = temp_dir_path("refresh_summary_empty_paths_root");
   3749         let coverage_dir = root.join("contracts");
   3750         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3751         write_file(
   3752             &coverage_dir.join("coverage.toml"),
   3753             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3754         );
   3755         write_file(
   3756             &root
   3757                 .join("target")
   3758                 .join("coverage")
   3759                 .join("radroots_a")
   3760                 .join("gate-report.json"),
   3761             r#"{
   3762   "scope": "radroots_a",
   3763   "thresholds": {
   3764     "executable_lines": 100.0,
   3765     "functions": 100.0,
   3766     "regions": 100.0,
   3767     "branches": 100.0,
   3768     "branches_required": true
   3769   },
   3770   "measured": {
   3771     "executable_lines_percent": 100.0,
   3772     "executable_lines_source": "da",
   3773     "functions_percent": 100.0,
   3774     "branches_percent": 100.0,
   3775     "branches_available": true,
   3776     "summary_lines_percent": 100.0,
   3777     "summary_regions_percent": 100.0
   3778   },
   3779   "counts": {
   3780     "executable_lines": {
   3781       "covered": 4,
   3782       "total": 4
   3783     },
   3784     "branches": {
   3785       "covered": 2,
   3786       "total": 2
   3787     }
   3788   },
   3789   "result": {
   3790     "pass": true,
   3791     "fail_reasons": []
   3792   }
   3793 }"#,
   3794         );
   3795 
   3796         let out_err = run_with_root(
   3797             &[
   3798                 "refresh-summary".to_string(),
   3799                 "--reports-root".to_string(),
   3800                 root.join("target").join("coverage").display().to_string(),
   3801                 "--out".to_string(),
   3802                 String::new(),
   3803             ],
   3804             &root,
   3805         )
   3806         .expect_err("empty out path should fail");
   3807         assert!(out_err.contains("failed to write"));
   3808 
   3809         let status_err = run_with_root(
   3810             &[
   3811                 "refresh-summary".to_string(),
   3812                 "--reports-root".to_string(),
   3813                 root.join("target").join("coverage").display().to_string(),
   3814                 "--out".to_string(),
   3815                 root.join("target")
   3816                     .join("coverage")
   3817                     .join("coverage-refresh.tsv")
   3818                     .display()
   3819                     .to_string(),
   3820                 "--status-out".to_string(),
   3821                 String::new(),
   3822             ],
   3823             &root,
   3824         )
   3825         .expect_err("empty status out path should fail");
   3826         assert!(status_err.contains("failed to write"));
   3827 
   3828         fs::remove_dir_all(root).expect("remove empty path root");
   3829     }
   3830 
   3831     #[test]
   3832     fn refresh_summary_reports_output_parent_creation_failure() {
   3833         let root = temp_dir_path("refresh_summary_out_parent_fail");
   3834         let coverage_dir = root.join("contracts");
   3835         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3836         write_file(
   3837             &coverage_dir.join("coverage.toml"),
   3838             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3839         );
   3840         write_file(
   3841             &root
   3842                 .join("target")
   3843                 .join("coverage")
   3844                 .join("radroots_a")
   3845                 .join("gate-report.json"),
   3846             r#"{
   3847   "scope": "radroots_a",
   3848   "thresholds": {
   3849     "executable_lines": 100.0,
   3850     "functions": 100.0,
   3851     "regions": 100.0,
   3852     "branches": 100.0,
   3853     "branches_required": true
   3854   },
   3855   "measured": {
   3856     "executable_lines_percent": 100.0,
   3857     "executable_lines_source": "da",
   3858     "functions_percent": 100.0,
   3859     "branches_percent": 100.0,
   3860     "branches_available": true,
   3861     "summary_lines_percent": 100.0,
   3862     "summary_regions_percent": 100.0
   3863   },
   3864   "counts": {
   3865     "executable_lines": {
   3866       "covered": 4,
   3867       "total": 4
   3868     },
   3869     "branches": {
   3870       "covered": 2,
   3871       "total": 2
   3872     }
   3873   },
   3874   "result": {
   3875     "pass": true,
   3876     "fail_reasons": []
   3877   }
   3878 }"#,
   3879         );
   3880         write_file(&root.join("out-blocker"), "x");
   3881 
   3882         let err = run_with_root(
   3883             &[
   3884                 "refresh-summary".to_string(),
   3885                 "--reports-root".to_string(),
   3886                 root.join("target").join("coverage").display().to_string(),
   3887                 "--out".to_string(),
   3888                 root.join("out-blocker")
   3889                     .join("nested")
   3890                     .join("coverage-refresh.tsv")
   3891                     .display()
   3892                     .to_string(),
   3893             ],
   3894             &root,
   3895         )
   3896         .expect_err("out parent create failure should bubble up");
   3897         assert!(err.contains("failed to create"));
   3898 
   3899         fs::remove_dir_all(root).expect("remove out parent fail root");
   3900     }
   3901 
   3902     #[test]
   3903     fn refresh_summary_reports_status_output_parent_creation_failure() {
   3904         let root = temp_dir_path("refresh_summary_status_parent_fail");
   3905         let coverage_dir = root.join("contracts");
   3906         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3907         write_file(
   3908             &coverage_dir.join("coverage.toml"),
   3909             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3910         );
   3911         write_file(
   3912             &root
   3913                 .join("target")
   3914                 .join("coverage")
   3915                 .join("radroots_a")
   3916                 .join("gate-report.json"),
   3917             r#"{
   3918   "scope": "radroots_a",
   3919   "thresholds": {
   3920     "executable_lines": 100.0,
   3921     "functions": 100.0,
   3922     "regions": 100.0,
   3923     "branches": 100.0,
   3924     "branches_required": true
   3925   },
   3926   "measured": {
   3927     "executable_lines_percent": 100.0,
   3928     "executable_lines_source": "da",
   3929     "functions_percent": 100.0,
   3930     "branches_percent": 100.0,
   3931     "branches_available": true,
   3932     "summary_lines_percent": 100.0,
   3933     "summary_regions_percent": 100.0
   3934   },
   3935   "counts": {
   3936     "executable_lines": {
   3937       "covered": 4,
   3938       "total": 4
   3939     },
   3940     "branches": {
   3941       "covered": 2,
   3942       "total": 2
   3943     }
   3944   },
   3945   "result": {
   3946     "pass": true,
   3947     "fail_reasons": []
   3948   }
   3949 }"#,
   3950         );
   3951         write_file(&root.join("status-blocker"), "x");
   3952 
   3953         let err = run_with_root(
   3954             &[
   3955                 "refresh-summary".to_string(),
   3956                 "--reports-root".to_string(),
   3957                 root.join("target").join("coverage").display().to_string(),
   3958                 "--out".to_string(),
   3959                 root.join("target")
   3960                     .join("coverage")
   3961                     .join("coverage-refresh.tsv")
   3962                     .display()
   3963                     .to_string(),
   3964                 "--status-out".to_string(),
   3965                 root.join("status-blocker")
   3966                     .join("nested")
   3967                     .join("coverage-refresh-status.tsv")
   3968                     .display()
   3969                     .to_string(),
   3970             ],
   3971             &root,
   3972         )
   3973         .expect_err("status-out parent create failure should bubble up");
   3974         assert!(err.contains("failed to create"));
   3975 
   3976         fs::remove_dir_all(root).expect("remove status parent fail root");
   3977     }
   3978 
   3979     #[test]
   3980     fn refresh_summary_reports_policy_and_gate_report_errors() {
   3981         let root = temp_dir_path("refresh_summary_error_root");
   3982         let policy_err = run_with_root(
   3983             &[
   3984                 "refresh-summary".to_string(),
   3985                 "--reports-root".to_string(),
   3986                 root.join("target").join("coverage").display().to_string(),
   3987             ],
   3988             &root,
   3989         )
   3990         .expect_err("missing policy should fail");
   3991         assert!(policy_err.contains("failed to read coverage policy"));
   3992 
   3993         let coverage_dir = root.join("contracts");
   3994         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   3995         write_file(
   3996             &coverage_dir.join("coverage.toml"),
   3997             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n",
   3998         );
   3999         let gate_err = run_with_root(
   4000             &[
   4001                 "refresh-summary".to_string(),
   4002                 "--reports-root".to_string(),
   4003                 root.join("target").join("coverage").display().to_string(),
   4004             ],
   4005             &root,
   4006         )
   4007         .expect_err("missing gate report should fail");
   4008         assert!(gate_err.contains("failed to read gate report"));
   4009 
   4010         fs::remove_dir_all(root).expect("remove refresh summary error root");
   4011     }
   4012 
   4013     #[test]
   4014     fn recover_lock_covers_ok_and_poisoned_paths() {
   4015         let ok_lock: &'static Mutex<()> = Box::leak(Box::new(Mutex::new(())));
   4016         let _ok_guard = recover_lock(ok_lock);
   4017 
   4018         let poisoned_lock: &'static Mutex<()> = Box::leak(Box::new(Mutex::new(())));
   4019         let handle = std::thread::spawn(move || {
   4020             let _guard = poisoned_lock.lock().expect("lock poisoned mutex");
   4021             panic!("poison test mutex");
   4022         });
   4023         assert!(handle.join().is_err());
   4024 
   4025         let _poisoned_guard = recover_lock(poisoned_lock);
   4026     }
   4027 
   4028     #[test]
   4029     fn read_summary_reports_empty_data_error() {
   4030         let path = temp_file_path("summary_empty_data");
   4031         write_file(&path, r#"{"data":[]}"#);
   4032         let err = read_summary(&path).expect_err("summary without data should fail");
   4033         assert!(err.contains("summary data is empty"));
   4034         fs::remove_file(path).expect("remove empty summary");
   4035     }
   4036 
   4037     #[test]
   4038     fn reads_lcov_da_and_branch_metrics() {
   4039         let path = temp_file_path("lcov");
   4040         fs::write(
   4041             &path,
   4042             "DA:1,1\nDA:2,0\nDA:3,1\nBRDA:1,0,0,1\nBRDA:1,0,1,0\nBRDA:2,0,0,3\nBRDA:2,0,1,-\n",
   4043         )
   4044         .expect("write lcov");
   4045 
   4046         let lcov = read_lcov(&path).expect("parse lcov");
   4047         assert_eq!(lcov.executable_total, 3);
   4048         assert_eq!(lcov.executable_covered, 2);
   4049         assert!(lcov.branches_available);
   4050         assert_eq!(lcov.branch_total, 3);
   4051         assert_eq!(lcov.branch_covered, 2);
   4052         assert_eq!(lcov.branch_percent, Some(66.66666666666666));
   4053 
   4054         fs::remove_file(path).expect("remove lcov");
   4055     }
   4056 
   4057     #[test]
   4058     fn read_lcov_ignores_non_semantic_source_lines() {
   4059         let root = temp_dir_path("lcov_ignorable_source_lines");
   4060         let source = root.join("lib.rs");
   4061         write_file(
   4062             &source,
   4063             "pub fn live() {}\n)?\n])?;\n#[cfg(test)]\nmod tests {\n    fn fallback() {\n        panic!(\"unexpected fallback\");\n    }\n}\npub fn impossible() { unreachable!() }\npub fn expected() { panic!(\"expected branch\") }\n",
   4064         );
   4065         let path = root.join("lcov.info");
   4066         write_file(
   4067             &path,
   4068             &format!(
   4069                 "SF:{}\nDA:1,1\nDA:2,0\nDA:3,0\nDA:4,0\nDA:6,0\nDA:7,0\nDA:10,0\nDA:11,0\nBRDA:1,0,0,1\nBRDA:2,0,0,0\nBRDA:3,0,0,0\nBRDA:6,0,0,0\nBRDA:10,0,0,0\nBRDA:11,0,0,0\n",
   4070                 source.display()
   4071             ),
   4072         );
   4073 
   4074         let lcov = read_lcov(&path).expect("parse filtered lcov");
   4075         assert_eq!(lcov.executable_total, 1);
   4076         assert_eq!(lcov.executable_covered, 1);
   4077         assert_eq!(lcov.executable_percent, 100.0);
   4078         assert_eq!(lcov.branch_total, 1);
   4079         assert_eq!(lcov.branch_covered, 1);
   4080         assert_eq!(lcov.branch_percent, Some(100.0));
   4081 
   4082         fs::remove_dir_all(root).expect("remove filtered lcov root");
   4083     }
   4084 
   4085     #[test]
   4086     fn ignorable_lcov_source_lines_cover_missing_and_out_of_range_paths() {
   4087         let root = temp_dir_path("lcov_source_line_false_paths");
   4088         let source = root.join("lib.rs");
   4089         write_file(&source, "pub fn live() {}\n");
   4090         let mut cache = BTreeMap::new();
   4091 
   4092         assert!(!is_ignorable_lcov_source_line(
   4093             source.to_str().expect("utf-8 path"),
   4094             99,
   4095             &mut cache,
   4096         ));
   4097         assert!(!is_ignorable_lcov_source_line(
   4098             root.join("missing.rs").to_str().expect("utf-8 path"),
   4099             1,
   4100             &mut cache,
   4101         ));
   4102 
   4103         fs::remove_dir_all(root).expect("remove lcov false path root");
   4104     }
   4105 
   4106     #[test]
   4107     fn reads_lcov_branch_metrics_from_brf_brh_when_brda_missing() {
   4108         let path = temp_file_path("lcov_fallback");
   4109         fs::write(&path, "DA:1,1\nDA:2,1\nBRF:4\nBRH:3\n").expect("write lcov");
   4110 
   4111         let lcov = read_lcov(&path).expect("parse lcov");
   4112         assert!(lcov.branches_available);
   4113         assert_eq!(lcov.branch_total, 4);
   4114         assert_eq!(lcov.branch_covered, 3);
   4115         assert_eq!(lcov.branch_percent, Some(75.0));
   4116 
   4117         fs::remove_file(path).expect("remove lcov");
   4118     }
   4119 
   4120     #[test]
   4121     fn gate_fails_when_branch_data_is_required_but_missing() {
   4122         let summary = CoverageSummary {
   4123             functions_percent: 100.0,
   4124             summary_lines_percent: 100.0,
   4125             summary_regions_percent: 100.0,
   4126             normalized_executable_lines: None,
   4127             normalized_branches: None,
   4128         };
   4129         let lcov = LcovCoverage {
   4130             executable_total: 10,
   4131             executable_covered: 10,
   4132             executable_percent: 100.0,
   4133             executable_source: ExecutableSource::Da,
   4134             branch_total: 0,
   4135             branch_covered: 0,
   4136             branches_available: false,
   4137             branch_percent: None,
   4138         };
   4139         let thresholds = CoverageThresholds {
   4140             fail_under_exec_lines: 100.0,
   4141             fail_under_functions: 100.0,
   4142             fail_under_regions: 100.0,
   4143             fail_under_branches: 100.0,
   4144             require_branches: true,
   4145         };
   4146 
   4147         let gate = evaluate_gate(&summary, &lcov, thresholds);
   4148         assert!(!gate.pass);
   4149         assert!(
   4150             gate.fail_reasons
   4151                 .iter()
   4152                 .any(|reason| reason == "branches=unavailable")
   4153         );
   4154     }
   4155 
   4156     #[test]
   4157     fn reads_required_crates_and_rejects_duplicates() {
   4158         let path = temp_file_path("required_crates");
   4159         fs::write(
   4160             &path,
   4161             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"a\", \"b\"]\n",
   4162         )
   4163         .expect("write required crates");
   4164         let crates = read_required_crates(&path).expect("parse required crates");
   4165         assert_eq!(crates, vec!["a".to_string(), "b".to_string()]);
   4166         fs::remove_file(&path).expect("remove required crates");
   4167 
   4168         let dup_path = temp_file_path("required_crates_dup");
   4169         fs::write(
   4170             &dup_path,
   4171             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"a\", \"a\"]\n",
   4172         )
   4173         .expect("write dup required crates");
   4174         let err = read_required_crates(&dup_path).expect_err("duplicate required crates");
   4175         assert!(err.contains("duplicate crate a"));
   4176         fs::remove_file(dup_path).expect("remove dup required crates");
   4177     }
   4178 
   4179     #[test]
   4180     fn read_required_crates_reports_read_and_parse_errors() {
   4181         let missing = temp_file_path("required_missing");
   4182         let read_err = read_required_crates(&missing).expect_err("missing required file");
   4183         assert!(read_err.contains("failed to read coverage policy"));
   4184 
   4185         let invalid = temp_file_path("required_invalid");
   4186         write_file(&invalid, "not = [toml");
   4187         let parse_err = read_required_crates(&invalid).expect_err("invalid required file");
   4188         assert!(parse_err.contains("failed to parse coverage policy"));
   4189         fs::remove_file(invalid).expect("remove invalid required file");
   4190     }
   4191 
   4192     #[test]
   4193     fn reads_workspace_crates_and_contains_xtask() {
   4194         let root = workspace_root();
   4195         let crates = read_workspace_crates(&root).expect("workspace crates");
   4196         assert!(!crates.is_empty());
   4197         assert!(crates.iter().any(|crate_name| crate_name == "xtask"));
   4198     }
   4199 
   4200     #[test]
   4201     fn coverage_profiles_default_when_contract_file_is_missing() {
   4202         let root = temp_dir_path("profile_missing");
   4203         fs::create_dir_all(&root).expect("create root");
   4204         let profile = read_coverage_profile(&root, "radroots_log").expect("read profile");
   4205         assert!(!profile.no_default_features);
   4206         assert!(profile.features.is_empty());
   4207         assert_eq!(profile.test_threads, None);
   4208         assert!(profile.test_packages.is_empty());
   4209         fs::remove_dir_all(root).expect("remove root");
   4210     }
   4211 
   4212     #[test]
   4213     fn coverage_profiles_merge_defaults_and_crate_overrides() {
   4214         let root = temp_dir_path("profile_merge");
   4215         let coverage_dir = root.join("contracts");
   4216         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   4217         fs::write(
   4218             coverage_dir.join("coverage-profiles.toml"),
   4219             r#"[profiles.default]
   4220 no_default_features = false
   4221 features = ["std"]
   4222 test_threads = 2
   4223 
   4224 [profiles.crates."radroots_log"]
   4225 no_default_features = true
   4226 features = ["rt"]
   4227 "#,
   4228         )
   4229         .expect("write profiles");
   4230 
   4231         let app_profile = read_coverage_profile(&root, "radroots_log").expect("app profile");
   4232         assert!(app_profile.no_default_features);
   4233         assert_eq!(app_profile.features, vec!["rt".to_string()]);
   4234         assert_eq!(app_profile.test_threads, Some(2));
   4235         assert!(app_profile.test_packages.is_empty());
   4236 
   4237         let other_profile = read_coverage_profile(&root, "radroots_core").expect("other profile");
   4238         assert!(!other_profile.no_default_features);
   4239         assert_eq!(other_profile.features, vec!["std".to_string()]);
   4240         assert_eq!(other_profile.test_threads, Some(2));
   4241         assert!(other_profile.test_packages.is_empty());
   4242 
   4243         fs::remove_dir_all(root).expect("remove root");
   4244     }
   4245 
   4246     #[test]
   4247     fn standalone_fixture_coverage_uses_the_owned_manifest_and_preserves_target_filters() {
   4248         let root = temp_dir_path("native_fixture_profile");
   4249         let fixture = root.join("crates/radroots_ffi/tests/native");
   4250         write_file(
   4251             &root.join("Cargo.toml"),
   4252             "[workspace]\nmembers = [\"crates/radroots_ffi\"]\n",
   4253         );
   4254         write_file(
   4255             &root.join("crates/radroots_ffi/Cargo.toml"),
   4256             "[package]\nname = \"radroots_ffi\"\n",
   4257         );
   4258         write_file(
   4259             &fixture.join("Cargo.toml"),
   4260             "[package]\nname = \"radroots_native_binding_fixture\"\n[workspace]\n",
   4261         );
   4262         write_file(
   4263             &root.join("contracts/coverage-profiles.toml"),
   4264             "[profiles.crates.radroots_ffi]\ntest_manifest = \"crates/radroots_ffi/tests/native/Cargo.toml\"\ntest_packages = [\"radroots_native_binding_fixture\"]\nfeatures = [\"radroots_ffi/bindgen\"]\n",
   4265         );
   4266         let profile = read_coverage_profile(&root, "radroots_ffi").expect("owned fixture profile");
   4267         assert_eq!(profile.test_packages, ["radroots_native_binding_fixture"]);
   4268         let out = root.join("evidence");
   4269         let execution_root = fixture.canonicalize().expect("canonical fixture root");
   4270         let mut commands = Vec::new();
   4271         let mut runner = |command: Command, _: &str| {
   4272             let expected = if commands.len() == 1 {
   4273                 execution_root.as_path()
   4274             } else {
   4275                 root.as_path()
   4276             };
   4277             assert_eq!(command.get_current_dir(), Some(expected));
   4278             commands.push(
   4279                 command
   4280                     .get_args()
   4281                     .map(|arg| arg.to_string_lossy().into_owned())
   4282                     .collect::<Vec<_>>(),
   4283             );
   4284             Ok(())
   4285         };
   4286         run_crate_with_runner_at_root(
   4287             &[
   4288                 "--crate".into(),
   4289                 "radroots_ffi".into(),
   4290                 "--out".into(),
   4291                 out.display().to_string(),
   4292             ],
   4293             &root,
   4294             &mut runner,
   4295         )
   4296         .expect("fixture commands");
   4297         assert_eq!(commands.len(), 5);
   4298         assert!(
   4299             commands[1]
   4300                 .windows(2)
   4301                 .any(|pair| pair == ["-p", "radroots_native_binding_fixture"])
   4302         );
   4303         assert!(
   4304             commands[1]
   4305                 .windows(2)
   4306                 .any(|pair| pair == ["--dep-coverage", "radroots_ffi"])
   4307         );
   4308         for command in commands.iter().skip(2) {
   4309             assert!(
   4310                 command
   4311                     .windows(2)
   4312                     .any(|pair| pair == ["-p", "radroots_ffi"])
   4313             );
   4314             assert!(!command.iter().any(|arg| arg == "--dep-coverage"));
   4315         }
   4316         assert!(commands[2].iter().any(|arg| arg.contains("tests/")));
   4317         for path in [
   4318             "../Cargo.toml",
   4319             "/tmp/Cargo.toml",
   4320             "crates/other/tests/Cargo.toml",
   4321             "crates/radroots_ffi/tests/../Cargo.toml",
   4322             "crates/radroots_ffi/tests/missing/Cargo.toml",
   4323         ] {
   4324             assert!(
   4325                 coverage_fixture_manifest(&root, "radroots_ffi", Some(path)).is_err(),
   4326                 "{path}"
   4327             );
   4328         }
   4329         assert!(
   4330             coverage_fixture_manifest(
   4331                 &root,
   4332                 "unknown",
   4333                 Some("crates/radroots_ffi/tests/native/Cargo.toml")
   4334             )
   4335             .is_err()
   4336         );
   4337         #[cfg(unix)]
   4338         {
   4339             std::os::unix::fs::symlink(&fixture, root.join("crates/radroots_ffi/tests/linked"))
   4340                 .expect("fixture symlink");
   4341             assert!(
   4342                 coverage_fixture_manifest(
   4343                     &root,
   4344                     "radroots_ffi",
   4345                     Some("crates/radroots_ffi/tests/linked/Cargo.toml")
   4346                 )
   4347                 .is_err()
   4348             );
   4349         }
   4350         fs::remove_dir_all(root).expect("remove fixture profile");
   4351     }
   4352 
   4353     #[test]
   4354     fn coverage_profiles_accept_positive_test_threads() {
   4355         let root = temp_dir_path("profile_positive_threads");
   4356         let coverage_dir = root.join("contracts");
   4357         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   4358         fs::write(
   4359             coverage_dir.join("coverage-profiles.toml"),
   4360             r#"[profiles.crates."radroots_log"]
   4361 test_threads = 4
   4362 "#,
   4363         )
   4364         .expect("write profiles");
   4365         let profile =
   4366             read_coverage_profile(&root, "radroots_log").expect("valid positive thread profile");
   4367         assert_eq!(profile.test_threads, Some(4));
   4368         fs::remove_dir_all(root).expect("remove root");
   4369     }
   4370 
   4371     #[test]
   4372     fn coverage_profiles_resolve_validated_downstream_test_packages() {
   4373         let root = temp_dir_path("profile_downstream_packages");
   4374         write_file(
   4375             &root.join("Cargo.toml"),
   4376             "[workspace]\nmembers = [\"crates/target\", \"crates/downstream\"]\n",
   4377         );
   4378         write_file(
   4379             &root.join("crates/target/Cargo.toml"),
   4380             "[package]\nname = \"radroots_target\"\nversion = \"0.1.0-alpha\"\n",
   4381         );
   4382         write_file(
   4383             &root.join("crates/downstream/Cargo.toml"),
   4384             "[package]\nname = \"radroots_downstream\"\nversion = \"0.1.0-alpha\"\n",
   4385         );
   4386         write_file(
   4387             &root.join("contracts/coverage-profiles.toml"),
   4388             "[profiles.crates.\"radroots_target\"]\ntest_packages = [\"radroots_downstream\"]\n",
   4389         );
   4390 
   4391         let profile =
   4392             read_coverage_profile(&root, "radroots_target").expect("target coverage profile");
   4393         assert_eq!(
   4394             profile.test_packages,
   4395             vec!["radroots_downstream".to_string()]
   4396         );
   4397         fs::remove_dir_all(root).expect("remove root");
   4398     }
   4399 
   4400     #[test]
   4401     fn coverage_profiles_reject_invalid_downstream_test_packages() {
   4402         let root = temp_dir_path("profile_invalid_test_packages");
   4403         write_file(
   4404             &root.join("Cargo.toml"),
   4405             "[workspace]\nmembers = [\"crates/a\", \"crates/b\"]\n",
   4406         );
   4407         write_file(
   4408             &root.join("crates/a/Cargo.toml"),
   4409             "[package]\nname = \"radroots_a\"\nversion = \"0.1.0-alpha\"\n",
   4410         );
   4411         write_file(
   4412             &root.join("crates/b/Cargo.toml"),
   4413             "[package]\nname = \"radroots_b\"\nversion = \"0.1.0-alpha\"\n",
   4414         );
   4415         let profiles = root.join("contracts/coverage-profiles.toml");
   4416 
   4417         for (test_packages, expected) in [
   4418             ("[\"\"]", "empty test package"),
   4419             ("[\"radroots_a\"]", "repeats the target"),
   4420             ("[\"radroots_unknown\"]", "unknown test package"),
   4421             ("[\"radroots_b\", \"radroots_b\"]", "repeats test package"),
   4422         ] {
   4423             write_file(
   4424                 &profiles,
   4425                 &format!("[profiles.crates.\"radroots_a\"]\ntest_packages = {test_packages}\n"),
   4426             );
   4427             let err = read_coverage_profile(&root, "radroots_a")
   4428                 .expect_err("invalid downstream test package");
   4429             assert!(err.contains(expected), "unexpected error: {err}");
   4430         }
   4431 
   4432         fs::remove_dir_all(root).expect("remove invalid test package root");
   4433     }
   4434 
   4435     #[test]
   4436     fn coverage_profiles_reject_invalid_feature_and_thread_values() {
   4437         let root = temp_dir_path("profile_invalid");
   4438         let coverage_dir = root.join("contracts");
   4439         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   4440         fs::write(
   4441             coverage_dir.join("coverage-profiles.toml"),
   4442             r#"[profiles.crates."radroots_log"]
   4443 features = [""]
   4444 test_threads = 0
   4445 "#,
   4446         )
   4447         .expect("write profiles");
   4448 
   4449         let err = read_coverage_profile(&root, "radroots_log").expect_err("invalid profile");
   4450         assert!(
   4451             err.contains("empty feature value"),
   4452             "unexpected error: {err}"
   4453         );
   4454 
   4455         fs::remove_dir_all(root).expect("remove root");
   4456     }
   4457 
   4458     #[test]
   4459     fn coverage_profiles_reject_invalid_toml() {
   4460         let root = temp_dir_path("profile_invalid_toml");
   4461         let coverage_dir = root.join("contracts");
   4462         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   4463         fs::write(
   4464             coverage_dir.join("coverage-profiles.toml"),
   4465             "[profiles.default\n",
   4466         )
   4467         .expect("write invalid profiles");
   4468         let err = read_coverage_profile(&root, "radroots_log").expect_err("invalid toml");
   4469         assert!(err.contains("failed to parse"));
   4470         fs::remove_dir_all(root).expect("remove root");
   4471     }
   4472 
   4473     #[test]
   4474     fn coverage_profiles_reject_zero_test_threads_without_feature_error() {
   4475         let root = temp_dir_path("profile_invalid_threads");
   4476         let coverage_dir = root.join("contracts");
   4477         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   4478         fs::write(
   4479             coverage_dir.join("coverage-profiles.toml"),
   4480             r#"[profiles.crates."radroots_log"]
   4481 test_threads = 0
   4482 "#,
   4483         )
   4484         .expect("write profiles");
   4485 
   4486         let err = read_coverage_profile(&root, "radroots_log").expect_err("invalid thread count");
   4487         assert!(err.contains("test_threads > 0"));
   4488 
   4489         fs::remove_dir_all(root).expect("remove root");
   4490     }
   4491 
   4492     #[test]
   4493     fn parse_helpers_cover_success_and_error_paths() {
   4494         let args = vec![
   4495             "--scope".to_string(),
   4496             "crate-a".to_string(),
   4497             "--value".to_string(),
   4498             "3.5".to_string(),
   4499             "--threads".to_string(),
   4500             "4".to_string(),
   4501             "--flag".to_string(),
   4502         ];
   4503         assert_eq!(
   4504             parse_string_arg(&args, "scope").expect("scope value"),
   4505             "crate-a".to_string()
   4506         );
   4507         assert_eq!(
   4508             parse_optional_string_arg(&args, "scope").expect("optional scope"),
   4509             "crate-a".to_string()
   4510         );
   4511         assert_eq!(parse_f64_arg(&args, "value", 1.0).expect("f64 value"), 3.5);
   4512         assert_eq!(
   4513             parse_optional_u32_arg(&args, "threads").expect("u32 value"),
   4514             Some(4)
   4515         );
   4516         assert!(parse_bool_flag(&args, "flag"));
   4517         assert_eq!(parse_optional_string_arg(&args, "missing"), None);
   4518         assert_eq!(
   4519             parse_f64_arg(&args, "missing", 2.25).expect("default f64"),
   4520             2.25
   4521         );
   4522         assert_eq!(
   4523             parse_optional_u32_arg(&args, "missing").expect("missing u32"),
   4524             None
   4525         );
   4526 
   4527         let missing_err = parse_string_arg(&args, "absent").expect_err("missing arg");
   4528         assert!(missing_err.contains("missing --absent"));
   4529 
   4530         let missing_value = vec!["--scope".to_string()];
   4531         let missing_value_err =
   4532             parse_string_arg(&missing_value, "scope").expect_err("missing arg value");
   4533         assert!(missing_value_err.contains("missing value for --scope"));
   4534 
   4535         let invalid_f64 = vec!["--value".to_string(), "bad".to_string()];
   4536         let invalid_f64_err = parse_f64_arg(&invalid_f64, "value", 1.0).expect_err("invalid f64");
   4537         assert!(invalid_f64_err.contains("invalid --value value"));
   4538 
   4539         let invalid_u32 = vec!["--threads".to_string(), "bad".to_string()];
   4540         let invalid_u32_err =
   4541             parse_optional_u32_arg(&invalid_u32, "threads").expect_err("invalid u32");
   4542         assert!(invalid_u32_err.contains("invalid --threads value"));
   4543     }
   4544 
   4545     #[test]
   4546     fn executable_source_labels_cover_all_variants() {
   4547         assert_eq!(executable_source_label(ExecutableSource::Da), "da");
   4548         assert_eq!(executable_source_label(ExecutableSource::LfLh), "lf_lh");
   4549         assert_eq!(percentage(0, 0), 100.0);
   4550     }
   4551 
   4552     #[test]
   4553     fn read_required_crates_rejects_empty_and_blank_entries() {
   4554         let empty_path = temp_file_path("required_empty");
   4555         write_file(
   4556             &empty_path,
   4557             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = []\n",
   4558         );
   4559         let empty_err = read_required_crates(&empty_path).expect_err("empty required list");
   4560         assert!(empty_err.contains("must not be empty"));
   4561         fs::remove_file(&empty_path).expect("remove empty required file");
   4562 
   4563         let blank_path = temp_file_path("required_blank");
   4564         write_file(
   4565             &blank_path,
   4566             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"a\", \" \"]\n",
   4567         );
   4568         let blank_err = read_required_crates(&blank_path).expect_err("blank crate name");
   4569         assert!(blank_err.contains("empty crate name"));
   4570         fs::remove_file(&blank_path).expect("remove blank required file");
   4571     }
   4572 
   4573     #[test]
   4574     fn read_workspace_crates_rejects_invalid_workspace_shapes() {
   4575         let root_empty = temp_dir_path("workspace_empty_members");
   4576         write_file(
   4577             &root_empty.join("Cargo.toml"),
   4578             "[workspace]\nmembers = []\n",
   4579         );
   4580         let empty_err = read_workspace_crates(&root_empty).expect_err("empty workspace members");
   4581         assert!(empty_err.contains("must not be empty"));
   4582         fs::remove_dir_all(&root_empty).expect("remove empty members root");
   4583 
   4584         let root_blank = temp_dir_path("workspace_blank_package_name");
   4585         write_file(
   4586             &root_blank.join("Cargo.toml"),
   4587             "[workspace]\nmembers = [\"crates/a\"]\n",
   4588         );
   4589         write_file(
   4590             &root_blank.join("crates").join("a").join("Cargo.toml"),
   4591             "[package]\nname = \"\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
   4592         );
   4593         let blank_err = read_workspace_crates(&root_blank).expect_err("blank package name");
   4594         assert!(blank_err.contains("empty package name"));
   4595         fs::remove_dir_all(&root_blank).expect("remove blank package root");
   4596 
   4597         let root_duplicate = temp_dir_path("workspace_duplicate_package");
   4598         write_file(
   4599             &root_duplicate.join("Cargo.toml"),
   4600             "[workspace]\nmembers = [\"crates/a\", \"crates/b\"]\n",
   4601         );
   4602         let package_manifest =
   4603             "[package]\nname = \"duplicate\"\nversion = \"0.1.0\"\nedition = \"2024\"\n";
   4604         write_file(
   4605             &root_duplicate.join("crates").join("a").join("Cargo.toml"),
   4606             package_manifest,
   4607         );
   4608         write_file(
   4609             &root_duplicate.join("crates").join("b").join("Cargo.toml"),
   4610             package_manifest,
   4611         );
   4612         let dup_err = read_workspace_crates(&root_duplicate).expect_err("duplicate package names");
   4613         assert!(dup_err.contains("duplicate package name"));
   4614         fs::remove_dir_all(&root_duplicate).expect("remove duplicate package root");
   4615 
   4616         let root_parse = temp_dir_path("workspace_parse_error");
   4617         write_file(
   4618             &root_parse.join("Cargo.toml"),
   4619             "[workspace]\nmembers = [\"crates/a\"]\n",
   4620         );
   4621         write_file(
   4622             &root_parse.join("crates").join("a").join("Cargo.toml"),
   4623             "[package",
   4624         );
   4625         let parse_err = read_workspace_crates(&root_parse).expect_err("invalid package manifest");
   4626         assert!(parse_err.contains("failed to parse"));
   4627         fs::remove_dir_all(&root_parse).expect("remove parse package root");
   4628     }
   4629 
   4630     #[test]
   4631     fn parse_toml_reports_read_and_parse_errors() {
   4632         let missing = temp_file_path("parse_toml_missing");
   4633         let read_err =
   4634             parse_toml::<CoveragePolicyFile>(&missing).expect_err("missing file should fail");
   4635         assert!(read_err.contains("failed to read"));
   4636 
   4637         let invalid = temp_file_path("parse_toml_invalid");
   4638         write_file(&invalid, "[gate]\nfail_under_exec_lines = 100.0\n");
   4639         let parse_err =
   4640             parse_toml::<CoveragePolicyFile>(&invalid).expect_err("invalid toml should fail");
   4641         assert!(parse_err.contains("failed to parse"));
   4642         fs::remove_file(invalid).expect("remove invalid toml");
   4643 
   4644         let workspace_missing = temp_file_path("parse_toml_workspace_missing");
   4645         let workspace_read_err = parse_toml::<WorkspaceManifest>(&workspace_missing)
   4646             .expect_err("missing workspace manifest should fail");
   4647         assert!(workspace_read_err.contains("failed to read"));
   4648 
   4649         let workspace_invalid = temp_file_path("parse_toml_workspace_invalid");
   4650         write_file(&workspace_invalid, "[workspace");
   4651         let workspace_parse_err = parse_toml::<WorkspaceManifest>(&workspace_invalid)
   4652             .expect_err("invalid workspace manifest should fail");
   4653         assert!(workspace_parse_err.contains("failed to parse"));
   4654         fs::remove_file(workspace_invalid).expect("remove invalid workspace manifest");
   4655 
   4656         let package_missing = temp_file_path("parse_toml_package_missing");
   4657         let package_read_err = parse_toml::<PackageManifest>(&package_missing)
   4658             .expect_err("missing package manifest should fail");
   4659         assert!(package_read_err.contains("failed to read"));
   4660 
   4661         let package_invalid = temp_file_path("parse_toml_package_invalid");
   4662         write_file(&package_invalid, "[package");
   4663         let package_parse_err = parse_toml::<PackageManifest>(&package_invalid)
   4664             .expect_err("invalid package manifest should fail");
   4665         assert!(package_parse_err.contains("failed to parse"));
   4666         fs::remove_file(package_invalid).expect("remove invalid package manifest");
   4667 
   4668         let profiles_missing = temp_file_path("parse_toml_profiles_missing");
   4669         let profiles_read_err = parse_toml::<CoverageProfilesFile>(&profiles_missing)
   4670             .expect_err("missing coverage profiles should fail");
   4671         assert!(profiles_read_err.contains("failed to read"));
   4672 
   4673         let profiles_invalid = temp_file_path("parse_toml_profiles_invalid");
   4674         write_file(&profiles_invalid, "[profiles.default");
   4675         let profiles_parse_err = parse_toml::<CoverageProfilesFile>(&profiles_invalid)
   4676             .expect_err("invalid coverage profiles should fail");
   4677         assert!(profiles_parse_err.contains("failed to parse"));
   4678         fs::remove_file(profiles_invalid).expect("remove invalid coverage profiles");
   4679     }
   4680 
   4681     #[test]
   4682     fn parse_toml_parses_valid_coverage_required_contract() {
   4683         let valid = temp_file_path("parse_toml_valid");
   4684         write_file(
   4685             &valid,
   4686             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_core\"]\n",
   4687         );
   4688         let parsed = parse_toml::<CoveragePolicyFile>(&valid).expect("valid toml");
   4689         assert_eq!(parsed.required.crates, vec!["radroots_core".to_string()]);
   4690         fs::remove_file(valid).expect("remove valid toml");
   4691     }
   4692 
   4693     #[test]
   4694     fn read_lcov_rejects_invalid_records() {
   4695         let cases = vec![
   4696             ("invalid_da_shape", "DA:1\n", "invalid DA record"),
   4697             ("invalid_da_line", "DA:bad,1\n", "invalid DA line number"),
   4698             ("invalid_da_hits", "DA:1,bad\n", "invalid DA hit count"),
   4699             ("invalid_lf", "LF:bad\n", "invalid LF value"),
   4700             ("invalid_lh", "LH:bad\n", "invalid LH value"),
   4701             ("invalid_brf", "BRF:bad\n", "invalid BRF value"),
   4702             ("invalid_brh", "BRH:bad\n", "invalid BRH value"),
   4703             ("invalid_brda_shape", "BRDA:1,0,0\n", "invalid BRDA record"),
   4704             (
   4705                 "invalid_brda_line",
   4706                 "BRDA:bad,0,0,1\n",
   4707                 "invalid BRDA line number",
   4708             ),
   4709             (
   4710                 "invalid_brda_taken",
   4711                 "BRDA:1,0,0,bad\n",
   4712                 "invalid BRDA taken count",
   4713             ),
   4714             (
   4715                 "invalid_brda_extra",
   4716                 "BRDA:1,0,0,1,extra\n",
   4717                 "invalid BRDA record",
   4718             ),
   4719         ];
   4720         for (prefix, raw, expected) in cases {
   4721             let path = temp_file_path(prefix);
   4722             write_file(&path, raw);
   4723             let err = read_lcov(&path).expect_err("invalid lcov record");
   4724             assert!(
   4725                 err.contains(expected),
   4726                 "expected `{expected}` in `{err}` for case {prefix}"
   4727             );
   4728             fs::remove_file(path).expect("remove invalid lcov file");
   4729         }
   4730     }
   4731 
   4732     #[test]
   4733     fn read_lcov_reports_read_error() {
   4734         let missing = temp_file_path("lcov_missing");
   4735         let err = read_lcov(&missing).expect_err("missing lcov should fail");
   4736         assert!(err.contains("failed to read lcov"));
   4737     }
   4738 
   4739     #[test]
   4740     fn read_lcov_uses_lf_lh_when_da_is_missing_and_branches_absent() {
   4741         let path = temp_file_path("lcov_lf_lh");
   4742         fs::write(&path, "LF:4\nLH:3\n").expect("write lcov");
   4743         let parsed = read_lcov(&path).expect("parse lcov");
   4744         assert_eq!(executable_source_label(parsed.executable_source), "lf_lh");
   4745         assert_eq!(parsed.executable_total, 4);
   4746         assert_eq!(parsed.executable_covered, 3);
   4747         assert_eq!(parsed.executable_percent, 75.0);
   4748         assert!(!parsed.branches_available);
   4749         assert_eq!(parsed.branch_percent, None);
   4750         fs::remove_file(path).expect("remove lcov");
   4751     }
   4752 
   4753     #[test]
   4754     fn read_lcov_defaults_to_full_when_no_line_records_exist() {
   4755         let path = temp_file_path("lcov_empty");
   4756         write_file(&path, "TN:probe\n");
   4757         let parsed = read_lcov(&path).expect("parse lcov");
   4758         assert_eq!(parsed.executable_total, 0);
   4759         assert_eq!(parsed.executable_covered, 0);
   4760         assert_eq!(parsed.executable_percent, 100.0);
   4761         assert!(!parsed.branches_available);
   4762         assert_eq!(parsed.branch_percent, None);
   4763         fs::remove_file(path).expect("remove lcov");
   4764     }
   4765 
   4766     #[test]
   4767     fn evaluate_gate_collects_all_failure_reasons() {
   4768         let summary = CoverageSummary {
   4769             functions_percent: 40.0,
   4770             summary_lines_percent: 50.0,
   4771             summary_regions_percent: 60.0,
   4772             normalized_executable_lines: None,
   4773             normalized_branches: None,
   4774         };
   4775         let lcov = LcovCoverage {
   4776             executable_total: 20,
   4777             executable_covered: 10,
   4778             executable_percent: 50.0,
   4779             executable_source: ExecutableSource::Da,
   4780             branch_total: 10,
   4781             branch_covered: 3,
   4782             branches_available: true,
   4783             branch_percent: Some(30.0),
   4784         };
   4785         let thresholds = CoverageThresholds {
   4786             fail_under_exec_lines: 90.0,
   4787             fail_under_functions: 90.0,
   4788             fail_under_regions: 90.0,
   4789             fail_under_branches: 90.0,
   4790             require_branches: true,
   4791         };
   4792 
   4793         let gate = evaluate_gate(&summary, &lcov, thresholds);
   4794         assert!(!gate.pass);
   4795         assert!(
   4796             gate.fail_reasons
   4797                 .iter()
   4798                 .any(|reason| reason.contains("executable_lines"))
   4799         );
   4800         assert!(
   4801             gate.fail_reasons
   4802                 .iter()
   4803                 .any(|reason| reason.contains("functions"))
   4804         );
   4805         assert!(
   4806             gate.fail_reasons
   4807                 .iter()
   4808                 .any(|reason| reason.contains("regions"))
   4809         );
   4810         assert!(
   4811             gate.fail_reasons
   4812                 .iter()
   4813                 .any(|reason| reason.contains("branches"))
   4814         );
   4815     }
   4816 
   4817     #[test]
   4818     fn run_command_covers_success_and_failure() {
   4819         let mut ok = Command::new("sh");
   4820         ok.arg("-c").arg("exit 0");
   4821         run_command(ok, "shell ok").expect("run ok command");
   4822 
   4823         let mut fail = Command::new("sh");
   4824         fail.arg("-c").arg("exit 9");
   4825         let err = run_command(fail, "shell fail").expect_err("run failing command");
   4826         assert!(err.contains("shell fail failed with status"));
   4827 
   4828         let missing = Command::new("/definitely/not/a/real/command");
   4829         let err = run_command(missing, "shell missing").expect_err("missing command");
   4830         assert!(
   4831             err.contains("failed to run shell missing")
   4832                 || err.contains("shell missing failed with status"),
   4833             "unexpected missing-command classification: {err}"
   4834         );
   4835     }
   4836 
   4837     #[test]
   4838     fn apply_coverage_profile_flags_writes_expected_args() {
   4839         let profile = CoverageProfile {
   4840             no_default_features: true,
   4841             features: vec!["std".to_string(), "serde".to_string()],
   4842             test_threads: Some(2),
   4843             test_packages: Vec::new(),
   4844             test_manifest: None,
   4845         };
   4846         let mut command = Command::new("cargo");
   4847         apply_coverage_profile_flags(&mut command, &profile);
   4848         let args = command
   4849             .get_args()
   4850             .map(|arg| arg.to_string_lossy().to_string())
   4851             .collect::<Vec<_>>();
   4852         assert_eq!(
   4853             args,
   4854             vec![
   4855                 "--no-default-features".to_string(),
   4856                 "--features".to_string(),
   4857                 "std,serde".to_string()
   4858             ]
   4859         );
   4860     }
   4861 
   4862     #[test]
   4863     fn run_crate_with_runner_builds_all_command_steps() {
   4864         let out = temp_dir_path("run_crate_runner");
   4865         let args = vec![
   4866             "--crate".to_string(),
   4867             "radroots_core".to_string(),
   4868             "--out".to_string(),
   4869             out.display().to_string(),
   4870             "--test-threads".to_string(),
   4871             "3".to_string(),
   4872         ];
   4873         let mut names = Vec::new();
   4874         let mut rendered_commands = Vec::new();
   4875         let mut runner = |cmd: Command, name: &str| {
   4876             names.push(name.to_string());
   4877             let rendered = cmd
   4878                 .get_args()
   4879                 .map(|arg| arg.to_string_lossy().to_string())
   4880                 .collect::<Vec<_>>()
   4881                 .join(" ");
   4882             assert!(!rendered.is_empty());
   4883             rendered_commands.push(rendered);
   4884             Ok(())
   4885         };
   4886         run_crate_with_runner(&args, &mut runner).expect("run crate with stub runner");
   4887         assert_eq!(
   4888             names,
   4889             vec![
   4890                 "cargo llvm-cov clean".to_string(),
   4891                 "cargo llvm-cov --no-report".to_string(),
   4892                 "cargo llvm-cov report --json --summary-only".to_string(),
   4893                 "cargo llvm-cov report --json".to_string(),
   4894                 "cargo llvm-cov report --lcov".to_string(),
   4895             ]
   4896         );
   4897         assert!(rendered_commands[0].ends_with("llvm-cov clean"));
   4898         assert!(
   4899             rendered_commands
   4900                 .iter()
   4901                 .filter(|rendered| rendered.contains("report -p radroots_core"))
   4902                 .all(|rendered| rendered.contains("--ignore-filename-regex"))
   4903         );
   4904         assert!(
   4905             rendered_commands
   4906                 .iter()
   4907                 .filter(|rendered| rendered.contains("report -p radroots_core"))
   4908                 .all(|rendered| rendered.contains(COVERAGE_EXTERNAL_IGNORE_FILENAME_REGEX))
   4909         );
   4910         fs::remove_dir_all(out).expect("remove run crate output dir");
   4911     }
   4912 
   4913     #[test]
   4914     fn run_crate_credits_declared_downstream_tests_only_to_the_target_report() {
   4915         let root = temp_dir_path("run_crate_downstream_tests");
   4916         write_file(
   4917             &root.join("Cargo.toml"),
   4918             "[workspace]\nmembers = [\"crates/target\", \"crates/downstream\"]\n",
   4919         );
   4920         write_file(
   4921             &root.join("crates/target/Cargo.toml"),
   4922             "[package]\nname = \"radroots_target\"\nversion = \"0.1.0-alpha\"\n",
   4923         );
   4924         write_file(
   4925             &root.join("crates/downstream/Cargo.toml"),
   4926             "[package]\nname = \"radroots_downstream\"\nversion = \"0.1.0-alpha\"\n",
   4927         );
   4928         write_file(
   4929             &root.join("contracts/coverage-profiles.toml"),
   4930             "[profiles.crates.\"radroots_target\"]\ntest_packages = [\"radroots_downstream\"]\n",
   4931         );
   4932         let out = temp_dir_path("run_crate_downstream_tests");
   4933         let args = vec![
   4934             "--crate".to_string(),
   4935             "radroots_target".to_string(),
   4936             "--out".to_string(),
   4937             out.display().to_string(),
   4938         ];
   4939         let mut rendered_commands = Vec::new();
   4940         let mut runner = |cmd: Command, _name: &str| {
   4941             rendered_commands.push(
   4942                 cmd.get_args()
   4943                     .map(|arg| arg.to_string_lossy().to_string())
   4944                     .collect::<Vec<_>>()
   4945                     .join(" "),
   4946             );
   4947             Ok(())
   4948         };
   4949         run_crate_with_runner_at_root(&args, &root, &mut runner)
   4950             .expect("run crate with downstream tests");
   4951         let test_command = rendered_commands
   4952             .iter()
   4953             .find(|command| command.contains("--no-report"))
   4954             .expect("coverage test command");
   4955         assert!(test_command.contains("-p radroots_target"));
   4956         assert!(test_command.contains("-p radroots_downstream"));
   4957         for report_command in rendered_commands
   4958             .iter()
   4959             .filter(|command| command.starts_with("report "))
   4960         {
   4961             assert!(report_command.contains("-p radroots_target"));
   4962             assert!(!report_command.contains("-p radroots_downstream"));
   4963         }
   4964         fs::remove_dir_all(out).expect("remove downstream test output dir");
   4965         fs::remove_dir_all(root).expect("remove root");
   4966     }
   4967 
   4968     #[test]
   4969     fn coverage_ignore_filename_regex_excludes_external_and_sibling_workspace_paths() {
   4970         let root = workspace_root();
   4971         let ignore_regex =
   4972             coverage_ignore_filename_regex(&root, "radroots_core").expect("build ignore regex");
   4973         assert!(ignore_regex.contains(COVERAGE_EXTERNAL_IGNORE_FILENAME_REGEX));
   4974         assert!(ignore_regex.contains("crates/identity"));
   4975         assert!(ignore_regex.contains("crates/core/([^/]+/)*tests/"));
   4976         assert!(!ignore_regex.contains("crates/core/src"));
   4977     }
   4978 
   4979     #[test]
   4980     fn escape_regex_literal_escapes_regex_metacharacters() {
   4981         let escaped = escape_regex_literal(r"\.+*?()|[]{}^$");
   4982         assert_eq!(escaped, r"\\\.\+\*\?\(\)\|\[\]\{\}\^\$");
   4983     }
   4984 
   4985     #[test]
   4986     fn coverage_cargo_command_defaults_to_rustup_nightly() {
   4987         let cmd = coverage_cargo_command_with_override(None);
   4988         let mut args = Vec::new();
   4989         for arg in cmd.get_args() {
   4990             args.push(arg.to_string_lossy().to_string());
   4991         }
   4992 
   4993         assert_eq!(cmd.get_program().to_string_lossy(), "rustup");
   4994         assert_eq!(
   4995             args,
   4996             vec![
   4997                 "run".to_string(),
   4998                 "nightly".to_string(),
   4999                 "cargo".to_string()
   5000             ]
   5001         );
   5002     }
   5003 
   5004     #[test]
   5005     fn normalized_coverage_cargo_override_trims_and_filters_values() {
   5006         assert_eq!(
   5007             normalized_coverage_cargo_override(Some("  /tmp/cargo  ".to_string())),
   5008             Some("/tmp/cargo".to_string())
   5009         );
   5010         assert_eq!(
   5011             normalized_coverage_cargo_override(Some("   ".to_string())),
   5012             None
   5013         );
   5014         assert_eq!(normalized_coverage_cargo_override(None), None);
   5015     }
   5016 
   5017     fn assert_coverage_command_shapes(
   5018         cargo_cmd: Command,
   5019         llvm_cov_cmd: Command,
   5020         override_binary: Option<&str>,
   5021     ) {
   5022         match override_binary {
   5023             Some(binary) => assert_eq!(cargo_cmd.get_program().to_string_lossy(), binary),
   5024             None => assert_eq!(cargo_cmd.get_program().to_string_lossy(), "rustup"),
   5025         }
   5026 
   5027         let llvm_args = llvm_cov_cmd
   5028             .get_args()
   5029             .map(|arg| arg.to_string_lossy().to_string())
   5030             .collect::<Vec<_>>();
   5031         match override_binary {
   5032             Some(_) => assert_eq!(llvm_args, vec!["llvm-cov".to_string()]),
   5033             None => assert_eq!(
   5034                 llvm_args,
   5035                 vec![
   5036                     "run".to_string(),
   5037                     "nightly".to_string(),
   5038                     "cargo".to_string(),
   5039                     "llvm-cov".to_string()
   5040                 ]
   5041             ),
   5042         }
   5043     }
   5044 
   5045     #[test]
   5046     fn coverage_public_command_helpers_match_current_env_resolution() {
   5047         let mut default_llvm_cov_cmd = coverage_cargo_command_with_override(None);
   5048         default_llvm_cov_cmd.arg("llvm-cov");
   5049         assert_coverage_command_shapes(
   5050             coverage_cargo_command_with_override(None),
   5051             default_llvm_cov_cmd,
   5052             None,
   5053         );
   5054 
   5055         let explicit_binary = temp_dir_path("coverage_command_override")
   5056             .join("nightly-cargo")
   5057             .to_string_lossy()
   5058             .to_string();
   5059         let mut explicit_llvm_cov_cmd =
   5060             coverage_cargo_command_with_override(Some(&explicit_binary));
   5061         explicit_llvm_cov_cmd.arg("llvm-cov");
   5062         assert_coverage_command_shapes(
   5063             coverage_cargo_command_with_override(Some(&explicit_binary)),
   5064             explicit_llvm_cov_cmd,
   5065             Some(explicit_binary.as_str()),
   5066         );
   5067 
   5068         let override_binary =
   5069             normalized_coverage_cargo_override(std::env::var("RADROOTS_COVERAGE_CARGO").ok());
   5070         assert_coverage_command_shapes(
   5071             coverage_cargo_command(),
   5072             coverage_llvm_cov_command(),
   5073             override_binary.as_deref(),
   5074         );
   5075     }
   5076 
   5077     #[test]
   5078     fn configure_coverage_toolchain_env_sets_existing_binary_envs() {
   5079         let toolchain_dir = temp_dir_path("coverage_toolchain_env");
   5080         fs::create_dir_all(&toolchain_dir).expect("create toolchain env dir");
   5081         for binary in ["rustc", "rustdoc", "llvm-cov", "llvm-profdata"] {
   5082             write_file(&toolchain_dir.join(binary), "");
   5083         }
   5084 
   5085         let mut cmd = Command::new("cargo");
   5086         configure_coverage_toolchain_env(&mut cmd, &toolchain_dir);
   5087         let envs = collect_command_envs(&cmd);
   5088         assert_eq!(
   5089             envs.get("RUSTC"),
   5090             Some(&Some(
   5091                 toolchain_dir.join("rustc").to_string_lossy().to_string()
   5092             ))
   5093         );
   5094         assert_eq!(
   5095             envs.get("RUSTDOC"),
   5096             Some(&Some(
   5097                 toolchain_dir.join("rustdoc").to_string_lossy().to_string()
   5098             ))
   5099         );
   5100         assert_eq!(
   5101             envs.get("LLVM_COV"),
   5102             Some(&Some(
   5103                 toolchain_dir.join("llvm-cov").to_string_lossy().to_string()
   5104             ))
   5105         );
   5106         assert_eq!(
   5107             envs.get("LLVM_PROFDATA"),
   5108             Some(&Some(
   5109                 toolchain_dir
   5110                     .join("llvm-profdata")
   5111                     .to_string_lossy()
   5112                     .to_string()
   5113             ))
   5114         );
   5115 
   5116         fs::remove_dir_all(toolchain_dir).expect("remove toolchain env dir");
   5117     }
   5118 
   5119     #[test]
   5120     fn configure_coverage_toolchain_env_skips_missing_binary_envs() {
   5121         let toolchain_dir = temp_dir_path("coverage_toolchain_missing_env");
   5122         fs::create_dir_all(&toolchain_dir).expect("create missing env dir");
   5123 
   5124         let mut cmd = Command::new("cargo");
   5125         configure_coverage_toolchain_env(&mut cmd, &toolchain_dir);
   5126         let envs = collect_command_envs(&cmd);
   5127         assert!(!envs.contains_key("RUSTC"));
   5128         assert!(!envs.contains_key("RUSTDOC"));
   5129         assert!(!envs.contains_key("LLVM_COV"));
   5130         assert!(!envs.contains_key("LLVM_PROFDATA"));
   5131 
   5132         fs::remove_dir_all(toolchain_dir).expect("remove missing env dir");
   5133     }
   5134 
   5135     #[test]
   5136     fn coverage_cargo_command_override_variants_cover_parented_and_parentless_paths() {
   5137         let toolchain_dir = temp_dir_path("coverage_toolchain_override");
   5138         fs::create_dir_all(&toolchain_dir).expect("create toolchain dir");
   5139         for binary in [
   5140             "nightly-cargo",
   5141             "rustc",
   5142             "rustdoc",
   5143             "llvm-cov",
   5144             "llvm-profdata",
   5145         ] {
   5146             write_file(&toolchain_dir.join(binary), "");
   5147         }
   5148 
   5149         let default_cmd = coverage_cargo_command_with_override(None);
   5150         let mut args = Vec::new();
   5151         for arg in default_cmd.get_args() {
   5152             args.push(arg.to_string_lossy().to_string());
   5153         }
   5154         assert_eq!(default_cmd.get_program().to_string_lossy(), "rustup");
   5155         assert_eq!(
   5156             args,
   5157             vec![
   5158                 "run".to_string(),
   5159                 "nightly".to_string(),
   5160                 "cargo".to_string()
   5161             ]
   5162         );
   5163 
   5164         let override_binary = toolchain_dir.join("nightly-cargo");
   5165         let cmd = coverage_cargo_command_with_override(Some(
   5166             override_binary
   5167                 .to_str()
   5168                 .expect("override path should be utf-8"),
   5169         ));
   5170 
   5171         assert_eq!(
   5172             cmd.get_program().to_string_lossy(),
   5173             override_binary.to_string_lossy()
   5174         );
   5175         assert!(cmd.get_args().next().is_none());
   5176         let mut envs = collect_command_envs(&cmd);
   5177         envs.insert("MISSING".to_string(), None);
   5178         assert_eq!(
   5179             envs.get("RUSTC"),
   5180             Some(&Some(
   5181                 toolchain_dir.join("rustc").to_string_lossy().to_string()
   5182             ))
   5183         );
   5184         assert_eq!(
   5185             envs.get("RUSTDOC"),
   5186             Some(&Some(
   5187                 toolchain_dir.join("rustdoc").to_string_lossy().to_string()
   5188             ))
   5189         );
   5190         assert_eq!(
   5191             envs.get("LLVM_COV"),
   5192             Some(&Some(
   5193                 toolchain_dir.join("llvm-cov").to_string_lossy().to_string()
   5194             ))
   5195         );
   5196         assert_eq!(
   5197             envs.get("LLVM_PROFDATA"),
   5198             Some(&Some(
   5199                 toolchain_dir
   5200                     .join("llvm-profdata")
   5201                     .to_string_lossy()
   5202                     .to_string()
   5203             ))
   5204         );
   5205         let path_env = envs
   5206             .get("PATH")
   5207             .and_then(|value| value.as_ref())
   5208             .expect("override binary should prepend PATH");
   5209         assert!(path_env.starts_with(toolchain_dir.to_string_lossy().as_ref()));
   5210         let mut cmd = coverage_cargo_command_with_override(Some("/"));
   5211         cmd.env_remove("RUSTC");
   5212         cmd.env_remove("LLVM_COV");
   5213         assert_eq!(cmd.get_program().to_string_lossy(), "/");
   5214         let envs = collect_command_envs(&cmd);
   5215         assert_eq!(envs.get("RUSTC"), Some(&None));
   5216         assert_eq!(envs.get("LLVM_COV"), Some(&None));
   5217 
   5218         fs::remove_dir_all(toolchain_dir).expect("remove toolchain dir");
   5219     }
   5220 
   5221     #[test]
   5222     fn workspace_root_override_takes_precedence() {
   5223         let root = workspace_root_with_override(Some("/tmp/radroots-coverage-root"));
   5224         assert_eq!(root, PathBuf::from("/tmp/radroots-coverage-root"));
   5225 
   5226         let fallback = workspace_root_with_override(Some(""));
   5227         assert!(fallback.join("Cargo.toml").exists());
   5228 
   5229         let default_root = workspace_root_with_override(None);
   5230         assert!(default_root.join("Cargo.toml").exists());
   5231     }
   5232 
   5233     #[test]
   5234     fn prepend_toolchain_bin_to_path_covers_missing_and_existing_path_inputs() {
   5235         let toolchain_dir = PathBuf::from("/tmp/radroots-coverage-toolchain");
   5236         let no_path = prepend_toolchain_bin_to_path(&toolchain_dir, None);
   5237         assert_eq!(no_path, OsString::from(&toolchain_dir));
   5238 
   5239         let joined =
   5240             prepend_toolchain_bin_to_path(&toolchain_dir, Some(OsString::from("/usr/bin:/bin")));
   5241         let joined = joined.to_string_lossy().to_string();
   5242         assert!(joined.starts_with("/tmp/radroots-coverage-toolchain"));
   5243         assert!(joined.contains("/usr/bin"));
   5244     }
   5245 
   5246     #[test]
   5247     fn collect_command_envs_cover_helper_paths() {
   5248         let mut cmd = Command::new("sh");
   5249         cmd.env("PRESENT", "value");
   5250         cmd.env_remove("REMOVED");
   5251         let envs = collect_command_envs(&cmd);
   5252         assert_eq!(envs.get("PRESENT"), Some(&Some("value".to_string())));
   5253         assert_eq!(envs.get("REMOVED"), Some(&None));
   5254     }
   5255 
   5256     #[test]
   5257     fn ok_runner_helper_returns_success() {
   5258         let cmd = Command::new("true");
   5259         assert!(ok_runner(cmd, "noop").is_ok());
   5260     }
   5261 
   5262     #[test]
   5263     fn run_crate_with_runner_uses_default_output_dir_when_out_is_missing() {
   5264         let args = vec!["--crate".to_string(), "radroots_core".to_string()];
   5265         let mut output_path_seen = false;
   5266         let mut runner = |cmd: Command, _: &str| {
   5267             let rendered = cmd
   5268                 .get_args()
   5269                 .map(|arg| arg.to_string_lossy().to_string())
   5270                 .collect::<Vec<_>>();
   5271             if rendered
   5272                 .iter()
   5273                 .any(|arg| arg.ends_with("coverage-summary.json"))
   5274                 || rendered
   5275                     .iter()
   5276                     .any(|arg| arg.ends_with("coverage-details.json"))
   5277                 || rendered
   5278                     .iter()
   5279                     .any(|arg| arg.ends_with("coverage-lcov.info"))
   5280             {
   5281                 output_path_seen = true;
   5282             }
   5283             Ok(())
   5284         };
   5285         run_crate_with_runner(&args, &mut runner).expect("run crate with default out");
   5286         assert!(output_path_seen);
   5287     }
   5288 
   5289     #[test]
   5290     fn run_crate_with_runner_propagates_runner_failures() {
   5291         let out = temp_dir_path("run_crate_runner_fail");
   5292         let args = vec![
   5293             "--crate".to_string(),
   5294             "radroots_core".to_string(),
   5295             "--out".to_string(),
   5296             out.display().to_string(),
   5297         ];
   5298         let mut runner = |_: Command, _: &str| Err("runner failed".to_string());
   5299         let err =
   5300             run_crate_with_runner(&args, &mut runner).expect_err("runner failure should bubble up");
   5301         assert_eq!(err, "runner failed".to_string());
   5302         fs::remove_dir_all(out).expect("remove run crate failure output dir");
   5303         let root = temp_dir_path("run_crate_create_out_error");
   5304         write_file(&root.join("blocker"), "x");
   5305         let args = vec![
   5306             "--crate".to_string(),
   5307             "radroots_core".to_string(),
   5308             "--out".to_string(),
   5309             root.join("blocker").join("nested").display().to_string(),
   5310         ];
   5311         let mut runner = run_command;
   5312         let err = run_crate_with_runner(&args, &mut runner)
   5313             .expect_err("output dir create error should fail");
   5314         assert!(err.contains("failed to create"));
   5315         fs::remove_dir_all(root).expect("remove run crate create error root");
   5316     }
   5317 
   5318     #[test]
   5319     fn run_crate_wrapper_returns_missing_crate_error_without_running_commands() {
   5320         let err = run_crate(&[]).expect_err("missing crate flag");
   5321         assert!(err.contains("missing --crate"));
   5322     }
   5323 
   5324     #[test]
   5325     fn run_crate_with_runner_at_root_covers_profile_and_runner_error_paths() {
   5326         let write_minimal_workspace = |root: &Path| {
   5327             write_file(
   5328                 &root.join("Cargo.toml"),
   5329                 "[workspace]\nmembers = [\"crates/core\"]\n",
   5330             );
   5331             write_file(
   5332                 &root.join("crates").join("core").join("Cargo.toml"),
   5333                 "[package]\nname = \"radroots_core\"\nversion = \"0.1.0-alpha\"\nedition = \"2024\"\n",
   5334             );
   5335         };
   5336 
   5337         let profile_root = temp_dir_path("run_crate_profile_invalid");
   5338         write_minimal_workspace(&profile_root);
   5339         write_file(
   5340             &profile_root
   5341                 .join("contracts")
   5342                 .join("coverage-profiles.toml"),
   5343             "[profiles.default]\nfeatures = [\"\"]\n",
   5344         );
   5345         let profile_args = vec![
   5346             "--crate".to_string(),
   5347             "radroots_core".to_string(),
   5348             "--out".to_string(),
   5349             profile_root.join("out").display().to_string(),
   5350         ];
   5351         let mut runner = run_command;
   5352         let profile_err = run_crate_with_runner_at_root(&profile_args, &profile_root, &mut runner)
   5353             .expect_err("invalid profile should fail");
   5354         assert!(profile_err.contains("empty feature value"));
   5355         fs::remove_dir_all(&profile_root).expect("remove profile root");
   5356 
   5357         let thread_root = temp_dir_path("run_crate_bad_threads");
   5358         fs::create_dir_all(&thread_root).expect("create thread root");
   5359         write_minimal_workspace(&thread_root);
   5360         let thread_args = vec![
   5361             "--crate".to_string(),
   5362             "radroots_core".to_string(),
   5363             "--out".to_string(),
   5364             thread_root.join("out").display().to_string(),
   5365             "--test-threads".to_string(),
   5366             "bad".to_string(),
   5367         ];
   5368         let mut runner = run_command;
   5369         let thread_err = run_crate_with_runner_at_root(&thread_args, &thread_root, &mut runner)
   5370             .expect_err("invalid test threads should fail");
   5371         assert!(thread_err.contains("invalid --test-threads value"));
   5372         fs::remove_dir_all(&thread_root).expect("remove thread root");
   5373 
   5374         for fail_step in [2usize, 3usize, 4usize] {
   5375             let step_root = temp_dir_path("run_crate_step_fail");
   5376             write_minimal_workspace(&step_root);
   5377             let step_args = vec![
   5378                 "--crate".to_string(),
   5379                 "radroots_core".to_string(),
   5380                 "--out".to_string(),
   5381                 step_root.join("out").display().to_string(),
   5382             ];
   5383             let mut calls = 0usize;
   5384             let mut runner = |_: Command, name: &str| {
   5385                 calls += 1;
   5386                 if calls == fail_step {
   5387                     return Err(format!("runner failure at {name}"));
   5388                 }
   5389                 Ok(())
   5390             };
   5391             let err = run_crate_with_runner_at_root(&step_args, &step_root, &mut runner)
   5392                 .expect_err("runner should fail at selected step");
   5393             assert!(err.contains("runner failure at"));
   5394             fs::remove_dir_all(&step_root).expect("remove step root");
   5395         }
   5396     }
   5397 
   5398     #[test]
   5399     fn report_gate_writes_report_file_on_success() {
   5400         let root = temp_dir_path("report_gate_success");
   5401         let summary_path = root.join("summary.json");
   5402         let lcov_path = root.join("coverage.info");
   5403         let out_path = root.join("gate-report.json");
   5404         write_file(
   5405             &summary_path,
   5406             r#"{"data":[{"totals":{"functions":{"percent":100.0},"lines":{"percent":100.0},"regions":{"percent":100.0}}}]}"#,
   5407         );
   5408         write_file(&lcov_path, "DA:1,1\nBRDA:1,0,0,1\n");
   5409 
   5410         let args = vec![
   5411             "--scope".to_string(),
   5412             "crate-x".to_string(),
   5413             "--summary".to_string(),
   5414             summary_path.display().to_string(),
   5415             "--lcov".to_string(),
   5416             lcov_path.display().to_string(),
   5417             "--out".to_string(),
   5418             out_path.display().to_string(),
   5419             "--policy-gate".to_string(),
   5420         ];
   5421         report_gate(&args).expect("report gate success");
   5422         let report_raw = fs::read_to_string(&out_path).expect("read report");
   5423         assert!(report_raw.contains("\"scope\": \"crate-x\""));
   5424         assert!(report_raw.contains("\"regions\": 90.0"));
   5425         assert!(report_raw.contains("\"pass\": true"));
   5426         fs::remove_dir_all(root).expect("remove report gate success root");
   5427     }
   5428 
   5429     #[test]
   5430     fn report_gate_normalizes_duplicate_generic_records_from_details() {
   5431         let root = temp_dir_path("report_gate_normalized_generics");
   5432         let summary_path = root.join("summary.json");
   5433         let lcov_path = root.join("coverage.info");
   5434         let out_path = root.join("gate-report.json");
   5435         write_file(
   5436             &summary_path,
   5437             r#"{
   5438   "data": [
   5439     {
   5440       "totals": {
   5441         "functions": {"percent": 96.0},
   5442         "lines": {"percent": 99.0},
   5443         "regions": {"percent": 22.0}
   5444       }
   5445     }
   5446   ]
   5447 }"#,
   5448         );
   5449         write_file(
   5450             &root.join("coverage-details.json"),
   5451             r#"{
   5452   "data": [
   5453     {
   5454       "functions": [
   5455         {
   5456           "count": 4,
   5457           "filenames": ["/tmp/crates/runtime_manager/src/lib.rs"],
   5458           "regions": [
   5459             [10, 1, 12, 2, 4, 0, 0, 0],
   5460             [13, 1, 13, 8, 4, 0, 0, 0]
   5461           ]
   5462         },
   5463         {
   5464           "count": 0,
   5465           "filenames": ["/tmp/crates/runtime_manager/src/lib.rs"],
   5466           "regions": [
   5467             [10, 1, 12, 2, 0, 0, 0, 0],
   5468             [13, 1, 13, 8, 0, 0, 0, 0]
   5469           ]
   5470         }
   5471       ]
   5472     }
   5473   ]
   5474 }"#,
   5475         );
   5476         write_file(
   5477             &lcov_path,
   5478             "DA:1,1\nDA:2,0\nLF:2\nLH:1\nBRDA:1,0,0,1\nBRDA:2,0,0,0\n",
   5479         );
   5480 
   5481         let args = vec![
   5482             "--scope".to_string(),
   5483             "radroots_runtime_manager".to_string(),
   5484             "--summary".to_string(),
   5485             summary_path.display().to_string(),
   5486             "--lcov".to_string(),
   5487             lcov_path.display().to_string(),
   5488             "--out".to_string(),
   5489             out_path.display().to_string(),
   5490             "--fail-under-exec-lines".to_string(),
   5491             "50.0".to_string(),
   5492             "--fail-under-functions".to_string(),
   5493             "100.0".to_string(),
   5494             "--fail-under-regions".to_string(),
   5495             "100.0".to_string(),
   5496             "--fail-under-branches".to_string(),
   5497             "50.0".to_string(),
   5498         ];
   5499         report_gate(&args).expect("normalized report gate success");
   5500 
   5501         let report_raw = fs::read_to_string(&out_path).expect("read normalized report");
   5502         assert!(report_raw.contains("\"functions_percent\": 100.0"));
   5503         assert!(report_raw.contains("\"summary_regions_percent\": 100.0"));
   5504         assert!(report_raw.contains("\"pass\": true"));
   5505 
   5506         fs::remove_dir_all(root).expect("remove normalized report gate root");
   5507     }
   5508 
   5509     #[test]
   5510     fn report_gate_with_root_uses_scope_specific_override_thresholds() {
   5511         let root = temp_dir_path("report_gate_override_success");
   5512         let coverage_dir = root.join("contracts");
   5513         fs::create_dir_all(&coverage_dir).expect("create coverage dir");
   5514         write_file(
   5515             &coverage_dir.join("coverage.toml"),
   5516             "[gate]\nfail_under_exec_lines = 100.0\nfail_under_functions = 100.0\nfail_under_regions = 100.0\nfail_under_branches = 100.0\nrequire_branches = true\n\n[required]\ncrates = [\"radroots_a\"]\n\n[overrides.radroots_a]\nfail_under_exec_lines = 88.5\nfail_under_functions = 77.5\nfail_under_regions = 66.5\nfail_under_branches = 55.5\nrequire_branches = false\ntemporary = true\nreason = \"temporary publish unblocker\"\n",
   5517         );
   5518 
   5519         let summary_path = root.join("summary.json");
   5520         let lcov_path = root.join("coverage.info");
   5521         let out_path = root.join("gate-report.json");
   5522         write_file(
   5523             &summary_path,
   5524             r#"{"data":[{"totals":{"functions":{"percent":80.0},"lines":{"percent":88.5},"regions":{"percent":70.0}}}]}"#,
   5525         );
   5526         write_file(&lcov_path, "DA:1,1\nLF:1\nLH:1\nBRDA:1,0,0,1\n");
   5527 
   5528         report_gate_with_root(
   5529             &[
   5530                 "--scope".to_string(),
   5531                 "radroots_a".to_string(),
   5532                 "--summary".to_string(),
   5533                 summary_path.display().to_string(),
   5534                 "--lcov".to_string(),
   5535                 lcov_path.display().to_string(),
   5536                 "--out".to_string(),
   5537                 out_path.display().to_string(),
   5538                 "--policy-gate".to_string(),
   5539             ],
   5540             &root,
   5541         )
   5542         .expect("report gate should honor override");
   5543 
   5544         let report_raw = fs::read_to_string(&out_path).expect("read override report");
   5545         assert!(report_raw.contains("\"functions\": 77.5"));
   5546         assert!(report_raw.contains("\"regions\": 66.5"));
   5547         assert!(report_raw.contains("\"branches_required\": false"));
   5548         assert!(report_raw.contains("\"pass\": true"));
   5549 
   5550         fs::remove_dir_all(root).expect("remove report gate override root");
   5551     }
   5552 
   5553     #[test]
   5554     fn report_gate_returns_error_on_failed_thresholds() {
   5555         let root = temp_dir_path("report_gate_fail");
   5556         let summary_path = root.join("summary.json");
   5557         let lcov_path = root.join("coverage.info");
   5558         let out_path = root.join("gate-report.json");
   5559         write_file(
   5560             &summary_path,
   5561             r#"{"data":[{"totals":{"functions":{"percent":10.0},"lines":{"percent":10.0},"regions":{"percent":10.0}}}]}"#,
   5562         );
   5563         write_file(&lcov_path, "DA:1,0\nBRDA:1,0,0,0\n");
   5564 
   5565         let args = vec![
   5566             "--scope".to_string(),
   5567             "crate-y".to_string(),
   5568             "--summary".to_string(),
   5569             summary_path.display().to_string(),
   5570             "--lcov".to_string(),
   5571             lcov_path.display().to_string(),
   5572             "--out".to_string(),
   5573             out_path.display().to_string(),
   5574             "--fail-under-exec-lines".to_string(),
   5575             "100.0".to_string(),
   5576             "--fail-under-functions".to_string(),
   5577             "100.0".to_string(),
   5578             "--fail-under-regions".to_string(),
   5579             "100.0".to_string(),
   5580             "--fail-under-branches".to_string(),
   5581             "100.0".to_string(),
   5582         ];
   5583         let err = report_gate(&args).expect_err("report gate failure");
   5584         assert!(err.contains("coverage gate failed"));
   5585         fs::remove_dir_all(root).expect("remove report gate failure root");
   5586     }
   5587 
   5588     #[test]
   5589     fn report_gate_handles_nan_threshold_input() {
   5590         let root = temp_dir_path("report_gate_nan");
   5591         let summary_path = root.join("summary.json");
   5592         let lcov_path = root.join("coverage.info");
   5593         let out_path = root.join("gate-report.json");
   5594         write_file(
   5595             &summary_path,
   5596             r#"{"data":[{"totals":{"functions":{"percent":100.0},"lines":{"percent":100.0},"regions":{"percent":100.0}}}]}"#,
   5597         );
   5598         write_file(&lcov_path, "DA:1,1\nBRDA:1,0,0,1\n");
   5599 
   5600         let args = vec![
   5601             "--scope".to_string(),
   5602             "crate-nan".to_string(),
   5603             "--summary".to_string(),
   5604             summary_path.display().to_string(),
   5605             "--lcov".to_string(),
   5606             lcov_path.display().to_string(),
   5607             "--out".to_string(),
   5608             out_path.display().to_string(),
   5609             "--fail-under-functions".to_string(),
   5610             "NaN".to_string(),
   5611         ];
   5612         let err = report_gate(&args).expect_err("nan threshold should fail coverage gate");
   5613         assert!(err.contains("invalid --fail-under-functions value"));
   5614         fs::remove_dir_all(root).expect("remove report gate nan root");
   5615     }
   5616 
   5617     #[test]
   5618     fn report_gate_reports_write_failure() {
   5619         let root = temp_dir_path("report_gate_write_fail");
   5620         let summary_path = root.join("summary.json");
   5621         let lcov_path = root.join("coverage.info");
   5622         let out_path = root.join("gate-report.json");
   5623         write_file(
   5624             &summary_path,
   5625             r#"{"data":[{"totals":{"functions":{"percent":100.0},"lines":{"percent":100.0},"regions":{"percent":100.0}}}]}"#,
   5626         );
   5627         write_file(&lcov_path, "DA:1,1\nBRDA:1,0,0,1\n");
   5628         fs::create_dir_all(&out_path).expect("create directory at output path");
   5629 
   5630         let args = vec![
   5631             "--scope".to_string(),
   5632             "crate-write".to_string(),
   5633             "--summary".to_string(),
   5634             summary_path.display().to_string(),
   5635             "--lcov".to_string(),
   5636             lcov_path.display().to_string(),
   5637             "--out".to_string(),
   5638             out_path.display().to_string(),
   5639             "--policy-gate".to_string(),
   5640         ];
   5641         let err = report_gate(&args).expect_err("writing report to directory should fail");
   5642         assert!(err.contains("failed to write"));
   5643         fs::remove_dir_all(root).expect("remove report gate write root");
   5644     }
   5645 
   5646     #[test]
   5647     fn report_gate_logs_branch_unavailable_path() {
   5648         let root = temp_dir_path("report_gate_no_branches");
   5649         let summary_path = root.join("summary.json");
   5650         let lcov_path = root.join("coverage.info");
   5651         let out_path = root.join("gate-report.json");
   5652         write_file(
   5653             &summary_path,
   5654             r#"{"data":[{"totals":{"functions":{"percent":100.0},"lines":{"percent":100.0},"regions":{"percent":100.0}}}]}"#,
   5655         );
   5656         write_file(&lcov_path, "DA:1,1\n");
   5657 
   5658         let args = vec![
   5659             "--scope".to_string(),
   5660             "crate-no-branch".to_string(),
   5661             "--summary".to_string(),
   5662             summary_path.display().to_string(),
   5663             "--lcov".to_string(),
   5664             lcov_path.display().to_string(),
   5665             "--out".to_string(),
   5666             out_path.display().to_string(),
   5667             "--fail-under-exec-lines".to_string(),
   5668             "100.0".to_string(),
   5669             "--fail-under-functions".to_string(),
   5670             "100.0".to_string(),
   5671             "--fail-under-regions".to_string(),
   5672             "100.0".to_string(),
   5673             "--fail-under-branches".to_string(),
   5674             "100.0".to_string(),
   5675         ];
   5676         report_gate(&args).expect("report gate no branches");
   5677         let report_raw = fs::read_to_string(&out_path).expect("read report");
   5678         assert!(report_raw.contains("\"branches_available\": false"));
   5679         fs::remove_dir_all(root).expect("remove no branch report root");
   5680     }
   5681 
   5682     #[test]
   5683     fn report_gate_reports_argument_and_input_errors() {
   5684         let missing_scope = report_gate(&[]).expect_err("missing scope");
   5685         assert!(missing_scope.contains("missing --scope"));
   5686 
   5687         let missing_summary = report_gate(&["--scope".to_string(), "crate".to_string()])
   5688             .expect_err("missing summary");
   5689         assert!(missing_summary.contains("missing --summary"));
   5690 
   5691         let missing_lcov = report_gate(&[
   5692             "--scope".to_string(),
   5693             "crate".to_string(),
   5694             "--summary".to_string(),
   5695             "summary.json".to_string(),
   5696         ])
   5697         .expect_err("missing lcov");
   5698         assert!(missing_lcov.contains("missing --lcov"));
   5699 
   5700         let missing_out = report_gate(&[
   5701             "--scope".to_string(),
   5702             "crate".to_string(),
   5703             "--summary".to_string(),
   5704             "summary.json".to_string(),
   5705             "--lcov".to_string(),
   5706             "coverage.info".to_string(),
   5707         ])
   5708         .expect_err("missing out");
   5709         assert!(missing_out.contains("missing --out"));
   5710 
   5711         let root = temp_dir_path("report_gate_arg_errors");
   5712         let summary_path = root.join("summary.json");
   5713         let lcov_path = root.join("coverage.info");
   5714         let out_path = root.join("gate-report.json");
   5715         write_file(
   5716             &summary_path,
   5717             r#"{"data":[{"totals":{"functions":{"percent":100.0},"lines":{"percent":100.0},"regions":{"percent":100.0}}}]}"#,
   5718         );
   5719         write_file(&lcov_path, "DA:1,1\nBRDA:1,0,0,1\n");
   5720 
   5721         let invalid_functions = report_gate(&[
   5722             "--scope".to_string(),
   5723             "crate".to_string(),
   5724             "--summary".to_string(),
   5725             summary_path.display().to_string(),
   5726             "--lcov".to_string(),
   5727             lcov_path.display().to_string(),
   5728             "--out".to_string(),
   5729             out_path.display().to_string(),
   5730             "--fail-under-functions".to_string(),
   5731             "bad".to_string(),
   5732         ])
   5733         .expect_err("invalid functions threshold");
   5734         assert!(invalid_functions.contains("invalid --fail-under-functions value"));
   5735 
   5736         let invalid_exec = report_gate(&[
   5737             "--scope".to_string(),
   5738             "crate".to_string(),
   5739             "--summary".to_string(),
   5740             summary_path.display().to_string(),
   5741             "--lcov".to_string(),
   5742             lcov_path.display().to_string(),
   5743             "--out".to_string(),
   5744             out_path.display().to_string(),
   5745             "--fail-under-exec-lines".to_string(),
   5746             "bad".to_string(),
   5747         ])
   5748         .expect_err("invalid executable threshold");
   5749         assert!(invalid_exec.contains("invalid --fail-under-exec-lines value"));
   5750 
   5751         let invalid_regions = report_gate(&[
   5752             "--scope".to_string(),
   5753             "crate".to_string(),
   5754             "--summary".to_string(),
   5755             summary_path.display().to_string(),
   5756             "--lcov".to_string(),
   5757             lcov_path.display().to_string(),
   5758             "--out".to_string(),
   5759             out_path.display().to_string(),
   5760             "--fail-under-regions".to_string(),
   5761             "bad".to_string(),
   5762         ])
   5763         .expect_err("invalid regions threshold");
   5764         assert!(invalid_regions.contains("invalid --fail-under-regions value"));
   5765 
   5766         let invalid_branches = report_gate(&[
   5767             "--scope".to_string(),
   5768             "crate".to_string(),
   5769             "--summary".to_string(),
   5770             summary_path.display().to_string(),
   5771             "--lcov".to_string(),
   5772             lcov_path.display().to_string(),
   5773             "--out".to_string(),
   5774             out_path.display().to_string(),
   5775             "--fail-under-branches".to_string(),
   5776             "bad".to_string(),
   5777         ])
   5778         .expect_err("invalid branches threshold");
   5779         assert!(invalid_branches.contains("invalid --fail-under-branches value"));
   5780 
   5781         let missing_thresholds = report_gate(&[
   5782             "--scope".to_string(),
   5783             "crate".to_string(),
   5784             "--summary".to_string(),
   5785             summary_path.display().to_string(),
   5786             "--lcov".to_string(),
   5787             lcov_path.display().to_string(),
   5788             "--out".to_string(),
   5789             out_path.display().to_string(),
   5790         ])
   5791         .expect_err("missing thresholds");
   5792         assert!(missing_thresholds.contains("missing coverage thresholds"));
   5793 
   5794         let missing_functions = report_gate(&[
   5795             "--scope".to_string(),
   5796             "crate".to_string(),
   5797             "--summary".to_string(),
   5798             summary_path.display().to_string(),
   5799             "--lcov".to_string(),
   5800             lcov_path.display().to_string(),
   5801             "--out".to_string(),
   5802             out_path.display().to_string(),
   5803             "--fail-under-exec-lines".to_string(),
   5804             "100".to_string(),
   5805         ])
   5806         .expect_err("missing functions threshold");
   5807         assert!(missing_functions.contains("missing coverage thresholds"));
   5808 
   5809         let missing_regions = report_gate(&[
   5810             "--scope".to_string(),
   5811             "crate".to_string(),
   5812             "--summary".to_string(),
   5813             summary_path.display().to_string(),
   5814             "--lcov".to_string(),
   5815             lcov_path.display().to_string(),
   5816             "--out".to_string(),
   5817             out_path.display().to_string(),
   5818             "--fail-under-exec-lines".to_string(),
   5819             "100".to_string(),
   5820             "--fail-under-functions".to_string(),
   5821             "100".to_string(),
   5822         ])
   5823         .expect_err("missing regions threshold");
   5824         assert!(missing_regions.contains("missing coverage thresholds"));
   5825 
   5826         let missing_branches = report_gate(&[
   5827             "--scope".to_string(),
   5828             "crate".to_string(),
   5829             "--summary".to_string(),
   5830             summary_path.display().to_string(),
   5831             "--lcov".to_string(),
   5832             lcov_path.display().to_string(),
   5833             "--out".to_string(),
   5834             out_path.display().to_string(),
   5835             "--fail-under-exec-lines".to_string(),
   5836             "100".to_string(),
   5837             "--fail-under-functions".to_string(),
   5838             "100".to_string(),
   5839             "--fail-under-regions".to_string(),
   5840             "100".to_string(),
   5841         ])
   5842         .expect_err("missing branches threshold");
   5843         assert!(missing_branches.contains("missing coverage thresholds"));
   5844 
   5845         let missing_summary_file = report_gate(&[
   5846             "--scope".to_string(),
   5847             "crate".to_string(),
   5848             "--summary".to_string(),
   5849             root.join("missing-summary.json").display().to_string(),
   5850             "--lcov".to_string(),
   5851             lcov_path.display().to_string(),
   5852             "--out".to_string(),
   5853             out_path.display().to_string(),
   5854             "--policy-gate".to_string(),
   5855         ])
   5856         .expect_err("missing summary file should fail");
   5857         assert!(missing_summary_file.contains("failed to read summary"));
   5858 
   5859         let missing_gate_report = read_gate_report(&root.join("missing-gate-report.json"))
   5860             .expect_err("missing gate report should fail");
   5861         assert!(missing_gate_report.contains("failed to read gate report"));
   5862 
   5863         write_file(&out_path, "{not-json");
   5864         let invalid_gate_report = read_gate_report(&out_path).expect_err("invalid gate report");
   5865         assert!(invalid_gate_report.contains("failed to parse gate report"));
   5866 
   5867         let missing_lcov_file = report_gate(&[
   5868             "--scope".to_string(),
   5869             "crate".to_string(),
   5870             "--summary".to_string(),
   5871             summary_path.display().to_string(),
   5872             "--lcov".to_string(),
   5873             root.join("missing-lcov.info").display().to_string(),
   5874             "--out".to_string(),
   5875             out_path.display().to_string(),
   5876             "--policy-gate".to_string(),
   5877         ])
   5878         .expect_err("missing lcov file should fail");
   5879         assert!(missing_lcov_file.contains("failed to read lcov"));
   5880 
   5881         let mixed_policy_gate = report_gate(&[
   5882             "--scope".to_string(),
   5883             "crate".to_string(),
   5884             "--summary".to_string(),
   5885             summary_path.display().to_string(),
   5886             "--lcov".to_string(),
   5887             lcov_path.display().to_string(),
   5888             "--out".to_string(),
   5889             out_path.display().to_string(),
   5890             "--policy-gate".to_string(),
   5891             "--fail-under-functions".to_string(),
   5892             "100.0".to_string(),
   5893         ])
   5894         .expect_err("policy gate mixed with explicit thresholds");
   5895         assert!(mixed_policy_gate.contains("cannot be combined"));
   5896 
   5897         let mixed_policy_gate_regions = report_gate(&[
   5898             "--scope".to_string(),
   5899             "crate".to_string(),
   5900             "--summary".to_string(),
   5901             summary_path.display().to_string(),
   5902             "--lcov".to_string(),
   5903             lcov_path.display().to_string(),
   5904             "--out".to_string(),
   5905             out_path.display().to_string(),
   5906             "--policy-gate".to_string(),
   5907             "--fail-under-regions".to_string(),
   5908             "100.0".to_string(),
   5909         ])
   5910         .expect_err("policy gate mixed with regions threshold");
   5911         assert!(mixed_policy_gate_regions.contains("cannot be combined"));
   5912 
   5913         let mixed_policy_gate_branches_flag = report_gate(&[
   5914             "--scope".to_string(),
   5915             "crate".to_string(),
   5916             "--summary".to_string(),
   5917             summary_path.display().to_string(),
   5918             "--lcov".to_string(),
   5919             lcov_path.display().to_string(),
   5920             "--out".to_string(),
   5921             out_path.display().to_string(),
   5922             "--policy-gate".to_string(),
   5923             "--require-branches".to_string(),
   5924         ])
   5925         .expect_err("policy gate mixed with require-branches");
   5926         assert!(mixed_policy_gate_branches_flag.contains("cannot be combined"));
   5927 
   5928         fs::remove_dir_all(root).expect("remove report arg errors root");
   5929     }
   5930 
   5931     #[test]
   5932     fn coverage_ignore_filename_regex_reports_unknown_crate() {
   5933         let root = temp_dir_path("coverage_unknown_crate_root");
   5934         write_file(
   5935             &root.join("Cargo.toml"),
   5936             "[workspace]\nmembers = [\"crates/core\"]\n",
   5937         );
   5938         write_file(
   5939             &root.join("crates").join("core").join("Cargo.toml"),
   5940             "[package]\nname = \"radroots_core\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
   5941         );
   5942 
   5943         let err = coverage_ignore_filename_regex(&root, "radroots_missing")
   5944             .expect_err("unknown crate should fail");
   5945         assert!(err.contains("could not resolve crate directory"));
   5946 
   5947         fs::remove_dir_all(root).expect("remove unknown crate root");
   5948     }
   5949 
   5950     #[test]
   5951     fn coverage_ignore_filename_regex_reports_workspace_manifest_errors() {
   5952         let root = temp_dir_path("coverage_regex_workspace_error_root");
   5953         let read_err = coverage_ignore_filename_regex(&root, "radroots_core")
   5954             .expect_err("missing workspace manifest should fail");
   5955         assert!(read_err.contains("failed to read"));
   5956 
   5957         write_file(&root.join("Cargo.toml"), "[workspace");
   5958         let parse_err = coverage_ignore_filename_regex(&root, "radroots_core")
   5959             .expect_err("invalid workspace manifest should fail");
   5960         assert!(parse_err.contains("failed to parse"));
   5961 
   5962         fs::remove_dir_all(root).expect("remove workspace error root");
   5963     }
   5964 
   5965     #[test]
   5966     fn run_crate_with_runner_at_root_reports_ignore_filter_errors() {
   5967         let root = temp_dir_path("run_crate_ignore_filter_error");
   5968         write_file(
   5969             &root.join("Cargo.toml"),
   5970             "[workspace]\nmembers = [\"crates/other\"]\n",
   5971         );
   5972         write_file(
   5973             &root.join("crates").join("other").join("Cargo.toml"),
   5974             "[package]\nname = \"radroots_other\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
   5975         );
   5976         let args = vec![
   5977             "--crate".to_string(),
   5978             "radroots_core".to_string(),
   5979             "--out".to_string(),
   5980             root.join("target").join("coverage").display().to_string(),
   5981         ];
   5982         let mut runner = ok_runner;
   5983         let err = run_crate_with_runner_at_root(&args, &root, &mut runner)
   5984             .expect_err("missing crate coverage filter should fail");
   5985         assert!(err.contains("could not resolve crate directory"));
   5986 
   5987         fs::remove_dir_all(root).expect("remove run crate ignore filter root");
   5988     }
   5989 
   5990     #[test]
   5991     fn run_dispatches_subcommands_and_errors() {
   5992         run(&["help".to_string()]).expect("help subcommand");
   5993         run(&["required-crates".to_string()]).expect("required crates subcommand");
   5994         run(&["workspace-crates".to_string()]).expect("workspace crates subcommand");
   5995         let run_crate_err = run(&["run-crate".to_string()]).expect_err("run crate missing args");
   5996         assert!(run_crate_err.contains("missing --crate"));
   5997         let unknown_err = run(&["unknown".to_string()]).expect_err("unknown subcommand");
   5998         assert!(unknown_err.contains("unknown coverage subcommand"));
   5999         let missing_err = run(&[]).expect_err("missing subcommand");
   6000         assert!(missing_err.contains("missing coverage subcommand"));
   6001     }
   6002 
   6003     #[test]
   6004     fn list_root_helpers_report_missing_contract_files() {
   6005         let root = temp_dir_path("list_helper_missing");
   6006         fs::create_dir_all(&root).expect("create list helper root");
   6007         let mut output = Vec::new();
   6008         let required_err = list_required_crates_with_root(&root, &mut output)
   6009             .expect_err("missing required crates file should fail");
   6010         assert!(required_err.contains("failed to read coverage policy"));
   6011 
   6012         let workspace_err = list_workspace_crates_with_root(&root, &mut output)
   6013             .expect_err("missing workspace manifest should fail");
   6014         assert!(workspace_err.contains("inspect artifact path"));
   6015 
   6016         fs::remove_dir_all(root).expect("remove list helper root");
   6017     }
   6018 
   6019     #[test]
   6020     fn write_crate_names_output_covers_success_and_error_paths() {
   6021         let mut output = Vec::new();
   6022         write_crate_names_output(
   6023             &mut output,
   6024             vec!["radroots_a".to_string(), "radroots_b".to_string()],
   6025             "required crates",
   6026         )
   6027         .expect("write crate names");
   6028         let rendered = String::from_utf8(output).expect("utf8");
   6029         assert!(rendered.contains("radroots_a"));
   6030         assert!(rendered.contains("radroots_b"));
   6031 
   6032         let mut failing = FailingWriter;
   6033         let err = write_crate_names_output(
   6034             &mut failing,
   6035             vec!["radroots_a".to_string()],
   6036             "workspace crates",
   6037         )
   6038         .expect_err("writer failure");
   6039         assert!(err.contains("failed to write workspace crates output"));
   6040         failing.flush().expect("flush failing writer");
   6041     }
   6042 
   6043     #[test]
   6044     fn run_report_subcommand_dispatches_to_report_gate() {
   6045         let root = temp_dir_path("run_dispatch_report");
   6046         let summary_path = root.join("summary.json");
   6047         let lcov_path = root.join("coverage.info");
   6048         let out_path = root.join("gate-report.json");
   6049         write_file(
   6050             &summary_path,
   6051             r#"{"data":[{"totals":{"functions":{"percent":100.0},"lines":{"percent":100.0},"regions":{"percent":100.0}}}]}"#,
   6052         );
   6053         write_file(&lcov_path, "DA:1,1\nBRDA:1,0,0,1\n");
   6054 
   6055         run(&[
   6056             "report".to_string(),
   6057             "--scope".to_string(),
   6058             "dispatch".to_string(),
   6059             "--summary".to_string(),
   6060             summary_path.display().to_string(),
   6061             "--lcov".to_string(),
   6062             lcov_path.display().to_string(),
   6063             "--out".to_string(),
   6064             out_path.display().to_string(),
   6065             "--policy-gate".to_string(),
   6066         ])
   6067         .expect("dispatch report");
   6068         assert!(out_path.exists());
   6069         fs::remove_dir_all(root).expect("remove report dispatch root");
   6070     }
   6071 
   6072     #[test]
   6073     fn report_gate_with_root_reports_policy_read_errors() {
   6074         let root = temp_dir_path("report_gate_policy_root_error");
   6075         let summary_path = root.join("summary.json");
   6076         let lcov_path = root.join("coverage.info");
   6077         let out_path = root.join("gate-report.json");
   6078         write_file(
   6079             &summary_path,
   6080             r#"{"data":[{"totals":{"functions":{"percent":100.0},"lines":{"percent":100.0},"regions":{"percent":100.0}}}]}"#,
   6081         );
   6082         write_file(&lcov_path, "DA:1,1\nBRDA:1,0,0,1\n");
   6083 
   6084         let err = report_gate_with_root(
   6085             &[
   6086                 "--scope".to_string(),
   6087                 "crate-x".to_string(),
   6088                 "--summary".to_string(),
   6089                 summary_path.display().to_string(),
   6090                 "--lcov".to_string(),
   6091                 lcov_path.display().to_string(),
   6092                 "--out".to_string(),
   6093                 out_path.display().to_string(),
   6094                 "--policy-gate".to_string(),
   6095             ],
   6096             &root,
   6097         )
   6098         .expect_err("missing policy should fail");
   6099         assert!(err.contains("failed to read coverage policy"));
   6100 
   6101         fs::remove_dir_all(root).expect("remove report gate policy error root");
   6102     }
   6103 }