X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/260e8ec69b8e08b9fd105bf688e7a3a9fafecd61..1c5797fadeea6be505c01f13508203ba234cbdfa:/src/file_finder.rs?ds=sidebyside diff --git a/src/file_finder.rs b/src/file_finder.rs index a94aa8a..43eee33 100644 --- a/src/file_finder.rs +++ b/src/file_finder.rs @@ -32,26 +32,44 @@ fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec< #[cfg(test)] mod tests { - use super::*; - use crate::file_handler::FileType; use std::collections::HashSet; + use std::path::PathBuf; use std::fs::create_dir_all; - fn fixtures_dir() -> PathBuf { - PathBuf::from("tests/fixtures") - } + use super::*; - fn get_paths(files: &Vec) -> HashSet { + use crate::file_handler::FileType; + use crate::file_handler::File; + use test_utilities::*; + + fn get_paths(root_directory: &PathBuf, files: &Vec) -> HashSet { files .iter() - .map(|f| f.path.strip_prefix(&fixtures_dir()).unwrap().to_string_lossy().to_string()) + .map( |file| + file.path + .strip_prefix(root_directory) + .unwrap() + .to_string_lossy() + .to_string() + ) .collect() } #[test] fn finds_all_files() { - let files = find_files(&fixtures_dir()); - let paths = get_paths(&files); + let test_dir = setup_test_dir(); + create_dir_all(&test_dir.join("nested")) + .expect("Could not create nested test directory"); + create_dir_all(&test_dir.join("assets")) + .expect("Could not create assets test directory"); + create_test_file(&test_dir.join("test1.gmi"), ""); + create_test_file(&test_dir.join("_layout.html"), ""); + create_test_file(&test_dir.join("nested/nested.gmi"), ""); + create_test_file(&test_dir.join("assets/style.css"), ""); + create_test_file(&test_dir.join("image.png"), ""); + + let files = find_files(&test_dir); + let paths = get_paths(&test_dir, &files); assert!(paths.contains("test1.gmi")); assert!(paths.contains("_layout.html")); @@ -62,7 +80,16 @@ mod tests { #[test] fn identifies_correct_file_types() { - let files = find_files(&fixtures_dir()); + let test_dir = setup_test_dir(); + create_dir_all(&test_dir.join("nested")) + .expect("Could not create nested test directory"); + create_dir_all(&test_dir.join("assets")) + .expect("Could not create assets test directory"); + create_test_file(&test_dir.join("_layout.html"), ""); + create_test_file(&test_dir.join("nested/nested.gmi"), ""); + create_test_file(&test_dir.join("assets/style.css"), ""); + create_test_file(&test_dir.join("image.png"), ""); + let files = find_files(&test_dir); for file in files { let extension = file.path.extension().and_then(|e| e.to_str()); @@ -82,35 +109,30 @@ mod tests { #[test] fn ignores_git_directory() { - let files = find_files(&fixtures_dir()); - let paths = get_paths(&files); + let test_dir = setup_test_dir(); + create_dir_all(&test_dir.join("nested")) + .expect("Could not create nested test directory"); + create_dir_all(&test_dir.join("assets")) + .expect("Could not create assets test directory"); + create_dir_all(&test_dir.join(".git")) + .expect("Could not create git test directory"); + create_test_file(&test_dir.join("_layout.html"), ""); + create_test_file(&test_dir.join("nested/nested.gmi"), ""); + create_test_file(&test_dir.join("assets/style.css"), ""); + create_test_file(&test_dir.join("image.png"), ""); + create_test_file(&test_dir.join(".git/config"), ""); + let files = find_files(&test_dir); + + let paths = get_paths(&test_dir, &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); + let test_dir = setup_test_dir(); + let files = find_files(&test_dir); assert!(files.is_empty()); } }