]> git.r.bdr.sh - rbdr/page/blame - src/file_handler/file_strategies/file.rs
Add Tarpaulin to CI, fail under 100
[rbdr/page] / src / file_handler / file_strategies / file.rs
CommitLineData
1e2d00b6
RBR
1pub struct Strategy {}
2
ea529736 3use std::fs::{copy, create_dir_all};
5732d284 4use std::path::Path;
1e2d00b6 5
8766e441 6use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
1e2d00b6 7
dd0a540c 8impl Strategy {
8766e441 9 fn handle(source: &Path, destination: &Path, file: &File) {
5732d284 10 let relative_path = file.path.strip_prefix(source).unwrap();
dd0a540c
RBR
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
1e2d00b6 18impl FileHandlerStrategy for Strategy {
5732d284 19 fn is(&self, path: &Path) -> bool {
1e2d00b6
RBR
20 !path.is_dir()
21 }
22
23 fn identify(&self) -> FileType {
24 FileType::File
25 }
26
4fd89b80 27 fn can_handle(&self, file_type: &FileType) -> bool {
5732d284 28 matches!(file_type, FileType::File)
1e2d00b6
RBR
29 }
30
5732d284 31 fn handle_html(&self, source: &Path, destination: &Path, file: &File, _l: &str) {
8766e441 32 Strategy::handle(source, destination, file);
dd0a540c
RBR
33 }
34
5732d284 35 fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) {
8766e441 36 Strategy::handle(source, destination, file);
1e2d00b6
RBR
37 }
38}
260e8ec6
RBR
39
40#[cfg(test)]
41mod tests {
260e8ec6
RBR
42 use std::fs;
43
d9f6d3c1 44 use super::*;
260e8ec6 45
d9f6d3c1
RBR
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")));
2cbae13c
RBR
56 }
57
58 #[test]
59 fn rejects_directories() {
60 let test_dir = setup_test_dir();
61 let strategy = Strategy {};
d9f6d3c1 62 assert!(!strategy.is(&test_dir));
260e8ec6
RBR
63 }
64
d9f6d3c1
RBR
65 #[test]
66 fn identifies_file_type() {
67 let strategy = Strategy {};
68 assert!(matches!(strategy.identify(), FileType::File));
260e8ec6
RBR
69 }
70
d9f6d3c1 71 #[test]
2cbae13c 72 fn handles_file_type() {
d9f6d3c1
RBR
73 let strategy = Strategy {};
74 assert!(strategy.can_handle(&FileType::File));
2cbae13c
RBR
75 }
76
77 #[test]
78 fn rejects_non_file_types() {
79 let strategy = Strategy {};
d9f6d3c1
RBR
80 assert!(!strategy.can_handle(&FileType::Layout));
81 assert!(!strategy.can_handle(&FileType::Gemini));
82 assert!(!strategy.can_handle(&FileType::Unknown));
260e8ec6
RBR
83 }
84
d9f6d3c1
RBR
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");
5732d284
RBR
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");
d9f6d3c1 92 create_test_file(&source_dir.join("image.png"), "A fish playing the banjo");
d9f6d3c1
RBR
93
94 let file = File {
95 path: source_dir.join("image.png"),
96 file_type: FileType::File,
97 };
98
3a7229d5 99 Strategy::handle(&source_dir, &output_dir, &file);
d9f6d3c1
RBR
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();
b0328413 106 let copied = fs::read(copied_path).unwrap();
d9f6d3c1
RBR
107 assert_eq!(original, copied);
108 }
260e8ec6 109
d9f6d3c1
RBR
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");
5732d284
RBR
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");
b0328413 117 create_dir_all(source_dir.join("nested")).expect("Could not create source test directory");
5732d284
RBR
118 create_test_file(
119 &source_dir.join("nested/style.css"),
120 "* { margin: 0; padding: 0 }",
121 );
d9f6d3c1
RBR
122
123 let file = File {
124 path: source_dir.join("nested/style.css"),
125 file_type: FileType::File,
126 };
127
3a7229d5 128 Strategy::handle(&source_dir, &output_dir, &file);
d9f6d3c1
RBR
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 }
260e8ec6 138
d9f6d3c1
RBR
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");
5732d284
RBR
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");
d9f6d3c1
RBR
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();
b0328413 161 let copied = fs::read(copied_path).unwrap();
d9f6d3c1
RBR
162 assert_eq!(original, copied);
163 }
260e8ec6 164
d9f6d3c1
RBR
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");
5732d284
RBR
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");
d9f6d3c1
RBR
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();
b0328413 187 let copied = fs::read(copied_path).unwrap();
d9f6d3c1 188 assert_eq!(original, copied);
260e8ec6
RBR
189 }
190}