1use std::{
12 collections::{BTreeMap, BTreeSet},
13 ffi::OsString,
14 fs,
15 io::Write,
16 path::{Component, Path, PathBuf},
17 process::{Command, Output},
18 sync::{
19 Mutex,
20 atomic::{AtomicUsize, Ordering},
21 },
22 time::Instant,
23};
24
25use serde::Deserialize;
26use supercov_contracts::{
27 AttributionPrecision, ExecutionModel, FrontendAttribution, FrontendLimitation,
28 FrontendLimitationScope, FrontendRunDeclaration, FrontendRunnerDeclaration,
29 LANGUAGE_FRONTEND_PROTOCOL_VERSION, StructuralSource,
30};
31
32use crate::{
33 coverage_analysis::McdcVector,
34 coverage_report::{
35 CoverageManifest, CoverageModelDeclaration, CoveragePhase, CoverageReportRequest,
36 DecisionMeta, DecisionSnapshot, ExecutionScope, ExitCodeInput, PersistedCoverageModel,
37 RawTestResult, RuntimeEvent, RuntimeSnapshot, TestProvenance,
38 },
39 evidence_archive::EvidenceArchiveEntry,
40 rust_project::PreparedRustProject,
41 rust_runtime::{RustProbeObservation, read_rust_probe_directory},
42 rust_test_context::preflight_rust_test_contexts,
43};
44
45#[derive(Debug, Clone, PartialEq)]
46pub struct RustFrontendRun {
47 pub declaration: FrontendRunDeclaration,
48 pub request: CoverageReportRequest,
49 pub exit_code: i32,
50 pub artifacts: usize,
51 pub artifact_files: Vec<PathBuf>,
52 pub build_ms: f64,
53 pub execution_ms: f64,
54}
55
56impl RustFrontendRun {
57 pub fn archive_entries(&self) -> Result<Vec<EvidenceArchiveEntry>, serde_json::Error> {
58 let model = PersistedCoverageModel::from_declaration(
59 self.request
60 .coverage_model
61 .as_ref()
62 .expect("Rust frontend always declares a coverage model"),
63 )
64 .expect("Rust coverage model is contract-valid");
65 let mut entries = vec![
66 EvidenceArchiveEntry {
67 path: "coverage-model.json".into(),
68 contents: serde_json::to_vec(&model)?,
69 },
70 EvidenceArchiveEntry {
71 path: "frontend.json".into(),
72 contents: serde_json::to_vec(&self.declaration)?,
73 },
74 EvidenceArchiveEntry {
75 path: "manifest.json".into(),
76 contents: serde_json::to_vec(&self.request.manifest)?,
77 },
78 ];
79 for (index, result) in self.request.raw_results.iter().enumerate() {
80 entries.push(EvidenceArchiveEntry {
81 path: format!("results/{index:08}/mcdc.json"),
82 contents: serde_json::to_vec(result)?,
83 });
84 }
85 Ok(entries)
86 }
87}
88
89#[derive(Debug)]
90pub enum RustTestRunnerError {
91 UnsupportedCommand(String),
92 Launch(String),
93 CargoFailed(String),
94 CargoJson(String),
95 UnsafeArtifact(String),
96 ListFailed(String),
97 Probe(String),
98 Context(String),
99 UnknownProbe(String),
100 InvalidVector {
101 id: String,
102 expected: usize,
103 actual: usize,
104 },
105 Json(serde_json::Error),
106 Io(String),
107}
108
109impl std::fmt::Display for RustTestRunnerError {
110 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 match self {
112 Self::UnsupportedCommand(reason) => formatter.write_str(reason),
113 Self::Launch(reason) => {
114 write!(formatter, "could not launch Rust test process: {reason}")
115 }
116 Self::CargoFailed(reason) => write!(formatter, "Cargo test build failed: {reason}"),
117 Self::CargoJson(reason) => write!(formatter, "invalid Cargo JSON output: {reason}"),
118 Self::UnsafeArtifact(path) => {
119 write!(formatter, "Cargo emitted an unsafe test artifact: {path}")
120 }
121 Self::ListFailed(reason) => {
122 write!(formatter, "could not enumerate Rust tests: {reason}")
123 }
124 Self::Probe(reason) => write!(formatter, "invalid Rust probe evidence: {reason}"),
125 Self::Context(reason) => write!(formatter, "invalid Rust test context: {reason}"),
126 Self::UnknownProbe(id) => write!(
127 formatter,
128 "Rust runtime emitted an unknown obligation: {id}"
129 ),
130 Self::InvalidVector {
131 id,
132 expected,
133 actual,
134 } => write!(
135 formatter,
136 "Rust decision {id} emitted vector width {actual}; expected {expected}"
137 ),
138 Self::Json(error) => write!(formatter, "could not encode Rust evidence: {error}"),
139 Self::Io(reason) => formatter.write_str(reason),
140 }
141 }
142}
143
144impl std::error::Error for RustTestRunnerError {}
145
146impl From<serde_json::Error> for RustTestRunnerError {
147 fn from(value: serde_json::Error) -> Self {
148 Self::Json(value)
149 }
150}
151
152#[derive(Debug, Deserialize)]
153struct CargoMessage {
154 reason: String,
155 #[serde(default)]
156 target: Option<CargoArtifactTarget>,
157 #[serde(default)]
158 profile: Option<CargoArtifactProfile>,
159 executable: Option<PathBuf>,
160 manifest_path: Option<PathBuf>,
161}
162
163#[derive(Debug, Deserialize)]
164struct CargoArtifactTarget {
165 name: String,
166 kind: Vec<String>,
167 src_path: PathBuf,
168}
169
170#[derive(Debug, Deserialize)]
171struct CargoArtifactProfile {
172 test: bool,
173}
174
175#[derive(Debug, Clone)]
176struct TestArtifact {
177 executable: PathBuf,
178 name: String,
179 kind: String,
180 source: String,
181 package_directory: PathBuf,
184}
185
186#[derive(Debug)]
187struct ProcessTask {
188 ordinal: usize,
189 artifact_index: usize,
190 test_index: usize,
191 artifact: TestArtifact,
192 test: String,
193 context_id: u64,
194 directory: PathBuf,
195}
196
197#[derive(Debug)]
198struct ProcessOutcome {
199 task: ProcessTask,
200 output: Output,
201}
202
203fn shell_words(value: &str) -> Result<Vec<String>, RustTestRunnerError> {
204 let mut words = Vec::new();
205 let mut current = String::new();
206 let mut quote = None;
207 let mut escaped = false;
208 for character in value.chars() {
209 if escaped {
210 current.push(character);
211 escaped = false;
212 } else if character == '\\' && quote != Some('\'') {
213 escaped = true;
214 } else if matches!(character, '\'' | '"') {
215 if quote == Some(character) {
216 quote = None;
217 } else if quote.is_none() {
218 quote = Some(character);
219 } else {
220 current.push(character);
221 }
222 } else if character.is_whitespace() && quote.is_none() {
223 if !current.is_empty() {
224 words.push(std::mem::take(&mut current));
225 }
226 } else {
227 current.push(character);
228 }
229 }
230 if escaped || quote.is_some() {
231 return Err(RustTestRunnerError::UnsupportedCommand(
232 "the expanded Cargo command contains an incomplete quote or escape".into(),
233 ));
234 }
235 if !current.is_empty() {
236 words.push(current);
237 }
238 Ok(words)
239}
240
241fn executable_name(value: &str) -> &str {
242 Path::new(value)
243 .file_name()
244 .and_then(|name| name.to_str())
245 .unwrap_or(value)
246 .trim_end_matches(".exe")
247 .trim_end_matches(".cmd")
248}
249
250#[derive(Debug, Clone, PartialEq, Eq)]
251pub(crate) struct CargoTestInvocation {
252 pub program: String,
253 pub kind: RustCargoCommandKind,
254 pub arguments: Vec<String>,
255 pub runner_arguments: Vec<String>,
256}
257
258#[derive(Debug, Clone, Copy, PartialEq, Eq)]
259pub(crate) enum RustCargoCommandKind {
260 CargoTest,
261 NextestRun,
262}
263
264impl CargoTestInvocation {
265 pub(crate) fn command_position(&self) -> usize {
269 match self.kind {
270 RustCargoCommandKind::CargoTest => self
271 .arguments
272 .iter()
273 .position(|argument| argument == "test"),
274 RustCargoCommandKind::NextestRun => self
275 .arguments
276 .windows(2)
277 .position(|pair| pair == ["nextest", "run"]),
278 }
279 .expect("cargo_invocation keeps the test subcommand")
280 }
281}
282
283#[derive(Debug, Clone, PartialEq, Eq)]
284pub(crate) struct RustLibtestSelection {
285 pub list_arguments: Vec<String>,
286}
287
288#[derive(Debug, Clone, PartialEq, Eq)]
289pub(crate) struct RustCargoExecutionSelection {
290 pub run_libtests: bool,
291 pub run_doctests: bool,
292 pub doctest_arguments: Vec<String>,
293}
294
295#[derive(Debug, Clone, PartialEq, Eq)]
296pub(crate) struct NextestListInvocation {
297 pub arguments: Vec<String>,
298 pub runner_arguments: Vec<String>,
299}
300
301pub(crate) fn nextest_version_arguments(invocation: &CargoTestInvocation) -> Vec<String> {
302 let command = invocation.command_position();
303 let mut arguments = invocation.arguments[..command].to_vec();
304 arguments.extend(["nextest".into(), "--version".into()]);
305 arguments
306}
307
308fn nextest_run_only_option(argument: &str) -> Option<bool> {
309 let name = argument.split_once('=').map_or(argument, |(name, _)| name);
310 match name {
311 "-j"
312 | "--jobs"
313 | "--test-threads"
314 | "--retries"
315 | "--flaky-result"
316 | "--max-fail"
317 | "--no-tests"
318 | "--failure-output"
319 | "--success-output"
320 | "--status-level"
321 | "--final-status-level"
322 | "--show-progress"
323 | "--max-progress-running"
324 | "--message-format"
325 | "--message-format-version" => Some(!argument.contains('=')),
326 "--fail-fast"
327 | "--ff"
328 | "--no-fail-fast"
329 | "--nff"
330 | "--no-capture"
331 | "--nocapture"
332 | "--no-output-indent"
333 | "--hide-progress-bar"
334 | "--no-input-handler" => Some(false),
335 _ if argument.starts_with("-j") && argument.len() > 2 => Some(false),
336 _ => None,
337 }
338}
339
340fn nextest_unsupported_run_option(argument: &str) -> Option<bool> {
341 let name = argument.split_once('=').map_or(argument, |(name, _)| name);
342 match name {
343 "-R"
344 | "--rerun"
345 | "--debugger"
346 | "--tracer"
347 | "--stress-count"
348 | "--stress-duration"
349 | "--archive-file"
350 | "--archive-format"
351 | "--extract-to"
352 | "--cargo-metadata"
353 | "--workspace-remap"
354 | "--binaries-metadata"
355 | "--target-dir-remap"
356 | "--build-dir-remap" => Some(!argument.contains('=')),
357 "--no-run" | "--extract-overwrite" | "--persist-extract-tempdir" => Some(false),
358 _ => None,
359 }
360}
361
362fn nextest_shared_option(argument: &str) -> Option<bool> {
363 let name = argument.split_once('=').map_or(argument, |(name, _)| name);
364 match name {
365 "--color"
366 | "-p"
367 | "--package"
368 | "--exclude"
369 | "--bin"
370 | "--example"
371 | "--test"
372 | "--bench"
373 | "-F"
374 | "--features"
375 | "--build-jobs"
376 | "--cargo-profile"
377 | "--target"
378 | "--target-dir"
379 | "--cargo-message-format"
380 | "--config"
381 | "--timings"
382 | "-Z"
383 | "--run-ignored"
384 | "--partition"
385 | "--platform-filter"
386 | "-E"
387 | "--filterset"
388 | "--filter-expr"
389 | "--manifest-path"
390 | "--config-file"
391 | "--user-config-file"
392 | "--tool-config-file"
393 | "-P"
394 | "--profile" => Some(!argument.contains('=')),
395 "--no-pager"
396 | "-v"
397 | "--verbose"
398 | "--workspace"
399 | "--all"
400 | "--lib"
401 | "--bins"
402 | "--examples"
403 | "--tests"
404 | "--benches"
405 | "--all-targets"
406 | "--all-features"
407 | "--no-default-features"
408 | "-r"
409 | "--release"
410 | "--unit-graph"
411 | "--frozen"
412 | "--locked"
413 | "--offline"
414 | "--cargo-quiet"
415 | "--cargo-verbose"
416 | "--ignore-rust-version"
417 | "--future-incompat-report"
418 | "--ignore-default-filter"
419 | "--override-version-check" => Some(false),
420 _ if argument.starts_with("-p") && argument.len() > 2 => Some(false),
421 _ if argument.starts_with("-F") && argument.len() > 2 => Some(false),
422 _ if argument.starts_with("-E") && argument.len() > 2 => Some(false),
423 _ if argument.starts_with("-P") && argument.len() > 2 => Some(false),
424 _ if argument.starts_with("-Z") && argument.len() > 2 => Some(false),
425 _ if argument.len() > 2
426 && argument.starts_with('-')
427 && argument[1..].bytes().all(|byte| byte == b'v') =>
428 {
429 Some(false)
430 }
431 _ if argument.starts_with("--timings=") => Some(false),
432 _ => None,
433 }
434}
435
436pub(crate) fn nextest_list_invocation(
442 invocation: &CargoTestInvocation,
443) -> Result<NextestListInvocation, RustTestRunnerError> {
444 let command = invocation.command_position();
445 let mut arguments = invocation.arguments[..command].to_vec();
446 arguments.extend(["nextest".into(), "list".into()]);
447 let mut index = command + 2;
448 while index < invocation.arguments.len() {
449 let argument = &invocation.arguments[index];
450 if argument == "--" {
451 arguments.extend(invocation.arguments[index..].iter().cloned());
452 break;
453 }
454 if let Some(takes_value) = nextest_unsupported_run_option(argument) {
455 if takes_value && invocation.arguments.get(index + 1).is_none() {
456 return Err(RustTestRunnerError::UnsupportedCommand(format!(
457 "nextest option {argument} has no value"
458 )));
459 }
460 return Err(RustTestRunnerError::UnsupportedCommand(format!(
461 "nextest option {argument} cannot yet be assigned exact selected-test identity"
462 )));
463 }
464 if let Some(takes_value) = nextest_run_only_option(argument) {
465 if takes_value {
466 index += 1;
467 if index == invocation.arguments.len() {
468 return Err(RustTestRunnerError::UnsupportedCommand(format!(
469 "nextest option {argument} has no value"
470 )));
471 }
472 }
473 } else if let Some(takes_value) = nextest_shared_option(argument) {
474 arguments.push(argument.clone());
475 if takes_value {
476 index += 1;
477 let value = invocation.arguments.get(index).ok_or_else(|| {
478 RustTestRunnerError::UnsupportedCommand(format!(
479 "nextest option {argument} has no value"
480 ))
481 })?;
482 arguments.push(value.clone());
483 }
484 } else if argument.starts_with('-') {
485 return Err(RustTestRunnerError::UnsupportedCommand(format!(
486 "the pinned nextest run contract does not recognize option {argument}"
487 )));
488 } else {
489 arguments.push(argument.clone());
490 }
491 index += 1;
492 }
493 arguments.extend(["--message-format".into(), "json".into()]);
494 Ok(NextestListInvocation {
495 arguments,
496 runner_arguments: invocation.runner_arguments.clone(),
497 })
498}
499
500pub(crate) fn cargo_invocation(
501 root: &Path,
502 command: &[String],
503) -> Result<CargoTestInvocation, RustTestRunnerError> {
504 let words = if command.iter().any(|word| executable_name(word) == "cargo") {
509 command.to_vec()
510 } else {
511 let expanded = crate::project_discovery::expanded_command(root, command);
512 shell_words(&expanded)?
513 };
514 let cargo = words
515 .iter()
516 .position(|word| executable_name(word) == "cargo")
517 .ok_or_else(|| RustTestRunnerError::UnsupportedCommand(
518 "Rust was detected, but the expanded command does not expose a stable Cargo invocation".into(),
519 ))?;
520 let cargo_test = words[cargo + 1..]
521 .iter()
522 .position(|word| word == "test")
523 .map(|position| cargo + 1 + position);
524 let nextest = words[cargo + 1..]
525 .windows(2)
526 .position(|pair| pair == ["nextest", "run"])
527 .map(|position| cargo + 1 + position);
528 let (kind, command) = match (cargo_test, nextest) {
529 (Some(test), None) => (RustCargoCommandKind::CargoTest, test),
530 (None, Some(nextest)) => (RustCargoCommandKind::NextestRun, nextest),
531 (Some(_), Some(_)) => {
532 return Err(RustTestRunnerError::UnsupportedCommand(
533 "the Cargo invocation ambiguously contains both test and nextest run".into(),
534 ));
535 }
536 (None, None) => {
537 return Err(RustTestRunnerError::UnsupportedCommand(
538 "the owned Rust runner currently requires `cargo test` or `cargo nextest run`; cross remains explicitly unsupported"
539 .into(),
540 ));
541 }
542 };
543 if words[cargo + 1..command]
544 .iter()
545 .any(|word| matches!(word.as_str(), "&&" | "||" | ";" | "|"))
546 {
547 return Err(RustTestRunnerError::UnsupportedCommand(
548 "the Cargo invocation contains a shell boundary before `test`".into(),
549 ));
550 }
551 let command_end = command
552 + if kind == RustCargoCommandKind::NextestRun {
553 1
554 } else {
555 0
556 };
557 let mut arguments = words[cargo + 1..=command_end].to_vec();
558 let mut runner_arguments = Vec::new();
559 let mut after_separator = false;
560 for argument in &words[command_end + 1..] {
561 if argument == "--" && !after_separator {
562 after_separator = true;
563 continue;
564 }
565 if matches!(argument.as_str(), "&&" | "||" | ";" | "|") {
566 return Err(RustTestRunnerError::UnsupportedCommand(
567 "the Cargo test command contains an unsupported shell boundary".into(),
568 ));
569 }
570 if after_separator {
571 runner_arguments.push(argument.clone());
572 } else {
573 arguments.push(argument.clone());
574 }
575 }
576 Ok(CargoTestInvocation {
577 program: words[cargo].clone(),
578 kind,
579 arguments,
580 runner_arguments,
581 })
582}
583
584fn cargo_option_takes_value(argument: &str) -> Option<bool> {
585 let name = argument.split_once('=').map_or(argument, |(name, _)| name);
586 match name {
587 "-p" | "--package" | "--exclude" | "--bin" | "--example" | "--test" | "--bench" | "-F"
588 | "--features" | "-j" | "--jobs" | "--profile" | "--target" | "--target-dir"
589 | "--message-format" | "--color" | "--config" | "-Z" | "--manifest-path" => {
590 Some(!argument.contains('='))
591 }
592 "--no-run"
593 | "--no-fail-fast"
594 | "--future-incompat-report"
595 | "-q"
596 | "--quiet"
597 | "-v"
598 | "--verbose"
599 | "--workspace"
600 | "--all"
601 | "--lib"
602 | "--bins"
603 | "--examples"
604 | "--tests"
605 | "--benches"
606 | "--all-targets"
607 | "--doc"
608 | "--all-features"
609 | "--no-default-features"
610 | "-r"
611 | "--release"
612 | "--timings"
613 | "--ignore-rust-version"
614 | "--locked"
615 | "--offline"
616 | "--frozen" => Some(false),
617 _ if argument.starts_with("-vv") => Some(false),
618 _ if argument.starts_with("-p") && argument.len() > 2 => Some(false),
619 _ if argument.starts_with("-F") && argument.len() > 2 => Some(false),
620 _ if argument.starts_with("-j") && argument.len() > 2 => Some(false),
621 _ => None,
622 }
623}
624
625pub(crate) fn rust_libtest_selection(
626 invocation: &CargoTestInvocation,
627) -> Result<RustLibtestSelection, RustTestRunnerError> {
628 let mut cargo_filter = None;
631 let mut index = invocation.command_position() + 1;
632 while index < invocation.arguments.len() {
633 let argument = &invocation.arguments[index];
634 if argument.starts_with('-') {
635 if cargo_option_takes_value(argument) == Some(true) {
636 index += 1;
637 }
638 } else {
639 cargo_filter = Some(argument.clone());
641 }
642 index += 1;
643 }
644
645 let mut list_arguments = cargo_filter.into_iter().collect::<Vec<_>>();
646 let mut test_threads = None;
647 let mut index = 0;
648 while index < invocation.runner_arguments.len() {
649 let argument = &invocation.runner_arguments[index];
650 match argument.as_str() {
651 "--ignored" | "--include-ignored" | "--exclude-should-panic" | "--test" | "--bench" => {
652 list_arguments.push(argument.clone());
653 }
654 "--exact" => list_arguments.push(argument.clone()),
655 "--skip" => {
656 let value = invocation.runner_arguments.get(index + 1).ok_or_else(|| {
657 RustTestRunnerError::UnsupportedCommand(
658 "libtest --skip has no filter value".into(),
659 )
660 })?;
661 list_arguments.extend([argument.clone(), value.clone()]);
662 index += 1;
663 }
664 _ if argument.starts_with("--skip=") && argument.len() > "--skip=".len() => {
665 list_arguments.push(argument.clone());
666 }
667 "--test-threads" => {
668 let value = invocation.runner_arguments.get(index + 1).ok_or_else(|| {
669 RustTestRunnerError::UnsupportedCommand(
670 "libtest --test-threads has no value".into(),
671 )
672 })?;
673 let parsed = parse_libtest_threads(value)?;
674 if test_threads.replace(parsed).is_some() {
675 return Err(RustTestRunnerError::UnsupportedCommand(
676 "libtest --test-threads was provided more than once".into(),
677 ));
678 }
679 index += 1;
680 }
681 _ if argument.starts_with("--test-threads=") => {
682 let value = &argument["--test-threads=".len()..];
683 let parsed = parse_libtest_threads(value)?;
684 if test_threads.replace(parsed).is_some() {
685 return Err(RustTestRunnerError::UnsupportedCommand(
686 "libtest --test-threads was provided more than once".into(),
687 ));
688 }
689 }
690 "-Z" => {
691 let value = invocation.runner_arguments.get(index + 1).ok_or_else(|| {
692 RustTestRunnerError::UnsupportedCommand(
693 "libtest -Z has no feature value".into(),
694 )
695 })?;
696 list_arguments.extend([argument.clone(), value.clone()]);
701 index += 1;
702 }
703 _ if argument.starts_with("-Z") && argument.len() > 2 => {
704 list_arguments.push(argument.clone());
705 }
706 "--logfile" | "--color" | "--format" | "--shuffle-seed" => {
707 if invocation.runner_arguments.get(index + 1).is_none() {
708 return Err(RustTestRunnerError::UnsupportedCommand(format!(
709 "libtest {argument} has no value"
710 )));
711 }
712 index += 1;
716 }
717 _ if ["--logfile=", "--color=", "--format=", "--shuffle-seed="]
718 .iter()
719 .any(|prefix| argument.starts_with(prefix) && argument.len() > prefix.len()) => {}
720 "--force-run-in-process"
721 | "--fail-fast"
722 | "--no-capture"
723 | "--nocapture"
724 | "-q"
725 | "--quiet"
726 | "--show-output"
727 | "--report-time"
728 | "--ensure-time"
729 | "--shuffle" => {
730 }
734 "--list" | "-h" | "--help" => {
735 return Err(RustTestRunnerError::UnsupportedCommand(format!(
736 "libtest {argument} does not execute a test suite; exact non-execution mode support is not implemented"
737 )));
738 }
739 _ if !argument.starts_with('-') => list_arguments.push(argument.clone()),
740 _ => {
741 return Err(RustTestRunnerError::UnsupportedCommand(format!(
742 "the pinned Rust 1.95 libtest discovery contract does not recognize option {argument}"
743 )));
744 }
745 }
746 index += 1;
747 }
748 Ok(RustLibtestSelection { list_arguments })
749}
750
751fn parse_libtest_threads(value: &str) -> Result<usize, RustTestRunnerError> {
752 match value.parse::<usize>() {
753 Ok(0) => Err(RustTestRunnerError::UnsupportedCommand(
754 "argument for --test-threads must not be 0".into(),
755 )),
756 Ok(value) => Ok(value),
757 Err(error) => Err(RustTestRunnerError::UnsupportedCommand(format!(
758 "argument for --test-threads must be a number > 0 (error: {error})"
759 ))),
760 }
761}
762
763pub(crate) fn rust_cargo_execution_selection(
764 invocation: &CargoTestInvocation,
765) -> Result<RustCargoExecutionSelection, RustTestRunnerError> {
766 if invocation.kind == RustCargoCommandKind::NextestRun {
767 return Ok(RustCargoExecutionSelection {
768 run_libtests: true,
769 run_doctests: false,
770 doctest_arguments: Vec::new(),
771 });
772 }
773 let test = invocation.command_position();
774 let mut doc = false;
775 let mut other_target = false;
776 let mut index = test + 1;
777 while index < invocation.arguments.len() {
778 let argument = &invocation.arguments[index];
779 let name = argument
780 .split_once('=')
781 .map_or(argument.as_str(), |(name, _)| name);
782 match name {
783 "--doc" => doc = true,
784 "--lib" | "--bins" | "--bin" | "--examples" | "--example" | "--tests" | "--test"
785 | "--benches" | "--bench" | "--all-targets" => other_target = true,
786 _ => {}
787 }
788 if argument.starts_with('-') {
789 let takes_value = cargo_option_takes_value(argument).ok_or_else(|| {
790 RustTestRunnerError::UnsupportedCommand(format!(
791 "the pinned Cargo test contract does not recognize option {argument}"
792 ))
793 })?;
794 if takes_value {
795 index += 1;
796 if index == invocation.arguments.len() {
797 return Err(RustTestRunnerError::UnsupportedCommand(format!(
798 "Cargo option {argument} has no value"
799 )));
800 }
801 }
802 }
803 index += 1;
804 }
805 if doc && other_target {
806 return Err(RustTestRunnerError::UnsupportedCommand(
807 "Cargo --doc cannot be combined with another explicit target selection".into(),
808 ));
809 }
810 let run_doctests = doc || !other_target;
811 let run_libtests = !doc;
812 let mut doctest_arguments = invocation.arguments.clone();
813 if run_doctests && !doc {
814 doctest_arguments.insert(test + 1, "--doc".into());
815 }
816 if !invocation.runner_arguments.is_empty() {
817 doctest_arguments.push("--".into());
818 doctest_arguments.extend(invocation.runner_arguments.iter().cloned());
819 }
820 Ok(RustCargoExecutionSelection {
821 run_libtests,
822 run_doctests,
823 doctest_arguments,
824 })
825}
826
827pub(crate) fn relative_source(root: &Path, path: &Path) -> Result<String, RustTestRunnerError> {
828 let relative = path
829 .strip_prefix(root)
830 .map_err(|_| RustTestRunnerError::UnsafeArtifact(path.display().to_string()))?;
831 if relative.as_os_str().is_empty()
832 || relative
833 .components()
834 .any(|part| !matches!(part, Component::Normal(_)))
835 {
836 return Err(RustTestRunnerError::UnsafeArtifact(
837 path.display().to_string(),
838 ));
839 }
840 Ok(relative.to_string_lossy().replace('\\', "/"))
841}
842
843fn build_owned_arguments(arguments: Vec<String>) -> Result<Vec<String>, RustTestRunnerError> {
849 let mut kept = Vec::with_capacity(arguments.len());
850 let mut arguments = arguments.into_iter();
851 while let Some(argument) = arguments.next() {
852 let name = argument
853 .split_once('=')
854 .map_or(argument.as_str(), |(name, _)| name);
855 match name {
856 "--no-run" => {
857 return Err(RustTestRunnerError::UnsupportedCommand(
858 "`--no-run` builds the tests without running any, so there is nothing to measure; run the command without it".into(),
859 ));
860 }
861 "--message-format" | "--target-dir" => {
862 if !argument.contains('=') {
863 arguments.next();
864 }
865 }
866 _ => kept.push(argument),
867 }
868 }
869 Ok(kept)
870}
871
872fn build_test_artifacts(
873 project: &PreparedRustProject,
874 command: &[String],
875) -> Result<Vec<TestArtifact>, RustTestRunnerError> {
876 let mut invocation = cargo_invocation(&project.workspace_root, command)?;
877 invocation.arguments = build_owned_arguments(invocation.arguments)?;
878 invocation
879 .arguments
880 .extend(["--no-run".into(), "--message-format=json".into()]);
881 let output = Command::new(&invocation.program)
888 .args(invocation.arguments)
889 .current_dir(&project.workspace_root)
890 .env("CARGO_TARGET_DIR", &project.target_directory)
891 .env("RUSTFLAGS", capped_rustflags())
892 .output()
893 .map_err(|error| RustTestRunnerError::Launch(error.to_string()))?;
894 if !output.status.success() {
895 let rendered = output
899 .stdout
900 .split(|byte| *byte == b'\n')
901 .filter_map(|line| serde_json::from_slice::<serde_json::Value>(line).ok())
902 .filter(|message| {
903 message["reason"] == "compiler-message" && message["message"]["level"] == "error"
904 })
905 .filter_map(|message| message["message"]["rendered"].as_str().map(str::to_owned))
906 .collect::<String>();
907 return Err(RustTestRunnerError::CargoFailed(format!(
908 "{rendered}{}",
909 String::from_utf8_lossy(&output.stderr).trim()
910 )));
911 }
912 let canonical_target = fs::canonicalize(&project.target_directory)
913 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
914 let mut artifacts = Vec::new();
915 for line in output
916 .stdout
917 .split(|byte| *byte == b'\n')
918 .filter(|line| !line.is_empty())
919 {
920 let message: CargoMessage = serde_json::from_slice(line)
921 .map_err(|error| RustTestRunnerError::CargoJson(error.to_string()))?;
922 if message.reason != "compiler-artifact"
923 || !message.profile.as_ref().is_some_and(|profile| profile.test)
924 {
925 continue;
926 }
927 let (Some(executable), Some(target)) = (message.executable, message.target) else {
928 continue;
929 };
930 let executable = fs::canonicalize(&executable)
931 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
932 if !executable.starts_with(&canonical_target)
933 || !fs::metadata(&executable).is_ok_and(|metadata| metadata.is_file())
934 {
935 return Err(RustTestRunnerError::UnsafeArtifact(
936 executable.display().to_string(),
937 ));
938 }
939 let source = fs::canonicalize(target.src_path)
940 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
941 let package_directory = message
942 .manifest_path
943 .as_deref()
944 .and_then(Path::parent)
945 .map_or_else(|| project.workspace_root.clone(), Path::to_path_buf);
946 artifacts.push(TestArtifact {
947 executable,
948 package_directory,
949 name: target.name,
950 kind: if target.kind.iter().any(|kind| kind == "test") {
951 "integration".into()
952 } else {
953 "unit".into()
954 },
955 source: relative_source(&project.workspace_root, &source)?,
956 });
957 }
958 artifacts.sort_by(|left, right| left.executable.cmp(&right.executable));
959 artifacts.dedup_by(|left, right| left.executable == right.executable);
960 if artifacts.is_empty() {
961 return Err(RustTestRunnerError::CargoJson(
962 "Cargo emitted no libtest artifacts".into(),
963 ));
964 }
965 Ok(artifacts)
966}
967
968fn list_tests(
973 executable: &Path,
974 filters: &[String],
975 environment: &[(&'static str, OsString)],
976) -> Result<Vec<String>, RustTestRunnerError> {
977 let output = Command::new(executable)
978 .args(["--list", "--format", "terse"])
979 .args(filters)
980 .envs(environment.iter().map(|(key, value)| (key, value)))
981 .output()
982 .map_err(|error| RustTestRunnerError::Launch(error.to_string()))?;
983 if !output.status.success() {
984 let stderr = String::from_utf8_lossy(&output.stderr).trim().to_owned();
988 let stdout = String::from_utf8_lossy(&output.stdout).trim().to_owned();
989 let detail = if !stderr.is_empty() {
990 stderr
991 } else if !stdout.is_empty() {
992 format!("no stderr; stdout was {stdout}")
993 } else {
994 "no output on either stream".to_owned()
995 };
996 return Err(RustTestRunnerError::ListFailed(format!(
997 "{} exited with {} when asked to --list: {detail}",
998 executable.display(),
999 output.status
1000 )));
1001 }
1002 let mut tests = String::from_utf8_lossy(&output.stdout)
1003 .lines()
1004 .filter_map(|line| line.strip_suffix(": test"))
1005 .map(str::to_owned)
1006 .collect::<Vec<_>>();
1007 tests.sort();
1008 tests.dedup();
1009 Ok(tests)
1010}
1011
1012pub(crate) struct RustEvidence {
1015 pub(crate) snapshot: RuntimeSnapshot,
1016 pub(crate) phases: Vec<CoveragePhase>,
1017}
1018
1019struct PendingRecord {
1021 event_type: &'static str,
1022 id: String,
1023 vector: Option<McdcVector>,
1024 sequence: i64,
1025}
1026
1027pub(crate) fn snapshot(
1031 manifest: &CoverageManifest,
1032 directory: &Path,
1033 attempt: &str,
1034) -> Result<RustEvidence, RustTestRunnerError> {
1035 let points = manifest
1036 .points
1037 .iter()
1038 .map(|point| (point.id.as_str(), point))
1039 .collect::<BTreeMap<_, _>>();
1040 let asserting = manifest
1042 .points
1043 .iter()
1044 .chain(&manifest.assertion_sites)
1045 .map(|point| (point.id.as_str(), point))
1046 .collect::<BTreeMap<_, _>>();
1047 let alternatives = manifest
1048 .branches
1049 .iter()
1050 .flat_map(|branch| {
1051 branch
1052 .alternatives
1053 .iter()
1054 .map(|alternative| alternative.id.as_str())
1055 })
1056 .collect::<BTreeSet<_>>();
1057 let decisions = manifest
1058 .decisions
1059 .iter()
1060 .map(|decision| (decision.id.as_str(), decision))
1061 .collect::<BTreeMap<_, _>>();
1062 let mut hits = BTreeSet::new();
1063 let mut vectors = BTreeMap::<String, BTreeSet<(Vec<Option<bool>>, bool)>>::new();
1064 let mut pending = BTreeMap::<u64, Vec<PendingRecord>>::new();
1068 let mut events = Vec::new();
1069 let mut phases = Vec::new();
1070 let mut sequence = 0_i64;
1073 let token = crate::rust_project::manifest_token(manifest);
1078 for (name, observations) in read_rust_probe_directory(directory)
1079 .map_err(|error| RustTestRunnerError::Probe(error.to_string()))?
1080 {
1081 if !name.starts_with(&token) {
1082 continue;
1083 }
1084 for entry in observations {
1085 sequence += 1;
1086 let thread = entry.thread;
1087 match entry.observation {
1088 RustProbeObservation::Hit { id } => {
1089 if !points.contains_key(id.as_str()) && !alternatives.contains(id.as_str()) {
1090 return Err(RustTestRunnerError::UnknownProbe(id));
1091 }
1092 hits.insert(id.clone());
1093 pending.entry(thread).or_default().push(PendingRecord {
1094 event_type: "hit",
1095 id,
1096 vector: None,
1097 sequence,
1098 });
1099 }
1100 RustProbeObservation::Decision {
1101 id,
1102 values,
1103 outcome,
1104 } => {
1105 let Some(meta) = decisions.get(id.as_str()) else {
1106 return Err(RustTestRunnerError::UnknownProbe(id));
1107 };
1108 if values.len() != meta.conditions.len() {
1109 return Err(RustTestRunnerError::InvalidVector {
1110 id,
1111 expected: meta.conditions.len(),
1112 actual: values.len(),
1113 });
1114 }
1115 hits.insert(format!(
1116 "{}:outcome:{}",
1117 meta.id,
1118 if outcome { "true" } else { "false" }
1119 ));
1120 vectors
1121 .entry(meta.id.clone())
1122 .or_default()
1123 .insert((values.clone(), outcome));
1124 pending.entry(thread).or_default().push(PendingRecord {
1125 event_type: "decision",
1126 id,
1127 vector: Some(McdcVector { values, outcome }),
1128 sequence,
1129 });
1130 }
1131 RustProbeObservation::Assertion { id } => {
1132 let Some(point) = asserting.get(id.as_str()) else {
1135 return Err(RustTestRunnerError::UnknownProbe(id));
1136 };
1137 let witnessed = pending.remove(&thread).unwrap_or_default();
1138 let phase_id = format!("{attempt}:assertion:{sequence}");
1139 phases.push(CoveragePhase {
1140 id: phase_id.clone(),
1141 kind: "assertion".into(),
1142 operation: format!(
1143 "Rust assertion at {}:{}:{}",
1144 point.file, point.line, point.column
1145 ),
1146 source: Some(point.source.clone()),
1147 caused_by_phase_id: None,
1148 started_at_ms: witnessed.first().map_or(sequence, |record| record.sequence),
1149 ended_at_ms: Some(sequence),
1150 status: Some("passed".into()),
1153 error: None,
1154 });
1155 for record in witnessed {
1156 events.push(RuntimeEvent {
1157 event_type: record.event_type.into(),
1158 id: record.id,
1159 vector: record.vector,
1160 timestamp_ms: record.sequence,
1161 phase_id: Some(phase_id.clone()),
1162 statement_id: None,
1163 environment: "server".into(),
1164 });
1165 }
1166 }
1167 }
1168 }
1169 }
1170 let mut decision_snapshots = Vec::new();
1171 for (id, observed) in vectors {
1172 let meta: DecisionMeta = (*decisions[id.as_str()]).clone();
1173 decision_snapshots.push(DecisionSnapshot {
1174 meta,
1175 vectors: observed
1176 .into_iter()
1177 .map(|(values, outcome)| McdcVector { values, outcome })
1178 .collect(),
1179 });
1180 }
1181 Ok(RustEvidence {
1182 snapshot: RuntimeSnapshot {
1183 decisions: decision_snapshots,
1184 hits: hits.into_iter().collect(),
1185 events,
1186 logicals: Vec::new(),
1187 phase_id: None,
1188 },
1189 phases,
1190 })
1191}
1192
1193fn rust_coverage_model() -> CoverageModelDeclaration {
1194 CoverageModelDeclaration {
1195 language: "rust".into(),
1196 variant: "rust-owned-probes-v1".into(),
1197 name: "supercov-rust-owned-v1".into(),
1198 completeness_meaning: "Every semantics-proven Rust obligation in the owned source denominator was observed; explicit manifest limitations identify unmeasured Rust surfaces.".into(),
1199 measured: vec![
1200 "owned Rust statements and function entries".into(),
1201 "owned atomic condition vectors and decision outcomes".into(),
1202 "exact process-per-libtest attribution".into(),
1203 "exact process-per-doctest attribution".into(),
1204 "evidence a passing assertion of the same thread witnessed".into(),
1205 ],
1206 not_measured: vec![
1207 "macro-expanded and generated Rust code".into(),
1208 "const-evaluated code and unsupported structural branch probes".into(),
1209 "causal linkage to individual actions".into(),
1210 "all input values, semantic partitions, paths, or concurrency interleavings".into(),
1211 "mutation score or assertion fault-detection strength".into(),
1212 ],
1213 }
1214}
1215
1216pub(crate) fn instrumented_stack_environment() -> Vec<(&'static str, &'static str)> {
1228 if std::env::var_os("RUST_MIN_STACK").is_some() {
1229 Vec::new()
1230 } else {
1231 vec![("RUST_MIN_STACK", "16777216")]
1232 }
1233}
1234
1235fn rustc_print_path(request: &str) -> Result<PathBuf, RustTestRunnerError> {
1237 let output = Command::new("rustc")
1238 .args(["--print", request])
1239 .output()
1240 .map_err(|error| {
1241 RustTestRunnerError::Launch(format!("rustc --print {request}: {error}"))
1242 })?;
1243 if !output.status.success() {
1244 return Err(RustTestRunnerError::Launch(format!(
1245 "rustc --print {request} exited with {}",
1246 output.status
1247 )));
1248 }
1249 Ok(PathBuf::from(
1250 String::from_utf8_lossy(&output.stdout).trim(),
1251 ))
1252}
1253
1254pub(crate) fn rustc_sysroot() -> Result<PathBuf, RustTestRunnerError> {
1256 rustc_print_path("sysroot")
1257}
1258
1259pub(crate) fn rustc_target_libdir() -> Result<PathBuf, RustTestRunnerError> {
1263 rustc_print_path("target-libdir")
1264}
1265
1266pub(crate) fn dynamic_library_environment(
1272 target_libdir: &Path,
1273 executable: &Path,
1274) -> Vec<(&'static str, OsString)> {
1275 let variable = if cfg!(target_os = "macos") {
1276 "DYLD_FALLBACK_LIBRARY_PATH"
1277 } else if cfg!(windows) {
1278 "PATH"
1279 } else {
1280 "LD_LIBRARY_PATH"
1281 };
1282 let mut entries = Vec::new();
1283 if let Some(deps) = executable.parent() {
1284 entries.push(deps.to_path_buf());
1285 if let Some(profile) = deps.parent() {
1286 entries.push(profile.to_path_buf());
1287 }
1288 }
1289 entries.push(target_libdir.to_path_buf());
1290 if let Some(existing) = std::env::var_os(variable) {
1291 entries.extend(std::env::split_paths(&existing));
1292 }
1293 match std::env::join_paths(entries) {
1294 Ok(value) => vec![(variable, value)],
1295 Err(_) => Vec::new(),
1296 }
1297}
1298
1299pub(crate) fn capped_rustflags() -> String {
1300 let mut rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();
1301 if !rustflags.is_empty() {
1302 rustflags.push(' ');
1303 }
1304 rustflags.push_str("--cap-lints=warn");
1305 rustflags
1306}
1307
1308pub(crate) fn io_error(error: impl std::fmt::Display) -> RustTestRunnerError {
1309 RustTestRunnerError::Io(error.to_string())
1310}
1311
1312pub(crate) fn libtest_skipped(exit: i32, stdout: &str) -> bool {
1315 exit == 0 && (stdout.contains("running 0 tests") || stdout.contains("; 1 ignored;"))
1316}
1317
1318fn rust_runner_limitations(runner: &str) -> Vec<FrontendLimitation> {
1321 let prefix = if runner == "rust-libtest" {
1322 "rust".to_owned()
1323 } else {
1324 runner.to_owned()
1325 };
1326 vec![
1327 FrontendLimitation {
1328 id: format!("{prefix}-action-linkage-unavailable"),
1329 scopes: vec![FrontendLimitationScope::Action],
1330 reason: "Rust test frameworks expose no general action lifecycle".into(),
1331 },
1332 FrontendLimitation {
1333 id: format!("{prefix}-assertion-linkage-unavailable"),
1334 scopes: vec![FrontendLimitationScope::Assertion],
1335 reason: "assertion macros do not expose a stable per-assertion success lifecycle"
1336 .into(),
1337 },
1338 ]
1339}
1340
1341fn rust_runner_declaration(runner: &str) -> FrontendRunnerDeclaration {
1342 FrontendRunnerDeclaration {
1343 runner: runner.into(),
1344 execution_model: ExecutionModel::ProcessPerTest,
1345 attribution: FrontendAttribution {
1346 run: AttributionPrecision::Exact,
1347 worker: AttributionPrecision::Exact,
1348 test: AttributionPrecision::Exact,
1349 retry: AttributionPrecision::Exact,
1350 phase: AttributionPrecision::Exact,
1351 action: AttributionPrecision::Unavailable,
1352 assertion: AttributionPrecision::Unavailable,
1353 },
1354 limitations: rust_runner_limitations(runner),
1355 }
1356}
1357
1358fn decline_uncompiled_sources(
1367 project: &PreparedRustProject,
1368) -> (CoverageManifest, BTreeSet<String>) {
1369 let mut manifest = project.manifest.clone();
1370 let Some(compiled) = crate::rust_project::compiled_source_files(
1371 &project.workspace_root,
1372 &project.target_directory,
1373 ) else {
1374 return (manifest, BTreeSet::new());
1375 };
1376 let declined = project
1377 .source_files
1378 .iter()
1379 .filter(|file| !compiled.contains(*file))
1380 .cloned()
1381 .collect::<BTreeSet<_>>();
1382 if declined.is_empty()
1385 || project
1386 .crate_roots
1387 .iter()
1388 .all(|root| !compiled.contains(root))
1389 {
1390 return (manifest, BTreeSet::new());
1391 }
1392 let mut unmeasured = manifest.unmeasured.iter().cloned().collect::<BTreeSet<_>>();
1393 unmeasured.extend(
1394 manifest
1395 .points
1396 .iter()
1397 .filter(|point| declined.contains(&point.file))
1398 .map(|point| point.id.clone()),
1399 );
1400 unmeasured.extend(
1401 manifest
1402 .decisions
1403 .iter()
1404 .filter(|decision| declined.contains(&decision.file))
1405 .map(|decision| decision.id.clone()),
1406 );
1407 unmeasured.extend(
1408 manifest
1409 .branches
1410 .iter()
1411 .filter(|branch| declined.contains(&branch.file))
1412 .map(|branch| branch.id.clone()),
1413 );
1414 manifest.unmeasured = unmeasured.into_iter().collect();
1415 manifest.limitations.retain(|limitation| {
1417 limitation
1418 .get("file")
1419 .and_then(|file| file.as_str())
1420 .is_none_or(|file| !declined.contains(file))
1421 });
1422 (manifest, declined)
1423}
1424
1425fn structural_limitations(manifest: &CoverageManifest) -> Vec<String> {
1427 manifest
1428 .limitations
1429 .iter()
1430 .filter_map(|item| {
1431 item.get("id")
1432 .and_then(|value| value.as_str())
1433 .map(str::to_owned)
1434 })
1435 .collect()
1436}
1437
1438pub fn run_prepared_rust_tests(
1439 project: &PreparedRustProject,
1440 command: &[String],
1441 run_id: &str,
1442 generated_at: &str,
1443 diagnostics: &mut dyn Write,
1444) -> Result<RustFrontendRun, RustTestRunnerError> {
1445 let invocation = cargo_invocation(&project.workspace_root, command)?;
1446 let selection = rust_cargo_execution_selection(&invocation)?;
1447 let build_started = Instant::now();
1448 let artifacts = if selection.run_libtests && invocation.kind == RustCargoCommandKind::CargoTest
1452 {
1453 build_test_artifacts(project, command)?
1454 } else {
1455 Vec::new()
1456 };
1457 let build_ms = build_started.elapsed().as_secs_f64() * 1000.0;
1458 let (manifest, declined) = decline_uncompiled_sources(project);
1462 if !declined.is_empty() {
1463 writeln!(
1464 diagnostics,
1465 "[supercov] {} source file(s) this build did not compile are outside the denominator: {}{}",
1466 declined.len(),
1467 declined
1468 .iter()
1469 .take(3)
1470 .cloned()
1471 .collect::<Vec<_>>()
1472 .join(", "),
1473 if declined.len() > 3 { ", ..." } else { "" }
1474 )
1475 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
1476 }
1477 let project = &PreparedRustProject {
1478 manifest,
1479 ..project.clone()
1480 };
1481 let _signal_guard = crate::child_signal_guard::ChildSignalGuard::install()
1485 .map_err(|error| RustTestRunnerError::Launch(error.to_string()))?;
1486 let evidence_root = project
1487 .workspace_root
1488 .join(".supercov/rust-evidence")
1489 .join(run_id);
1490 fs::create_dir_all(&evidence_root)
1491 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
1492 let mut results = Vec::new();
1493 let mut overall_exit = 0;
1494 let execution_started = Instant::now();
1495 if invocation.kind == RustCargoCommandKind::NextestRun {
1496 let outcome = crate::rust_owned_nextest::run_nextest(
1499 project,
1500 &invocation,
1501 &evidence_root.join("nextest"),
1502 run_id,
1503 diagnostics,
1504 )?;
1505 let artifact_count = outcome.artifact_files.len();
1506 return Ok(RustFrontendRun {
1507 declaration: FrontendRunDeclaration {
1508 protocol_version: LANGUAGE_FRONTEND_PROTOCOL_VERSION,
1509 frontend_id: "rust".into(),
1510 frontend_version: "rust-owned-v1".into(),
1511 language: "rust".into(),
1512 structural_source: StructuralSource::OwnedProbes,
1513 runners: vec![rust_runner_declaration("nextest")],
1514 structural_limitations: structural_limitations(&project.manifest),
1515 },
1516 request: CoverageReportRequest {
1517 run_id: run_id.into(),
1518 manifest: project.manifest.clone(),
1519 raw_results: outcome.results,
1520 generated_at: generated_at.into(),
1521 coverage_model: Some(rust_coverage_model()),
1522 integrity: None,
1523 test_exit_code: ExitCodeInput::Present(Some(outcome.exit_code)),
1524 },
1525 exit_code: outcome.exit_code,
1526 artifacts: artifact_count,
1527 artifact_files: outcome.artifact_files,
1528 build_ms,
1529 execution_ms: execution_started.elapsed().as_secs_f64() * 1000.0,
1530 });
1531 }
1532 let libtest_selection = rust_libtest_selection(&invocation)?;
1538 let ignored_mode = libtest_selection
1541 .list_arguments
1542 .iter()
1543 .filter(|argument| {
1544 matches!(
1545 argument.as_str(),
1546 "--ignored" | "--include-ignored" | "--exclude-should-panic"
1547 )
1548 })
1549 .cloned()
1550 .collect::<Vec<_>>();
1551 let mut tasks = Vec::new();
1552 let target_libdir = if artifacts.is_empty() {
1553 PathBuf::new()
1554 } else {
1555 rustc_target_libdir()?
1556 };
1557 for (artifact_index, artifact) in artifacts.iter().enumerate() {
1558 let tests = list_tests(
1559 &artifact.executable,
1560 &libtest_selection.list_arguments,
1561 &dynamic_library_environment(&target_libdir, &artifact.executable),
1562 )?;
1563 let contexts = preflight_rust_test_contexts(tests.clone())
1564 .map_err(|error| RustTestRunnerError::Context(error.to_string()))?;
1565 for (test_index, test) in tests.into_iter().enumerate() {
1566 let directory = evidence_root.join(format!("{artifact_index:04}-{test_index:08}"));
1567 fs::create_dir(&directory)
1568 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
1569 tasks.push(ProcessTask {
1570 ordinal: tasks.len(),
1571 artifact_index,
1572 test_index,
1573 artifact: artifact.clone(),
1574 context_id: contexts[&test],
1575 test,
1576 directory,
1577 });
1578 }
1579 }
1580 let workers = std::thread::available_parallelism()
1581 .map(usize::from)
1582 .unwrap_or(1)
1583 .min(tasks.len().max(1));
1584 let next = AtomicUsize::new(0);
1585 let outcomes = Mutex::new(Vec::<Result<ProcessOutcome, String>>::with_capacity(
1586 tasks.len(),
1587 ));
1588 std::thread::scope(|scope| {
1589 for _ in 0..workers {
1590 scope.spawn(|| {
1591 loop {
1592 let index = next.fetch_add(1, Ordering::Relaxed);
1593 let Some(task) = tasks.get(index) else { break };
1594 let result = crate::child_signal_guard::output(
1595 Command::new(&task.artifact.executable)
1596 .args(["--exact", &task.test])
1605 .args(&ignored_mode)
1606 .current_dir(&task.artifact.package_directory)
1607 .envs(instrumented_stack_environment())
1608 .envs(dynamic_library_environment(
1609 &target_libdir,
1610 &task.artifact.executable,
1611 ))
1612 .env("SUPERCOV_RUST_EVIDENCE_DIR", &task.directory)
1613 .env(
1614 crate::rust_probe_transport::RUST_CONTEXT_ENV,
1615 format!("{:016x}", task.context_id),
1616 ),
1617 )
1618 .map(|output| ProcessOutcome {
1619 task: ProcessTask {
1620 ordinal: task.ordinal,
1621 artifact_index: task.artifact_index,
1622 test_index: task.test_index,
1623 artifact: task.artifact.clone(),
1624 test: task.test.clone(),
1625 context_id: task.context_id,
1626 directory: task.directory.clone(),
1627 },
1628 output,
1629 })
1630 .map_err(|error| error.to_string());
1631 outcomes
1632 .lock()
1633 .expect("Rust test result lock poisoned")
1634 .push(result);
1635 }
1636 });
1637 }
1638 });
1639 let mut outcomes = outcomes
1640 .into_inner()
1641 .map_err(|_| RustTestRunnerError::Io("Rust test result lock poisoned".into()))?
1642 .into_iter()
1643 .map(|result| result.map_err(RustTestRunnerError::Launch))
1644 .collect::<Result<Vec<_>, _>>()?;
1645 outcomes.sort_by_key(|outcome| outcome.task.ordinal);
1646 for outcome in outcomes {
1647 let ProcessTask {
1648 artifact_index,
1649 test_index,
1650 artifact,
1651 test,
1652 directory,
1653 ..
1654 } = outcome.task;
1655 let test_id = format!("{}::{test}", artifact.source);
1659 let worker_id = format!("artifact-{artifact_index:04}");
1660 let attempt_id = format!("{run_id}:{artifact_index:04}:{test_index:08}");
1661 let output = outcome.output;
1662 let exit = output.status.code().unwrap_or(1);
1663 let stdout = String::from_utf8_lossy(&output.stdout);
1664 let skipped = libtest_skipped(exit, &stdout);
1665 if exit != 0 {
1666 writeln!(diagnostics, "[supercov] Rust test failed: {test_id}")
1667 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
1668 diagnostics
1669 .write_all(&output.stdout)
1670 .and_then(|_| diagnostics.write_all(&output.stderr))
1671 .map_err(|error| RustTestRunnerError::Io(error.to_string()))?;
1672 }
1673 if exit != 0 {
1674 overall_exit = exit;
1675 }
1676 let evidence = snapshot(&project.manifest, &directory, &attempt_id)?;
1677 results.push(RawTestResult {
1678 test_id: Some(test_id.clone()),
1679 scope: Some(ExecutionScope {
1680 version: 1,
1681 run_id: run_id.into(),
1682 worker_id,
1683 test_id: test_id.clone(),
1684 test_key: format!("{}::{test}", artifact.source),
1685 retry: 0,
1686 attempt_id,
1687 }),
1688 test: test_id,
1689 test_file: Some(artifact.source.clone()),
1690 title: Some(test),
1691 retry: Some(0),
1692 status: Some(
1693 if exit != 0 {
1694 "failed"
1695 } else if skipped {
1696 "skipped"
1697 } else {
1698 "passed"
1699 }
1700 .into(),
1701 ),
1702 expected_status: Some("passed".into()),
1703 flaky: false,
1704 provenance: TestProvenance {
1705 runner: "rust-libtest".into(),
1706 kind: artifact.kind,
1707 project: Some(artifact.name),
1708 source: "supercov-owned-process-per-test".into(),
1709 },
1710 role: "test".into(),
1711 attribution: crate::coverage_report::ATTRIBUTION_EXACT.into(),
1712 phases: evidence.phases,
1713 runtime: vec![evidence.snapshot],
1714 browser: Vec::new(),
1715 server: Vec::new(),
1716 });
1717 }
1718 let doctest_results =
1719 if selection.run_doctests && invocation.kind == RustCargoCommandKind::CargoTest {
1720 crate::rust_owned_doctests::run_doctests(
1721 project,
1722 &invocation,
1723 &selection,
1724 &evidence_root.join("doctests"),
1725 run_id,
1726 diagnostics,
1727 &mut overall_exit,
1728 )?
1729 } else {
1730 Vec::new()
1731 };
1732 let ran_libtests = !results.is_empty();
1735 let ran_doctests = !doctest_results.is_empty();
1736 results.extend(doctest_results);
1737 let mut runners = Vec::new();
1738 if ran_libtests || !ran_doctests {
1739 runners.push(rust_runner_declaration("rust-libtest"));
1740 }
1741 if ran_doctests {
1742 runners.push(rust_runner_declaration("rustdoc"));
1743 }
1744 let structural_limitations = structural_limitations(&project.manifest);
1745 Ok(RustFrontendRun {
1746 declaration: FrontendRunDeclaration {
1747 protocol_version: LANGUAGE_FRONTEND_PROTOCOL_VERSION,
1748 frontend_id: "rust".into(),
1749 frontend_version: "rust-owned-v1".into(),
1750 language: "rust".into(),
1751 structural_source: StructuralSource::OwnedProbes,
1752 runners,
1753 structural_limitations,
1754 },
1755 request: CoverageReportRequest {
1756 run_id: run_id.into(),
1757 manifest: project.manifest.clone(),
1758 raw_results: results,
1759 generated_at: generated_at.into(),
1760 coverage_model: Some(rust_coverage_model()),
1761 integrity: None,
1762 test_exit_code: ExitCodeInput::Present(Some(overall_exit)),
1763 },
1764 exit_code: overall_exit,
1765 artifacts: artifacts.len(),
1766 artifact_files: artifacts
1767 .iter()
1768 .map(|artifact| artifact.executable.clone())
1769 .collect(),
1770 build_ms,
1771 execution_ms: execution_started.elapsed().as_secs_f64() * 1000.0,
1772 })
1773}
1774
1775#[cfg(test)]
1776mod tests {
1777 use std::time::{SystemTime, UNIX_EPOCH};
1778
1779 use super::*;
1780 use crate::{
1781 coverage_report::{ArchiveReportRequest, analyze_coverage_archive},
1782 evidence_archive::write_archive,
1783 frontend_protocol::validate_frontend_report_request,
1784 rust_project::prepare_rust_project,
1785 };
1786
1787 #[test]
1788 fn cargo_and_libtest_selection_is_preserved_without_presentation_guessing() {
1789 let root = Path::new(".");
1790 let invocation = cargo_invocation(
1791 root,
1792 &[
1793 "cargo".into(),
1794 "test".into(),
1795 "-p".into(),
1796 "fixture".into(),
1797 "authored".into(),
1798 "--".into(),
1799 "generated".into(),
1800 "--skip".into(),
1801 "slow".into(),
1802 "--include-ignored".into(),
1803 ],
1804 )
1805 .unwrap();
1806 assert_eq!(invocation.arguments, ["test", "-p", "fixture", "authored"]);
1807 assert_eq!(
1808 invocation.runner_arguments,
1809 ["generated", "--skip", "slow", "--include-ignored"]
1810 );
1811 let selection = rust_libtest_selection(&invocation).unwrap();
1812 assert_eq!(
1813 selection.list_arguments,
1814 [
1815 "authored",
1816 "generated",
1817 "--skip",
1818 "slow",
1819 "--include-ignored"
1820 ]
1821 );
1822 }
1823
1824 #[test]
1825 fn direct_cargo_argv_preserves_toml_quotes_inside_config_values() {
1826 let config = "target.host.runner=[\"runner with spaces\",\"--fixed\"]";
1827 let invocation = cargo_invocation(
1828 Path::new("."),
1829 &[
1830 "cargo".into(),
1831 "test".into(),
1832 "--config".into(),
1833 config.into(),
1834 ],
1835 )
1836 .unwrap();
1837 assert_eq!(invocation.arguments, ["test", "--config", config]);
1838 }
1839
1840 #[test]
1841 fn nextest_run_is_detected_without_reclassifying_its_filters_or_retries() {
1842 let invocation = cargo_invocation(
1843 Path::new("."),
1844 &[
1845 "cargo".into(),
1846 "+1.95.0".into(),
1847 "nextest".into(),
1848 "run".into(),
1849 "--retries".into(),
1850 "2".into(),
1851 "-E".into(),
1852 "test(/flaky/)".into(),
1853 "--".into(),
1854 "--nocapture".into(),
1855 ],
1856 )
1857 .unwrap();
1858 assert_eq!(invocation.kind, RustCargoCommandKind::NextestRun);
1859 assert_eq!(
1860 invocation.arguments,
1861 [
1862 "+1.95.0",
1863 "nextest",
1864 "run",
1865 "--retries",
1866 "2",
1867 "-E",
1868 "test(/flaky/)",
1869 ]
1870 );
1871 assert_eq!(invocation.runner_arguments, ["--nocapture"]);
1872 let execution = rust_cargo_execution_selection(&invocation).unwrap();
1873 assert!(execution.run_libtests);
1874 assert!(!execution.run_doctests);
1875 assert!(execution.doctest_arguments.is_empty());
1876 assert_eq!(
1877 nextest_list_invocation(&invocation).unwrap(),
1878 NextestListInvocation {
1879 arguments: vec![
1880 "+1.95.0".into(),
1881 "nextest".into(),
1882 "list".into(),
1883 "-E".into(),
1884 "test(/flaky/)".into(),
1885 "--message-format".into(),
1886 "json".into(),
1887 ],
1888 runner_arguments: vec!["--nocapture".into()],
1889 }
1890 );
1891 }
1892
1893 #[test]
1894 fn nextest_list_projection_preserves_selection_and_rejects_external_state() {
1895 let invocation = CargoTestInvocation {
1896 program: "cargo".into(),
1897 kind: RustCargoCommandKind::NextestRun,
1898 arguments: vec![
1899 "nextest".into(),
1900 "run".into(),
1901 "--package=fixture".into(),
1902 "--partition".into(),
1903 "hash:1/2".into(),
1904 "--test-threads=8".into(),
1905 "--failure-output".into(),
1906 "final".into(),
1907 "name".into(),
1908 ],
1909 runner_arguments: vec!["--exact".into(), "full::name".into()],
1910 };
1911 assert_eq!(
1912 nextest_list_invocation(&invocation).unwrap(),
1913 NextestListInvocation {
1914 arguments: vec![
1915 "nextest".into(),
1916 "list".into(),
1917 "--package=fixture".into(),
1918 "--partition".into(),
1919 "hash:1/2".into(),
1920 "name".into(),
1921 "--message-format".into(),
1922 "json".into(),
1923 ],
1924 runner_arguments: vec!["--exact".into(), "full::name".into()],
1925 }
1926 );
1927
1928 let mut rerun = invocation;
1929 rerun.arguments.extend(["--rerun".into(), "latest".into()]);
1930 assert!(
1931 nextest_list_invocation(&rerun)
1932 .unwrap_err()
1933 .to_string()
1934 .contains("cannot yet be assigned exact selected-test identity")
1935 );
1936 }
1937
1938 #[test]
1939 fn nextest_list_projection_preserves_post_separator_libtest_selection() {
1940 let invocation = cargo_invocation(
1941 Path::new("."),
1942 &[
1943 "cargo".into(),
1944 "nextest".into(),
1945 "run".into(),
1946 "--timings".into(),
1947 "-vv".into(),
1948 "--".into(),
1949 "--include-ignored".into(),
1950 "--skip".into(),
1951 "slow".into(),
1952 "--exact".into(),
1953 "tests::selected".into(),
1954 ],
1955 )
1956 .unwrap();
1957 assert_eq!(
1958 nextest_list_invocation(&invocation).unwrap(),
1959 NextestListInvocation {
1960 arguments: vec![
1961 "nextest".to_owned(),
1962 "list".to_owned(),
1963 "--timings".to_owned(),
1964 "-vv".to_owned(),
1965 "--message-format".to_owned(),
1966 "json".to_owned(),
1967 ],
1968 runner_arguments: vec![
1969 "--include-ignored".to_owned(),
1970 "--skip".to_owned(),
1971 "slow".to_owned(),
1972 "--exact".to_owned(),
1973 "tests::selected".to_owned(),
1974 ],
1975 }
1976 );
1977 }
1978
1979 #[test]
1980 fn nextest_version_handshake_preserves_the_cargo_toolchain_selector() {
1981 let invocation = CargoTestInvocation {
1982 program: "cargo".into(),
1983 kind: RustCargoCommandKind::NextestRun,
1984 arguments: vec![
1985 "+1.95.0".into(),
1986 "nextest".into(),
1987 "run".into(),
1988 "-p".into(),
1989 "fixture".into(),
1990 ],
1991 runner_arguments: Vec::new(),
1992 };
1993 assert_eq!(
1994 nextest_version_arguments(&invocation),
1995 ["+1.95.0", "nextest", "--version"]
1996 );
1997 }
1998
1999 #[test]
2000 fn stock_libtest_presentation_and_scheduling_options_do_not_change_discovery() {
2001 let invocation = CargoTestInvocation {
2002 program: "cargo".into(),
2003 kind: RustCargoCommandKind::CargoTest,
2004 arguments: vec!["test".into(), "cargo-filter".into()],
2005 runner_arguments: [
2006 "runner-filter",
2007 "--nocapture",
2008 "--show-output",
2009 "--format=json",
2010 "--color",
2011 "never",
2012 "--test-threads=4",
2013 "--fail-fast",
2014 "--shuffle-seed",
2015 "17",
2016 "-Zunstable-options",
2017 "--exclude-should-panic",
2018 ]
2019 .into_iter()
2020 .map(str::to_owned)
2021 .collect(),
2022 };
2023 let selection = rust_libtest_selection(&invocation).unwrap();
2024 assert_eq!(
2025 selection.list_arguments,
2026 [
2027 "cargo-filter",
2028 "runner-filter",
2029 "-Zunstable-options",
2030 "--exclude-should-panic"
2031 ]
2032 );
2033 }
2034
2035 #[test]
2036 fn cargo_test_options_are_not_mistaken_for_the_test_name_filter() {
2037 let invocation = CargoTestInvocation {
2038 program: "cargo".into(),
2039 kind: RustCargoCommandKind::CargoTest,
2040 arguments: vec![
2041 "test".into(),
2042 "--manifest-path".into(),
2043 "nested/Cargo.toml".into(),
2044 "--features=one,two".into(),
2045 "needle".into(),
2046 ],
2047 runner_arguments: vec!["--ignored".into(), "other".into()],
2048 };
2049 let selection = rust_libtest_selection(&invocation).unwrap();
2050 assert_eq!(selection.list_arguments, ["needle", "--ignored", "other"]);
2051 }
2052
2053 #[test]
2054 fn libtest_thread_count_is_preserved_as_runner_scheduling() {
2055 for arguments in [vec!["--test-threads", "1"], vec!["--test-threads=8"]] {
2056 let invocation = CargoTestInvocation {
2057 program: "cargo".into(),
2058 kind: RustCargoCommandKind::CargoTest,
2059 arguments: vec!["test".into()],
2060 runner_arguments: arguments.into_iter().map(str::to_owned).collect(),
2061 };
2062 let selection = rust_libtest_selection(&invocation).unwrap();
2063 assert!(selection.list_arguments.is_empty());
2064 }
2065 }
2066
2067 #[test]
2068 fn invalid_or_duplicate_libtest_thread_counts_fail_closed() {
2069 for arguments in [
2070 vec!["--test-threads"],
2071 vec!["--test-threads=0"],
2072 vec!["--test-threads=abc"],
2073 vec!["--test-threads", "1", "--test-threads=2"],
2074 ] {
2075 let invocation = CargoTestInvocation {
2076 program: "cargo".into(),
2077 kind: RustCargoCommandKind::CargoTest,
2078 arguments: vec!["test".into()],
2079 runner_arguments: arguments.into_iter().map(str::to_owned).collect(),
2080 };
2081 assert!(rust_libtest_selection(&invocation).is_err());
2082 }
2083 }
2084
2085 #[test]
2086 fn cargo_target_selection_reproduces_when_cargo_runs_doctests() {
2087 let invocation = CargoTestInvocation {
2088 program: "cargo".into(),
2089 kind: RustCargoCommandKind::CargoTest,
2090 arguments: vec![
2091 "test".into(),
2092 "-p".into(),
2093 "fixture".into(),
2094 "needle".into(),
2095 ],
2096 runner_arguments: vec!["--include-ignored".into()],
2097 };
2098 let selection = rust_cargo_execution_selection(&invocation).unwrap();
2099 assert!(selection.run_libtests);
2100 assert!(selection.run_doctests);
2101 assert_eq!(
2102 selection.doctest_arguments,
2103 [
2104 "test",
2105 "--doc",
2106 "-p",
2107 "fixture",
2108 "needle",
2109 "--",
2110 "--include-ignored"
2111 ]
2112 );
2113
2114 let mut explicit_doc = invocation.clone();
2115 explicit_doc.arguments.insert(1, "--doc".into());
2116 let selection = rust_cargo_execution_selection(&explicit_doc).unwrap();
2117 assert!(!selection.run_libtests);
2118 assert!(selection.run_doctests);
2119
2120 for target in ["--lib", "--tests", "--all-targets", "--example=demo"] {
2121 let mut selected = invocation.clone();
2122 selected.arguments.insert(1, target.into());
2123 let selection = rust_cargo_execution_selection(&selected).unwrap();
2124 assert!(selection.run_libtests);
2125 assert!(!selection.run_doctests);
2126 }
2127 }
2128
2129 #[test]
2130 fn a_module_the_build_never_compiles_leaves_the_denominator() {
2131 let nonce = SystemTime::now()
2135 .duration_since(UNIX_EPOCH)
2136 .unwrap()
2137 .as_nanos();
2138 let root = std::env::temp_dir().join(format!(
2139 "supercov-rust-runner-cfg-{}-{nonce}",
2140 std::process::id()
2141 ));
2142 fs::create_dir_all(root.join("src")).unwrap();
2143 fs::write(
2144 root.join("Cargo.toml"),
2145 "[package]\nname='fixture'\nversion='0.0.0'\nedition='2024'\n\n[features]\nextra=[]\n",
2146 )
2147 .unwrap();
2148 fs::write(
2149 root.join("src/lib.rs"),
2150 r#"
2151#[cfg(feature = "extra")]
2152mod extra;
2153
2154pub fn kept(value: i32) -> i32 {
2155 if value > 0 { value } else { -value }
2156}
2157#[cfg(test)]
2158mod tests {
2159 #[test] fn positive() { assert_eq!(super::kept(2), 2); }
2160 #[test] fn negative() { assert_eq!(super::kept(-2), 2); }
2161}
2162"#,
2163 )
2164 .unwrap();
2165 fs::write(
2166 root.join("src/extra.rs"),
2167 "pub fn never_built(value: i32) -> i32 {\n if value > 0 { 1 } else { 0 }\n}\n",
2168 )
2169 .unwrap();
2170 let project = prepare_rust_project(&root, None).unwrap();
2171 assert!(
2173 project
2174 .source_files
2175 .iter()
2176 .any(|file| file == "src/extra.rs"),
2177 "{:?}",
2178 project.source_files
2179 );
2180 let run = run_prepared_rust_tests(
2181 &project,
2182 &["cargo".into(), "test".into(), "--lib".into()],
2183 "rust-fixture-cfg",
2184 "2026-08-26T00:00:00.000Z",
2185 &mut Vec::new(),
2186 )
2187 .unwrap();
2188 assert_eq!(run.exit_code, 0);
2189 let manifest = &run.request.manifest;
2192 assert!(
2193 manifest
2194 .points
2195 .iter()
2196 .any(|point| point.file == "src/extra.rs"),
2197 "the obligations must stay in the manifest"
2198 );
2199 let declined = manifest.unmeasured.iter().collect::<BTreeSet<_>>();
2200 for point in &manifest.points {
2201 assert_eq!(
2202 declined.contains(&point.id),
2203 point.file == "src/extra.rs",
2204 "{} in {}",
2205 point.id,
2206 point.file
2207 );
2208 }
2209 for decision in &manifest.decisions {
2210 assert_eq!(
2211 declined.contains(&decision.id),
2212 decision.file == "src/extra.rs"
2213 );
2214 }
2215 let report =
2217 crate::frontend_protocol::analyze_frontend_results(&run.declaration, &run.request)
2218 .unwrap();
2219 assert_eq!(
2220 report.view.summary.lines.percentage, 100.0,
2221 "declined obligations must not read as uncovered"
2222 );
2223 fs::remove_dir_all(&root).ok();
2224 }
2225
2226 #[test]
2227 fn assertion_occurrences_are_retained_without_automatic_asserted_credit() {
2228 let nonce = SystemTime::now()
2233 .duration_since(UNIX_EPOCH)
2234 .unwrap()
2235 .as_nanos();
2236 let root = std::env::temp_dir().join(format!(
2237 "supercov-rust-runner-assert-{}-{nonce}",
2238 std::process::id()
2239 ));
2240 fs::create_dir_all(root.join("src")).unwrap();
2241 fs::write(
2242 root.join("Cargo.toml"),
2243 "[package]\nname='fixture'\nversion='0.0.0'\nedition='2024'\n",
2244 )
2245 .unwrap();
2246 fs::write(
2247 root.join("src/lib.rs"),
2248 r#"
2249pub fn checked(value: i32) -> i32 {
2250 value * 2
2251}
2252pub fn unchecked(value: i32) -> i32 {
2253 value + 1
2254}
2255#[cfg(test)]
2256mod tests {
2257 #[test]
2258 fn asserts() {
2259 let doubled = super::checked(2);
2260 assert_eq!(doubled, 4);
2261 }
2262 #[test]
2263 fn asserts_nothing() {
2264 let _ = super::unchecked(1);
2265 }
2266}
2267"#,
2268 )
2269 .unwrap();
2270 let project = prepare_rust_project(&root, None).unwrap();
2271 let run = run_prepared_rust_tests(
2272 &project,
2273 &["cargo".into(), "test".into(), "--lib".into()],
2274 "rust-fixture-assert",
2275 "2026-08-26T00:00:00.000Z",
2276 &mut Vec::new(),
2277 )
2278 .unwrap();
2279 assert_eq!(run.exit_code, 0);
2280
2281 let phases = |test: &str| {
2284 run.request
2285 .raw_results
2286 .iter()
2287 .find(|result| result.test.ends_with(test))
2288 .unwrap_or_else(|| panic!("no test {test}"))
2289 .phases
2290 .clone()
2291 };
2292 let asserting = phases("asserts");
2293 assert!(
2294 !asserting.is_empty(),
2295 "the asserting test recorded no phase"
2296 );
2297 assert!(
2298 asserting
2299 .iter()
2300 .all(|phase| phase.kind == "assertion" && phase.status.as_deref() == Some("passed"))
2301 );
2302 assert!(
2303 phases("asserts_nothing").is_empty(),
2304 "a test that checks nothing witnesses nothing"
2305 );
2306
2307 let report =
2308 crate::frontend_protocol::analyze_frontend_results(&run.declaration, &run.request)
2309 .unwrap();
2310 let level = |line: usize| {
2311 report
2312 .view
2313 .lines
2314 .iter()
2315 .find(|entry| entry.file == "src/lib.rs" && entry.line == line)
2316 .map(|entry| entry.confidence.level.clone())
2317 .unwrap_or_else(|| panic!("no line {line}"))
2318 };
2319 assert_eq!(level(3), "executed");
2321 assert_eq!(level(6), "executed");
2322 fs::remove_dir_all(&root).ok();
2323 }
2324
2325 #[test]
2326 fn libtest_filters_select_the_tests_plain_cargo_would_run() {
2327 let nonce = SystemTime::now()
2331 .duration_since(UNIX_EPOCH)
2332 .unwrap()
2333 .as_nanos();
2334 let root = std::env::temp_dir().join(format!(
2335 "supercov-rust-runner-filters-{}-{nonce}",
2336 std::process::id()
2337 ));
2338 fs::create_dir_all(root.join("src")).unwrap();
2339 fs::write(
2340 root.join("Cargo.toml"),
2341 "[package]\nname='fixture'\nversion='0.0.0'\nedition='2024'\n",
2342 )
2343 .unwrap();
2344 fs::write(
2345 root.join("src/lib.rs"),
2346 r#"
2347pub fn value(flag: bool) -> i32 { if flag { 1 } else { 0 } }
2348#[cfg(test)]
2349mod tests {
2350 #[test] fn alpha() { assert_eq!(super::value(true), 1); }
2351 #[test] fn beta() { assert_eq!(super::value(false), 0); }
2352 #[test] #[ignore] fn gamma_slow() { assert_eq!(super::value(true), 1); }
2353}
2354"#,
2355 )
2356 .unwrap();
2357 let project = prepare_rust_project(&root, None).unwrap();
2358 let names = |run: &str, command: &[&str]| {
2359 let run = run_prepared_rust_tests(
2360 &project,
2361 &command
2362 .iter()
2363 .map(|word| (*word).into())
2364 .collect::<Vec<String>>(),
2365 run,
2366 "2026-08-26T00:00:00.000Z",
2367 &mut Vec::new(),
2368 )
2369 .unwrap();
2370 assert_eq!(run.exit_code, 0, "{:?}", run.request.raw_results);
2371 let mut names = run
2372 .request
2373 .raw_results
2374 .iter()
2375 .map(|result| {
2376 (
2377 result.test.rsplit("::").next().unwrap().to_owned(),
2378 result.status.clone().unwrap(),
2379 )
2380 })
2381 .collect::<Vec<_>>();
2382 names.sort();
2383 names
2384 };
2385 assert_eq!(
2387 names("rust-fixture-filters-1", &["cargo", "test", "--lib"]),
2388 [
2389 ("alpha".to_owned(), "passed".to_owned()),
2390 ("beta".to_owned(), "passed".to_owned()),
2391 ("gamma_slow".to_owned(), "skipped".to_owned()),
2392 ]
2393 );
2394 assert_eq!(
2396 names(
2397 "rust-fixture-filters-2",
2398 &["cargo", "test", "--lib", "alpha"]
2399 ),
2400 [("alpha".to_owned(), "passed".to_owned())]
2401 );
2402 assert_eq!(
2403 names(
2404 "rust-fixture-filters-3",
2405 &["cargo", "test", "--lib", "--", "--skip", "beta"]
2406 ),
2407 [
2408 ("alpha".to_owned(), "passed".to_owned()),
2409 ("gamma_slow".to_owned(), "skipped".to_owned()),
2410 ]
2411 );
2412 assert_eq!(
2414 names(
2415 "rust-fixture-filters-4",
2416 &["cargo", "test", "--lib", "--", "--ignored"]
2417 ),
2418 [("gamma_slow".to_owned(), "passed".to_owned())]
2419 );
2420 fs::remove_dir_all(&root).ok();
2421 }
2422
2423 #[test]
2424 fn libtest_processes_run_in_their_package_directory() {
2425 let nonce = SystemTime::now()
2429 .duration_since(UNIX_EPOCH)
2430 .unwrap()
2431 .as_nanos();
2432 let root = std::env::temp_dir().join(format!(
2433 "supercov-rust-runner-cwd-{}-{nonce}",
2434 std::process::id()
2435 ));
2436 fs::create_dir_all(root.join("member/src")).unwrap();
2437 fs::write(
2438 root.join("Cargo.toml"),
2439 "[workspace]\nmembers = ['member']\nresolver = '2'\n",
2440 )
2441 .unwrap();
2442 fs::write(
2443 root.join("member/Cargo.toml"),
2444 "[package]\nname='member'\nversion='0.0.0'\nedition='2024'\n",
2445 )
2446 .unwrap();
2447 fs::write(
2448 root.join("member/src/lib.rs"),
2449 r#"
2450pub fn manifest() -> String {
2451 std::fs::read_to_string("Cargo.toml").unwrap()
2452}
2453#[cfg(test)]
2454mod tests {
2455 #[test] fn reads_own_manifest() { assert!(super::manifest().contains("name='member'")); }
2456}
2457"#,
2458 )
2459 .unwrap();
2460 let project = prepare_rust_project(&root, None).unwrap();
2461 let run = run_prepared_rust_tests(
2462 &project,
2463 &["cargo".into(), "test".into(), "--lib".into()],
2464 "rust-fixture-cwd",
2465 "2026-08-26T00:00:00.000Z",
2466 &mut Vec::new(),
2467 )
2468 .unwrap();
2469 assert_eq!(run.exit_code, 0, "{:?}", run.request.raw_results);
2470 assert_eq!(run.request.raw_results.len(), 1);
2471 assert_eq!(run.request.raw_results[0].status.as_deref(), Some("passed"));
2472 fs::remove_dir_all(&root).ok();
2473 }
2474
2475 #[test]
2476 fn cargo_libtest_runs_produce_queryable_owned_evidence() {
2477 let nonce = SystemTime::now()
2478 .duration_since(UNIX_EPOCH)
2479 .unwrap()
2480 .as_nanos();
2481 let root = std::env::temp_dir().join(format!(
2482 "supercov-rust-runner-{}-{nonce}",
2483 std::process::id()
2484 ));
2485 fs::create_dir_all(root.join("src")).unwrap();
2486 fs::write(
2487 root.join("Cargo.toml"),
2488 "[package]\nname='fixture'\nversion='0.0.0'\nedition='2024'\n",
2489 )
2490 .unwrap();
2491 fs::write(
2492 root.join("src/lib.rs"),
2493 r#"
2494pub fn choose(left: bool, right: bool) -> i32 {
2495 if left && right { 1 } else { 0 }
2496}
2497pub fn pick(value: i32) -> &'static str {
2498 match value {
2499 0 => "zero",
2500 1 => "one",
2501 _ => "many",
2502 }
2503}
2504pub fn total(values: &[i32]) -> i32 {
2505 let mut sum = 0;
2506 for value in values {
2507 sum += value;
2508 }
2509 sum
2510}
2511pub fn first_even(values: &[i32]) -> Option<i32> {
2512 let mut index = 0;
2513 while index < values.len() {
2514 if values[index] % 2 == 0 {
2515 return Some(values[index]);
2516 }
2517 index += 1;
2518 }
2519 None
2520}
2521pub fn parse_twice(text: &str) -> Option<i32> {
2522 let value: i32 = text.parse().ok()?;
2523 Some(value * 2)
2524}
2525pub fn describe(value: Option<i32>, flag: bool) -> &'static str {
2526 if let Some(inner) = value && inner > 0 && flag {
2527 "positive"
2528 } else {
2529 "other"
2530 }
2531}
2532pub fn depth(n: u32) -> Result<u32, String> {
2533 if n == 0 {
2534 Ok(0)
2535 } else {
2536 let below = depth(n - 1)?;
2537 Ok(below + 1)
2538 }
2539}
2540#[cfg(test)]
2541mod tests {
2542 #[test] fn false_path() { assert_eq!(super::choose(false, true), 0); }
2543 #[test] fn true_path() { assert_eq!(super::choose(true, true), 1); }
2544 #[test] #[ignore] fn ignored_path() { unreachable!(); }
2545 #[test] fn pick_zero() { assert_eq!(super::pick(0), "zero"); }
2546 #[test] fn pick_many() { assert_eq!(super::pick(7), "many"); }
2547 #[test] fn total_empty() { assert_eq!(super::total(&[]), 0); }
2548 #[test] fn total_some() { assert_eq!(super::total(&[1, 2]), 3); }
2549 #[test] fn first_even_empty() { assert_eq!(super::first_even(&[]), None); }
2550 #[test] fn first_even_found() { assert_eq!(super::first_even(&[1, 4]), Some(4)); }
2551 #[test] fn parse_ok() { assert_eq!(super::parse_twice("4"), Some(8)); }
2552 #[test] fn parse_bad() { assert_eq!(super::parse_twice("x"), None); }
2553 #[test] fn chain_taken() { assert_eq!(super::describe(Some(1), true), "positive"); }
2554 #[test] fn chain_pattern_fails() { assert_eq!(super::describe(None, true), "other"); }
2555 #[test] fn chain_negative() { assert_eq!(super::describe(Some(-1), true), "other"); }
2556 #[test] fn chain_flag_fails() { assert_eq!(super::describe(Some(1), false), "other"); }
2557 #[test] fn deep_recursion() { assert_eq!(super::depth(5_000), Ok(5_000)); }
2558}
2559"#,
2560 )
2561 .unwrap();
2562 let project = prepare_rust_project(&root, None).unwrap();
2563 let run = run_prepared_rust_tests(
2567 &project,
2568 &["cargo".into(), "test".into(), "--lib".into()],
2569 "rust-fixture",
2570 "2026-08-26T00:00:00.000Z",
2571 &mut Vec::new(),
2572 )
2573 .unwrap();
2574 assert_eq!(run.exit_code, 0);
2575 assert_eq!(run.request.raw_results.len(), 16);
2576 let statuses = run
2577 .request
2578 .raw_results
2579 .iter()
2580 .filter_map(|result| result.status.as_deref())
2581 .collect::<Vec<_>>();
2582 assert_eq!(
2583 statuses
2584 .iter()
2585 .filter(|status| **status == "skipped")
2586 .count(),
2587 1
2588 );
2589 assert_eq!(
2590 statuses
2591 .iter()
2592 .filter(|status| **status == "passed")
2593 .count(),
2594 15
2595 );
2596
2597 let chain_vectors = |test: &str| {
2600 let result = run
2601 .request
2602 .raw_results
2603 .iter()
2604 .find(|result| result.test.ends_with(test))
2605 .unwrap_or_else(|| panic!("no test {test}"));
2606 let snapshot = result
2607 .runtime
2608 .iter()
2609 .flat_map(|snapshot| &snapshot.decisions)
2610 .find(|decision| decision.meta.source.starts_with("let Some(inner) = value"))
2611 .unwrap_or_else(|| panic!("{test} recorded no chain decision"));
2612 snapshot
2613 .vectors
2614 .iter()
2615 .map(|vector| (vector.values.clone(), vector.outcome))
2616 .collect::<Vec<_>>()
2617 };
2618 assert_eq!(
2619 chain_vectors("chain_taken"),
2620 [(vec![Some(true), Some(true), Some(true)], true)]
2621 );
2622 assert_eq!(
2623 chain_vectors("chain_pattern_fails"),
2624 [(vec![Some(false), None, None], false)]
2625 );
2626 assert_eq!(
2627 chain_vectors("chain_negative"),
2628 [(vec![Some(true), Some(false), None], false)]
2629 );
2630 assert_eq!(
2631 chain_vectors("chain_flag_fails"),
2632 [(vec![Some(true), Some(true), Some(false)], false)]
2633 );
2634 validate_frontend_report_request(&run.declaration, &run.request).unwrap();
2635 let archive = root.join("evidence.raw.gz");
2636 write_archive(run.archive_entries().unwrap(), &archive).unwrap();
2637 let report = analyze_coverage_archive(&ArchiveReportRequest {
2638 archive_path: archive,
2639 run_id: "rust-fixture".into(),
2640 generated_at: "2026-08-26T00:00:00.000Z".into(),
2641 integrity: None,
2642 test_exit_code: ExitCodeInput::Present(Some(0)),
2643 })
2644 .unwrap();
2645 assert_eq!(report.view.tests.len(), 16);
2646 assert!(report.view.summary.lines.covered > 0);
2647 assert!(report.view.summary.decisions > 0);
2648
2649 let single = |kind: &str| {
2652 let mut found = report
2653 .view
2654 .branches
2655 .iter()
2656 .filter(|branch| branch.meta.kind == kind);
2657 let branch = found.next().unwrap_or_else(|| panic!("no {kind} branch"));
2658 assert!(found.next().is_none(), "more than one {kind} branch");
2659 branch
2660 };
2661 let tests_of = |branch: &crate::coverage_report::BranchResult, label: &str| {
2662 branch
2663 .alternatives
2664 .iter()
2665 .find(|alternative| alternative.label == label)
2666 .unwrap_or_else(|| panic!("{} has no alternative {label}", branch.meta.kind))
2667 .tests
2668 .clone()
2669 };
2670 let for_loop = single("for-loop");
2671 assert_eq!(
2672 tests_of(for_loop, "zero iterations"),
2673 ["src/lib.rs::tests::total_empty"]
2674 );
2675 assert_eq!(
2676 tests_of(for_loop, "entered"),
2677 ["src/lib.rs::tests::total_some"]
2678 );
2679 let while_loop = single("while-loop");
2680 assert_eq!(
2681 tests_of(while_loop, "zero iterations"),
2682 ["src/lib.rs::tests::first_even_empty"]
2683 );
2684 assert_eq!(
2685 tests_of(while_loop, "entered"),
2686 ["src/lib.rs::tests::first_even_found"]
2687 );
2688 let try_operator = report
2691 .view
2692 .branches
2693 .iter()
2694 .find(|branch| {
2695 branch.meta.kind == "try-operator" && branch.meta.source.contains("parse().ok()")
2696 })
2697 .expect("parse_twice's try operator");
2698 assert_eq!(
2699 tests_of(try_operator, "continued"),
2700 ["src/lib.rs::tests::parse_ok"]
2701 );
2702 assert_eq!(
2703 tests_of(try_operator, "early return"),
2704 ["src/lib.rs::tests::parse_bad"]
2705 );
2706 let mut logical = report
2707 .view
2708 .branches
2709 .iter()
2710 .filter(|branch| branch.meta.kind == "logical-and")
2711 .collect::<Vec<_>>();
2712 logical.sort_by_key(|branch| (branch.meta.line, branch.meta.column));
2713 assert_eq!(logical.len(), 3);
2715 assert_eq!(
2716 tests_of(logical[0], "short-circuited"),
2717 ["src/lib.rs::tests::false_path"]
2718 );
2719 assert_eq!(
2720 tests_of(logical[0], "right operand evaluated"),
2721 ["src/lib.rs::tests::true_path"]
2722 );
2723 assert_eq!(
2724 tests_of(logical[1], "short-circuited"),
2725 ["src/lib.rs::tests::chain_pattern_fails"]
2726 );
2727 assert_eq!(
2728 tests_of(logical[1], "right operand evaluated"),
2729 [
2730 "src/lib.rs::tests::chain_flag_fails",
2731 "src/lib.rs::tests::chain_negative",
2732 "src/lib.rs::tests::chain_taken",
2733 ]
2734 );
2735 assert_eq!(
2736 tests_of(logical[2], "short-circuited"),
2737 [
2738 "src/lib.rs::tests::chain_negative",
2739 "src/lib.rs::tests::chain_pattern_fails",
2740 ]
2741 );
2742 assert_eq!(
2743 tests_of(logical[2], "right operand evaluated"),
2744 [
2745 "src/lib.rs::tests::chain_flag_fails",
2746 "src/lib.rs::tests::chain_taken",
2747 ]
2748 );
2749 assert!(for_loop.covered && while_loop.covered && try_operator.covered);
2750 assert!(logical.iter().all(|branch| branch.covered));
2751
2752 let mut arms = report
2755 .view
2756 .branches
2757 .iter()
2758 .filter(|branch| branch.meta.kind == "match-arm")
2759 .collect::<Vec<_>>();
2760 arms.sort_by_key(|branch| branch.meta.line);
2761 assert_eq!(arms.len(), 3);
2762 let alternative = |arm: usize, label: &str| {
2763 arms[arm]
2764 .alternatives
2765 .iter()
2766 .find(|alternative| alternative.label == label)
2767 .unwrap_or_else(|| panic!("arm {arm} has no alternative {label}"))
2768 };
2769 assert_eq!(
2770 alternative(0, "selected").tests,
2771 ["src/lib.rs::tests::pick_zero"]
2772 );
2773 assert_eq!(
2774 alternative(0, "not selected").tests,
2775 ["src/lib.rs::tests::pick_many"]
2776 );
2777 assert!(!alternative(1, "selected").covered);
2778 assert_eq!(
2779 alternative(1, "not selected").tests,
2780 ["src/lib.rs::tests::pick_many"]
2781 );
2782 assert_eq!(arms[2].alternatives.len(), 1);
2783 assert_eq!(
2784 alternative(2, "selected").tests,
2785 ["src/lib.rs::tests::pick_many"]
2786 );
2787 assert!(arms[0].covered && !arms[1].covered && arms[2].covered);
2788
2789 fs::remove_dir_all(root).unwrap();
2790 }
2791}