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