]>
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 | 5 | pub fn find_files(directory_path: &PathBuf) -> Vec<File> { |
5732d284 | 6 | 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(); | |
5732d284 | 12 | let entries = read_dir(directory_path).unwrap(); |
1e2d00b6 RBR |
13 | for entry in entries { |
14 | let path = entry.unwrap().path(); | |
5732d284 | 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() { | |
5732d284 | 20 | result.append(&mut find_files_recursively(root_path, &path)) |
1e2d00b6 RBR |
21 | } else { |
22 | let file_type = file_handler.identify(&path); | |
5732d284 | 23 | result.push(File { path, file_type }); |
1e2d00b6 RBR |
24 | } |
25 | } | |
5732d284 | 26 | result |
1e2d00b6 | 27 | } |
260e8ec6 | 28 | |
260e8ec6 RBR |
29 | #[cfg(test)] |
30 | mod tests { | |
260e8ec6 RBR |
31 | use std::collections::HashSet; |
32 | use std::fs::create_dir_all; | |
5732d284 | 33 | use std::path::PathBuf; |
260e8ec6 | 34 | |
1c5797fa | 35 | use super::*; |
260e8ec6 | 36 | |
1c5797fa | 37 | use crate::file_handler::File; |
5732d284 | 38 | use crate::file_handler::FileType; |
1c5797fa RBR |
39 | use test_utilities::*; |
40 | ||
41 | fn get_paths(root_directory: &PathBuf, files: &Vec<File>) -> HashSet<String> { | |
260e8ec6 RBR |
42 | files |
43 | .iter() | |
5732d284 | 44 | .map(|file| { |
1c5797fa RBR |
45 | file.path |
46 | .strip_prefix(root_directory) | |
47 | .unwrap() | |
48 | .to_string_lossy() | |
49 | .to_string() | |
5732d284 | 50 | }) |
260e8ec6 RBR |
51 | .collect() |
52 | } | |
53 | ||
54 | #[test] | |
55 | fn finds_all_files() { | |
1c5797fa | 56 | let test_dir = setup_test_dir(); |
5732d284 RBR |
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"); | |
1c5797fa RBR |
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); | |
260e8ec6 RBR |
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() { | |
1c5797fa | 77 | let test_dir = setup_test_dir(); |
5732d284 RBR |
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"); | |
1c5797fa RBR |
80 | create_test_file(&test_dir.join("_layout.html"), ""); |
81 | create_test_file(&test_dir.join("nested/nested.gmi"), ""); | |
82 | create_test_file(&test_dir.join("assets/style.css"), ""); | |
83 | create_test_file(&test_dir.join("image.png"), ""); | |
84 | let files = find_files(&test_dir); | |
260e8ec6 RBR |
85 | |
86 | for file in files { | |
87 | let extension = file.path.extension().and_then(|e| e.to_str()); | |
88 | match extension { | |
89 | Some("gmi") => assert_eq!(file.file_type, FileType::Gemini), | |
90 | Some("html") => { | |
91 | if file.path.ends_with("_layout.html") { | |
92 | assert_eq!(file.file_type, FileType::Layout) | |
93 | } else { | |
94 | assert_eq!(file.file_type, FileType::File) | |
95 | } | |
5732d284 | 96 | } |
260e8ec6 RBR |
97 | _ => assert_eq!(file.file_type, FileType::File), |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | #[test] | |
103 | fn ignores_git_directory() { | |
1c5797fa | 104 | let test_dir = setup_test_dir(); |
5732d284 RBR |
105 | create_dir_all(&test_dir.join("nested")).expect("Could not create nested test directory"); |
106 | create_dir_all(&test_dir.join("assets")).expect("Could not create assets test directory"); | |
107 | create_dir_all(&test_dir.join(".git")).expect("Could not create git test directory"); | |
1c5797fa RBR |
108 | create_test_file(&test_dir.join("_layout.html"), ""); |
109 | create_test_file(&test_dir.join("nested/nested.gmi"), ""); | |
110 | create_test_file(&test_dir.join("assets/style.css"), ""); | |
111 | create_test_file(&test_dir.join("image.png"), ""); | |
112 | create_test_file(&test_dir.join(".git/config"), ""); | |
113 | let files = find_files(&test_dir); | |
114 | ||
115 | let paths = get_paths(&test_dir, &files); | |
260e8ec6 | 116 | |
260e8ec6 RBR |
117 | assert!(!paths.iter().any(|p| p.starts_with(".git/"))); |
118 | assert!(!paths.contains(".gitignore")); | |
119 | } | |
120 | ||
260e8ec6 RBR |
121 | #[test] |
122 | fn returns_empty_for_empty_directory() { | |
1c5797fa RBR |
123 | let test_dir = setup_test_dir(); |
124 | let files = find_files(&test_dir); | |
260e8ec6 RBR |
125 | assert!(files.is_empty()); |
126 | } | |
127 | } |