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")));
59 assert!(!strategy.is(&test_dir));
63 fn identifies_file_type() {
64 let strategy = Strategy {};
65 assert!(matches!(strategy.identify(), FileType::File));
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));
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 {};
90 path: source_dir.join("image.png"),
91 file_type: FileType::File,
94 strategy.handle(&source_dir, &output_dir, &file);
96 let copied_path = &output_dir.join("image.png");
97 assert!(copied_path.exists());
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);
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 {};
120 path: source_dir.join("nested/style.css"),
121 file_type: FileType::File,
124 strategy.handle(&source_dir, &output_dir, &file);
126 let copied_path = output_dir.join("nested/style.css");
127 assert!(copied_path.exists());
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);
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 {};
148 path: source_dir.join("image.png"),
149 file_type: FileType::File,
152 strategy.handle_html(&source_dir, &output_dir, &file, "unused layout");
154 let copied_path = &output_dir.join("image.png");
155 assert!(copied_path.exists());
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);
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 {};
176 path: source_dir.join("image.png"),
177 file_type: FileType::File,
180 strategy.handle_gemini(&source_dir, &output_dir, &file);
182 let copied_path = &output_dir.join("image.png");
183 assert!(copied_path.exists());
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);