use std::collections::BTreeSet;
use fallow_output::{MutantResult, MutationBaselineFailure};
use rustc_hash::{FxHashMap, FxHashSet};
pub struct Scope<'a> {
baseline: &'a [MutationBaselineFailure],
results: &'a [MutantResult],
test_files: &'a FxHashMap<String, String>,
}
impl<'a> Scope<'a> {
pub fn new(
baseline: &'a [MutationBaselineFailure],
results: &'a [MutantResult],
test_files: &'a FxHashMap<String, String>,
) -> Self {
Self {
baseline,
results,
test_files,
}
}
pub fn failures(&self, tests: &[String]) -> Option<Vec<String>> {
if self.baseline.is_empty() {
return None;
}
let tests: FxHashSet<&str> = tests.iter().map(String::as_str).collect();
let files: FxHashSet<&str> = tests
.iter()
.filter_map(|test| self.test_files.get(*test))
.filter(|file| !file.is_empty())
.map(String::as_str)
.collect();
let mutants: Vec<&MutantResult> = self
.results
.iter()
.filter(|result| touches(result, |test| tests.contains(test)))
.collect();
let mut names = BTreeSet::new();
let mut affected = false;
for failure in self.baseline {
let shares_file = failure
.file
.as_deref()
.is_some_and(|file| files.contains(file));
let unscoped = failure.test_id.is_none() && failure.file.is_none();
let shares_mutant = failure.test_id.as_deref().is_some_and(|id| {
tests.contains(id)
|| mutants.iter().any(|result| {
touches(result, |test| test == id)
|| failure.mutant_ids.contains(&result.location.mutant_id)
})
});
if !(shares_file || unscoped || shares_mutant) {
continue;
}
affected = true;
names.extend(failure.test_id.as_ref().or(failure.file.as_ref()).cloned());
}
affected.then(|| names.into_iter().collect())
}
}
fn touches(result: &MutantResult, test: impl Fn(&str) -> bool) -> bool {
result
.covering_tests
.iter()
.chain(&result.killing_tests)
.any(|id| test(id))
}