-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_ops.rs
More file actions
322 lines (264 loc) · 11.4 KB
/
Copy pathfile_ops.rs
File metadata and controls
322 lines (264 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! File operations for template processing
//!
//! This module provides file operations including copying and writing files
//! with automatic directory creation for template processing workflows.
use std::path::Path;
use thiserror::Error;
/// Errors that can occur during file operations
#[derive(Error, Debug)]
pub enum FileOperationError {
/// Failed to create the output directory
#[error("Failed to create directory: {path}")]
DirectoryCreation { path: String },
/// Failed to write the file to the output path
#[error("Failed to write file to: {path}")]
FileWrite { path: String },
/// Failed to copy the file from source to destination
#[error("Failed to copy file from {source_path} to {dest_path}")]
FileCopy {
source_path: String,
dest_path: String,
},
}
/// Copy a file, creating parent directories if necessary
///
/// This function copies files without template processing and creates
/// any necessary parent directories in the destination path.
///
/// # Errors
/// Returns `FileOperationError::DirectoryCreation` if the destination directory cannot be created,
/// or `FileOperationError::FileCopy` if the file cannot be copied
pub fn copy_file_with_dir_creation(
source: &Path,
destination: &Path,
) -> Result<(), FileOperationError> {
// Ensure destination directory exists
if let Some(parent) = destination.parent() {
std::fs::create_dir_all(parent).map_err(|_| FileOperationError::DirectoryCreation {
path: parent.display().to_string(),
})?;
}
std::fs::copy(source, destination).map_err(|_| FileOperationError::FileCopy {
source_path: source.display().to_string(),
dest_path: destination.display().to_string(),
})?;
Ok(())
}
/// Write content to a file, creating parent directories if necessary
///
/// # Errors
/// Returns `FileOperationError::DirectoryCreation` if the parent directory cannot be created,
/// or `FileOperationError::FileWrite` if the file cannot be written
pub fn write_file_with_dir_creation(
output_path: &Path,
content: &str,
) -> Result<(), FileOperationError> {
// Create output directory if it doesn't exist
if let Some(parent) = output_path.parent() {
std::fs::create_dir_all(parent).map_err(|_| FileOperationError::DirectoryCreation {
path: parent.display().to_string(),
})?;
}
// Write the content to the file
std::fs::write(output_path, content).map_err(|_| FileOperationError::FileWrite {
path: output_path.display().to_string(),
})?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
mod copy_file_with_dir_creation {
use super::*;
#[test]
fn it_should_copy_file_to_existing_directory() {
let temp_dir = TempDir::new().unwrap();
let source_file = temp_dir.path().join("source.txt");
let dest_file = temp_dir.path().join("dest.txt");
fs::write(&source_file, "test content").unwrap();
let result = copy_file_with_dir_creation(&source_file, &dest_file);
assert!(result.is_ok());
assert!(dest_file.exists());
let content = fs::read_to_string(&dest_file).unwrap();
assert_eq!(content, "test content");
}
#[test]
fn it_should_copy_file_and_create_parent_directories() {
let temp_dir = TempDir::new().unwrap();
let source_file = temp_dir.path().join("source.txt");
let dest_file = temp_dir.path().join("deep/nested/path/dest.txt");
fs::write(&source_file, "nested test").unwrap();
let result = copy_file_with_dir_creation(&source_file, &dest_file);
assert!(result.is_ok());
assert!(dest_file.exists());
let content = fs::read_to_string(&dest_file).unwrap();
assert_eq!(content, "nested test");
}
#[test]
fn it_should_fail_when_source_file_does_not_exist() {
let temp_dir = TempDir::new().unwrap();
let source_file = temp_dir.path().join("nonexistent.txt");
let dest_file = temp_dir.path().join("dest.txt");
let result = copy_file_with_dir_creation(&source_file, &dest_file);
assert!(result.is_err());
match result.unwrap_err() {
FileOperationError::FileCopy {
source_path,
dest_path,
} => {
assert!(source_path.contains("nonexistent.txt"));
assert!(dest_path.contains("dest.txt"));
}
FileOperationError::DirectoryCreation { .. } => {
panic!("Expected FileCopy error, got DirectoryCreation")
}
FileOperationError::FileWrite { .. } => {
panic!("Expected FileCopy error, got FileWrite")
}
}
}
#[test]
fn it_should_overwrite_existing_destination_file() {
let temp_dir = TempDir::new().unwrap();
let source_file = temp_dir.path().join("source.txt");
let dest_file = temp_dir.path().join("dest.txt");
fs::write(&source_file, "new content").unwrap();
fs::write(&dest_file, "old content").unwrap();
let result = copy_file_with_dir_creation(&source_file, &dest_file);
assert!(result.is_ok());
let content = fs::read_to_string(&dest_file).unwrap();
assert_eq!(content, "new content");
}
#[test]
fn it_should_copy_binary_file_correctly() {
let temp_dir = TempDir::new().unwrap();
let source_file = temp_dir.path().join("binary.bin");
let dest_file = temp_dir.path().join("copied.bin");
let binary_data = vec![0x00, 0x01, 0xFF, 0x7F, 0x80];
fs::write(&source_file, &binary_data).unwrap();
let result = copy_file_with_dir_creation(&source_file, &dest_file);
assert!(result.is_ok());
let copied_data = fs::read(&dest_file).unwrap();
assert_eq!(copied_data, binary_data);
}
}
mod write_file_with_dir_creation {
use super::*;
#[test]
fn it_should_write_content_to_existing_directory() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("test.txt");
let content = "hello world";
let result = write_file_with_dir_creation(&file_path, content);
assert!(result.is_ok());
assert!(file_path.exists());
let read_content = fs::read_to_string(&file_path).unwrap();
assert_eq!(read_content, content);
}
#[test]
fn it_should_write_file_and_create_parent_directories() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("deep/nested/structure/file.txt");
let content = "nested content";
let result = write_file_with_dir_creation(&file_path, content);
assert!(result.is_ok());
assert!(file_path.exists());
let read_content = fs::read_to_string(&file_path).unwrap();
assert_eq!(read_content, content);
}
#[test]
fn it_should_overwrite_existing_file() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("existing.txt");
fs::write(&file_path, "original content").unwrap();
let new_content = "updated content";
let result = write_file_with_dir_creation(&file_path, new_content);
assert!(result.is_ok());
let read_content = fs::read_to_string(&file_path).unwrap();
assert_eq!(read_content, new_content);
}
#[test]
fn it_should_handle_empty_content() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("empty.txt");
let result = write_file_with_dir_creation(&file_path, "");
assert!(result.is_ok());
assert!(file_path.exists());
let read_content = fs::read_to_string(&file_path).unwrap();
assert_eq!(read_content, "");
}
#[test]
fn it_should_handle_unicode_content() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("unicode.txt");
let content = "Hello 世界! 🚀 Émojis and spëcial chars";
let result = write_file_with_dir_creation(&file_path, content);
assert!(result.is_ok());
let read_content = fs::read_to_string(&file_path).unwrap();
assert_eq!(read_content, content);
}
#[test]
fn it_should_handle_multiline_content() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("multiline.txt");
let content = "Line 1\nLine 2\nLine 3\n\nLine 5";
let result = write_file_with_dir_creation(&file_path, content);
assert!(result.is_ok());
let read_content = fs::read_to_string(&file_path).unwrap();
assert_eq!(read_content, content);
}
}
mod error_handling {
use super::*;
#[test]
fn it_should_display_directory_creation_error_correctly() {
let error = FileOperationError::DirectoryCreation {
path: "/some/path".to_string(),
};
let error_string = format!("{error}");
assert!(error_string.contains("Failed to create directory"));
assert!(error_string.contains("/some/path"));
}
#[test]
fn it_should_display_file_write_error_correctly() {
let error = FileOperationError::FileWrite {
path: "/output/file.txt".to_string(),
};
let error_string = format!("{error}");
assert!(error_string.contains("Failed to write file to"));
assert!(error_string.contains("/output/file.txt"));
}
#[test]
fn it_should_display_file_copy_error_correctly() {
let error = FileOperationError::FileCopy {
source_path: "/source/file.txt".to_string(),
dest_path: "/dest/file.txt".to_string(),
};
let error_string = format!("{error}");
assert!(error_string.contains("Failed to copy file from"));
assert!(error_string.contains("/source/file.txt"));
assert!(error_string.contains("/dest/file.txt"));
}
#[test]
fn it_should_support_debug_formatting_for_errors() {
let write_error = FileOperationError::FileWrite {
path: "/test/path".to_string(),
};
let copy_error = FileOperationError::DirectoryCreation {
path: "/test/dir".to_string(),
};
let file_copy_error = FileOperationError::FileCopy {
source_path: "/src/file".to_string(),
dest_path: "/dst/file".to_string(),
};
let write_debug = format!("{write_error:?}");
let copy_debug = format!("{copy_error:?}");
let file_copy_debug = format!("{file_copy_error:?}");
assert!(write_debug.contains("FileWrite"));
assert!(copy_debug.contains("DirectoryCreation"));
assert!(file_copy_debug.contains("FileCopy"));
}
}
}