]>
Commit | Line | Data |
---|---|---|
1e2d00b6 RBR |
1 | pub struct Strategy {} |
2 | ||
ea529736 | 3 | use std::fs::{copy, create_dir_all}; |
5732d284 | 4 | use std::path::Path; |
1e2d00b6 | 5 | |
8766e441 | 6 | use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy}; |
1e2d00b6 | 7 | |
dd0a540c | 8 | impl 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 | 18 | impl 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)] | |
41 | mod 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 RBR |
92 | create_test_file(&source_dir.join("image.png"), "A fish playing the banjo"); |
93 | let strategy = Strategy {}; | |
94 | ||
95 | let file = File { | |
96 | path: source_dir.join("image.png"), | |
97 | file_type: FileType::File, | |
98 | }; | |
99 | ||
100 | strategy.handle(&source_dir, &output_dir, &file); | |
101 | ||
102 | let copied_path = &output_dir.join("image.png"); | |
103 | assert!(copied_path.exists()); | |
104 | ||
105 | // Verify file contents are identical | |
106 | let original = fs::read(&file.path).unwrap(); | |
b0328413 | 107 | let copied = fs::read(copied_path).unwrap(); |
d9f6d3c1 RBR |
108 | assert_eq!(original, copied); |
109 | } | |
260e8ec6 | 110 | |
d9f6d3c1 RBR |
111 | #[test] |
112 | fn copies_nested_file() { | |
113 | let test_dir = setup_test_dir(); | |
114 | let source_dir = test_dir.join("source"); | |
115 | let output_dir = test_dir.join("output"); | |
5732d284 RBR |
116 | create_dir_all(&source_dir).expect("Could not create source test directory"); |
117 | create_dir_all(&output_dir).expect("Could not create output test directory"); | |
b0328413 | 118 | create_dir_all(source_dir.join("nested")).expect("Could not create source test directory"); |
5732d284 RBR |
119 | create_test_file( |
120 | &source_dir.join("nested/style.css"), | |
121 | "* { margin: 0; padding: 0 }", | |
122 | ); | |
d9f6d3c1 RBR |
123 | let strategy = Strategy {}; |
124 | ||
125 | let file = File { | |
126 | path: source_dir.join("nested/style.css"), | |
127 | file_type: FileType::File, | |
128 | }; | |
129 | ||
130 | strategy.handle(&source_dir, &output_dir, &file); | |
131 | ||
132 | let copied_path = output_dir.join("nested/style.css"); | |
133 | assert!(copied_path.exists()); | |
134 | ||
135 | // Verify file contents are identical | |
136 | let original = fs::read(&file.path).unwrap(); | |
137 | let copied = fs::read(&copied_path).unwrap(); | |
138 | assert_eq!(original, copied); | |
139 | } | |
260e8ec6 | 140 | |
d9f6d3c1 RBR |
141 | #[test] |
142 | fn handle_html_copies_file() { | |
143 | let test_dir = setup_test_dir(); | |
144 | let source_dir = test_dir.join("source"); | |
145 | let output_dir = test_dir.join("output"); | |
5732d284 RBR |
146 | create_dir_all(&source_dir).expect("Could not create source test directory"); |
147 | create_dir_all(&output_dir).expect("Could not create output test directory"); | |
d9f6d3c1 RBR |
148 | create_test_file(&source_dir.join("image.png"), "A fish playing the banjo"); |
149 | let strategy = Strategy {}; | |
150 | ||
151 | let file = File { | |
152 | path: source_dir.join("image.png"), | |
153 | file_type: FileType::File, | |
154 | }; | |
155 | ||
156 | strategy.handle_html(&source_dir, &output_dir, &file, "unused layout"); | |
157 | ||
158 | let copied_path = &output_dir.join("image.png"); | |
159 | assert!(copied_path.exists()); | |
160 | ||
161 | // Verify file contents are identical | |
162 | let original = fs::read(&file.path).unwrap(); | |
b0328413 | 163 | let copied = fs::read(copied_path).unwrap(); |
d9f6d3c1 RBR |
164 | assert_eq!(original, copied); |
165 | } | |
260e8ec6 | 166 | |
d9f6d3c1 RBR |
167 | #[test] |
168 | fn handle_gemini_copies_file() { | |
169 | let test_dir = setup_test_dir(); | |
170 | let source_dir = test_dir.join("source"); | |
171 | let output_dir = test_dir.join("output"); | |
5732d284 RBR |
172 | create_dir_all(&source_dir).expect("Could not create source test directory"); |
173 | create_dir_all(&output_dir).expect("Could not create output test directory"); | |
d9f6d3c1 RBR |
174 | create_test_file(&source_dir.join("image.png"), "A fish playing the banjo"); |
175 | let strategy = Strategy {}; | |
176 | ||
177 | let file = File { | |
178 | path: source_dir.join("image.png"), | |
179 | file_type: FileType::File, | |
180 | }; | |
181 | ||
182 | strategy.handle_gemini(&source_dir, &output_dir, &file); | |
183 | ||
184 | let copied_path = &output_dir.join("image.png"); | |
185 | assert!(copied_path.exists()); | |
186 | ||
187 | // Verify file contents are identical | |
188 | let original = fs::read(&file.path).unwrap(); | |
b0328413 | 189 | let copied = fs::read(copied_path).unwrap(); |
d9f6d3c1 | 190 | assert_eq!(original, copied); |
260e8ec6 RBR |
191 | } |
192 | } |