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