]>
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 | return 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 { | |
24 | path: path, | |
25 | file_type: file_type, | |
26 | }); | |
27 | } | |
28 | } | |
29 | return result; | |
30 | } | |
31 | ||
32 | ||
33 | #[cfg(test)] | |
34 | mod tests { | |
35 | use std::collections::HashSet; | |
36 | use std::path::PathBuf; | |
37 | use std::fs::create_dir_all; | |
38 | ||
39 | use super::*; | |
40 | ||
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> { | |
46 | files | |
47 | .iter() | |
48 | .map( |file| | |
49 | file.path | |
50 | .strip_prefix(root_directory) | |
51 | .unwrap() | |
52 | .to_string_lossy() | |
53 | .to_string() | |
54 | ) | |
55 | .collect() | |
56 | } | |
57 | ||
58 | #[test] | |
59 | fn finds_all_files() { | |
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); | |
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() { | |
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); | |
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() { | |
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); | |
127 | ||
128 | assert!(!paths.iter().any(|p| p.starts_with(".git/"))); | |
129 | assert!(!paths.contains(".gitignore")); | |
130 | } | |
131 | ||
132 | #[test] | |
133 | fn returns_empty_for_empty_directory() { | |
134 | let test_dir = setup_test_dir(); | |
135 | let files = find_files(&test_dir); | |
136 | assert!(files.is_empty()); | |
137 | } | |
138 | } |