1use anyhow::Result;
8use std::path::{Path, PathBuf};
9
10use crate::coverage_parser::{self, map_coverage_to_features, parse_coverage_reports};
11use crate::file_scanner::{
12 list_features_ownership_only, list_files_recursive, list_files_recursive_with_changes,
13};
14use crate::models::Feature;
15
16#[derive(Debug, Clone)]
18pub struct ScanConfig<'a> {
19 pub skip_changes: bool,
21
22 pub should_add_coverage: bool,
24
25 pub coverage_dir_override: Option<&'a Path>,
28
29 pub current_dir: &'a Path,
31
32 pub project_dir: Option<&'a Path>,
34
35 pub ignore_paths: Vec<PathBuf>,
37
38 pub ownership_only: bool,
41
42 pub include_skipped_dirs: bool,
44}
45
46impl<'a> ScanConfig<'a> {
47 pub fn new(current_dir: &'a Path) -> Self {
49 Self {
50 skip_changes: false,
51 should_add_coverage: false,
52 coverage_dir_override: None,
53 current_dir,
54 project_dir: None,
55 ignore_paths: Vec::new(),
56 ownership_only: false,
57 include_skipped_dirs: false,
58 }
59 }
60
61 pub fn skip_changes(mut self, skip: bool) -> Self {
63 self.skip_changes = skip;
64 self
65 }
66
67 pub fn with_coverage(mut self, should_add: bool) -> Self {
69 self.should_add_coverage = should_add;
70 self
71 }
72
73 pub fn coverage_dir(mut self, dir: &'a Path) -> Self {
75 self.coverage_dir_override = Some(dir);
76 self
77 }
78
79 pub fn project_dir(mut self, dir: &'a Path) -> Self {
81 self.project_dir = Some(dir);
82 self
83 }
84
85 pub fn ignore_paths(mut self, paths: Vec<PathBuf>) -> Self {
87 self.ignore_paths = paths;
88 self
89 }
90
91 pub fn ownership_only(mut self, ownership_only: bool) -> Self {
93 self.ownership_only = ownership_only;
94 self
95 }
96
97 pub fn include_skipped_dirs(mut self, include_skipped_dirs: bool) -> Self {
99 self.include_skipped_dirs = include_skipped_dirs;
100 self
101 }
102}
103
104pub fn scan_features(base_path: &Path, config: ScanConfig) -> Result<Vec<Feature>> {
130 if config.ownership_only {
131 return list_features_ownership_only(
132 base_path,
133 &config.ignore_paths,
134 config.include_skipped_dirs,
135 );
136 }
137
138 let mut features = if config.skip_changes {
140 list_files_recursive(base_path, &config.ignore_paths, config.include_skipped_dirs)?
141 } else {
142 list_files_recursive_with_changes(
143 base_path,
144 &config.ignore_paths,
145 config.include_skipped_dirs,
146 )?
147 };
148
149 if config.should_add_coverage {
151 add_coverage_to_features(
152 &mut features,
153 base_path,
154 config.coverage_dir_override,
155 config.current_dir,
156 config.project_dir,
157 );
158 }
159
160 Ok(features)
161}
162
163fn add_coverage_to_features(
168 features: &mut [Feature],
169 base_path: &Path,
170 coverage_dir_override: Option<&Path>,
171 current_dir: &Path,
172 project_dir: Option<&Path>,
173) {
174 let coverage_dirs = if let Some(override_dir) = coverage_dir_override {
175 vec![override_dir.to_path_buf()]
177 } else {
178 let mut dirs = vec![
183 base_path.join(".coverage"),
184 base_path.join("coverage"),
185 current_dir.join(".coverage"),
186 current_dir.join("coverage"),
187 ];
188
189 if let Some(proj_dir) = project_dir {
191 let proj_coverage = proj_dir.join(".coverage");
192 let proj_coverage_plain = proj_dir.join("coverage");
193
194 if !dirs.contains(&proj_coverage) {
196 dirs.push(proj_coverage);
197 }
198 if !dirs.contains(&proj_coverage_plain) {
199 dirs.push(proj_coverage_plain);
200 }
201 }
202
203 dirs
204 };
205
206 for coverage_dir in &coverage_dirs {
207 if let Ok(coverage_map) = parse_coverage_reports(coverage_dir, base_path)
209 && !coverage_map.is_empty()
210 {
211 let feature_coverage = map_coverage_to_features(features, coverage_map, base_path);
213 update_features_with_coverage(features, &feature_coverage);
214 break; }
216 }
217}
218
219fn update_features_with_coverage(
221 features: &mut [Feature],
222 feature_coverage: &std::collections::HashMap<String, coverage_parser::CoverageStats>,
223) {
224 for feature in features {
225 if let Some(coverage) = feature_coverage.get(&feature.path) {
226 if let Some(stats) = &mut feature.stats {
228 stats.coverage = Some(coverage.clone());
229 } else {
230 feature.stats = Some(crate::models::Stats {
231 files_count: None,
232 lines_count: None,
233 todos_count: None,
234 commits: std::collections::BTreeMap::new(),
235 coverage: Some(coverage.clone()),
236 });
237 }
238 }
239
240 update_features_with_coverage(&mut feature.features, feature_coverage);
242 }
243}
244
245#[cfg(test)]
246mod tests {
247 use super::*;
248 use std::path::PathBuf;
249
250 #[test]
251 fn test_scan_config_builder() {
252 let current_dir = PathBuf::from("/tmp");
253 let project_dir = PathBuf::from("/project");
254
255 let config = ScanConfig::new(¤t_dir)
256 .skip_changes(true)
257 .with_coverage(true)
258 .project_dir(&project_dir);
259
260 assert!(config.skip_changes);
261 assert!(config.should_add_coverage);
262 assert!(config.project_dir.is_some());
263 }
264
265 #[test]
266 fn test_scan_config_defaults() {
267 let current_dir = PathBuf::from("/tmp");
268 let config = ScanConfig::new(¤t_dir);
269
270 assert!(!config.skip_changes);
271 assert!(!config.should_add_coverage);
272 assert!(config.coverage_dir_override.is_none());
273 assert!(config.project_dir.is_none());
274 assert!(!config.ownership_only);
275 assert!(!config.include_skipped_dirs);
276 }
277
278 #[test]
279 fn test_scan_features_basic() {
280 let test_path = PathBuf::from("../../examples/tests_skip_changes/src");
281
282 if !test_path.exists() {
283 println!("Skipping test - test path does not exist");
284 return;
285 }
286
287 let current_dir = std::env::current_dir().unwrap();
288 let config = ScanConfig::new(¤t_dir).skip_changes(true);
289
290 let result = scan_features(&test_path, config);
291 assert!(result.is_ok(), "Failed to scan features");
292
293 let features = result.unwrap();
294 assert!(!features.is_empty(), "Should find at least one feature");
295 }
296
297 #[test]
298 fn test_scan_features_with_changes() {
299 let test_path = PathBuf::from("../../examples/tests-with-changes/src");
300
301 if !test_path.exists() {
302 println!("Skipping test - test path does not exist");
303 return;
304 }
305
306 let current_dir = std::env::current_dir().unwrap();
307 let config = ScanConfig::new(¤t_dir).skip_changes(false);
308
309 let result = scan_features(&test_path, config);
310 assert!(result.is_ok(), "Failed to scan features with changes");
311
312 let features = result.unwrap();
313 assert!(!features.is_empty(), "Should find at least one feature");
314
315 let has_changes = features.iter().any(|f| !f.changes.is_empty());
317 assert!(has_changes, "At least one feature should have git history");
318 }
319}