]> git.r.bdr.sh - rbdr/page/blobdiff - src/file_finder.rs
Update main and file_finder tests
[rbdr/page] / src / file_finder.rs
index bcd368dbf94089280c36010ac35f51c6b4618aca..43eee33ca0233fbc2a6d60eaacb4c1cc632d7115 100644 (file)
@@ -28,3 +28,111 @@ fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec<
     }
     return result;
 }
+
+
+#[cfg(test)]
+mod tests {
+    use std::collections::HashSet;
+    use std::path::PathBuf;
+    use std::fs::create_dir_all;
+
+    use super::*;
+
+    use crate::file_handler::FileType;
+    use crate::file_handler::File;
+    use test_utilities::*;
+
+    fn get_paths(root_directory: &PathBuf, files: &Vec<File>) -> HashSet<String> {
+        files
+            .iter()
+            .map( |file|
+                file.path
+                    .strip_prefix(root_directory)
+                    .unwrap()
+                    .to_string_lossy()
+                    .to_string()
+            )
+            .collect()
+    }
+
+    #[test]
+    fn finds_all_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"));
+        assert!(paths.contains("nested/nested.gmi"));
+        assert!(paths.contains("assets/style.css"));
+        assert!(paths.contains("image.png"));
+    }
+
+    #[test]
+    fn identifies_correct_file_types() {
+        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());
+            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 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);
+
+        assert!(!paths.iter().any(|p| p.starts_with(".git/")));
+        assert!(!paths.contains(".gitignore"));
+    }
+
+    #[test]
+    fn returns_empty_for_empty_directory() {
+        let test_dir = setup_test_dir();
+        let files = find_files(&test_dir);
+        assert!(files.is_empty());
+    }
+}