| 1 |
// use std::io::{ErrorKind, Result}; |
| 2 |
use std::path::{Path, PathBuf}; |
| 3 |
use std::sync::mpsc; |
| 4 |
use std::thread; |
| 5 |
|
| 6 |
extern crate fs_extra; |
| 7 |
use fs_extra::error::*; |
| 8 |
use fs_extra::file::*; |
| 9 |
|
| 10 |
const TEST_FOLDER: &'static str = "./tests/temp/file"; |
| 11 |
|
| 12 |
fn files_eq<P, Q>(file1: P, file2: Q) -> Result<bool> |
| 13 |
where |
| 14 |
P: AsRef<Path>, |
| 15 |
Q: AsRef<Path>, |
| 16 |
{ |
| 17 |
let content1 = read_to_string(file1)?; |
| 18 |
let content2 = read_to_string(file2)?; |
| 19 |
Ok(content1 == content2) |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn it_read_and_write_work() { |
| 24 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 25 |
test_file.push("it_read_and_write_work"); |
| 26 |
test_file.push("test.txt"); |
| 27 |
fs_extra::dir::create_all(test_file.parent().unwrap(), true).unwrap(); |
| 28 |
let content1 = "test_1"; |
| 29 |
let content2 = "test_2"; |
| 30 |
write_all(&test_file, &content1).unwrap(); |
| 31 |
assert!(test_file.exists()); |
| 32 |
let read1 = read_to_string(&test_file).unwrap(); |
| 33 |
assert_eq!(content1, read1); |
| 34 |
write_all(&test_file, &content2).unwrap(); |
| 35 |
let read2 = read_to_string(&test_file).unwrap(); |
| 36 |
assert_eq!(content2, read2); |
| 37 |
} |
| 38 |
|
| 39 |
#[test] |
| 40 |
fn it_read_not_exist_file() { |
| 41 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 42 |
test_file.push("it_read_not_exist_file"); |
| 43 |
test_file.push("test.txt"); |
| 44 |
fs_extra::dir::create_all(test_file.parent().unwrap(), true).unwrap(); |
| 45 |
assert!(!test_file.exists()); |
| 46 |
match read_to_string(&test_file) { |
| 47 |
Ok(_) => panic!("should be error"), |
| 48 |
Err(err) => match err.kind { |
| 49 |
ErrorKind::NotFound => {} |
| 50 |
_ => panic!("wrong error"), |
| 51 |
}, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn it_read_not_file() { |
| 57 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 58 |
test_file.push("it_read_not_file"); |
| 59 |
fs_extra::dir::create_all(&test_file, true).unwrap(); |
| 60 |
match read_to_string(&test_file) { |
| 61 |
Ok(_) => panic!("should be error"), |
| 62 |
Err(err) => match err.kind { |
| 63 |
ErrorKind::InvalidFile => {} |
| 64 |
_ => panic!("wrong error"), |
| 65 |
}, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn it_write_not_file() { |
| 71 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 72 |
test_file.push("it_write_not_file"); |
| 73 |
test_file.push("test.txt"); |
| 74 |
fs_extra::dir::create_all(test_file.parent().unwrap(), true).unwrap(); |
| 75 |
assert!(!test_file.exists()); |
| 76 |
test_file.pop(); |
| 77 |
match write_all(test_file, "content") { |
| 78 |
Ok(_) => panic!("should be error"), |
| 79 |
Err(err) => match err.kind { |
| 80 |
ErrorKind::InvalidFile => {} |
| 81 |
_ => panic!("wrong error"), |
| 82 |
}, |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn it_remove_file() { |
| 88 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 89 |
test_file.push("it_remove_file"); |
| 90 |
test_file.push("test.txt"); |
| 91 |
fs_extra::dir::create_all(test_file.parent().unwrap(), true).unwrap(); |
| 92 |
write_all(&test_file, "test").unwrap(); |
| 93 |
assert!(test_file.exists()); |
| 94 |
remove(&test_file).unwrap(); |
| 95 |
assert!(!test_file.exists()); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn it_copy_work() { |
| 100 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 101 |
test_file.push("it_copy_work"); |
| 102 |
let mut test_file_out = test_file.clone(); |
| 103 |
test_file.push("test.txt"); |
| 104 |
test_file_out.push("out"); |
| 105 |
test_file_out.push("test.txt"); |
| 106 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 107 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 108 |
|
| 109 |
write_all(&test_file, "test_data").unwrap(); |
| 110 |
assert!(test_file.exists()); |
| 111 |
assert!(!test_file_out.exists()); |
| 112 |
let options = CopyOptions::new(); |
| 113 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 114 |
assert!(test_file.exists()); |
| 115 |
assert!(test_file_out.exists()); |
| 116 |
assert_eq!(test_file.file_name(), test_file_out.file_name()); |
| 117 |
assert!(files_eq(test_file, test_file_out).unwrap()); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn it_copy_not_file() { |
| 122 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 123 |
test_file.push("it_copy_work"); |
| 124 |
let mut test_file_out = test_file.clone(); |
| 125 |
test_file.push("test.txt"); |
| 126 |
test_file_out.push("out"); |
| 127 |
test_file_out.push("test.txt"); |
| 128 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 129 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 130 |
|
| 131 |
write_all(&test_file, "test_data").unwrap(); |
| 132 |
assert!(test_file.exists()); |
| 133 |
assert!(!test_file_out.exists()); |
| 134 |
test_file.pop(); |
| 135 |
let options = CopyOptions::new(); |
| 136 |
|
| 137 |
match copy(&test_file, &test_file_out, &options) { |
| 138 |
Err(err) => match err.kind { |
| 139 |
ErrorKind::InvalidFile => { |
| 140 |
let wrong_path = format!("Path \"{}\" is not a file!", test_file.to_str().unwrap()); |
| 141 |
assert_eq!(wrong_path, err.to_string()); |
| 142 |
} |
| 143 |
_ => { |
| 144 |
panic!("wrong error"); |
| 145 |
} |
| 146 |
}, |
| 147 |
Ok(_) => { |
| 148 |
panic!("should be error"); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn it_copy_source_not_exist() { |
| 155 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 156 |
test_file.push("it_copy_source_not_exist"); |
| 157 |
let mut test_file_out = test_file.clone(); |
| 158 |
test_file.push("test1.txt"); |
| 159 |
test_file_out.push("out"); |
| 160 |
test_file_out.push("test.txt"); |
| 161 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 162 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 163 |
|
| 164 |
assert!(!test_file.exists()); |
| 165 |
let options = CopyOptions::new(); |
| 166 |
match copy(&test_file, test_file_out, &options) { |
| 167 |
Ok(_) => panic!("should be error"), |
| 168 |
Err(err) => match err.kind { |
| 169 |
ErrorKind::NotFound => { |
| 170 |
let wrong_path = format!( |
| 171 |
"Path \"{}\" does not exist or you don't have \ |
| 172 |
access!", |
| 173 |
test_file.to_str().unwrap() |
| 174 |
); |
| 175 |
assert_eq!(wrong_path, err.to_string()); |
| 176 |
() |
| 177 |
} |
| 178 |
_ => panic!("wrong error"), |
| 179 |
}, |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
#[test] |
| 184 |
fn it_copy_exist_overwrite() { |
| 185 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 186 |
test_file.push("it_copy_exist_overwrite"); |
| 187 |
let mut test_file_out = test_file.clone(); |
| 188 |
test_file.push("test.txt"); |
| 189 |
test_file_out.push("out"); |
| 190 |
test_file_out.push("test.txt"); |
| 191 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 192 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 193 |
|
| 194 |
write_all(&test_file, "test_data").unwrap(); |
| 195 |
let mut options = CopyOptions::new(); |
| 196 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 197 |
assert!(test_file.exists()); |
| 198 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 199 |
options.overwrite = true; |
| 200 |
write_all(&test_file, "test_data2").unwrap(); |
| 201 |
match copy(&test_file, &test_file_out, &options) { |
| 202 |
Ok(_) => { |
| 203 |
assert!(test_file.exists()); |
| 204 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 205 |
() |
| 206 |
} |
| 207 |
Err(err) => panic!(err.to_string()), |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn it_copy_exist_not_overwrite() { |
| 213 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 214 |
test_file.push("it_copy_exist_not_overwrite"); |
| 215 |
let mut test_file_out = test_file.clone(); |
| 216 |
test_file.push("test.txt"); |
| 217 |
test_file_out.push("out"); |
| 218 |
test_file_out.push("test.txt"); |
| 219 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 220 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 221 |
|
| 222 |
write_all(&test_file, "test_data").unwrap(); |
| 223 |
let mut options = CopyOptions::new(); |
| 224 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 225 |
assert!(test_file.exists()); |
| 226 |
options.overwrite = false; |
| 227 |
write_all(&test_file, "test_data2").unwrap(); |
| 228 |
match copy(&test_file, &test_file_out, &options) { |
| 229 |
Ok(_) => panic!("should be error"), |
| 230 |
Err(err) => { |
| 231 |
let file_path = format!("Path \"{}\" exists", test_file_out.to_str().unwrap()); |
| 232 |
assert_eq!(file_path, err.to_string()); |
| 233 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 234 |
() |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn it_copy_exist_skip_exist() { |
| 241 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 242 |
test_file.push("it_copy_exist_skip_exist"); |
| 243 |
let mut test_file_out = test_file.clone(); |
| 244 |
test_file.push("test.txt"); |
| 245 |
test_file_out.push("out"); |
| 246 |
test_file_out.push("test.txt"); |
| 247 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 248 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 249 |
|
| 250 |
write_all(&test_file, "test_data").unwrap(); |
| 251 |
let mut options = CopyOptions::new(); |
| 252 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 253 |
assert!(test_file.exists()); |
| 254 |
options.skip_exist = true; |
| 255 |
write_all(&test_file, "test_data2").unwrap(); |
| 256 |
match copy(&test_file, &test_file_out, &options) { |
| 257 |
Ok(_) => { |
| 258 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 259 |
() |
| 260 |
} |
| 261 |
Err(_) => panic!("should be error"), |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
#[test] |
| 266 |
fn it_copy_exist_overwrite_and_skip_exist() { |
| 267 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 268 |
test_file.push("it_copy_exist_overwrite_and_skip_exist"); |
| 269 |
let mut test_file_out = test_file.clone(); |
| 270 |
test_file.push("test.txt"); |
| 271 |
test_file_out.push("out"); |
| 272 |
test_file_out.push("test.txt"); |
| 273 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 274 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 275 |
|
| 276 |
write_all(&test_file, "test_data").unwrap(); |
| 277 |
let mut options = CopyOptions::new(); |
| 278 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 279 |
assert!(test_file.exists()); |
| 280 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 281 |
options.overwrite = true; |
| 282 |
options.skip_exist = true; |
| 283 |
write_all(&test_file, "test_data2").unwrap(); |
| 284 |
match copy(&test_file, &test_file_out, &options) { |
| 285 |
Ok(_) => { |
| 286 |
assert!(test_file.exists()); |
| 287 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 288 |
() |
| 289 |
} |
| 290 |
Err(err) => panic!(err.to_string()), |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn it_copy_with_progress_work() { |
| 296 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 297 |
test_file.push("it_copy_with_progress_work"); |
| 298 |
let mut test_file_out = test_file.clone(); |
| 299 |
test_file.push("test.txt"); |
| 300 |
test_file_out.push("out"); |
| 301 |
test_file_out.push("test.txt"); |
| 302 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 303 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 304 |
|
| 305 |
write_all(&test_file, "test_data").unwrap(); |
| 306 |
assert!(test_file.exists()); |
| 307 |
assert!(!test_file_out.exists()); |
| 308 |
let mut options = CopyOptions::new(); |
| 309 |
options.buffer_size = 1; |
| 310 |
let (tx, rx) = mpsc::channel(); |
| 311 |
thread::spawn(move || { |
| 312 |
let func_test = |process_info: TransitProcess| { |
| 313 |
tx.send(process_info).unwrap(); |
| 314 |
}; |
| 315 |
copy_with_progress(&test_file, &test_file_out, &options, func_test).unwrap(); |
| 316 |
assert!(test_file.exists()); |
| 317 |
assert!(test_file_out.exists()); |
| 318 |
assert_eq!(test_file.file_name(), test_file_out.file_name()); |
| 319 |
assert!(files_eq(test_file, test_file_out).unwrap()); |
| 320 |
}); |
| 321 |
for i in 1..10 { |
| 322 |
let process_info: TransitProcess = rx.recv().unwrap(); |
| 323 |
assert_eq!(i, process_info.copied_bytes); |
| 324 |
assert_eq!(9, process_info.total_bytes); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn it_copy_progress_not_file() { |
| 330 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 331 |
test_file.push("it_copy_progress_not_file"); |
| 332 |
let mut test_file_out = test_file.clone(); |
| 333 |
test_file.push("test.txt"); |
| 334 |
test_file_out.push("out"); |
| 335 |
test_file_out.push("test.txt"); |
| 336 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 337 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 338 |
|
| 339 |
write_all(&test_file, "test_data").unwrap(); |
| 340 |
assert!(test_file.exists()); |
| 341 |
assert!(!test_file_out.exists()); |
| 342 |
test_file.pop(); |
| 343 |
let options = CopyOptions::new(); |
| 344 |
let func_test = |process_info: TransitProcess| println!("{}", process_info.total_bytes); |
| 345 |
|
| 346 |
match copy_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 347 |
Err(err) => match err.kind { |
| 348 |
ErrorKind::InvalidFile => { |
| 349 |
let wrong_path = format!("Path \"{}\" is not a file!", test_file.to_str().unwrap()); |
| 350 |
assert_eq!(wrong_path, err.to_string()); |
| 351 |
} |
| 352 |
_ => { |
| 353 |
panic!("wrong error"); |
| 354 |
} |
| 355 |
}, |
| 356 |
Ok(_) => { |
| 357 |
panic!("should be error"); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
#[test] |
| 363 |
fn it_copy_with_progress_work_dif_buf_size() { |
| 364 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 365 |
test_file.push("it_copy_with_progress_work_dif_buf_size"); |
| 366 |
let mut test_file_out = test_file.clone(); |
| 367 |
test_file.push("test.txt"); |
| 368 |
test_file_out.push("out"); |
| 369 |
test_file_out.push("test.txt"); |
| 370 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 371 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 372 |
|
| 373 |
write_all(&test_file, "test_data_").unwrap(); |
| 374 |
assert!(test_file.exists()); |
| 375 |
assert!(!test_file_out.exists()); |
| 376 |
let mut options = CopyOptions::new(); |
| 377 |
options.buffer_size = 1; |
| 378 |
let (tx, rx) = mpsc::channel(); |
| 379 |
thread::spawn(move || { |
| 380 |
let func_test = |process_info: TransitProcess| { |
| 381 |
tx.send(process_info).unwrap(); |
| 382 |
}; |
| 383 |
copy_with_progress(&test_file, &test_file_out, &options, func_test).unwrap(); |
| 384 |
assert!(test_file.exists()); |
| 385 |
assert!(test_file_out.exists()); |
| 386 |
assert_eq!(test_file.file_name(), test_file_out.file_name()); |
| 387 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 388 |
|
| 389 |
let mut options = CopyOptions::new(); |
| 390 |
options.buffer_size = 2; |
| 391 |
options.overwrite = true; |
| 392 |
let (tx, rx) = mpsc::channel(); |
| 393 |
thread::spawn(move || { |
| 394 |
let func_test = |process_info: TransitProcess| { |
| 395 |
tx.send(process_info).unwrap(); |
| 396 |
}; |
| 397 |
copy_with_progress(&test_file, &test_file_out, &options, func_test).unwrap(); |
| 398 |
}); |
| 399 |
for i in 1..6 { |
| 400 |
let process_info: TransitProcess = rx.recv().unwrap(); |
| 401 |
assert_eq!(i * 2, process_info.copied_bytes); |
| 402 |
assert_eq!(10, process_info.total_bytes); |
| 403 |
} |
| 404 |
}); |
| 405 |
for i in 1..11 { |
| 406 |
let process_info: TransitProcess = rx.recv().unwrap(); |
| 407 |
assert_eq!(i, process_info.copied_bytes); |
| 408 |
assert_eq!(10, process_info.total_bytes); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
#[test] |
| 413 |
fn it_copy_with_progress_source_not_exist() { |
| 414 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 415 |
test_file.push("it_copy_with_progress_source_not_exist"); |
| 416 |
let mut test_file_out = test_file.clone(); |
| 417 |
test_file.push("test1.txt"); |
| 418 |
test_file_out.push("out"); |
| 419 |
test_file_out.push("test.txt"); |
| 420 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 421 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 422 |
|
| 423 |
assert!(!test_file.exists()); |
| 424 |
let options = CopyOptions::new(); |
| 425 |
let func_test = |process_info: TransitProcess| { |
| 426 |
println!("{}", process_info.total_bytes); |
| 427 |
}; |
| 428 |
match copy_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 429 |
Ok(_) => panic!("should be error"), |
| 430 |
Err(err) => match err.kind { |
| 431 |
ErrorKind::NotFound => { |
| 432 |
let wrong_path = format!( |
| 433 |
"Path \"{}\" does not exist or you don't have \ |
| 434 |
access!", |
| 435 |
test_file.to_str().unwrap() |
| 436 |
); |
| 437 |
|
| 438 |
assert_eq!(wrong_path, err.to_string()); |
| 439 |
() |
| 440 |
} |
| 441 |
_ => panic!("wrong error"), |
| 442 |
}, |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
#[test] |
| 447 |
fn it_copy_with_progress_exist_overwrite() { |
| 448 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 449 |
test_file.push("it_copy_with_progress_exist_overwrite"); |
| 450 |
let mut test_file_out = test_file.clone(); |
| 451 |
test_file.push("test.txt"); |
| 452 |
test_file_out.push("out"); |
| 453 |
test_file_out.push("test.txt"); |
| 454 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 455 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 456 |
|
| 457 |
write_all(&test_file, "test_data").unwrap(); |
| 458 |
let mut options = CopyOptions::new(); |
| 459 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 460 |
assert!(test_file.exists()); |
| 461 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 462 |
options.overwrite = true; |
| 463 |
write_all(&test_file, "test_data2").unwrap(); |
| 464 |
let func_test = |process_info: TransitProcess| { |
| 465 |
println!("{}", process_info.total_bytes); |
| 466 |
}; |
| 467 |
match copy_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 468 |
Ok(_) => { |
| 469 |
assert!(test_file.exists()); |
| 470 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 471 |
() |
| 472 |
} |
| 473 |
Err(err) => panic!(err.to_string()), |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
#[test] |
| 478 |
fn it_copy_with_progress_exist_not_overwrite() { |
| 479 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 480 |
test_file.push("it_copy_with_progress_exist_not_overwrite"); |
| 481 |
let mut test_file_out = test_file.clone(); |
| 482 |
test_file.push("test.txt"); |
| 483 |
test_file_out.push("out"); |
| 484 |
test_file_out.push("test.txt"); |
| 485 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 486 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 487 |
|
| 488 |
write_all(&test_file, "test_data").unwrap(); |
| 489 |
let mut options = CopyOptions::new(); |
| 490 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 491 |
assert!(test_file.exists()); |
| 492 |
options.overwrite = false; |
| 493 |
write_all(&test_file, "test_data2").unwrap(); |
| 494 |
let func_test = |process_info: TransitProcess| { |
| 495 |
println!("{}", process_info.total_bytes); |
| 496 |
}; |
| 497 |
match copy_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 498 |
Ok(_) => panic!("should be error"), |
| 499 |
Err(err) => { |
| 500 |
let file_path = format!("Path \"{}\" exists", test_file_out.to_str().unwrap()); |
| 501 |
|
| 502 |
assert_eq!(file_path, err.to_string()); |
| 503 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 504 |
() |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
#[test] |
| 510 |
fn it_copy_with_progress_exist_skip_exist() { |
| 511 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 512 |
test_file.push("it_copy_with_progress_exist_skip_exist"); |
| 513 |
let mut test_file_out = test_file.clone(); |
| 514 |
test_file.push("test.txt"); |
| 515 |
test_file_out.push("out"); |
| 516 |
test_file_out.push("test.txt"); |
| 517 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 518 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 519 |
|
| 520 |
write_all(&test_file, "test_data").unwrap(); |
| 521 |
let mut options = CopyOptions::new(); |
| 522 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 523 |
assert!(test_file.exists()); |
| 524 |
options.skip_exist = true; |
| 525 |
write_all(&test_file, "test_data2").unwrap(); |
| 526 |
let func_test = |process_info: TransitProcess| { |
| 527 |
println!("{}", process_info.total_bytes); |
| 528 |
}; |
| 529 |
match copy_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 530 |
Ok(_) => { |
| 531 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 532 |
() |
| 533 |
} |
| 534 |
Err(_) => panic!("should be error"), |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
#[test] |
| 539 |
fn it_copy_with_progress_exist_overwrite_and_skip_exist() { |
| 540 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 541 |
test_file.push("it_copy_with_progress_exist_overwrite_and_skip_exist"); |
| 542 |
let mut test_file_out = test_file.clone(); |
| 543 |
test_file.push("test.txt"); |
| 544 |
test_file_out.push("out"); |
| 545 |
test_file_out.push("test.txt"); |
| 546 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 547 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 548 |
|
| 549 |
write_all(&test_file, "test_data").unwrap(); |
| 550 |
let mut options = CopyOptions::new(); |
| 551 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 552 |
assert!(test_file.exists()); |
| 553 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 554 |
options.overwrite = true; |
| 555 |
options.skip_exist = true; |
| 556 |
write_all(&test_file, "test_data2").unwrap(); |
| 557 |
let func_test = |process_info: TransitProcess| { |
| 558 |
println!("{}", process_info.total_bytes); |
| 559 |
}; |
| 560 |
match copy_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 561 |
Ok(_) => { |
| 562 |
assert!(test_file.exists()); |
| 563 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 564 |
() |
| 565 |
} |
| 566 |
Err(err) => panic!(err.to_string()), |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
#[test] |
| 571 |
fn it_move_work() { |
| 572 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 573 |
test_file.push("it_move_work"); |
| 574 |
let mut test_file_out = test_file.clone(); |
| 575 |
test_file.push("test.txt"); |
| 576 |
test_file_out.push("out"); |
| 577 |
test_file_out.push("test.txt"); |
| 578 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 579 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 580 |
|
| 581 |
write_all(&test_file, "test_data").unwrap(); |
| 582 |
assert!(test_file.exists()); |
| 583 |
assert!(!test_file_out.exists()); |
| 584 |
let options = CopyOptions::new(); |
| 585 |
let old_name = test_file.file_name(); |
| 586 |
let old_content = read_to_string(&test_file).unwrap(); |
| 587 |
move_file(&test_file, &test_file_out, &options).unwrap(); |
| 588 |
assert!(!test_file.exists()); |
| 589 |
assert!(test_file_out.exists()); |
| 590 |
assert_eq!(old_name, test_file_out.file_name()); |
| 591 |
let new_content = read_to_string(&test_file_out).unwrap(); |
| 592 |
assert_eq!(old_content, new_content); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn it_move_not_file() { |
| 597 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 598 |
test_file.push("it_move_work"); |
| 599 |
let mut test_file_out = test_file.clone(); |
| 600 |
test_file.push("test.txt"); |
| 601 |
test_file_out.push("out"); |
| 602 |
test_file_out.push("test.txt"); |
| 603 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 604 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 605 |
|
| 606 |
write_all(&test_file, "test_data").unwrap(); |
| 607 |
assert!(test_file.exists()); |
| 608 |
assert!(!test_file_out.exists()); |
| 609 |
test_file.pop(); |
| 610 |
let options = CopyOptions::new(); |
| 611 |
|
| 612 |
match move_file(&test_file, &test_file_out, &options) { |
| 613 |
Err(err) => match err.kind { |
| 614 |
ErrorKind::InvalidFile => { |
| 615 |
let wrong_path = format!("Path \"{}\" is not a file!", test_file.to_str().unwrap()); |
| 616 |
assert_eq!(wrong_path, err.to_string()); |
| 617 |
} |
| 618 |
_ => { |
| 619 |
panic!("wrong error"); |
| 620 |
} |
| 621 |
}, |
| 622 |
Ok(_) => { |
| 623 |
panic!("should be error"); |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
#[test] |
| 629 |
fn it_move_source_not_exist() { |
| 630 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 631 |
test_file.push("it_move_source_not_exist"); |
| 632 |
let mut test_file_out = test_file.clone(); |
| 633 |
test_file.push("test1.txt"); |
| 634 |
test_file_out.push("out"); |
| 635 |
test_file_out.push("test.txt"); |
| 636 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 637 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 638 |
|
| 639 |
assert!(!test_file.exists()); |
| 640 |
let options = CopyOptions::new(); |
| 641 |
match move_file(&test_file, &test_file_out, &options) { |
| 642 |
Ok(_) => panic!("should be error"), |
| 643 |
Err(err) => match err.kind { |
| 644 |
ErrorKind::NotFound => { |
| 645 |
let wrong_path = format!( |
| 646 |
"Path \"{}\" does not exist or you don't have \ |
| 647 |
access!", |
| 648 |
test_file.to_str().unwrap() |
| 649 |
); |
| 650 |
|
| 651 |
assert_eq!(wrong_path, err.to_string()); |
| 652 |
() |
| 653 |
} |
| 654 |
_ => panic!("wrong error"), |
| 655 |
}, |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn it_move_exist_overwrite() { |
| 661 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 662 |
test_file.push("it_move_exist_overwrite"); |
| 663 |
let mut test_file_out = test_file.clone(); |
| 664 |
test_file.push("test.txt"); |
| 665 |
test_file_out.push("out"); |
| 666 |
test_file_out.push("test.txt"); |
| 667 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 668 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 669 |
|
| 670 |
write_all(&test_file, "test_data").unwrap(); |
| 671 |
let mut options = CopyOptions::new(); |
| 672 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 673 |
assert!(test_file.exists()); |
| 674 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 675 |
options.overwrite = true; |
| 676 |
write_all(&test_file, "test_data2").unwrap(); |
| 677 |
match move_file(&test_file, &test_file_out, &options) { |
| 678 |
Ok(_) => { |
| 679 |
assert!(!test_file.exists()); |
| 680 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 681 |
() |
| 682 |
} |
| 683 |
Err(err) => panic!(err.to_string()), |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
#[test] |
| 688 |
fn it_move_exist_not_overwrite() { |
| 689 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 690 |
test_file.push("it_move_exist_not_overwrite"); |
| 691 |
let mut test_file_out = test_file.clone(); |
| 692 |
test_file.push("test.txt"); |
| 693 |
test_file_out.push("out"); |
| 694 |
test_file_out.push("test.txt"); |
| 695 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 696 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 697 |
|
| 698 |
write_all(&test_file, "test_data").unwrap(); |
| 699 |
let mut options = CopyOptions::new(); |
| 700 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 701 |
assert!(test_file.exists()); |
| 702 |
options.overwrite = false; |
| 703 |
write_all(&test_file, "test_data2").unwrap(); |
| 704 |
match move_file(&test_file, &test_file_out, &options) { |
| 705 |
Ok(_) => panic!("should be error"), |
| 706 |
Err(err) => { |
| 707 |
let file_path = format!("Path \"{}\" exists", test_file_out.to_str().unwrap()); |
| 708 |
|
| 709 |
assert_eq!(file_path, err.to_string()); |
| 710 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 711 |
() |
| 712 |
} |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn it_move_exist_skip_exist() { |
| 718 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 719 |
test_file.push("it_move_exist_skip_exist"); |
| 720 |
let mut test_file_out = test_file.clone(); |
| 721 |
test_file.push("test.txt"); |
| 722 |
test_file_out.push("out"); |
| 723 |
test_file_out.push("test.txt"); |
| 724 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 725 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 726 |
|
| 727 |
write_all(&test_file, "test_data").unwrap(); |
| 728 |
let mut options = CopyOptions::new(); |
| 729 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 730 |
assert!(test_file.exists()); |
| 731 |
options.skip_exist = true; |
| 732 |
write_all(&test_file, "test_data2").unwrap(); |
| 733 |
match move_file(&test_file, &test_file_out, &options) { |
| 734 |
Ok(_) => { |
| 735 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 736 |
() |
| 737 |
} |
| 738 |
Err(_) => panic!("should be error"), |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
#[test] |
| 743 |
fn it_move_exist_overwrite_and_skip_exist() { |
| 744 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 745 |
test_file.push("it_move_exist_overwrite_and_skip_exist"); |
| 746 |
let mut test_file_out = test_file.clone(); |
| 747 |
test_file.push("test.txt"); |
| 748 |
test_file_out.push("out"); |
| 749 |
test_file_out.push("test.txt"); |
| 750 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 751 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 752 |
|
| 753 |
write_all(&test_file, "test_data").unwrap(); |
| 754 |
let mut options = CopyOptions::new(); |
| 755 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 756 |
assert!(test_file.exists()); |
| 757 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 758 |
options.overwrite = true; |
| 759 |
options.skip_exist = true; |
| 760 |
write_all(&test_file, "test_data2").unwrap(); |
| 761 |
match move_file(&test_file, &test_file_out, &options) { |
| 762 |
Ok(_) => { |
| 763 |
assert!(!test_file.exists()); |
| 764 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 765 |
() |
| 766 |
} |
| 767 |
Err(err) => panic!(err.to_string()), |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
#[test] |
| 772 |
fn it_move_with_progress_work() { |
| 773 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 774 |
test_file.push("it_move_with_progress_work"); |
| 775 |
let mut test_file_out = test_file.clone(); |
| 776 |
test_file.push("test.txt"); |
| 777 |
test_file_out.push("out"); |
| 778 |
test_file_out.push("test.txt"); |
| 779 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 780 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 781 |
|
| 782 |
write_all(&test_file, "test_data").unwrap(); |
| 783 |
assert!(test_file.exists()); |
| 784 |
assert!(!test_file_out.exists()); |
| 785 |
let mut options = CopyOptions::new(); |
| 786 |
options.buffer_size = 1; |
| 787 |
let (tx, rx) = mpsc::channel(); |
| 788 |
thread::spawn(move || { |
| 789 |
let old_name = test_file.file_name(); |
| 790 |
let old_content = read_to_string(&test_file).unwrap(); |
| 791 |
let func_test = |process_info: TransitProcess| { |
| 792 |
tx.send(process_info).unwrap(); |
| 793 |
}; |
| 794 |
move_file_with_progress(&test_file, &test_file_out, &options, func_test).unwrap(); |
| 795 |
assert!(!test_file.exists()); |
| 796 |
assert!(test_file_out.exists()); |
| 797 |
assert_eq!(old_name, test_file_out.file_name()); |
| 798 |
let new_content = read_to_string(&test_file_out).unwrap(); |
| 799 |
assert_eq!(old_content, new_content); |
| 800 |
}); |
| 801 |
for i in 1..10 { |
| 802 |
let process_info: TransitProcess = rx.recv().unwrap(); |
| 803 |
assert_eq!(i, process_info.copied_bytes); |
| 804 |
assert_eq!(9, process_info.total_bytes); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
#[test] |
| 809 |
fn it_move_progress_not_file() { |
| 810 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 811 |
test_file.push("it_move_progress_not_file"); |
| 812 |
let mut test_file_out = test_file.clone(); |
| 813 |
test_file.push("test.txt"); |
| 814 |
test_file_out.push("out"); |
| 815 |
test_file_out.push("test.txt"); |
| 816 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 817 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 818 |
|
| 819 |
write_all(&test_file, "test_data").unwrap(); |
| 820 |
assert!(test_file.exists()); |
| 821 |
assert!(!test_file_out.exists()); |
| 822 |
test_file.pop(); |
| 823 |
let options = CopyOptions::new(); |
| 824 |
let func_test = |process_info: TransitProcess| println!("{}", process_info.total_bytes); |
| 825 |
|
| 826 |
match move_file_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 827 |
Err(err) => match err.kind { |
| 828 |
ErrorKind::InvalidFile => { |
| 829 |
let wrong_path = format!("Path \"{}\" is not a file!", test_file.to_str().unwrap()); |
| 830 |
assert_eq!(wrong_path, err.to_string()); |
| 831 |
} |
| 832 |
_ => { |
| 833 |
panic!("wrong error"); |
| 834 |
} |
| 835 |
}, |
| 836 |
Ok(_) => { |
| 837 |
panic!("should be error"); |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
#[test] |
| 843 |
fn it_move_with_progress_work_dif_buf_size() { |
| 844 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 845 |
test_file.push("it_move_with_progress_work_dif_buf_size"); |
| 846 |
let mut test_file_out = test_file.clone(); |
| 847 |
test_file.push("test.txt"); |
| 848 |
test_file_out.push("out"); |
| 849 |
test_file_out.push("test.txt"); |
| 850 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 851 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 852 |
|
| 853 |
write_all(&test_file, "test_data_").unwrap(); |
| 854 |
assert!(test_file.exists()); |
| 855 |
assert!(!test_file_out.exists()); |
| 856 |
let mut options = CopyOptions::new(); |
| 857 |
options.buffer_size = 2; |
| 858 |
let (tx, rx) = mpsc::channel(); |
| 859 |
thread::spawn(move || { |
| 860 |
let old_name = test_file.file_name(); |
| 861 |
let old_content = read_to_string(&test_file).unwrap(); |
| 862 |
let func_test = |process_info: TransitProcess| { |
| 863 |
tx.send(process_info).unwrap(); |
| 864 |
}; |
| 865 |
move_file_with_progress(&test_file, &test_file_out, &options, func_test).unwrap(); |
| 866 |
assert!(!test_file.exists()); |
| 867 |
assert!(test_file_out.exists()); |
| 868 |
assert_eq!(old_name, test_file_out.file_name()); |
| 869 |
let new_content = read_to_string(&test_file_out).unwrap(); |
| 870 |
assert_eq!(old_content, new_content); |
| 871 |
}); |
| 872 |
for i in 1..6 { |
| 873 |
let process_info: TransitProcess = rx.recv().unwrap(); |
| 874 |
assert_eq!(i * 2, process_info.copied_bytes); |
| 875 |
assert_eq!(10, process_info.total_bytes); |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
#[test] |
| 880 |
fn it_move_with_progress_source_not_exist() { |
| 881 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 882 |
test_file.push("it_move_with_progress_source_not_exist"); |
| 883 |
let mut test_file_out = test_file.clone(); |
| 884 |
test_file.push("test1.txt"); |
| 885 |
test_file_out.push("out"); |
| 886 |
test_file_out.push("test.txt"); |
| 887 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 888 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 889 |
|
| 890 |
assert!(!test_file.exists()); |
| 891 |
let options = CopyOptions::new(); |
| 892 |
let func_test = |process_info: TransitProcess| { |
| 893 |
println!("{}", process_info.total_bytes); |
| 894 |
}; |
| 895 |
match move_file_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 896 |
Ok(_) => panic!("should be error"), |
| 897 |
Err(err) => match err.kind { |
| 898 |
ErrorKind::NotFound => { |
| 899 |
let wrong_path = format!( |
| 900 |
"Path \"{}\" does not exist or you don't have \ |
| 901 |
access!", |
| 902 |
test_file.to_str().unwrap() |
| 903 |
); |
| 904 |
|
| 905 |
assert_eq!(wrong_path, err.to_string()); |
| 906 |
() |
| 907 |
} |
| 908 |
_ => panic!("wrong error"), |
| 909 |
}, |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
#[test] |
| 914 |
fn it_move_with_progress_exist_overwrite() { |
| 915 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 916 |
test_file.push("it_move_with_progress_exist_overwrite"); |
| 917 |
let mut test_file_out = test_file.clone(); |
| 918 |
test_file.push("test.txt"); |
| 919 |
test_file_out.push("out"); |
| 920 |
test_file_out.push("test.txt"); |
| 921 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 922 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 923 |
|
| 924 |
write_all(&test_file, "test_data").unwrap(); |
| 925 |
let mut options = CopyOptions::new(); |
| 926 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 927 |
assert!(test_file.exists()); |
| 928 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 929 |
options.overwrite = true; |
| 930 |
write_all(&test_file, "test_data2").unwrap(); |
| 931 |
let func_test = |process_info: TransitProcess| { |
| 932 |
println!("{}", process_info.total_bytes); |
| 933 |
}; |
| 934 |
match move_file_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 935 |
Ok(_) => { |
| 936 |
assert!(!test_file.exists()); |
| 937 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 938 |
() |
| 939 |
} |
| 940 |
Err(err) => panic!(err.to_string()), |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
#[test] |
| 945 |
fn it_move_with_progress_exist_not_overwrite() { |
| 946 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 947 |
test_file.push("it_move_with_progress_exist_not_overwrite"); |
| 948 |
let mut test_file_out = test_file.clone(); |
| 949 |
test_file.push("test.txt"); |
| 950 |
test_file_out.push("out"); |
| 951 |
test_file_out.push("test.txt"); |
| 952 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 953 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 954 |
|
| 955 |
write_all(&test_file, "test_data").unwrap(); |
| 956 |
let mut options = CopyOptions::new(); |
| 957 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 958 |
assert!(test_file.exists()); |
| 959 |
options.overwrite = false; |
| 960 |
write_all(&test_file, "test_data2").unwrap(); |
| 961 |
let func_test = |process_info: TransitProcess| { |
| 962 |
println!("{}", process_info.total_bytes); |
| 963 |
}; |
| 964 |
match move_file_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 965 |
Ok(_) => panic!("should be error"), |
| 966 |
Err(err) => { |
| 967 |
let file_path = format!("Path \"{}\" exists", test_file_out.to_str().unwrap()); |
| 968 |
|
| 969 |
assert_eq!(file_path, err.to_string()); |
| 970 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 971 |
() |
| 972 |
} |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn it_move_with_progress_exist_skip_exist() { |
| 978 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 979 |
test_file.push("it_move_with_progress_exist_skip_exist"); |
| 980 |
let mut test_file_out = test_file.clone(); |
| 981 |
test_file.push("test.txt"); |
| 982 |
test_file_out.push("out"); |
| 983 |
test_file_out.push("test.txt"); |
| 984 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 985 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 986 |
|
| 987 |
write_all(&test_file, "test_data").unwrap(); |
| 988 |
let mut options = CopyOptions::new(); |
| 989 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 990 |
assert!(test_file.exists()); |
| 991 |
options.skip_exist = true; |
| 992 |
write_all(&test_file, "test_data2").unwrap(); |
| 993 |
let func_test = |process_info: TransitProcess| { |
| 994 |
println!("{}", process_info.total_bytes); |
| 995 |
}; |
| 996 |
match move_file_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 997 |
Ok(_) => { |
| 998 |
assert!(!files_eq(test_file, test_file_out).unwrap()); |
| 999 |
() |
| 1000 |
} |
| 1001 |
Err(_) => panic!("should be error"), |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
#[test] |
| 1006 |
fn it_move_with_progress_exist_overwrite_and_skip_exist() { |
| 1007 |
let mut test_file = PathBuf::from(TEST_FOLDER); |
| 1008 |
test_file.push("it_move_with_progress_exist_overwrite_and_skip_exist"); |
| 1009 |
let mut test_file_out = test_file.clone(); |
| 1010 |
test_file.push("test.txt"); |
| 1011 |
test_file_out.push("out"); |
| 1012 |
test_file_out.push("test.txt"); |
| 1013 |
fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); |
| 1014 |
fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); |
| 1015 |
|
| 1016 |
write_all(&test_file, "test_data").unwrap(); |
| 1017 |
let mut options = CopyOptions::new(); |
| 1018 |
copy(&test_file, &test_file_out, &options).unwrap(); |
| 1019 |
assert!(test_file.exists()); |
| 1020 |
assert!(files_eq(&test_file, &test_file_out).unwrap()); |
| 1021 |
options.overwrite = true; |
| 1022 |
options.skip_exist = true; |
| 1023 |
write_all(&test_file, "test_data2").unwrap(); |
| 1024 |
let func_test = |process_info: TransitProcess| { |
| 1025 |
println!("{}", process_info.total_bytes); |
| 1026 |
}; |
| 1027 |
match move_file_with_progress(&test_file, &test_file_out, &options, func_test) { |
| 1028 |
Ok(_) => { |
| 1029 |
assert!(!test_file.exists()); |
| 1030 |
assert!(test_file_out.exists()); |
| 1031 |
assert_eq!(read_to_string(test_file_out).unwrap(), "test_data2"); |
| 1032 |
() |
| 1033 |
} |
| 1034 |
Err(err) => panic!(err.to_string()), |
| 1035 |
} |
| 1036 |
} |