]> git.r.bdr.sh - rbdr/page/blame_incremental - src/file_finder.rs
Add first tests
[rbdr/page] / src / file_finder.rs
... / ...
CommitLineData
1use crate::file_handler::{File, FileHandler};
2use std::fs::read_dir;
3use std::path::PathBuf;
4
5pub fn find_files(directory_path: &PathBuf) -> Vec<File> {
6 return find_files_recursively(directory_path, directory_path);
7}
8
9fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec<File> {
10 let mut result: Vec<File> = vec![];
11 let file_handler = FileHandler::default();
12 let entries = read_dir(&directory_path).unwrap();
13 for entry in entries {
14 let path = entry.unwrap().path();
15 let relative_path = path.strip_prefix(&root_path).unwrap();
16 if relative_path.starts_with(".git") || relative_path.starts_with(".gitignore") {
17 continue;
18 }
19 if path.is_dir() {
20 result.append(&mut find_files_recursively(&root_path, &path))
21 } else {
22 let file_type = file_handler.identify(&path);
23 result.push(File {
24 path: path,
25 file_type: file_type,
26 });
27 }
28 }
29 return result;
30}
31
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36 use crate::file_handler::FileType;
37 use std::collections::HashSet;
38 use std::fs::create_dir_all;
39
40 fn fixtures_dir() -> PathBuf {
41 PathBuf::from("tests/fixtures")
42 }
43
44 fn get_paths(files: &Vec<File>) -> HashSet<String> {
45 files
46 .iter()
47 .map(|f| f.path.strip_prefix(&fixtures_dir()).unwrap().to_string_lossy().to_string())
48 .collect()
49 }
50
51 #[test]
52 fn finds_all_files() {
53 let files = find_files(&fixtures_dir());
54 let paths = get_paths(&files);
55
56 assert!(paths.contains("test1.gmi"));
57 assert!(paths.contains("_layout.html"));
58 assert!(paths.contains("nested/nested.gmi"));
59 assert!(paths.contains("assets/style.css"));
60 assert!(paths.contains("image.png"));
61 }
62
63 #[test]
64 fn identifies_correct_file_types() {
65 let files = find_files(&fixtures_dir());
66
67 for file in files {
68 let extension = file.path.extension().and_then(|e| e.to_str());
69 match extension {
70 Some("gmi") => assert_eq!(file.file_type, FileType::Gemini),
71 Some("html") => {
72 if file.path.ends_with("_layout.html") {
73 assert_eq!(file.file_type, FileType::Layout)
74 } else {
75 assert_eq!(file.file_type, FileType::File)
76 }
77 },
78 _ => assert_eq!(file.file_type, FileType::File),
79 }
80 }
81 }
82
83 #[test]
84 fn ignores_git_directory() {
85 let files = find_files(&fixtures_dir());
86 let paths = get_paths(&files);
87
88 // These files should exist in the fixtures but not be included
89 assert!(!paths.iter().any(|p| p.starts_with(".git/")));
90 assert!(!paths.contains(".gitignore"));
91 }
92
93 #[test]
94 fn handles_nested_directories() {
95 let files = find_files(&fixtures_dir());
96 let paths = get_paths(&files);
97
98 // Check that files in nested directories are found
99 assert!(paths.contains("nested/nested.gmi"));
100 assert!(paths.contains("assets/style.css"));
101
102 // Verify directory structure is preserved in paths
103 let nested_files: Vec<_> = files.iter()
104 .filter(|f| f.path.starts_with(fixtures_dir().join("nested")))
105 .collect();
106 assert!(!nested_files.is_empty());
107 }
108
109 #[test]
110 fn returns_empty_for_empty_directory() {
111 let empty_dir = fixtures_dir().join("empty");
112 let _ = create_dir_all(&empty_dir);
113 let files = find_files(&empty_dir);
114 assert!(files.is_empty());
115 }
116}