#[cfg(test)]
mod tests {
use super::super::*;
use std::fs::{self, File};
use std::io::Write;
use tempfile::TempDir;
#[test]
fn test_collect_files_basic() -> Result<()> {
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path();
println!("Temporary directory created at: {}", temp_path.display());
let file_paths = ["file1.txt", "file2.rs"];
for &filepath in &file_paths {
let file_path = temp_path.join(filepath);
println!("Creating test file: {}", file_path.display());
let mut file = File::create(file_path)?;
file.write_all(b"Test content\n")?;
}
println!("Files created in test directory:");
for entry in fs::read_dir(temp_path)? {
let entry = entry?;
println!(" {}", entry.path().display());
}
let options = SearchOptions::default(); let files = collect_files(temp_path, &options)?;
println!("Files found by collect_files: {}", files.len());
for file in &files {
println!(" {}", file.display());
}
assert_eq!(files.len(), 2, "Should find both test files");
Ok(())
}
#[test]
fn test_collect_files_nested() -> Result<()> {
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path();
println!("Temporary directory created at: {}", temp_path.display());
let nested_dir = temp_path.join("nested");
fs::create_dir(&nested_dir)?;
let deep_dir = nested_dir.join("deep");
fs::create_dir(&deep_dir)?;
println!("Created directory structure:");
println!(" {}", nested_dir.display());
println!(" {}", deep_dir.display());
let file_paths = [
"file1.txt",
"file2.rs",
"nested/file3.txt",
"nested/file4.rs",
"nested/deep/file5.json",
];
for &filepath in &file_paths {
let file_path = temp_path.join(filepath.replace("/", std::path::MAIN_SEPARATOR_STR));
println!("Creating test file: {}", file_path.display());
let mut file = File::create(&file_path)?;
file.write_all(b"Test content\n")?;
}
println!("\nFiles created in test directory:");
list_dir_recursive(temp_path, 0)?;
let options = SearchOptions::default();
let files = collect_files(temp_path, &options)?;
println!("\nFiles found by collect_files: {}", files.len());
for file in &files {
println!(" {}", file.display());
}
assert_eq!(files.len(), 5, "Should find all 5 test files");
let mut options_txt = SearchOptions::default();
options_txt.include_glob = Some(vec!["**/*.txt".to_string()]);
let files_txt = collect_files(temp_path, &options_txt)?;
println!("\nTXT files found by collect_files: {}", files_txt.len());
for file in &files_txt {
println!(" {}", file.display());
}
assert_eq!(files_txt.len(), 2, "Should find 2 .txt files");
let mut options_nested = SearchOptions::default();
options_nested.include_glob = Some(vec!["nested/**".to_string()]);
println!("\nTesting nested glob pattern: 'nested/**'");
for &filepath in &file_paths {
let path = PathBuf::from(filepath);
let matches = crate::traverse::common::path_matches_any_glob(
&path,
&options_nested.include_glob.as_ref().unwrap(),
options_nested.case_sensitive,
)?;
println!(" {} matches? {}", path.display(), matches);
}
println!("\nTesting with full paths:");
for entry in fs::read_dir(temp_path)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
let rel_path = path.strip_prefix(temp_path).unwrap_or(&path);
let matches = crate::traverse::common::path_matches_any_glob(
rel_path,
&options_nested.include_glob.as_ref().unwrap(),
options_nested.case_sensitive,
)?;
println!(
" {} (rel: {}) matches? {}",
path.display(),
rel_path.display(),
matches
);
}
}
let files_nested = collect_files(temp_path, &options_nested)?;
println!(
"\nNested files found by collect_files: {}",
files_nested.len()
);
for file in &files_nested {
println!(" {}", file.display());
}
println!("\nTrying with modified glob pattern 'nested*/**':");
let mut options_nested_modified = SearchOptions::default();
options_nested_modified.include_glob = Some(vec!["**/nested/**".to_string()]);
let files_nested_modified = collect_files(temp_path, &options_nested_modified)?;
println!(
"Files found with modified pattern: {}",
files_nested_modified.len()
);
for file in &files_nested_modified {
println!(" {}", file.display());
}
assert_eq!(
files_nested_modified.len(),
3,
"Should find 3 files in nested directories with modified pattern"
);
Ok(())
}
fn list_dir_recursive(dir: &Path, level: usize) -> Result<()> {
let indent = " ".repeat(level);
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
println!("{}{}", indent, path.display());
if path.is_dir() {
list_dir_recursive(&path, level + 1)?;
}
}
Ok(())
}
}