]>
Commit | Line | Data |
---|---|---|
1e2d00b6 RBR |
1 | pub struct Strategy {} |
2 | ||
3 | use std::path::PathBuf; | |
ea529736 | 4 | use std::fs::{copy, create_dir_all}; |
1e2d00b6 | 5 | |
4fd89b80 | 6 | use crate::file_handler::{File, FileType, FileHandlerStrategy}; |
1e2d00b6 | 7 | |
dd0a540c RBR |
8 | impl Strategy { |
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(); | |
15 | } | |
16 | } | |
17 | ||
1e2d00b6 RBR |
18 | impl FileHandlerStrategy for Strategy { |
19 | fn is(&self, path: &PathBuf) -> bool { | |
20 | !path.is_dir() | |
21 | } | |
22 | ||
23 | fn identify(&self) -> FileType { | |
24 | FileType::File | |
25 | } | |
26 | ||
4fd89b80 RBR |
27 | fn can_handle(&self, file_type: &FileType) -> bool { |
28 | match file_type { | |
29 | FileType::File => true, | |
30 | _ => false, | |
31 | } | |
1e2d00b6 RBR |
32 | } |
33 | ||
260e8ec6 | 34 | fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &str) { |
dd0a540c RBR |
35 | return self.handle(source, destination, file); |
36 | } | |
37 | ||
38 | fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File) { | |
39 | return self.handle(source, destination, file); | |
1e2d00b6 RBR |
40 | } |
41 | } | |
260e8ec6 RBR |
42 | |
43 | #[cfg(test)] | |
44 | mod tests { | |
45 | use super::*; | |
46 | use std::fs; | |
47 | ||
48 | fn setup() -> Strategy { | |
49 | Strategy {} | |
50 | } | |
51 | ||
52 | fn fixtures_dir() -> PathBuf { | |
53 | PathBuf::from("tests/fixtures") | |
54 | } | |
55 | ||
56 | fn fixture_path(filename: &str) -> PathBuf { | |
57 | fixtures_dir().join(filename) | |
58 | } | |
59 | ||
60 | mod file_type_tests { | |
61 | use super::*; | |
62 | ||
63 | #[test] | |
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())); | |
69 | } | |
70 | ||
71 | #[test] | |
72 | fn identifies_file_type() { | |
73 | let strategy = setup(); | |
74 | assert!(matches!(strategy.identify(), FileType::File)); | |
75 | } | |
76 | ||
77 | #[test] | |
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)); | |
84 | } | |
85 | } | |
86 | ||
87 | mod file_handling_tests { | |
88 | use super::*; | |
89 | ||
90 | #[test] | |
91 | fn copies_single_file() { | |
92 | let strategy = setup(); | |
93 | let source = fixtures_dir(); | |
94 | let output = fixture_path("output"); | |
95 | ||
96 | let file = File { | |
97 | path: fixture_path("image.png"), | |
98 | file_type: FileType::File, | |
99 | }; | |
100 | ||
101 | strategy.handle(&source, &output, &file); | |
102 | ||
103 | let copied_path = output.join("image.png"); | |
104 | assert!(copied_path.exists()); | |
105 | ||
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); | |
110 | ||
111 | // Cleanup | |
112 | let _ = fs::remove_file(copied_path); | |
113 | } | |
114 | ||
115 | #[test] | |
116 | fn copies_nested_file() { | |
117 | let strategy = setup(); | |
118 | let source = fixtures_dir(); | |
119 | let output = fixture_path("output"); | |
120 | ||
121 | let file = File { | |
122 | path: fixture_path("assets/style.css"), | |
123 | file_type: FileType::File, | |
124 | }; | |
125 | ||
126 | strategy.handle(&source, &output, &file); | |
127 | ||
128 | let copied_path = output.join("assets").join("style.css"); | |
129 | assert!(copied_path.exists()); | |
130 | ||
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); | |
135 | ||
136 | // Cleanup | |
137 | let _ = fs::remove_file(copied_path); | |
138 | let _ = fs::remove_dir(output.join("assets")); | |
139 | } | |
140 | ||
141 | #[test] | |
142 | fn handle_html_copies_file() { | |
143 | let strategy = setup(); | |
144 | let source = fixtures_dir(); | |
145 | let output = fixture_path("output_html"); | |
146 | ||
147 | let file = File { | |
148 | path: fixture_path("image.png"), | |
149 | file_type: FileType::File, | |
150 | }; | |
151 | ||
152 | strategy.handle_html(&source, &output, &file, "unused layout"); | |
153 | ||
154 | let copied_path = output.join("image.png"); | |
155 | assert!(copied_path.exists()); | |
156 | ||
157 | // Cleanup | |
158 | let _ = fs::remove_file(copied_path); | |
159 | } | |
160 | ||
161 | #[test] | |
162 | fn handle_gemini_copies_file() { | |
163 | let strategy = setup(); | |
164 | let source = fixtures_dir(); | |
165 | let output = fixture_path("output_gemini"); | |
166 | ||
167 | let file = File { | |
168 | path: fixture_path("image.png"), | |
169 | file_type: FileType::File, | |
170 | }; | |
171 | ||
172 | strategy.handle_gemini(&source, &output, &file); | |
173 | ||
174 | let copied_path = output.join("image.png"); | |
175 | assert!(copied_path.exists()); | |
176 | ||
177 | // Cleanup | |
178 | let _ = fs::remove_file(copied_path); | |
179 | } | |
180 | } | |
181 | } |