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);
48 fn setup() -> Strategy {
52 fn fixtures_dir() -> PathBuf {
53 PathBuf::from("tests/fixtures")
56 fn fixture_path(filename: &str) -> PathBuf {
57 fixtures_dir().join(filename)
64 fn identifies_regular_files() {
65 let strategy = setup();
66 assert!(strategy.is(&fixture_path("image.png")));
67 assert!(strategy.is(&fixture_path("style.css")));
68 assert!(!strategy.is(&fixtures_dir()));
72 fn identifies_file_type() {
73 let strategy = setup();
74 assert!(matches!(strategy.identify(), FileType::File));
78 fn handles_correct_file_type() {
79 let strategy = setup();
80 assert!(strategy.can_handle(&FileType::File));
81 assert!(!strategy.can_handle(&FileType::Layout));
82 assert!(!strategy.can_handle(&FileType::Gemini));
83 assert!(!strategy.can_handle(&FileType::Unknown));
87 mod file_handling_tests {
91 fn copies_single_file() {
92 let strategy = setup();
93 let source = fixtures_dir();
94 let output = fixture_path("output");
97 path: fixture_path("image.png"),
98 file_type: FileType::File,
101 strategy.handle(&source, &output, &file);
103 let copied_path = output.join("image.png");
104 assert!(copied_path.exists());
106 // Verify file contents are identical
107 let original = fs::read(&file.path).unwrap();
108 let copied = fs::read(&copied_path).unwrap();
109 assert_eq!(original, copied);
112 let _ = fs::remove_file(copied_path);
116 fn copies_nested_file() {
117 let strategy = setup();
118 let source = fixtures_dir();
119 let output = fixture_path("output");
122 path: fixture_path("assets/style.css"),
123 file_type: FileType::File,
126 strategy.handle(&source, &output, &file);
128 let copied_path = output.join("assets").join("style.css");
129 assert!(copied_path.exists());
131 // Verify file contents are identical
132 let original = fs::read(&file.path).unwrap();
133 let copied = fs::read(&copied_path).unwrap();
134 assert_eq!(original, copied);
137 let _ = fs::remove_file(copied_path);
138 let _ = fs::remove_dir(output.join("assets"));
142 fn handle_html_copies_file() {
143 let strategy = setup();
144 let source = fixtures_dir();
145 let output = fixture_path("output_html");
148 path: fixture_path("image.png"),
149 file_type: FileType::File,
152 strategy.handle_html(&source, &output, &file, "unused layout");
154 let copied_path = output.join("image.png");
155 assert!(copied_path.exists());
158 let _ = fs::remove_file(copied_path);
162 fn handle_gemini_copies_file() {
163 let strategy = setup();
164 let source = fixtures_dir();
165 let output = fixture_path("output_gemini");
168 path: fixture_path("image.png"),
169 file_type: FileType::File,
172 strategy.handle_gemini(&source, &output, &file);
174 let copied_path = output.join("image.png");
175 assert!(copied_path.exists());
178 let _ = fs::remove_file(copied_path);