diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-03 20:59:02 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-03 20:59:02 +0100 |
| commit | 260e8ec69b8e08b9fd105bf688e7a3a9fafecd61 (patch) | |
| tree | 3cbbccc74357dc8ddc37fb507005f4866df3b3fb /src/file_finder.rs | |
| parent | 45fbf824248215a737d5d0b52920b43b68941ad3 (diff) | |
Add first tests
Diffstat (limited to 'src/file_finder.rs')
| -rw-r--r-- | src/file_finder.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/file_finder.rs b/src/file_finder.rs index bcd368d..a94aa8a 100644 --- a/src/file_finder.rs +++ b/src/file_finder.rs @@ -28,3 +28,89 @@ fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec< } return result; } + + +#[cfg(test)] +mod tests { + use super::*; + use crate::file_handler::FileType; + use std::collections::HashSet; + use std::fs::create_dir_all; + + fn fixtures_dir() -> PathBuf { + PathBuf::from("tests/fixtures") + } + + fn get_paths(files: &Vec<File>) -> HashSet<String> { + files + .iter() + .map(|f| f.path.strip_prefix(&fixtures_dir()).unwrap().to_string_lossy().to_string()) + .collect() + } + + #[test] + fn finds_all_files() { + let files = find_files(&fixtures_dir()); + let paths = get_paths(&files); + + assert!(paths.contains("test1.gmi")); + assert!(paths.contains("_layout.html")); + assert!(paths.contains("nested/nested.gmi")); + assert!(paths.contains("assets/style.css")); + assert!(paths.contains("image.png")); + } + + #[test] + fn identifies_correct_file_types() { + let files = find_files(&fixtures_dir()); + + for file in files { + let extension = file.path.extension().and_then(|e| e.to_str()); + match extension { + Some("gmi") => assert_eq!(file.file_type, FileType::Gemini), + Some("html") => { + if file.path.ends_with("_layout.html") { + assert_eq!(file.file_type, FileType::Layout) + } else { + assert_eq!(file.file_type, FileType::File) + } + }, + _ => assert_eq!(file.file_type, FileType::File), + } + } + } + + #[test] + fn ignores_git_directory() { + let files = find_files(&fixtures_dir()); + let paths = get_paths(&files); + + // These files should exist in the fixtures but not be included + assert!(!paths.iter().any(|p| p.starts_with(".git/"))); + assert!(!paths.contains(".gitignore")); + } + + #[test] + fn handles_nested_directories() { + let files = find_files(&fixtures_dir()); + let paths = get_paths(&files); + + // Check that files in nested directories are found + assert!(paths.contains("nested/nested.gmi")); + assert!(paths.contains("assets/style.css")); + + // Verify directory structure is preserved in paths + let nested_files: Vec<_> = files.iter() + .filter(|f| f.path.starts_with(fixtures_dir().join("nested"))) + .collect(); + assert!(!nested_files.is_empty()); + } + + #[test] + fn returns_empty_for_empty_directory() { + let empty_dir = fixtures_dir().join("empty"); + let _ = create_dir_all(&empty_dir); + let files = find_files(&empty_dir); + assert!(files.is_empty()); + } +} |