]> git.r.bdr.sh - rbdr/page/blob - src/file_handler/file_strategies/file.rs
Update file strategy tests
[rbdr/page] / src / file_handler / file_strategies / file.rs
1 pub struct Strategy {}
2
3 use std::path::PathBuf;
4 use std::fs::{copy, create_dir_all};
5
6 use crate::file_handler::{File, FileType, FileHandlerStrategy};
7
8 impl Strategy {
9 fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
10 let relative_path = file.path.strip_prefix(&source).unwrap();
11 let complete_destination = destination.join(relative_path);
12 let destination_parent = complete_destination.parent().unwrap();
13 create_dir_all(destination_parent).unwrap();
14 copy(&file.path, &complete_destination).unwrap();
15 }
16 }
17
18 impl FileHandlerStrategy for Strategy {
19 fn is(&self, path: &PathBuf) -> bool {
20 !path.is_dir()
21 }
22
23 fn identify(&self) -> FileType {
24 FileType::File
25 }
26
27 fn can_handle(&self, file_type: &FileType) -> bool {
28 match file_type {
29 FileType::File => true,
30 _ => false,
31 }
32 }
33
34 fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &str) {
35 return self.handle(source, destination, file);
36 }
37
38 fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
39 return self.handle(source, destination, file);
40 }
41 }
42
43 #[cfg(test)]
44 mod tests {
45 use std::fs;
46
47 use super::*;
48
49 use test_utilities::*;
50
51 #[test]
52 fn identifies_regular_files() {
53 let test_dir = setup_test_dir();
54 create_test_file(&test_dir.join("image.png"), "");
55 create_test_file(&test_dir.join("style.css"), "");
56 let strategy = Strategy {};
57 assert!(strategy.is(&test_dir.join("image.png")));
58 assert!(strategy.is(&test_dir.join("style.css")));
59 assert!(!strategy.is(&test_dir));
60 }
61
62 #[test]
63 fn identifies_file_type() {
64 let strategy = Strategy {};
65 assert!(matches!(strategy.identify(), FileType::File));
66 }
67
68 #[test]
69 fn handles_correct_file_type() {
70 let strategy = Strategy {};
71 assert!(strategy.can_handle(&FileType::File));
72 assert!(!strategy.can_handle(&FileType::Layout));
73 assert!(!strategy.can_handle(&FileType::Gemini));
74 assert!(!strategy.can_handle(&FileType::Unknown));
75 }
76
77 #[test]
78 fn copies_single_file() {
79 let test_dir = setup_test_dir();
80 let source_dir = test_dir.join("source");
81 let output_dir = test_dir.join("output");
82 create_dir_all(&source_dir)
83 .expect("Could not create source test directory");
84 create_dir_all(&output_dir)
85 .expect("Could not create output test directory");
86 create_test_file(&source_dir.join("image.png"), "A fish playing the banjo");
87 let strategy = Strategy {};
88
89 let file = File {
90 path: source_dir.join("image.png"),
91 file_type: FileType::File,
92 };
93
94 strategy.handle(&source_dir, &output_dir, &file);
95
96 let copied_path = &output_dir.join("image.png");
97 assert!(copied_path.exists());
98
99 // Verify file contents are identical
100 let original = fs::read(&file.path).unwrap();
101 let copied = fs::read(&copied_path).unwrap();
102 assert_eq!(original, copied);
103 }
104
105 #[test]
106 fn copies_nested_file() {
107 let test_dir = setup_test_dir();
108 let source_dir = test_dir.join("source");
109 let output_dir = test_dir.join("output");
110 create_dir_all(&source_dir)
111 .expect("Could not create source test directory");
112 create_dir_all(&output_dir)
113 .expect("Could not create output test directory");
114 create_dir_all(&source_dir.join("nested"))
115 .expect("Could not create source test directory");
116 create_test_file(&source_dir.join("nested/style.css"), "* { margin: 0; padding: 0 }");
117 let strategy = Strategy {};
118
119 let file = File {
120 path: source_dir.join("nested/style.css"),
121 file_type: FileType::File,
122 };
123
124 strategy.handle(&source_dir, &output_dir, &file);
125
126 let copied_path = output_dir.join("nested/style.css");
127 assert!(copied_path.exists());
128
129 // Verify file contents are identical
130 let original = fs::read(&file.path).unwrap();
131 let copied = fs::read(&copied_path).unwrap();
132 assert_eq!(original, copied);
133 }
134
135 #[test]
136 fn handle_html_copies_file() {
137 let test_dir = setup_test_dir();
138 let source_dir = test_dir.join("source");
139 let output_dir = test_dir.join("output");
140 create_dir_all(&source_dir)
141 .expect("Could not create source test directory");
142 create_dir_all(&output_dir)
143 .expect("Could not create output test directory");
144 create_test_file(&source_dir.join("image.png"), "A fish playing the banjo");
145 let strategy = Strategy {};
146
147 let file = File {
148 path: source_dir.join("image.png"),
149 file_type: FileType::File,
150 };
151
152 strategy.handle_html(&source_dir, &output_dir, &file, "unused layout");
153
154 let copied_path = &output_dir.join("image.png");
155 assert!(copied_path.exists());
156
157 // Verify file contents are identical
158 let original = fs::read(&file.path).unwrap();
159 let copied = fs::read(&copied_path).unwrap();
160 assert_eq!(original, copied);
161 }
162
163 #[test]
164 fn handle_gemini_copies_file() {
165 let test_dir = setup_test_dir();
166 let source_dir = test_dir.join("source");
167 let output_dir = test_dir.join("output");
168 create_dir_all(&source_dir)
169 .expect("Could not create source test directory");
170 create_dir_all(&output_dir)
171 .expect("Could not create output test directory");
172 create_test_file(&source_dir.join("image.png"), "A fish playing the banjo");
173 let strategy = Strategy {};
174
175 let file = File {
176 path: source_dir.join("image.png"),
177 file_type: FileType::File,
178 };
179
180 strategy.handle_gemini(&source_dir, &output_dir, &file);
181
182 let copied_path = &output_dir.join("image.png");
183 assert!(copied_path.exists());
184
185 // Verify file contents are identical
186 let original = fs::read(&file.path).unwrap();
187 let copied = fs::read(&copied_path).unwrap();
188 assert_eq!(original, copied);
189 }
190 }