]>
Commit | Line | Data |
---|---|---|
1e2d00b6 RBR |
1 | pub struct Strategy {} |
2 | ||
3 | use std::path::PathBuf; | |
ea529736 | 4 | use std::fs::{copy, create_dir_all}; |
1e2d00b6 | 5 | |
4fd89b80 | 6 | use crate::file_handler::{File, FileType, FileHandlerStrategy}; |
1e2d00b6 | 7 | |
dd0a540c RBR |
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 | ||
1e2d00b6 RBR |
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 | ||
4fd89b80 RBR |
27 | fn can_handle(&self, file_type: &FileType) -> bool { |
28 | match file_type { | |
29 | FileType::File => true, | |
30 | _ => false, | |
31 | } | |
1e2d00b6 RBR |
32 | } |
33 | ||
260e8ec6 | 34 | fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &str) { |
dd0a540c RBR |
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); | |
1e2d00b6 RBR |
40 | } |
41 | } | |
260e8ec6 RBR |
42 | |
43 | #[cfg(test)] | |
44 | mod tests { | |
260e8ec6 RBR |
45 | use std::fs; |
46 | ||
d9f6d3c1 | 47 | use super::*; |
260e8ec6 | 48 | |
d9f6d3c1 RBR |
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"))); | |
2cbae13c RBR |
59 | } |
60 | ||
61 | #[test] | |
62 | fn rejects_directories() { | |
63 | let test_dir = setup_test_dir(); | |
64 | let strategy = Strategy {}; | |
d9f6d3c1 | 65 | assert!(!strategy.is(&test_dir)); |
260e8ec6 RBR |
66 | } |
67 | ||
d9f6d3c1 RBR |
68 | #[test] |
69 | fn identifies_file_type() { | |
70 | let strategy = Strategy {}; | |
71 | assert!(matches!(strategy.identify(), FileType::File)); | |
260e8ec6 RBR |
72 | } |
73 | ||
d9f6d3c1 | 74 | #[test] |
2cbae13c | 75 | fn handles_file_type() { |
d9f6d3c1 RBR |
76 | let strategy = Strategy {}; |
77 | assert!(strategy.can_handle(&FileType::File)); | |
2cbae13c RBR |
78 | } |
79 | ||
80 | #[test] | |
81 | fn rejects_non_file_types() { | |
82 | let strategy = Strategy {}; | |
d9f6d3c1 RBR |
83 | assert!(!strategy.can_handle(&FileType::Layout)); |
84 | assert!(!strategy.can_handle(&FileType::Gemini)); | |
85 | assert!(!strategy.can_handle(&FileType::Unknown)); | |
260e8ec6 RBR |
86 | } |
87 | ||
d9f6d3c1 RBR |
88 | #[test] |
89 | fn copies_single_file() { | |
90 | let test_dir = setup_test_dir(); | |
91 | let source_dir = test_dir.join("source"); | |
92 | let output_dir = test_dir.join("output"); | |
93 | create_dir_all(&source_dir) | |
94 | .expect("Could not create source test directory"); | |
95 | create_dir_all(&output_dir) | |
96 | .expect("Could not create output test directory"); | |
97 | create_test_file(&source_dir.join("image.png"), "A fish playing the banjo"); | |
98 | let strategy = Strategy {}; | |
99 | ||
100 | let file = File { | |
101 | path: source_dir.join("image.png"), | |
102 | file_type: FileType::File, | |
103 | }; | |
104 | ||
105 | strategy.handle(&source_dir, &output_dir, &file); | |
106 | ||
107 | let copied_path = &output_dir.join("image.png"); | |
108 | assert!(copied_path.exists()); | |
109 | ||
110 | // Verify file contents are identical | |
111 | let original = fs::read(&file.path).unwrap(); | |
112 | let copied = fs::read(&copied_path).unwrap(); | |
113 | assert_eq!(original, copied); | |
114 | } | |
260e8ec6 | 115 | |
d9f6d3c1 RBR |
116 | #[test] |
117 | fn copies_nested_file() { | |
118 | let test_dir = setup_test_dir(); | |
119 | let source_dir = test_dir.join("source"); | |
120 | let output_dir = test_dir.join("output"); | |
121 | create_dir_all(&source_dir) | |
122 | .expect("Could not create source test directory"); | |
123 | create_dir_all(&output_dir) | |
124 | .expect("Could not create output test directory"); | |
125 | create_dir_all(&source_dir.join("nested")) | |
126 | .expect("Could not create source test directory"); | |
127 | create_test_file(&source_dir.join("nested/style.css"), "* { margin: 0; padding: 0 }"); | |
128 | let strategy = Strategy {}; | |
129 | ||
130 | let file = File { | |
131 | path: source_dir.join("nested/style.css"), | |
132 | file_type: FileType::File, | |
133 | }; | |
134 | ||
135 | strategy.handle(&source_dir, &output_dir, &file); | |
136 | ||
137 | let copied_path = output_dir.join("nested/style.css"); | |
138 | assert!(copied_path.exists()); | |
139 | ||
140 | // Verify file contents are identical | |
141 | let original = fs::read(&file.path).unwrap(); | |
142 | let copied = fs::read(&copied_path).unwrap(); | |
143 | assert_eq!(original, copied); | |
144 | } | |
260e8ec6 | 145 | |
d9f6d3c1 RBR |
146 | #[test] |
147 | fn handle_html_copies_file() { | |
148 | let test_dir = setup_test_dir(); | |
149 | let source_dir = test_dir.join("source"); | |
150 | let output_dir = test_dir.join("output"); | |
151 | create_dir_all(&source_dir) | |
152 | .expect("Could not create source test directory"); | |
153 | create_dir_all(&output_dir) | |
154 | .expect("Could not create output test directory"); | |
155 | create_test_file(&source_dir.join("image.png"), "A fish playing the banjo"); | |
156 | let strategy = Strategy {}; | |
157 | ||
158 | let file = File { | |
159 | path: source_dir.join("image.png"), | |
160 | file_type: FileType::File, | |
161 | }; | |
162 | ||
163 | strategy.handle_html(&source_dir, &output_dir, &file, "unused layout"); | |
164 | ||
165 | let copied_path = &output_dir.join("image.png"); | |
166 | assert!(copied_path.exists()); | |
167 | ||
168 | // Verify file contents are identical | |
169 | let original = fs::read(&file.path).unwrap(); | |
170 | let copied = fs::read(&copied_path).unwrap(); | |
171 | assert_eq!(original, copied); | |
172 | } | |
260e8ec6 | 173 | |
d9f6d3c1 RBR |
174 | #[test] |
175 | fn handle_gemini_copies_file() { | |
176 | let test_dir = setup_test_dir(); | |
177 | let source_dir = test_dir.join("source"); | |
178 | let output_dir = test_dir.join("output"); | |
179 | create_dir_all(&source_dir) | |
180 | .expect("Could not create source test directory"); | |
181 | create_dir_all(&output_dir) | |
182 | .expect("Could not create output test directory"); | |
183 | create_test_file(&source_dir.join("image.png"), "A fish playing the banjo"); | |
184 | let strategy = Strategy {}; | |
185 | ||
186 | let file = File { | |
187 | path: source_dir.join("image.png"), | |
188 | file_type: FileType::File, | |
189 | }; | |
190 | ||
191 | strategy.handle_gemini(&source_dir, &output_dir, &file); | |
192 | ||
193 | let copied_path = &output_dir.join("image.png"); | |
194 | assert!(copied_path.exists()); | |
195 | ||
196 | // Verify file contents are identical | |
197 | let original = fs::read(&file.path).unwrap(); | |
198 | let copied = fs::read(&copied_path).unwrap(); | |
199 | assert_eq!(original, copied); | |
260e8ec6 RBR |
200 | } |
201 | } |