use anyhow::Result;
use lumin::traverse::{TraverseOptions, traverse_directory};
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
#[cfg(test)]
mod traverse_prefix_tests {
use super::*;
#[test]
fn test_traverse_root_prefix_matching() -> Result<()> {
let directory = Path::new("tests/fixtures");
let test_files = [
"tests/fixtures/prefix_test1.txt",
"tests/fixtures/prefix_test2.md",
"tests/fixtures/not_matching.txt",
"tests/fixtures/nested/prefix_test3.txt",
];
for file_path in &test_files {
let path = PathBuf::from(file_path);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let mut file = File::create(path)?;
writeln!(file, "Test content for {}", file_path)?;
}
let _cleanup = defer::defer(|| {
for file_path in &test_files {
let _ = std::fs::remove_file(file_path);
}
});
let options = TraverseOptions {
pattern: Some("prefix_*".to_string()),
..TraverseOptions::default()
};
let results = traverse_directory(directory, &options)?;
assert_eq!(
results.len(),
2,
"Should match exactly 2 files at the root level"
);
assert!(
results
.iter()
.any(|r| r.file_path.to_string_lossy().ends_with("prefix_test1.txt")),
"Should find prefix_test1.txt"
);
assert!(
results
.iter()
.any(|r| r.file_path.to_string_lossy().ends_with("prefix_test2.md")),
"Should find prefix_test2.md"
);
assert!(
!results.iter().any(|r| r
.file_path
.to_string_lossy()
.contains("nested/prefix_test3.txt")),
"Should NOT find nested/prefix_test3.txt"
);
let options = TraverseOptions {
pattern: Some("**/prefix_*".to_string()),
..TraverseOptions::default()
};
let results = traverse_directory(directory, &options)?;
assert_eq!(results.len(), 3, "Should match all 3 prefix_* files");
assert!(
results.iter().any(|r| r
.file_path
.to_string_lossy()
.ends_with("nested/prefix_test3.txt")),
"Should find nested/prefix_test3.txt with **/ pattern"
);
Ok(())
}
}