]> git.r.bdr.sh - rbdr/page/blobdiff - src/file_finder.rs
Use String::new() for empty strings
[rbdr/page] / src / file_finder.rs
index a94aa8aea59a4763cd927fcf41c72c18f354adba..a834bd99e33f6f90d178453028a8a5bb9b30f3e8 100644 (file)
@@ -3,55 +3,67 @@ use std::fs::read_dir;
 use std::path::PathBuf;
 
 pub fn find_files(directory_path: &PathBuf) -> Vec<File> {
-    return find_files_recursively(directory_path, directory_path);
+    find_files_recursively(directory_path, directory_path)
 }
 
 fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec<File> {
     let mut result: Vec<File> = vec![];
     let file_handler = FileHandler::default();
-    let entries = read_dir(&directory_path).unwrap();
+    let entries = read_dir(directory_path).unwrap();
     for entry in entries {
         let path = entry.unwrap().path();
-        let relative_path = path.strip_prefix(&root_path).unwrap();
+        let relative_path = path.strip_prefix(root_path).unwrap();
         if relative_path.starts_with(".git") || relative_path.starts_with(".gitignore") {
             continue;
         }
         if path.is_dir() {
-            result.append(&mut find_files_recursively(&root_path, &path))
+            result.append(&mut find_files_recursively(root_path, &path));
         } else {
             let file_type = file_handler.identify(&path);
-            result.push(File {
-                path: path,
-                file_type: file_type,
-            });
+            result.push(File { path, file_type });
         }
     }
-    return result;
+    result
 }
 
-
 #[cfg(test)]
 mod tests {
-    use super::*;
-    use crate::file_handler::FileType;
     use std::collections::HashSet;
     use std::fs::create_dir_all;
+    use std::path::PathBuf;
 
-    fn fixtures_dir() -> PathBuf {
-        PathBuf::from("tests/fixtures")
-    }
+    use super::*;
+
+    use crate::file_handler::File;
+    use crate::file_handler::FileType;
+    use test_utilities::*;
 
-    fn get_paths(files: &Vec<File>) -> HashSet<String> {
+    fn get_paths(root_directory: &PathBuf, files: &[File]) -> HashSet<String> {
         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 +74,14 @@ 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());
@@ -70,11 +89,11 @@ mod tests {
                 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)
+                        assert_eq!(file.file_type, FileType::Layout);
                     } else {
-                        assert_eq!(file.file_type, FileType::File)
+                        assert_eq!(file.file_type, FileType::File);
                     }
-                },
+                }
                 _ => assert_eq!(file.file_type, FileType::File),
             }
         }
@@ -82,35 +101,27 @@ 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());
     }
 }