use std::path::{Path, PathBuf};
use std::process::Command;
use fallow_output::{MutantStatus, MutationCompletionStatus, MutationReport};
use super::template::{self, Runner};
use crate::mutation::{MutationRunOptions, MutationRunScope, Operator};
use crate::{AnalysisOptions, run_mutation};
const REQUIRED_ENV: &str = "FALLOW_MUTATION_E2E_REQUIRED";
pub const SOURCE: &str = "src/math.ts";
#[expect(
clippy::print_stderr,
reason = "a skipped model test says why it did not run"
)]
fn skip(reason: &str) {
assert!(
std::env::var_os(REQUIRED_ENV).is_none_or(|value| value != "1"),
"{REQUIRED_ENV}=1 is set, but {reason}"
);
eprintln!("skipped: {reason}");
}
fn on_path(program: &str) -> bool {
Command::new(program)
.arg("--version")
.output()
.is_ok_and(|output| output.status.success())
}
fn node_has_sync_hooks() -> bool {
Command::new("node")
.args([
"-e",
"process.exit(typeof require('node:module').registerHooks === 'function' ? 0 : 1)",
])
.output()
.is_ok_and(|output| output.status.success())
}
pub fn vitest_package() -> Option<PathBuf> {
let Some(manifest_dir) = std::env::var_os("CARGO_MANIFEST_DIR") else {
skip("the result cache model needs CARGO_MANIFEST_DIR, which cargo sets");
return None;
};
let package = Path::new(&manifest_dir).join("../../tools/mutation-runner/node_modules/vitest");
if package.join("package.json").is_file() && node_has_sync_hooks() {
return Some(dunce::canonicalize(package).expect("vitest path"));
}
skip(&format!(
"the result cache model needs `node` 22.15 or later on PATH (for `module.registerHooks`) and `npm ci` in {}",
package.display()
));
None
}
pub fn available_runners() -> Vec<Runner> {
let mut runners = vec![Runner::Vitest];
if on_path("bun") {
runners.push(Runner::Bun);
} else {
skip("the result cache model of `bun test` needs `bun` on PATH");
}
runners
}
pub type Observation = Vec<(String, MutantStatus, Option<String>)>;
pub struct Run {
pub observation: Observation,
pub hits: usize,
pub stored: usize,
}
pub struct Project {
_dir: tempfile::TempDir,
pub root: PathBuf,
}
impl Project {
pub fn new(runner: Runner, vitest: &Path) -> Self {
let dir = tempfile::tempdir().expect("dir");
let root = dunce::canonicalize(dir.path()).expect("root");
template::write(&root, runner, vitest);
Self { _dir: dir, root }
}
pub fn run(&self, cached: bool) -> Run {
let options = MutationRunOptions {
analysis: AnalysisOptions {
root: Some(self.root.clone()),
no_cache: !cached,
..AnalysisOptions::default()
},
scope: MutationRunScope::Paths(vec![PathBuf::from(SOURCE)]),
operators: Some(vec![Operator::ArithmeticOperator]),
allow_dirty: true,
concurrency: Some(1),
..MutationRunOptions::default()
};
let report = run_mutation(&options).expect("the mutation run completes");
check_complete(&report);
let (hits, stored) = report
.cache
.as_ref()
.map_or((0, 0), |cache| (cache.hits, cache.stored));
let mut observation: Observation = report
.mutants
.into_iter()
.map(|mutant| (mutant.location.mutant_id, mutant.status, mutant.killed_by))
.collect();
observation.sort();
Run {
observation,
hits,
stored,
}
}
}
fn check_complete(report: &MutationReport) {
assert!(report.errors.is_empty(), "{:#?}", report.errors);
assert_eq!(
report.completion.status,
MutationCompletionStatus::Complete,
"{:#?}",
report.completion
);
assert!(!report.mutants.is_empty(), "the run has no mutant");
}