3 use std::path::PathBuf;
4 use std::fs::{copy, create_dir_all};
6 use crate::file_handler::{File, FileType, FileHandlerStrategy};
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();
18 impl FileHandlerStrategy for Strategy {
19 fn is(&self, path: &PathBuf) -> bool {
23 fn identify(&self) -> FileType {
27 fn can_handle(&self, file_type: &FileType) -> bool {
29 FileType::File => true,
34 fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &str) {
35 return self.handle(source, destination, file);
38 fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
39 return self.handle(source, destination, file);
49 use test_utilities::*;
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")));
62 fn rejects_directories() {
63 let test_dir = setup_test_dir();
64 let strategy = Strategy {};
65 assert!(!strategy.is(&test_dir));
69 fn identifies_file_type() {
70 let strategy = Strategy {};
71 assert!(matches!(strategy.identify(), FileType::File));
75 fn handles_file_type() {
76 let strategy = Strategy {};
77 assert!(strategy.can_handle(&FileType::File));
81 fn rejects_non_file_types() {
82 let strategy = Strategy {};
83 assert!(!strategy.can_handle(&FileType::Layout));
84 assert!(!strategy.can_handle(&FileType::Gemini));
85 assert!(!strategy.can_handle(&FileType::Unknown));
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 {};
101 path: source_dir.join("image.png"),
102 file_type: FileType::File,
105 strategy.handle(&source_dir, &output_dir, &file);
107 let copied_path = &output_dir.join("image.png");
108 assert!(copied_path.exists());
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);
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 {};
131 path: source_dir.join("nested/style.css"),
132 file_type: FileType::File,
135 strategy.handle(&source_dir, &output_dir, &file);
137 let copied_path = output_dir.join("nested/style.css");
138 assert!(copied_path.exists());
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);
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 {};
159 path: source_dir.join("image.png"),
160 file_type: FileType::File,
163 strategy.handle_html(&source_dir, &output_dir, &file, "unused layout");
165 let copied_path = &output_dir.join("image.png");
166 assert!(copied_path.exists());
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);
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 {};
187 path: source_dir.join("image.png"),
188 file_type: FileType::File,
191 strategy.handle_gemini(&source_dir, &output_dir, &file);
193 let copied_path = &output_dir.join("image.png");
194 assert!(copied_path.exists());
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);