]>
Commit | Line | Data |
---|---|---|
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, FileHandlerStrategy, FileType}; | |
7 | ||
8 | impl Strategy { | |
9 | fn handle(&self, 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 | self.handle(source, destination, file) | |
33 | } | |
34 | ||
35 | fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) { | |
36 | self.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 | 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(); | |
107 | let copied = fs::read(copied_path).unwrap(); | |
108 | assert_eq!(original, copied); | |
109 | } | |
110 | ||
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"); | |
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"); | |
118 | create_dir_all(source_dir.join("nested")).expect("Could not create source test directory"); | |
119 | create_test_file( | |
120 | &source_dir.join("nested/style.css"), | |
121 | "* { margin: 0; padding: 0 }", | |
122 | ); | |
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 | } | |
140 | ||
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"); | |
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"); | |
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(); | |
163 | let copied = fs::read(copied_path).unwrap(); | |
164 | assert_eq!(original, copied); | |
165 | } | |
166 | ||
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"); | |
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"); | |
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(); | |
189 | let copied = fs::read(copied_path).unwrap(); | |
190 | assert_eq!(original, copied); | |
191 | } | |
192 | } |