use anyhow::Result;
use std::path::{Path, PathBuf};
use crate::coverage_parser::{self, map_coverage_to_features, parse_coverage_reports};
use crate::file_scanner::{list_files_recursive, list_files_recursive_with_changes};
use crate::models::Feature;
#[derive(Debug, Clone)]
pub struct ScanConfig<'a> {
pub skip_changes: bool,
pub should_add_coverage: bool,
pub coverage_dir_override: Option<&'a Path>,
pub current_dir: &'a Path,
pub project_dir: Option<&'a Path>,
pub ignore_paths: Vec<PathBuf>,
}
impl<'a> ScanConfig<'a> {
pub fn new(current_dir: &'a Path) -> Self {
Self {
skip_changes: false,
should_add_coverage: false,
coverage_dir_override: None,
current_dir,
project_dir: None,
ignore_paths: Vec::new(),
}
}
pub fn skip_changes(mut self, skip: bool) -> Self {
self.skip_changes = skip;
self
}
pub fn with_coverage(mut self, should_add: bool) -> Self {
self.should_add_coverage = should_add;
self
}
pub fn coverage_dir(mut self, dir: &'a Path) -> Self {
self.coverage_dir_override = Some(dir);
self
}
pub fn project_dir(mut self, dir: &'a Path) -> Self {
self.project_dir = Some(dir);
self
}
pub fn ignore_paths(mut self, paths: Vec<PathBuf>) -> Self {
self.ignore_paths = paths;
self
}
}
pub fn scan_features(base_path: &Path, config: ScanConfig) -> Result<Vec<Feature>> {
let mut features = if config.skip_changes {
list_files_recursive(base_path, &config.ignore_paths)?
} else {
list_files_recursive_with_changes(base_path, &config.ignore_paths)?
};
if config.should_add_coverage {
add_coverage_to_features(
&mut features,
base_path,
config.coverage_dir_override,
config.current_dir,
config.project_dir,
);
}
Ok(features)
}
fn add_coverage_to_features(
features: &mut [Feature],
base_path: &Path,
coverage_dir_override: Option<&Path>,
current_dir: &Path,
project_dir: Option<&Path>,
) {
let coverage_dirs = if let Some(override_dir) = coverage_dir_override {
vec![override_dir.to_path_buf()]
} else {
let mut dirs = vec![
base_path.join(".coverage"),
base_path.join("coverage"),
current_dir.join(".coverage"),
current_dir.join("coverage"),
];
if let Some(proj_dir) = project_dir {
let proj_coverage = proj_dir.join(".coverage");
let proj_coverage_plain = proj_dir.join("coverage");
if !dirs.contains(&proj_coverage) {
dirs.push(proj_coverage);
}
if !dirs.contains(&proj_coverage_plain) {
dirs.push(proj_coverage_plain);
}
}
dirs
};
for coverage_dir in &coverage_dirs {
if let Ok(coverage_map) = parse_coverage_reports(coverage_dir, base_path)
&& !coverage_map.is_empty()
{
let feature_coverage = map_coverage_to_features(features, coverage_map, base_path);
update_features_with_coverage(features, &feature_coverage);
break; }
}
}
fn update_features_with_coverage(
features: &mut [Feature],
feature_coverage: &std::collections::HashMap<String, coverage_parser::CoverageStats>,
) {
for feature in features {
if let Some(coverage) = feature_coverage.get(&feature.path) {
if let Some(stats) = &mut feature.stats {
stats.coverage = Some(coverage.clone());
} else {
feature.stats = Some(crate::models::Stats {
files_count: None,
lines_count: None,
todos_count: None,
commits: std::collections::BTreeMap::new(),
coverage: Some(coverage.clone()),
});
}
}
update_features_with_coverage(&mut feature.features, feature_coverage);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_scan_config_builder() {
let current_dir = PathBuf::from("/tmp");
let project_dir = PathBuf::from("/project");
let config = ScanConfig::new(¤t_dir)
.skip_changes(true)
.with_coverage(true)
.project_dir(&project_dir);
assert!(config.skip_changes);
assert!(config.should_add_coverage);
assert!(config.project_dir.is_some());
}
#[test]
fn test_scan_config_defaults() {
let current_dir = PathBuf::from("/tmp");
let config = ScanConfig::new(¤t_dir);
assert!(!config.skip_changes);
assert!(!config.should_add_coverage);
assert!(config.coverage_dir_override.is_none());
assert!(config.project_dir.is_none());
}
#[test]
fn test_scan_features_basic() {
let test_path = PathBuf::from("../../examples/tests_skip_changes/src");
if !test_path.exists() {
println!("Skipping test - test path does not exist");
return;
}
let current_dir = std::env::current_dir().unwrap();
let config = ScanConfig::new(¤t_dir).skip_changes(true);
let result = scan_features(&test_path, config);
assert!(result.is_ok(), "Failed to scan features");
let features = result.unwrap();
assert!(!features.is_empty(), "Should find at least one feature");
}
#[test]
fn test_scan_features_with_changes() {
let test_path = PathBuf::from("../../examples/tests-with-changes/src");
if !test_path.exists() {
println!("Skipping test - test path does not exist");
return;
}
let current_dir = std::env::current_dir().unwrap();
let config = ScanConfig::new(¤t_dir).skip_changes(false);
let result = scan_features(&test_path, config);
assert!(result.is_ok(), "Failed to scan features with changes");
let features = result.unwrap();
assert!(!features.is_empty(), "Should find at least one feature");
let has_changes = features.iter().any(|f| !f.changes.is_empty());
assert!(has_changes, "At least one feature should have git history");
}
}