]>
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"))); | |
59 | assert!(!strategy.is(&test_dir)); | |
260e8ec6 RBR |
60 | } |
61 | ||
d9f6d3c1 RBR |
62 | #[test] |
63 | fn identifies_file_type() { | |
64 | let strategy = Strategy {}; | |
65 | assert!(matches!(strategy.identify(), FileType::File)); | |
260e8ec6 RBR |
66 | } |
67 | ||
d9f6d3c1 RBR |
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)); | |
260e8ec6 RBR |
75 | } |
76 | ||
d9f6d3c1 RBR |
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 | } | |
260e8ec6 | 104 | |
d9f6d3c1 RBR |
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 | } | |
260e8ec6 | 134 | |
d9f6d3c1 RBR |
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 | } | |
260e8ec6 | 162 | |
d9f6d3c1 RBR |
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); | |
260e8ec6 RBR |
189 | } |
190 | } |