3 use std::fs::{copy, create_dir_all};
6 use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
9 fn handle(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();
18 impl FileHandlerStrategy for Strategy {
19 fn is(&self, path: &Path) -> bool {
23 fn identify(&self) -> FileType {
27 fn can_handle(&self, file_type: &FileType) -> bool {
28 matches!(file_type, FileType::File)
31 fn handle_html(&self, source: &Path, destination: &Path, file: &File, _l: &str) {
32 Strategy::handle(source, destination, file);
35 fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) {
36 Strategy::handle(source, destination, file);
46 use test_utilities::*;
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")));
59 fn rejects_directories() {
60 let test_dir = setup_test_dir();
61 let strategy = Strategy {};
62 assert!(!strategy.is(&test_dir));
66 fn identifies_file_type() {
67 let strategy = Strategy {};
68 assert!(matches!(strategy.identify(), FileType::File));
72 fn handles_file_type() {
73 let strategy = Strategy {};
74 assert!(strategy.can_handle(&FileType::File));
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));
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");
95 path: source_dir.join("image.png"),
96 file_type: FileType::File,
99 Strategy::handle(&source_dir, &output_dir, &file);
101 let copied_path = &output_dir.join("image.png");
102 assert!(copied_path.exists());
104 // Verify file contents are identical
105 let original = fs::read(&file.path).unwrap();
106 let copied = fs::read(copied_path).unwrap();
107 assert_eq!(original, copied);
111 fn copies_nested_file() {
112 let test_dir = setup_test_dir();
113 let source_dir = test_dir.join("source");
114 let output_dir = test_dir.join("output");
115 create_dir_all(&source_dir).expect("Could not create source test directory");
116 create_dir_all(&output_dir).expect("Could not create output test directory");
117 create_dir_all(source_dir.join("nested")).expect("Could not create source test directory");
119 &source_dir.join("nested/style.css"),
120 "* { margin: 0; padding: 0 }",
124 path: source_dir.join("nested/style.css"),
125 file_type: FileType::File,
128 Strategy::handle(&source_dir, &output_dir, &file);
130 let copied_path = output_dir.join("nested/style.css");
131 assert!(copied_path.exists());
133 // Verify file contents are identical
134 let original = fs::read(&file.path).unwrap();
135 let copied = fs::read(&copied_path).unwrap();
136 assert_eq!(original, copied);
140 fn handle_html_copies_file() {
141 let test_dir = setup_test_dir();
142 let source_dir = test_dir.join("source");
143 let output_dir = test_dir.join("output");
144 create_dir_all(&source_dir).expect("Could not create source test directory");
145 create_dir_all(&output_dir).expect("Could not create output test directory");
146 create_test_file(&source_dir.join("image.png"), "A fish playing the banjo");
147 let strategy = Strategy {};
150 path: source_dir.join("image.png"),
151 file_type: FileType::File,
154 strategy.handle_html(&source_dir, &output_dir, &file, "unused layout");
156 let copied_path = &output_dir.join("image.png");
157 assert!(copied_path.exists());
159 // Verify file contents are identical
160 let original = fs::read(&file.path).unwrap();
161 let copied = fs::read(copied_path).unwrap();
162 assert_eq!(original, copied);
166 fn handle_gemini_copies_file() {
167 let test_dir = setup_test_dir();
168 let source_dir = test_dir.join("source");
169 let output_dir = test_dir.join("output");
170 create_dir_all(&source_dir).expect("Could not create source test directory");
171 create_dir_all(&output_dir).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);