use crate::finding::unit_test_finder::UnitTestFinder;
use crate::reporting::offence::Offence;
use crate::rule::Rule;
use crate::source_file::SourceFile;
pub struct TestFreeSourceRule;
impl TestFreeSourceRule {
pub const ROOT: &'static str = "tests/";
pub fn new() -> Self {
Self
}
fn applies_to(file: &SourceFile) -> bool {
!file.relative_path().starts_with(Self::ROOT)
}
}
impl Default for TestFreeSourceRule {
fn default() -> Self {
Self::new()
}
}
impl Rule for TestFreeSourceRule {
fn name(&self) -> &'static str {
"test-free-source"
}
fn check(&self, file: &SourceFile) -> Vec<Offence> {
if !Self::applies_to(file) {
return Vec::new();
}
UnitTestFinder::sites(file)
.unwrap_or_default()
.into_iter()
.map(|site| {
Offence::new(
file.relative_path(),
site.line,
self.name(),
format!("{} does not belong in the source tree", site.label),
site.correction.clone(),
)
.with_subject(&site.label)
})
.collect()
}
fn check_workspace(&self, _files: &[SourceFile]) -> Vec<Offence> {
Vec::new()
}
fn is_configured(&self) -> bool {
true
}
}