use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::time::{Duration, Instant};
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use crate::propagation::{self};
use crate::trace::Trace;
use crate::work_lock::WorkLock;
use crate::{leftovers, progress, witness};
use super::*;
#[derive(Clone)]
pub(super) enum Runner {
Exe(PathBuf),
Isolated {
exe: PathBuf,
test: String,
ignored: bool,
wrapper: Option<PathBuf>,
},
Nextest {
copy: PathBuf,
target_dir: PathBuf,
profile: Option<String>,
opt_level: Option<u8>,
flags: Vec<String>,
},
Doc {
copy: PathBuf,
target_dir: PathBuf,
package: String,
opt_level: Option<u8>,
flags: Vec<String>,
},
}
impl Runner {
pub(super) fn executable(&self) -> Option<&Path> {
match self {
Self::Exe(exe) | Self::Isolated { exe, .. } => Some(exe),
Self::Doc { .. } | Self::Nextest { .. } => None,
}
}
}
#[derive(Clone, Copy)]
pub(super) enum BaselineTiming {
Completed(Duration),
StillRunning(Duration),
}
pub(super) fn explicit_timeout_conflict(
timeout: Option<Duration>,
baseline: Duration,
selected_count: Option<usize>,
test_count: usize,
) -> Option<Duration> {
let entire_runner = selected_count.is_none_or(|count| count == test_count);
timeout.filter(|&limit| entire_runner && baseline >= limit)
}
pub(super) type FilteredTimings =
std::sync::Mutex<BTreeMap<(usize, Vec<String>), std::sync::Arc<std::sync::OnceLock<Duration>>>>;
pub(super) struct TestBinary {
pub(super) name: String,
pub(super) cwd: PathBuf,
pub(super) marks: PathBuf,
pub(super) git_ceiling: PathBuf,
pub(super) work_lock: Option<Arc<WorkLock>>,
pub(super) tests: BTreeSet<String>,
pub(super) runner: Runner,
pub(super) baseline: Duration,
pub(super) untraced_baseline: Option<BaselineTiming>,
pub(super) timeout_diagnostic: std::sync::Once,
pub(super) traced: Duration,
pub(super) timeout: Option<Duration>,
pub(super) wrapper_grace: Duration,
pub(super) report_slow: bool,
pub(super) filtered_timings: FilteredTimings,
pub(super) trace: Trace,
pub(super) effects_baseline: std::sync::OnceLock<propagation::BaselineIndex>,
}
impl TestBinary {
pub(super) fn mutant_limit(&self, baseline: Duration) -> Duration {
self.timeout.unwrap_or_else(|| mutant_timeout(baseline))
}
pub(super) fn warn_timeout_conflict(&self, filter: Option<&[String]>) {
let Some(limit) = explicit_timeout_conflict(
self.timeout,
self.baseline,
filter.map(<[String]>::len),
self.tests.len(),
) else {
return;
};
self.timeout_diagnostic.call_once(|| {
let timing = self.untraced_baseline.or_else(|| {
let started = Instant::now();
match run_binary(self, None, &[], self.traced, RunOutput::Quiet, 1) {
Ok(Outcome::Pass) => Some(BaselineTiming::Completed(started.elapsed())),
Ok(Outcome::Timeout) => Some(BaselineTiming::StillRunning(self.traced)),
_ => None,
}
});
let (elapsed, message) = match timing {
Some(BaselineTiming::Completed(elapsed)) => (elapsed, "took"),
Some(BaselineTiming::StillRunning(elapsed)) => (elapsed, "was still running after"),
None => return,
};
if elapsed >= limit {
eprintln!(
"tmt: warning: unmutated baseline for `{}` {message} {:.3}s, meeting or exceeding --timeout {}",
crate::report::sanitize_inline(&self.name),
elapsed.as_secs_f64(),
limit.as_secs()
);
}
});
}
pub(super) fn filtered_limit(
&self,
filter: Option<&[String]>,
traced: bool,
sharing: usize,
) -> Duration {
let baseline = if traced { self.traced } else { self.baseline };
let Some(filter) =
filter.filter(|names| !names.is_empty() && names.len() < self.tests.len())
else {
return self.mutant_limit(baseline);
};
if self.timeout.is_some() || baseline < SLOW_TRACE || !matches!(self.runner, Runner::Exe(_))
{
return self.mutant_limit(baseline);
}
let cores = std::thread::available_parallelism().map_or(2, |n| n.get());
let threads = test_threads(cores, sharing);
let timed = {
let mut groups = self
.filtered_timings
.lock()
.unwrap_or_else(|e| e.into_inner());
groups
.entry((threads, filter.to_vec()))
.or_default()
.clone()
};
let timing = *timed.get_or_init(|| {
let untraced = self.baseline;
let started = Instant::now();
match run_binary(
self,
Some(filter),
&[],
untraced,
RunOutput::Quiet,
sharing.max(1),
) {
Ok(Outcome::Pass) => started.elapsed(),
_ => untraced,
}
});
self.mutant_limit(if traced {
timing.max(
timing.mul_f64(self.traced.as_secs_f64() / self.baseline.as_secs_f64().max(0.001)),
)
} else {
timing
})
}
}
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum Outcome {
Pass,
Fail,
Timeout,
Unable,
}
impl From<Outcome> for progress::Outcome {
fn from(outcome: Outcome) -> Self {
match outcome {
Outcome::Pass => Self::Survived,
Outcome::Fail => Self::Failed,
Outcome::Timeout => Self::TimedOut,
Outcome::Unable => Self::Unable,
}
}
}
pub(super) fn test_filter(
tests: &BTreeSet<String>,
known: &BTreeSet<String>,
) -> Option<Vec<String>> {
if tests.len() > MAX_TEST_FILTERS || !tests.iter().all(|t| known.contains(t)) {
None
} else {
Some(tests.iter().cloned().collect())
}
}
pub(super) fn parse_test_list(stdout: &str) -> BTreeSet<String> {
stdout
.lines()
.filter_map(|line| line.strip_suffix(": test"))
.map(str::to_string)
.collect()
}
pub(super) fn list_tests(
exe: &Path,
cwd: &Path,
work: &Path,
ignored: bool,
strict: bool,
lock: Option<&WorkLock>,
) -> Result<BTreeSet<String>> {
let mut command = Command::new(exe);
isolate_copy_command(&mut command, work);
if let Some(lock) = lock {
lock.retain_for(&mut command);
}
command.args(["--list", "--format", "terse"]);
if ignored {
command.arg("--ignored");
}
let listing = command
.current_dir(cwd)
.stdin(Stdio::null())
.output()
.with_context(|| format!("listing tests of {}", exe.display()))?;
if !listing.status.success() {
if !strict {
return Ok(BTreeSet::new());
}
bail!(
"listing tests of {} failed: {}",
exe.display(),
String::from_utf8_lossy(&listing.stderr)
);
}
Ok(parse_test_list(&String::from_utf8_lossy(&listing.stdout)))
}
pub(super) fn profile_env(opt_level: Option<u8>) -> Vec<(&'static str, String)> {
opt_level.map_or(vec![], |level| {
vec![
("CARGO_PROFILE_DEV_OPT_LEVEL", level.to_string()),
("CARGO_PROFILE_TEST_OPT_LEVEL", level.to_string()),
]
})
}
pub(super) const SLOW_TRACE: Duration = Duration::from_secs(2);
pub(super) fn mutant_timeout(baseline: Duration) -> Duration {
Duration::from_secs(5) + baseline * 5
}
pub(super) const TEST_POLL_INITIAL: Duration = Duration::from_micros(250);
pub(super) const TEST_POLL_CAP: Duration = Duration::from_millis(5);
pub(super) fn next_test_poll(delay: &mut Duration, remaining: Duration) -> Duration {
let wait = (*delay).min(remaining);
*delay = (*delay * 2).min(TEST_POLL_CAP);
wait
}
pub(super) fn test_threads(cores: usize, sharing: usize) -> usize {
(cores / sharing.max(1)).max(2)
}
pub(super) enum RunOutput<'a> {
Quiet,
FirstFailure,
Capture(&'a fs::File),
}
pub(super) fn nextest_outcome(code: Option<i32>) -> Result<Outcome> {
match code {
Some(0) => Ok(Outcome::Pass),
Some(100) => Ok(Outcome::Fail),
_ => bail!("cargo nextest failed outside a test (exit code {code:?})"),
}
}
pub(super) fn run_binary(
bin: &TestBinary,
tests: Option<&[String]>,
env: &[(&str, &str)],
timeout: Duration,
output: RunOutput<'_>,
sharing: usize,
) -> Result<Outcome> {
let cores = std::thread::available_parallelism().map_or(2, |n| n.get());
let threads = test_threads(cores, sharing);
static RUNS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let unable_mark = reserve_file(&bin.marks, "unable", &RUNS)?;
let mut cmd = match &bin.runner {
Runner::Exe(exe) => {
let mut cmd = Command::new(exe);
cmd.arg(format!("--test-threads={threads}"));
if let Some(tests) = tests {
cmd.arg("--exact").args(tests);
}
cmd.current_dir(&bin.cwd)
.env("CARGO_MANIFEST_DIR", &bin.cwd);
cmd
}
Runner::Isolated {
exe,
test,
ignored,
wrapper,
} => {
let mut cmd = if let Some(wrapper) = wrapper {
let mut cmd = Command::new(wrapper);
cmd.arg(exe);
cmd
} else {
Command::new(exe)
};
cmd.args(["--exact", "--test-threads=1", test]);
if *ignored {
cmd.arg("--ignored");
}
cmd.current_dir(&bin.cwd)
.env("CARGO_MANIFEST_DIR", &bin.cwd);
cmd
}
Runner::Nextest {
copy,
target_dir,
profile,
opt_level,
flags,
} => {
let mut cmd = Command::new("cargo");
cmd.args(["nextest", "run", "--no-tests=fail"]);
if let Some(profile) = profile {
cmd.args(["--profile", profile]);
}
cmd.args(flags)
.current_dir(copy)
.env("CARGO_TARGET_DIR", target_dir)
.env("CARGO_BUILD_BUILD_DIR", target_dir)
.envs(profile_env(*opt_level));
cmd
}
Runner::Doc {
copy,
target_dir,
package,
opt_level,
flags,
} => {
let mut cmd = Command::new("cargo");
cmd.args(["test", "--doc", "-p", package])
.args(flags)
.current_dir(copy)
.env("CARGO_TARGET_DIR", target_dir)
.env("CARGO_BUILD_BUILD_DIR", target_dir)
.envs(profile_env(*opt_level));
cmd
}
};
isolate_copy_command(&mut cmd, &bin.git_ceiling);
if let Some(lock) = &bin.work_lock {
lock.retain_for(&mut cmd);
}
cmd.env_remove("TMT_TRACE")
.env_remove("TMT_MUTANT")
.env_remove("TMT_TEST")
.env_remove(witness::OUTPUT_ENV)
.env("TMT_UNABLE", &unable_mark)
.envs(env.iter().copied())
.stdin(Stdio::null());
if let Runner::Isolated { test, .. } = &bin.runner {
cmd.env("TMT_TEST", test);
}
let nextest = matches!(bin.runner, Runner::Nextest { .. });
let mut output_to_watch = None;
match output {
RunOutput::Capture(file) => {
cmd.stdout(file.try_clone()?).stderr(file.try_clone()?);
}
RunOutput::FirstFailure if !nextest && !matches!(bin.runner, Runner::Isolated { .. }) => {
let writer = fs::OpenOptions::new().append(true).open(&unable_mark)?;
output_to_watch = Some(FailureOutput::new(fs::File::open(&unable_mark)?));
cmd.stdout(writer).stderr(Stdio::null());
}
RunOutput::Quiet | RunOutput::FirstFailure => {
cmd.stdout(Stdio::null()).stderr(Stdio::null());
}
}
#[cfg(unix)]
std::os::unix::process::CommandExt::process_group(&mut cmd, 0);
let mut watch = leftovers::Watch::new(reserve_file(&bin.marks, "held", &RUNS)?);
let mut child = match watch.spawn(&mut cmd) {
Ok(child) => child,
Err(error) => {
watch.finish(true);
let _ = release_mark(&unable_mark);
return Err(error).with_context(|| format!("starting the tests of `{}`", bin.name));
}
};
let group = child.id();
let kill = |child: &mut std::process::Child| {
if nextest {
stop_nextest(child, Duration::from_secs(12));
return;
}
kill_test_group(group);
let _ = child.kill();
let _ = child.wait();
};
let started = Instant::now();
let mut delay = TEST_POLL_INITIAL;
let mut waiting_reported = false;
let running_mutant = env.iter().any(|(key, _)| *key == "TMT_MUTANT")
|| matches!(output, RunOutput::FirstFailure);
let result: Result<Outcome> = (|| loop {
if let Some(status) = child.try_wait()? {
if matches!(bin.runner, Runner::Isolated { .. }) || output_to_watch.is_some() {
kill_test_group(group);
}
if nextest {
return nextest_outcome(status.code());
}
return Ok(if status.success() {
Outcome::Pass
} else {
Outcome::Fail
});
}
if output_to_watch
.as_mut()
.map(FailureOutput::failed)
.transpose()?
.unwrap_or(false)
{
kill(&mut child);
return Ok(Outcome::Fail);
}
let elapsed = started.elapsed();
if bin.report_slow
&& running_mutant
&& !waiting_reported
&& elapsed >= Duration::from_secs(5).min(timeout / 2)
{
let selected = tests
.filter(|names| !names.is_empty())
.map(|names| {
names
.iter()
.map(|name| crate::report::sanitize_inline(name))
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_else(|| crate::report::sanitize_inline(&bin.name));
eprintln!(
"tmt: waiting for mutant tests `{selected}` (limit {:.2}s)",
timeout.as_secs_f64()
);
waiting_reported = true;
}
if elapsed >= timeout {
if cfg!(unix)
&& !bin.wrapper_grace.is_zero()
&& matches!(
&bin.runner,
Runner::Isolated {
wrapper: Some(_),
..
}
)
{
signal_test_group(group, 15);
let began = Instant::now();
while began.elapsed() < bin.wrapper_grace && child.try_wait().ok() == Some(None) {
std::thread::sleep(
Duration::from_millis(10)
.min(bin.wrapper_grace.saturating_sub(began.elapsed())),
);
}
}
kill(&mut child);
return Ok(Outcome::Timeout);
}
std::thread::sleep(next_test_poll(&mut delay, timeout - elapsed));
})();
drop(output_to_watch);
if result.is_err() {
kill(&mut child);
let _ = release_mark(&unable_mark);
}
leftovers::count(&watch.finish(leftovers::leave()));
let outcome = result?;
if release_mark(&unable_mark)? {
return Ok(Outcome::Unable);
}
Ok(outcome)
}
pub(super) struct FailureOutput {
pub(super) file: fs::File,
pub(super) line: Vec<u8>,
pub(super) discarded: bool,
}
impl FailureOutput {
pub(super) fn new(file: fs::File) -> Self {
Self {
file,
line: Vec::new(),
discarded: false,
}
}
pub(super) fn failed(&mut self) -> std::io::Result<bool> {
use std::io::Read as _;
const MAX_LINE: usize = 8 * 1024;
let mut chunk = [0; MAX_LINE];
let read = self.file.read(&mut chunk)?;
for part in chunk[..read].split_inclusive(|byte| *byte == b'\n') {
self.discarded |= self.line.len() + part.len() > MAX_LINE;
if !self.discarded {
self.line.extend_from_slice(part);
}
if part.ends_with(b"\n") {
if !self.discarded
&& is_failure_line(String::from_utf8_lossy(&self.line).trim_end())
{
return Ok(true);
}
self.discarded = false;
self.line.clear();
}
}
Ok(false)
}
}
pub(super) fn stop_nextest(child: &mut std::process::Child, grace: Duration) {
let wait = |child: &mut std::process::Child, limit: Duration| {
let started = Instant::now();
loop {
match child.try_wait() {
Ok(Some(_)) => return true,
Ok(None) if started.elapsed() < limit => {
std::thread::sleep(Duration::from_millis(10));
}
_ => return false,
}
}
};
let group = child.id();
#[cfg(unix)]
let mut owned = owned_test_processes(group);
if wait(child, Duration::ZERO) {
#[cfg(unix)]
for surviving in surviving_groups(&owned, &test_processes()) {
kill_test_group(surviving);
}
return;
}
signal_test_group(group, 15);
let mut reaped = wait(child, grace);
if !reaped {
signal_test_group(group, 15);
reaped = wait(child, grace.min(Duration::from_secs(1)));
}
#[cfg(unix)]
{
if !reaped {
owned.extend(owned_test_processes(group));
}
for surviving in surviving_groups(&owned, &test_processes()) {
kill_test_group(surviving);
}
}
if !reaped {
kill_test_group(group);
let _ = child.kill();
let _ = child.wait();
}
}
#[cfg(unix)]
#[derive(Debug, PartialEq)]
pub(super) struct TestProcess {
pub(super) pid: u32,
pub(super) parent: u32,
pub(super) group: u32,
pub(super) started: String,
}
#[cfg(unix)]
pub(super) fn test_processes() -> Vec<TestProcess> {
let Ok(processes) = Command::new("ps")
.args([
"-e", "-o", "pid=", "-o", "ppid=", "-o", "pgid=", "-o", "lstart=",
])
.output()
else {
return vec![];
};
String::from_utf8_lossy(&processes.stdout)
.lines()
.filter_map(|line| {
let mut fields = line.split_whitespace();
Some(TestProcess {
pid: fields.next()?.parse().ok()?,
parent: fields.next()?.parse().ok()?,
group: fields.next()?.parse().ok()?,
started: fields.collect::<Vec<_>>().join(" "),
})
})
.collect()
}
#[cfg(unix)]
pub(super) fn surviving_groups(owned: &[TestProcess], current: &[TestProcess]) -> BTreeSet<u32> {
current
.iter()
.filter(|process| {
owned.iter().any(|before| {
process.pid == before.pid
&& process.group == before.group
&& process.started == before.started
})
})
.map(|process| process.group)
.collect()
}
#[cfg(unix)]
pub(super) fn owned_test_processes(root: u32) -> Vec<TestProcess> {
owned_among(test_processes(), root)
}
#[cfg(unix)]
pub(super) fn owned_among(data: Vec<TestProcess>, root: u32) -> Vec<TestProcess> {
let mut owned = HashSet::from([root]);
loop {
let before = owned.len();
for process in &data {
if owned.contains(&process.parent) {
owned.insert(process.pid);
}
}
if owned.len() == before {
break;
}
}
let groups: HashSet<_> = data
.iter()
.filter(|process| process.pid == process.group && owned.contains(&process.pid))
.map(|process| process.group)
.collect();
data.into_iter()
.filter(|process| owned.contains(&process.pid) && groups.contains(&process.group))
.collect()
}
pub(super) fn kill_test_group(id: u32) {
signal_test_group(id, 9);
}
pub(super) fn signal_test_group(id: u32, signal: i32) {
if id <= 1 {
return;
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
unsafe extern "C" {
fn killpg(group: std::ffi::c_int, signal: std::ffi::c_int) -> std::ffi::c_int;
}
if let Ok(group) = std::ffi::c_int::try_from(id)
&& group > 1
{
unsafe { killpg(group, signal) };
}
}
#[cfg(all(unix, not(any(target_os = "linux", target_os = "macos"))))]
let _ = Command::new("kill")
.args([format!("-{signal}"), "--".to_owned(), format!("-{id}")])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
#[cfg(not(unix))]
let _ = (id, signal);
}
pub(super) fn run_baseline(
bin: &TestBinary,
env: &[(&str, &str)],
timeout: Duration,
path: &Path,
timing_only: bool,
) -> Result<bool> {
use std::io::{Read, Seek, SeekFrom};
let mut suffix = 0;
let (path, mut log) = loop {
let candidate = if suffix == 0 {
path.to_path_buf()
} else {
path.with_extension(format!("{suffix}.log"))
};
match fs::OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.open(&candidate)
{
Ok(log) => break (candidate, log),
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => suffix += 1,
Err(err) => {
return Err(err)
.with_context(|| format!("creating baseline log {}", candidate.display()));
}
}
};
let result =
run_binary(bin, None, env, timeout, RunOutput::Capture(&log), 1).with_context(|| {
format!(
"running baseline for `{}`; output: {}",
bin.name,
path.display()
)
})?;
if result == Outcome::Pass || (timing_only && result == Outcome::Timeout) {
drop(log);
fs::remove_file(&path).with_context(|| format!("removing {}", path.display()))?;
return Ok(result == Outcome::Pass);
}
let reason = match result {
Outcome::Pass => unreachable!("pass handled above"),
Outcome::Fail => "do not pass on the unmutated code",
Outcome::Timeout => "timed out on the unmutated code",
Outcome::Unable => "could not be evaluated on the unmutated code",
};
const LIMIT: u64 = 16 * 1024;
log.seek(SeekFrom::Start(log.metadata()?.len().saturating_sub(LIMIT)))?;
let mut tail = Vec::new();
log.take(LIMIT).read_to_end(&mut tail)?;
bail!(
"the tests in `{}` {reason}; full output: {}\n{}",
bin.name,
path.display(),
String::from_utf8_lossy(&tail).trim_end()
);
}
pub(super) fn run_source_tests(binaries: &[&TestBinary]) -> Result<Outcome> {
for bin in binaries {
bin.warn_timeout_conflict(None);
let outcome = run_binary(
bin,
None,
&[],
bin.mutant_limit(bin.baseline),
RunOutput::FirstFailure,
1,
)?;
if outcome != Outcome::Pass {
return Ok(outcome);
}
}
Ok(Outcome::Pass)
}
pub(super) fn is_failure_line(line: &str) -> bool {
line.starts_with("test ") && line.ends_with(" ... FAILED")
}
pub(super) fn run_pool<T: Send>(
count: usize,
workers: usize,
work: impl Fn(usize) -> Result<T> + Sync,
) -> Result<Vec<T>> {
if count == 0 {
return Ok(vec![]);
}
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
let next = AtomicUsize::new(0);
let stop = AtomicBool::new(false);
let done: Vec<Vec<(usize, Result<T>)>> = std::thread::scope(|scope| {
let handles: Vec<_> = (0..workers.clamp(1, count))
.map(|_| {
scope.spawn(|| {
let mut mine = vec![];
while !stop.load(Ordering::Acquire) {
let index = next.fetch_add(1, Ordering::Relaxed);
if index >= count {
break;
}
let result = work(index);
if result.is_err() {
stop.store(true, Ordering::Release);
}
mine.push((index, result));
}
mine
})
})
.collect();
handles
.into_iter()
.map(|handle| handle.join().expect("worker panicked"))
.collect()
});
let mut ordered: Vec<Option<Result<T>>> = (0..count).map(|_| None).collect();
for (index, result) in done.into_iter().flatten() {
ordered[index] = Some(result);
}
ordered.into_iter().flatten().collect()
}