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