]>
Commit | Line | Data |
---|---|---|
1 | use crate::file_handler::{File, FileHandler}; | |
2 | use std::fs::read_dir; | |
3 | use std::path::PathBuf; | |
4 | ||
5 | pub fn find_files(directory_path: &PathBuf) -> Vec<File> { | |
6 | find_files_recursively(directory_path, directory_path) | |
7 | } | |
8 | ||
9 | fn 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 { path, file_type }); | |
24 | } | |
25 | } | |
26 | result | |
27 | } | |
28 | ||
29 | #[cfg(test)] | |
30 | mod tests { | |
31 | use std::collections::HashSet; | |
32 | use std::fs::create_dir_all; | |
33 | use std::path::PathBuf; | |
34 | ||
35 | use super::*; | |
36 | ||
37 | use crate::file_handler::File; | |
38 | use crate::file_handler::FileType; | |
39 | use test_utilities::*; | |
40 | ||
41 | fn get_paths(root_directory: &PathBuf, files: &[File]) -> HashSet<String> { | |
42 | files | |
43 | .iter() | |
44 | .map(|file| { | |
45 | file.path | |
46 | .strip_prefix(root_directory) | |
47 | .unwrap() | |
48 | .to_string_lossy() | |
49 | .to_string() | |
50 | }) | |
51 | .collect() | |
52 | } | |
53 | ||
54 | #[test] | |
55 | fn finds_all_files() { | |
56 | let test_dir = setup_test_dir(); | |
57 | create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory"); | |
58 | create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory"); | |
59 | create_test_file(&test_dir.join("test1.gmi"), ""); | |
60 | create_test_file(&test_dir.join("_layout.html"), ""); | |
61 | create_test_file(&test_dir.join("nested/nested.gmi"), ""); | |
62 | create_test_file(&test_dir.join("assets/style.css"), ""); | |
63 | create_test_file(&test_dir.join("image.png"), ""); | |
64 | ||
65 | let files = find_files(&test_dir); | |
66 | let paths = get_paths(&test_dir, &files); | |
67 | ||
68 | assert!(paths.contains("test1.gmi")); | |
69 | assert!(paths.contains("_layout.html")); | |
70 | assert!(paths.contains("nested/nested.gmi")); | |
71 | assert!(paths.contains("assets/style.css")); | |
72 | assert!(paths.contains("image.png")); | |
73 | } | |
74 | ||
75 | #[test] | |
76 | fn identifies_correct_file_types() { | |
77 | let test_dir = setup_test_dir(); | |
78 | create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory"); | |
79 | create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory"); | |
80 | create_test_file(&test_dir.join("_layout.html"), ""); | |
81 | create_test_file(&test_dir.join("notalayout.html"), ""); | |
82 | create_test_file(&test_dir.join("nested/nested.gmi"), ""); | |
83 | create_test_file(&test_dir.join("assets/style.css"), ""); | |
84 | create_test_file(&test_dir.join("image.png"), ""); | |
85 | let files = find_files(&test_dir); | |
86 | ||
87 | for file in files { | |
88 | let extension = file.path.extension().and_then(|e| e.to_str()); | |
89 | match extension { | |
90 | Some("gmi") => assert_eq!(file.file_type, FileType::Gemini), | |
91 | Some("html") => { | |
92 | if file.path.ends_with("_layout.html") { | |
93 | assert_eq!(file.file_type, FileType::Layout); | |
94 | } else { | |
95 | assert_eq!(file.file_type, FileType::File); | |
96 | } | |
97 | } | |
98 | _ => assert_eq!(file.file_type, FileType::File), | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | #[test] | |
104 | fn ignores_git_directory() { | |
105 | let test_dir = setup_test_dir(); | |
106 | create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory"); | |
107 | create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory"); | |
108 | create_dir_all(test_dir.join(".git")).expect("Could not create git test directory"); | |
109 | create_test_file(&test_dir.join("_layout.html"), ""); | |
110 | create_test_file(&test_dir.join("nested/nested.gmi"), ""); | |
111 | create_test_file(&test_dir.join("assets/style.css"), ""); | |
112 | create_test_file(&test_dir.join("image.png"), ""); | |
113 | create_test_file(&test_dir.join(".git/config"), ""); | |
114 | let files = find_files(&test_dir); | |
115 | ||
116 | let paths = get_paths(&test_dir, &files); | |
117 | ||
118 | assert!(!paths.iter().any(|p| p.starts_with(".git/"))); | |
119 | assert!(!paths.contains(".gitignore")); | |
120 | } | |
121 | ||
122 | #[test] | |
123 | fn returns_empty_for_empty_directory() { | |
124 | let test_dir = setup_test_dir(); | |
125 | let files = find_files(&test_dir); | |
126 | assert!(files.is_empty()); | |
127 | } | |
128 | } |