use std::path::PathBuf;
use fallow_config::ResolvedConfig;
use fixedbitset::FixedBitSet;
use globset::GlobSet;
use rustc_hash::FxHashSet;
use crate::discover::{FileId, production_exclude_globs};
use crate::graph::{ModuleGraph, ModuleNode};
pub struct TestSideMatcher {
root: PathBuf,
excludes: Option<GlobSet>,
support_reachable: Option<FixedBitSet>,
}
impl TestSideMatcher {
#[must_use]
pub fn new(config: &ResolvedConfig) -> Self {
Self {
root: config.root.clone(),
excludes: production_exclude_globs(config),
support_reachable: None,
}
}
#[must_use]
pub fn with_support_reach(config: &ResolvedConfig, graph: &ModuleGraph) -> Self {
Self {
support_reachable: Some(graph.support_reachable()),
..Self::new(config)
}
}
#[must_use]
pub fn is_test_side(&self, node: &ModuleNode) -> bool {
if !node.is_reachable() {
return false;
}
let support_reached = self
.support_reachable
.as_ref()
.is_some_and(|reach| reach.contains(node.file_id.0 as usize));
if !node.is_runtime_reachable() && !support_reached {
return true;
}
let relative = node.path.strip_prefix(&self.root).unwrap_or(&node.path);
self.excludes
.as_ref()
.is_some_and(|excludes| excludes.is_match(relative))
}
}
pub(crate) fn test_side_files(graph: &ModuleGraph, config: &ResolvedConfig) -> FxHashSet<FileId> {
let matcher = TestSideMatcher::with_support_reach(config, graph);
graph
.modules()
.iter()
.filter(|module| matcher.is_test_side(module))
.map(|module| module.file_id)
.collect()
}