+use std::collections::HashSet;
+use std::fs::read_dir;
use std::path::{Path, PathBuf};
-use std::thread;
use std::sync::mpsc::{self, TryRecvError};
-use std::fs::read_dir;
-use std::collections::HashSet;
+use std::thread;
extern crate fs_extra;
use fs_extra::dir::*;
use fs_extra::error::*;
-
-
fn files_eq<P, Q>(file1: P, file2: Q) -> bool
where
P: AsRef<Path>,
let content1 = fs_extra::file::read_to_string(file1).unwrap();
let content2 = fs_extra::file::read_to_string(file2).unwrap();
content1 == content2
-
}
-
fn compare_dir<P, Q>(path_from: P, path_to: Q) -> bool
where
P: AsRef<Path>,
true
}
-
const TEST_FOLDER: &'static str = "./tests/temp/dir";
-
-
#[test]
fn it_create_all_work() {
let mut test_dir = PathBuf::from(TEST_FOLDER);
match create(&test_dir, false) {
Ok(_) => panic!("Should be error!"),
- Err(err) => {
- match err.kind {
- ErrorKind::AlreadyExists => {
- assert!(test_dir.exists());
- assert!(file_path.exists());
- let new_content = fs_extra::file::read_to_string(file_path).unwrap();
- assert_eq!(new_content, content);
-
- }
- _ => panic!("Wrong error"),
+ Err(err) => match err.kind {
+ ErrorKind::AlreadyExists => {
+ assert!(test_dir.exists());
+ assert!(file_path.exists());
+ let new_content = fs_extra::file::read_to_string(file_path).unwrap();
+ assert_eq!(new_content, content);
}
- }
-
+ _ => panic!("Wrong error"),
+ },
}
}
}
Err(err) => panic!(err.to_string()),
}
-
}
#[test]
fs_extra::file::write_all(&path_from, "test").unwrap();
match copy(&path_from, &path_to, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::InvalidFolder => {
- let wrong_path = format!(
- "Path \"{}\" is not a directory!",
- path_from.to_str().unwrap()
- );
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!("wrong error");
- }
+ Err(err) => match err.kind {
+ ErrorKind::InvalidFolder => {
+ let wrong_path = format!(
+ "Path \"{}\" is not a directory!",
+ path_from.to_str().unwrap()
+ );
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!("wrong error");
+ }
+ },
Ok(_) => {
panic!("should be error");
}
let options = CopyOptions::new();
match copy(&path_from, &path_to, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::NotFound => {
- let wrong_path = format!(
- "Path \"{}\" does not exist or you don't have \
- access!",
- path_from.to_str().unwrap()
- );
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::NotFound => {
+ let wrong_path = format!(
+ "Path \"{}\" does not exist or you don't have \
+ access!",
+ path_from.to_str().unwrap()
+ );
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
fs_extra::file::write_all(&file2_path, &content2).unwrap();
assert!(file2_path.exists());
-
let mut exist_path = path_to.clone();
exist_path.push(&test_name);
create(&exist_path, true).unwrap();
fs_extra::file::write_all(&exist_path, exist_content).unwrap();
assert!(exist_path.exists());
-
let mut options = CopyOptions::new();
options.overwrite = true;
copy(&path_from, &path_to, &options).unwrap();
let options = CopyOptions::new();
match copy(&path_from, &path_to, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::AlreadyExists => {
- let wrong_path = format!("Path \"{}\" is exist", exist_path.to_str().unwrap());
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::AlreadyExists => {
+ let wrong_path = format!("Path \"{}\" is exist", exist_path.to_str().unwrap());
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
fs_extra::file::write_all(&file2_path, &content2).unwrap();
assert!(file2_path.exists());
-
let mut exist_path = path_to.clone();
exist_path.push(&test_name);
create(&exist_path, true).unwrap();
fs_extra::file::write_all(&exist_path, exist_content).unwrap();
assert!(exist_path.exists());
-
let mut options = CopyOptions::new();
options.skip_exist = true;
copy(&path_from, &path_to, &options).unwrap();
fs_extra::file::write_all(&file2_path, &content2).unwrap();
assert!(file2_path.exists());
-
let mut exist_path = path_to.clone();
exist_path.push(&test_name);
create(&exist_path, true).unwrap();
fs_extra::file::write_all(&exist_path, exist_content).unwrap();
assert!(exist_path.exists());
-
let mut options = CopyOptions::new();
options.overwrite = true;
options.skip_exist = true;
}
#[test]
-fn it_copy_using_first_levels(){
-
+fn it_copy_using_first_levels() {
let test_dir = Path::new(TEST_FOLDER).join("it_copy_using_first_levels");
let path_to = test_dir.join("out");
let d_level_1 = (test_dir.join("d_level_1"), path_to.join("d_level_1"));
let d_level_4 = (d_level_3.0.join("d_level_4"), d_level_3.1.join("d_level_4"));
let d_level_5 = (d_level_4.0.join("d_level_5"), d_level_4.1.join("d_level_5"));
- let file1 = (d_level_1.0.join("file1.txt"),d_level_1.1.join("file1.txt"));
- let file2 = (d_level_2.0.join("file2.txt"),d_level_2.1.join("file2.txt"));
- let file3 = (d_level_3.0.join("file3.txt"),d_level_3.1.join("file3.txt"));
- let file4 = (d_level_4.0.join("file4.txt"),d_level_4.1.join("file4.txt"));
- let file5 = (d_level_5.0.join("file5.txt"),d_level_5.1.join("file5.txt"));
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+ let file4 = (d_level_4.0.join("file4.txt"), d_level_4.1.join("file4.txt"));
+ let file5 = (d_level_5.0.join("file5.txt"), d_level_5.1.join("file5.txt"));
create_all(&d_level_1.0, true).unwrap();
create_all(&d_level_2.0, true).unwrap();
assert!(d_level_4.0.exists());
assert!(d_level_5.0.exists());
-
assert!(!d_level_1.1.exists());
assert!(!d_level_2.1.exists());
assert!(!d_level_3.1.exists());
assert!(files_eq(&file1.0, &file1.1));
}
-
#[test]
-fn it_copy_using_four_levels(){
-
+fn it_copy_using_four_levels() {
let test_dir = Path::new(TEST_FOLDER).join("it_copy_using_four_levels");
let path_to = test_dir.join("out");
let d_level_1 = (test_dir.join("d_level_1"), path_to.join("d_level_1"));
let d_level_4 = (d_level_3.0.join("d_level_4"), d_level_3.1.join("d_level_4"));
let d_level_5 = (d_level_4.0.join("d_level_5"), d_level_4.1.join("d_level_5"));
- let file1 = (d_level_1.0.join("file1.txt"),d_level_1.1.join("file1.txt"));
- let file2 = (d_level_2.0.join("file2.txt"),d_level_2.1.join("file2.txt"));
- let file3 = (d_level_3.0.join("file3.txt"),d_level_3.1.join("file3.txt"));
- let file4 = (d_level_4.0.join("file4.txt"),d_level_4.1.join("file4.txt"));
- let file5 = (d_level_5.0.join("file5.txt"),d_level_5.1.join("file5.txt"));
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+ let file4 = (d_level_4.0.join("file4.txt"), d_level_4.1.join("file4.txt"));
+ let file5 = (d_level_5.0.join("file5.txt"), d_level_5.1.join("file5.txt"));
create_all(&d_level_1.0, true).unwrap();
create_all(&d_level_2.0, true).unwrap();
assert!(d_level_4.0.exists());
assert!(d_level_5.0.exists());
-
assert!(!d_level_1.1.exists());
assert!(!d_level_2.1.exists());
assert!(!d_level_3.1.exists());
assert!(files_eq(&file3.0, &file3.1));
assert!(files_eq(&file4.0, &file4.1));
}
+#[test]
+fn it_copy_content_only_option() {
+ let test_dir = Path::new(TEST_FOLDER).join("it_copy_content_only_option");
+ let path_to = test_dir.join("out");
+ let d_level_1 = (test_dir.join("d_level_1"), path_to.clone());
+ let d_level_2 = (d_level_1.0.join("d_level_2"), d_level_1.1.join("d_level_2"));
+ let d_level_3 = (d_level_2.0.join("d_level_3"), d_level_2.1.join("d_level_3"));
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+
+ create_all(&d_level_1.0, true).unwrap();
+ create_all(&d_level_2.0, true).unwrap();
+ create_all(&d_level_3.0, true).unwrap();
+ create_all(&path_to, true).unwrap();
+
+ assert!(path_to.exists());
+ assert!(d_level_1.0.exists());
+ assert!(d_level_2.0.exists());
+ assert!(d_level_3.0.exists());
+
+ assert!(!d_level_2.1.exists());
+ assert!(!d_level_3.1.exists());
+
+ fs_extra::file::write_all(&file1.0, "content1").unwrap();
+ fs_extra::file::write_all(&file2.0, "content2").unwrap();
+ fs_extra::file::write_all(&file3.0, "content3").unwrap();
+
+ assert!(file1.0.exists());
+ assert!(file2.0.exists());
+ assert!(file3.0.exists());
+
+ assert!(!file1.1.exists());
+ assert!(!file2.1.exists());
+ assert!(!file3.1.exists());
+
+ let mut options = CopyOptions::new();
+ options.content_only = true;
+ let result = copy(&d_level_1.0, path_to, &options).unwrap();
+
+ assert_eq!(24, result);
+
+ assert!(d_level_1.0.exists());
+ assert!(d_level_2.0.exists());
+ assert!(d_level_3.0.exists());
+
+ assert!(d_level_1.1.exists());
+ assert!(d_level_2.1.exists());
+ assert!(d_level_3.1.exists());
+
+ assert!(file1.0.exists());
+ assert!(file2.0.exists());
+ assert!(file3.0.exists());
+
+ assert!(file1.1.exists());
+ assert!(file2.1.exists());
+ assert!(file3.1.exists());
+
+ assert!(files_eq(&file1.0, &file1.1));
+ assert!(files_eq(&file2.0, &file2.1));
+ assert!(files_eq(&file3.0, &file3.1));
+}
#[test]
fn it_copy_progress_work() {
assert_eq!(15, result);
assert!(path_to.exists());
assert!(compare_dir(&path_from, &path_to));
-
}).join();
loop {
Ok(_) => {}
Err(err) => panic!(err),
}
-
}
#[test]
TransitProcessResult::ContinueOrAbort
};
match copy_with_progress(&path_from, &path_to, &options, func_test) {
- Err(err) => {
- match err.kind {
- ErrorKind::InvalidFolder => {
- let wrong_path = format!(
- "Path \"{}\" is not a directory!",
- path_from.to_str().unwrap()
- );
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!("wrong error");
- }
+ Err(err) => match err.kind {
+ ErrorKind::InvalidFolder => {
+ let wrong_path = format!(
+ "Path \"{}\" is not a directory!",
+ path_from.to_str().unwrap()
+ );
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!("wrong error");
+ }
+ },
Ok(_) => {
panic!("should be error");
}
}
}
-
#[test]
fn it_copy_with_progress_work_dif_buf_size() {
let mut path_from = PathBuf::from(TEST_FOLDER);
Ok(_) => {}
Err(err) => panic!(err),
}
-
}).join();
for i in 1..9 {
};
match copy_with_progress(&path_from, &path_to, &options, func_test) {
- Err(err) => {
- match err.kind {
- ErrorKind::NotFound => {
- let wrong_path = format!(
- "Path \"{}\" does not exist or you don't \
- have access!",
- path_from.to_str().unwrap()
- );
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::NotFound => {
+ let wrong_path = format!(
+ "Path \"{}\" does not exist or you don't \
+ have access!",
+ path_from.to_str().unwrap()
+ );
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
match rx.recv() {
Err(_) => {}
_ => panic!("should be error"),
-
}
-
}
#[test]
assert_eq!(23, result);
assert!(path_to.exists());
assert!(compare_dir(&path_from, &path_to));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
match rx.recv() {
Err(_) => panic!("Errors should not be!"),
_ => {}
-
}
-
}
#[test]
let result = copy_with_progress(&path_from, &path_to, &options, func_test);
match result {
Ok(_) => panic!("Should be error!"),
- Err(err) => {
- match err.kind {
- ErrorKind::AlreadyExists => {}
- _ => panic!("Wrong wrror"),
- }
- }
+ Err(err) => match err.kind {
+ ErrorKind::AlreadyExists => {}
+ _ => panic!("Wrong wrror"),
+ },
}
-
}
#[test]
assert_eq!(0, result);
assert!(path_to.exists());
assert!(!compare_dir(&path_from, &path_to));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
match rx.recv() {
Err(_) => {}
_ => panic!("should be error"),
-
}
-
}
-
#[test]
fn it_copy_with_progress_exist_overwrite_and_skip_exist() {
let mut path_from = PathBuf::from(TEST_FOLDER);
assert_eq!(23, result);
assert!(path_to.exists());
assert!(compare_dir(&path_from, &path_to));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
}
rx.recv().unwrap();
-
}
#[test]
-fn it_copy_with_progress_using_first_levels(){
-
+fn it_copy_with_progress_using_first_levels() {
let test_dir = Path::new(TEST_FOLDER).join("it_copy_with_progress_using_first_levels");
let path_to = test_dir.join("out");
let d_level_1 = (test_dir.join("d_level_1"), path_to.join("d_level_1"));
let d_level_4 = (d_level_3.0.join("d_level_4"), d_level_3.1.join("d_level_4"));
let d_level_5 = (d_level_4.0.join("d_level_5"), d_level_4.1.join("d_level_5"));
- let file1 = (d_level_1.0.join("file1.txt"),d_level_1.1.join("file1.txt"));
- let file2 = (d_level_2.0.join("file2.txt"),d_level_2.1.join("file2.txt"));
- let file3 = (d_level_3.0.join("file3.txt"),d_level_3.1.join("file3.txt"));
- let file4 = (d_level_4.0.join("file4.txt"),d_level_4.1.join("file4.txt"));
- let file5 = (d_level_5.0.join("file5.txt"),d_level_5.1.join("file5.txt"));
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+ let file4 = (d_level_4.0.join("file4.txt"), d_level_4.1.join("file4.txt"));
+ let file5 = (d_level_5.0.join("file5.txt"), d_level_5.1.join("file5.txt"));
create_all(&d_level_1.0, true).unwrap();
create_all(&d_level_2.0, true).unwrap();
assert!(d_level_4.0.exists());
assert!(d_level_5.0.exists());
-
assert!(!d_level_1.1.exists());
assert!(!d_level_2.1.exists());
assert!(!d_level_3.1.exists());
assert!(!file4.1.exists());
assert!(!file5.1.exists());
assert!(files_eq(&file1.0, &file1.1));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
match rx.recv() {
Err(_) => panic!("Errors should not be!"),
_ => {}
-
}
-
}
-
#[test]
fn it_copy_with_progress_using_four_levels() {
-
let test_dir = Path::new(TEST_FOLDER).join("it_copy_with_progress_using_four_levels");
let path_to = test_dir.join("out");
let d_level_1 = (test_dir.join("d_level_1"), path_to.join("d_level_1"));
let d_level_4 = (d_level_3.0.join("d_level_4"), d_level_3.1.join("d_level_4"));
let d_level_5 = (d_level_4.0.join("d_level_5"), d_level_4.1.join("d_level_5"));
- let file1 = (d_level_1.0.join("file1.txt"),d_level_1.1.join("file1.txt"));
- let file2 = (d_level_2.0.join("file2.txt"),d_level_2.1.join("file2.txt"));
- let file3 = (d_level_3.0.join("file3.txt"),d_level_3.1.join("file3.txt"));
- let file4 = (d_level_4.0.join("file4.txt"),d_level_4.1.join("file4.txt"));
- let file5 = (d_level_5.0.join("file5.txt"),d_level_5.1.join("file5.txt"));
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+ let file4 = (d_level_4.0.join("file4.txt"), d_level_4.1.join("file4.txt"));
+ let file5 = (d_level_5.0.join("file5.txt"), d_level_5.1.join("file5.txt"));
create_all(&d_level_1.0, true).unwrap();
create_all(&d_level_2.0, true).unwrap();
assert!(d_level_4.0.exists());
assert!(d_level_5.0.exists());
-
assert!(!d_level_1.1.exists());
assert!(!d_level_2.1.exists());
assert!(!d_level_3.1.exists());
assert!(files_eq(&file2.0, &file2.1));
assert!(files_eq(&file3.0, &file3.1));
assert!(files_eq(&file4.0, &file4.1));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
match rx.recv() {
Err(_) => panic!("Errors should not be!"),
_ => {}
+ }
+}
+#[test]
+fn it_copy_with_progress_content_only_option() {
+ let test_dir = Path::new(TEST_FOLDER).join("it_copy_with_progress_content_only_option");
+ let path_to = test_dir.join("out");
+ let d_level_1 = (test_dir.join("d_level_1"), path_to.clone());
+ let d_level_2 = (d_level_1.0.join("d_level_2"), d_level_1.1.join("d_level_2"));
+ let d_level_3 = (d_level_2.0.join("d_level_3"), d_level_2.1.join("d_level_3"));
+
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+
+ create_all(&d_level_1.0, true).unwrap();
+ create_all(&d_level_2.0, true).unwrap();
+ create_all(&d_level_3.0, true).unwrap();
+ create_all(&path_to, true).unwrap();
+
+ assert!(path_to.exists());
+ assert!(d_level_1.0.exists());
+ assert!(d_level_2.0.exists());
+ assert!(d_level_3.0.exists());
+
+ assert!(!d_level_2.1.exists());
+ assert!(!d_level_3.1.exists());
+
+ fs_extra::file::write_all(&file1.0, "content1").unwrap();
+ fs_extra::file::write_all(&file2.0, "content2").unwrap();
+ fs_extra::file::write_all(&file3.0, "content3").unwrap();
+
+ assert!(file1.0.exists());
+ assert!(file2.0.exists());
+ assert!(file3.0.exists());
+
+ assert!(!file1.1.exists());
+ assert!(!file2.1.exists());
+ assert!(!file3.1.exists());
+
+ let mut options = CopyOptions::new();
+ options.content_only = true;
+ let (tx, rx) = mpsc::channel();
+ let result = thread::spawn(move || {
+ let func_test = |process_info: TransitProcess| {
+ tx.send(process_info).unwrap();
+ TransitProcessResult::ContinueOrAbort
+ };
+
+ let result = copy_with_progress(&d_level_1.0, &path_to, &options, func_test).unwrap();
+
+ assert_eq!(24, result);
+
+ assert!(d_level_1.0.exists());
+ assert!(d_level_2.0.exists());
+ assert!(d_level_3.0.exists());
+
+ assert!(d_level_1.1.exists());
+ assert!(d_level_2.1.exists());
+ assert!(d_level_3.1.exists());
+
+ assert!(file1.0.exists());
+ assert!(file2.0.exists());
+ assert!(file3.0.exists());
+
+ assert!(file1.1.exists());
+ assert!(file2.1.exists());
+ assert!(file3.1.exists());
+ assert!(files_eq(&file1.0, &file1.1));
+ assert!(files_eq(&file2.0, &file2.1));
+ assert!(files_eq(&file3.0, &file3.1));
+ }).join();
+ match result {
+ Ok(_) => {}
+ Err(err) => panic!(err),
+ }
+
+ match rx.recv() {
+ Err(_) => panic!("Errors should not be!"),
+ _ => {}
}
}
#[test]
fn it_copy_inside_work_target_dir_exist_with_no_source_dir_named_sub_dir() {
let path_root = Path::new(TEST_FOLDER);
- let root = path_root.join(
- "it_copy_inside_work_target_dir_exist_with_no_source_dir_named_sub_dir",
- );
+ let root =
+ path_root.join("it_copy_inside_work_target_dir_exist_with_no_source_dir_named_sub_dir");
let root_dir1 = root.join("dir1");
let root_dir1_sub = root_dir1.join("sub");
let root_dir2 = root.join("dir2");
let mut options = CopyOptions::new();
options.copy_inside = true;
match copy(&root_dir1, &root_dir2, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::AlreadyExists => {
- assert_eq!(1, 1);
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::AlreadyExists => {
+ assert_eq!(1, 1);
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
assert!(compare_dir(&root_dir1, &root_dir2));
}
-
// The compare_dir method assumes that the folder `path_to` must have a sub folder named the last component of the `path_from`.
// In order to compare two folders with different name but share the same structure, rewrite a new compare method to do that!
fn compare_dir_recursively<P, Q>(path_from: P, path_to: Q) -> bool
fs_extra::file::write_all(&path_from, "test").unwrap();
match move_dir(&path_from, &path_to, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::InvalidFolder => {
- let wrong_path = format!(
- "Path \"{}\" is not a directory or you don't have \
- access!",
- path_from.to_str().unwrap()
- );
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!("wrong error");
- }
+ Err(err) => match err.kind {
+ ErrorKind::InvalidFolder => {
+ let wrong_path = format!(
+ "Path \"{}\" is not a directory or you don't have \
+ access!",
+ path_from.to_str().unwrap()
+ );
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!("wrong error");
+ }
+ },
Ok(_) => {
panic!("should be error");
}
let options = CopyOptions::new();
match move_dir(&path_from, &path_to, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::NotFound => {
- let wrong_path =
- format!("Path \"{}\" does not exist", path_from.to_str().unwrap());
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::NotFound => {
+ let wrong_path = format!("Path \"{}\" does not exist", path_from.to_str().unwrap());
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
fs_extra::file::write_all(&file2_path, &content2).unwrap();
assert!(file2_path.exists());
-
let mut exist_path = path_to.clone();
exist_path.push(&test_name);
create(&exist_path, true).unwrap();
fs_extra::file::write_all(&exist_path, exist_content).unwrap();
assert!(exist_path.exists());
-
let mut options = CopyOptions::new();
options.overwrite = true;
move_dir(&path_from, &path_to, &options).unwrap();
let options = CopyOptions::new();
match move_dir(&path_from, &path_to, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::AlreadyExists => {
- let wrong_path = format!("Path \"{}\" is exist", exist_path.to_str().unwrap());
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::AlreadyExists => {
+ let wrong_path = format!("Path \"{}\" is exist", exist_path.to_str().unwrap());
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
fs_extra::file::write_all(&file2_path, &content2).unwrap();
assert!(file2_path.exists());
-
let mut exist_path = path_to.clone();
exist_path.push(&test_name);
create(&exist_path, true).unwrap();
fs_extra::file::write_all(&exist_path, exist_content).unwrap();
assert!(exist_path.exists());
-
let mut options = CopyOptions::new();
options.skip_exist = true;
move_dir(&path_from, &path_to, &options).unwrap();
fs_extra::file::write_all(&file2_path, &content2).unwrap();
assert!(file2_path.exists());
-
let mut exist_path = path_to.clone();
exist_path.push(&test_name);
create(&exist_path, true).unwrap();
fs_extra::file::write_all(&exist_path, exist_content).unwrap();
assert!(exist_path.exists());
-
let mut options = CopyOptions::new();
options.overwrite = true;
options.skip_exist = true;
#[test]
fn it_move_inside_work_target_dir_exist_with_no_source_dir_named_sub_dir() {
let path_root = Path::new(TEST_FOLDER);
- let root = path_root.join(
- "it_move_inside_work_target_dir_exist_with_no_source_dir_named_sub_dir",
- );
+ let root =
+ path_root.join("it_move_inside_work_target_dir_exist_with_no_source_dir_named_sub_dir");
let root_dir1 = root.join("dir1");
let root_dir1_sub = root_dir1.join("sub");
let root_dir2 = root.join("dir2");
let mut options = CopyOptions::new();
options.copy_inside = true;
match copy(&root_dir1, &root_dir2, &options) {
- Err(err) => {
- match err.kind {
- ErrorKind::AlreadyExists => {
- assert_eq!(1, 1);
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::AlreadyExists => {
+ assert_eq!(1, 1);
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
assert!(root_dir2_dir1_sub_file2.exists());
assert!(root_dir2_dir3_file3.exists());
}
+#[test]
+fn it_move_content_only_option() {
+ let test_dir = Path::new(TEST_FOLDER).join("it_move_content_only_option");
+ let path_to = test_dir.join("out");
+ let d_level_1 = (test_dir.join("d_level_1"), path_to.clone());
+ let d_level_2 = (d_level_1.0.join("d_level_2"), d_level_1.1.join("d_level_2"));
+ let d_level_3 = (d_level_2.0.join("d_level_3"), d_level_2.1.join("d_level_3"));
+
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+
+ create_all(&d_level_1.0, true).unwrap();
+ create_all(&d_level_2.0, true).unwrap();
+ create_all(&d_level_3.0, true).unwrap();
+ create_all(&path_to, true).unwrap();
+
+ assert!(path_to.exists());
+ assert!(d_level_1.0.exists());
+ assert!(d_level_2.0.exists());
+ assert!(d_level_3.0.exists());
+
+ assert!(!d_level_2.1.exists());
+ assert!(!d_level_3.1.exists());
+
+ fs_extra::file::write_all(&file1.0, "content1").unwrap();
+ fs_extra::file::write_all(&file2.0, "content2").unwrap();
+ fs_extra::file::write_all(&file3.0, "content3").unwrap();
+
+ assert!(file1.0.exists());
+ assert!(file2.0.exists());
+ assert!(file3.0.exists());
+
+ assert!(!file1.1.exists());
+ assert!(!file2.1.exists());
+ assert!(!file3.1.exists());
+ let mut options = CopyOptions::new();
+ options.content_only = true;
+ let result = move_dir(&d_level_1.0, path_to, &options).unwrap();
+
+ assert_eq!(24, result);
+
+ assert!(!d_level_1.0.exists());
+ assert!(!d_level_2.0.exists());
+ assert!(!d_level_3.0.exists());
+
+ assert!(d_level_1.1.exists());
+ assert!(d_level_2.1.exists());
+ assert!(d_level_3.1.exists());
+
+ assert!(!file1.0.exists());
+ assert!(!file2.0.exists());
+ assert!(!file3.0.exists());
+
+ assert!(file1.1.exists());
+ assert!(file2.1.exists());
+ assert!(file3.1.exists());
+}
#[test]
fn it_move_progress_work() {
let mut path_from = PathBuf::from(TEST_FOLDER);
Ok(_) => {}
Err(err) => panic!(err),
}
-
}
#[test]
TransitProcessResult::ContinueOrAbort
};
-
match move_dir_with_progress(&path_from, &path_to, &options, func_test) {
- Err(err) => {
- match err.kind {
- ErrorKind::InvalidFolder => {
- let wrong_path = format!(
- "Path \"{}\" is not a directory!",
- path_from.to_str().unwrap()
- );
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!("wrong error");
- }
+ Err(err) => match err.kind {
+ ErrorKind::InvalidFolder => {
+ let wrong_path = format!(
+ "Path \"{}\" is not a directory!",
+ path_from.to_str().unwrap()
+ );
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!("wrong error");
+ }
+ },
Ok(_) => {
panic!("should be error");
}
}
}
-
#[test]
fn it_move_with_progress_work_dif_buf_size() {
let mut path_from = PathBuf::from(TEST_FOLDER);
Ok(_) => {}
Err(err) => panic!(err),
}
-
}).join();
for i in 1..9 {
TransitProcessResult::ContinueOrAbort
};
-
match move_dir_with_progress(&path_from, &path_to, &options, func_test) {
- Err(err) => {
- match err.kind {
- ErrorKind::NotFound => {
- let wrong_path = format!(
- "Path \"{}\" does not exist or you don't \
- have access!",
- path_from.to_str().unwrap()
- );
- assert_eq!(wrong_path, err.to_string());
- }
- _ => {
- panic!(format!("wrong error {}", err.to_string()));
- }
+ Err(err) => match err.kind {
+ ErrorKind::NotFound => {
+ let wrong_path = format!(
+ "Path \"{}\" does not exist or you don't \
+ have access!",
+ path_from.to_str().unwrap()
+ );
+ assert_eq!(wrong_path, err.to_string());
}
- }
+ _ => {
+ panic!(format!("wrong error {}", err.to_string()));
+ }
+ },
Ok(_) => {
panic!("should be error");
}
match rx.recv() {
Err(_) => {}
_ => panic!("should be error"),
-
}
}
assert_eq!(23, result);
assert!(path_to.exists());
assert!(!path_from.exists());
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
}
rx.recv().unwrap();
-
}
#[test]
let result = move_dir_with_progress(&path_from, &path_to, &options, func_test);
match result {
Ok(_) => panic!("Should be error!"),
- Err(err) => {
- match err.kind {
- ErrorKind::AlreadyExists => {}
- _ => panic!("Wrong wrror"),
- }
- }
+ Err(err) => match err.kind {
+ ErrorKind::AlreadyExists => {}
+ _ => panic!("Wrong wrror"),
+ },
}
-
}).join();
match result {
Err(_) => {
panic!("Error not should be!");
}
- _ => {}
-
+ _ => {}
}
}
assert_eq!(0, result);
assert!(path_from.exists());
assert!(path_to.exists());
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
match rx.recv() {
Err(_) => {}
_ => panic!("should be error"),
-
}
-
}
-
#[test]
fn it_move_with_progress_exist_overwrite_and_skip_exist() {
let mut path_from = PathBuf::from(TEST_FOLDER);
options.skip_exist = true;
let (tx, rx) = mpsc::channel();
let result = thread::spawn(move || {
-
let func_test = |process_info: TransitProcess| {
tx.send(process_info).unwrap();
TransitProcessResult::ContinueOrAbort
assert_eq!(23, result);
assert!(path_to.exists());
assert!(!path_from.exists());
-
}).join();
-
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
}
rx.recv().unwrap();
-
}
-
#[test]
fn it_get_folder_size() {
let mut path = PathBuf::from(TEST_FOLDER);
match get_size(&path) {
Ok(_) => panic!("Should be a error!"),
- Err(err) => {
- match err.kind {
- ErrorKind::NotFound => {}
- _ => panic!("Wrong error!"),
- }
- }
+ Err(err) => match err.kind {
+ ErrorKind::NotFound => {}
+ _ => panic!("Wrong error!"),
+ },
};
-
}
-
#[test]
fn it_get_dir_content() {
let mut path = PathBuf::from(TEST_FOLDER);
}
}
assert!(directories_correct);
- assert!(result.directories.contains(
- &file1.parent().unwrap().to_str().unwrap().to_string(),
- ));
- assert!(result.directories.contains(
- &file2.parent().unwrap().to_str().unwrap().to_string(),
- ));
+ assert!(
+ result
+ .directories
+ .contains(&file1.parent().unwrap().to_str().unwrap().to_string(),)
+ );
+ assert!(
+ result
+ .directories
+ .contains(&file2.parent().unwrap().to_str().unwrap().to_string(),)
+ );
// fourth level
options.depth = 4;
assert!(result.files.contains(&file3.to_str().unwrap().to_string()));
assert!(result.files.contains(&file4.to_str().unwrap().to_string()));
-
directories_correct = true;
for dir in &result.directories {
if !directories.contains(&dir) {
}
}
assert!(directories_correct);
- assert!(result.directories.contains(
- &file1.parent().unwrap().to_str().unwrap().to_string(),
- ));
- assert!(result.directories.contains(
- &file2.parent().unwrap().to_str().unwrap().to_string(),
- ));
- assert!(result.directories.contains(
- &file3.parent().unwrap().to_str().unwrap().to_string(),
- ));
- assert!(result.directories.contains(
- &file4.parent().unwrap().to_str().unwrap().to_string(),
- ));
- assert!(result.directories.contains(
- &file5.parent().unwrap().to_str().unwrap().to_string(),
- ));
+ assert!(
+ result
+ .directories
+ .contains(&file1.parent().unwrap().to_str().unwrap().to_string(),)
+ );
+ assert!(
+ result
+ .directories
+ .contains(&file2.parent().unwrap().to_str().unwrap().to_string(),)
+ );
+ assert!(
+ result
+ .directories
+ .contains(&file3.parent().unwrap().to_str().unwrap().to_string(),)
+ );
+ assert!(
+ result
+ .directories
+ .contains(&file4.parent().unwrap().to_str().unwrap().to_string(),)
+ );
+ assert!(
+ result
+ .directories
+ .contains(&file5.parent().unwrap().to_str().unwrap().to_string(),)
+ );
}
-
-
#[test]
fn it_get_dir_content_path_file() {
let mut path = PathBuf::from(TEST_FOLDER);
assert!(!path.exists());
-
match get_dir_content(&path) {
Ok(_) => panic!("Should be a error!"),
- Err(err) => {
- match err.kind {
- ErrorKind::NotFound => {}
- _ => panic!("Wrong error!"),
- }
- }
+ Err(err) => match err.kind {
+ ErrorKind::NotFound => {}
+ _ => panic!("Wrong error!"),
+ },
}
-
}
#[test]
}
assert_eq!(10, fields);
-
}
#[test]
}
assert_eq!(11, fields);
-
}
#[test]
assert_eq!(0, size);
}
}
-
}
#[test]
assert_eq!(7, size);
}
}
-
}
-
#[test]
fn it_ls() {
let test_dir = Path::new(TEST_FOLDER).join("it_ls");
assert_eq!(false, is_dir);
}
}
-
-
}
-
}
-
#[test]
fn it_copy_with_progress_exist_user_decide_overwrite() {
let test_dir = Path::new(TEST_FOLDER).join("it_copy_with_progress_exist_user_decide_overwrite");
let file1 = (dir.0.join("file1.txt"), dir.1.join("file1.txt"));
let file2 = (dir.0.join("file2.txt"), dir.1.join("file2.txt"));
-
create_all(&dir.0, true).unwrap();
create_all(&dir.1, true).unwrap();
-
assert!(&dir.0.exists());
assert!(&dir.1.exists());
assert!(dir.0.exists());
assert!(dir.1.exists());
assert!(compare_dir(&dir.0, &out));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
rx.try_recv().unwrap();
}
-
#[test]
fn it_copy_with_progress_exist_user_decide_overwrite_all() {
let test_dir =
let file1 = (dir.0.join("file1.txt"), dir.1.join("file1.txt"));
let file2 = (dir.0.join("file2.txt"), dir.1.join("file2.txt"));
-
create_all(&dir.0, true).unwrap();
create_all(&dir.1, true).unwrap();
-
assert!(&dir.0.exists());
assert!(&dir.1.exists());
assert!(dir.0.exists());
assert!(dir.1.exists());
assert!(compare_dir(&dir.0, &out));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
let file1 = (dir.0.join("file1.txt"), dir.1.join("file1.txt"));
let file2 = (dir.0.join("file2.txt"), dir.1.join("file2.txt"));
-
create_all(&dir.0, true).unwrap();
create_all(&dir.1, true).unwrap();
-
assert!(&dir.0.exists());
assert!(&dir.1.exists());
assert!(dir.0.exists());
assert!(dir.1.exists());
assert!(!compare_dir(&dir.0, &out));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
assert!(dir.0.exists());
assert!(dir.1.exists());
assert!(!compare_dir(&dir.0, &out));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
assert!(dir.0.exists());
assert!(dir.1.exists());
assert!(!compare_dir(&dir.0, &out));
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
assert!(root_dir1_sub.exists());
assert!(root_dir2.exists());
assert!(compare_dir_recursively(&root_dir1, &root_dir2));
-
}).join();
loop {
#[test]
fn it_copy_with_progress_inside_overwrite_work_target_dir_exist_with_source_dir_exist() {
let path_root = Path::new(TEST_FOLDER);
- let root = path_root.join(
- "it_copy_with_progress_inside_overwrite_work_target_dir_exist_with_source_dir_exist",
- );
+ let root = path_root
+ .join("it_copy_with_progress_inside_overwrite_work_target_dir_exist_with_source_dir_exist");
let root_dir1 = root.join("dir1");
let root_dir1_sub = root_dir1.join("sub");
let root_dir2 = root.join("dir2");
let file1 = (dir.0.join("file1.txt"), dir.1.join("file1.txt"));
let file2 = (dir.0.join("file2.txt"), dir.1.join("file2.txt"));
-
create_all(&dir.0, true).unwrap();
create_all(&dir.1, true).unwrap();
-
assert!(&dir.0.exists());
assert!(&dir.1.exists());
assert_eq!(16, result);
assert!(!dir.0.exists());
assert!(dir.1.exists());
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
rx.try_recv().unwrap();
}
-
#[test]
fn it_move_with_progress_exist_user_decide_overwrite_all() {
let test_dir =
let file1 = (dir.0.join("file1.txt"), dir.1.join("file1.txt"));
let file2 = (dir.0.join("file2.txt"), dir.1.join("file2.txt"));
-
create_all(&dir.0, true).unwrap();
create_all(&dir.1, true).unwrap();
-
assert!(&dir.0.exists());
assert!(&dir.1.exists());
assert_eq!(16, result);
assert!(!dir.0.exists());
assert!(dir.1.exists());
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
let file1 = (dir.0.join("file1.txt"), dir.1.join("file1.txt"));
let file2 = (dir.0.join("file2.txt"), dir.1.join("file2.txt"));
-
create_all(&dir.0, true).unwrap();
create_all(&dir.1, true).unwrap();
-
assert!(&dir.0.exists());
assert!(&dir.1.exists());
assert_eq!(0, result);
assert!(dir.0.exists());
assert!(dir.1.exists());
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
assert_eq!(0, result);
assert!(dir.0.exists());
assert!(dir.1.exists());
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
assert_eq!(0, result);
assert!(dir.0.exists());
assert!(dir.1.exists());
-
}).join();
-
match result {
Ok(_) => {}
Err(err) => panic!(err),
Err(err) => panic!(err),
}
}
+#[test]
+fn it_move_with_progress_content_only_option() {
+ let test_dir = Path::new(TEST_FOLDER).join("it_move_with_progress_content_only_option");
+ let path_to = test_dir.join("out");
+ let d_level_1 = (test_dir.join("d_level_1"), path_to.clone());
+ let d_level_2 = (d_level_1.0.join("d_level_2"), d_level_1.1.join("d_level_2"));
+ let d_level_3 = (d_level_2.0.join("d_level_3"), d_level_2.1.join("d_level_3"));
+
+ let file1 = (d_level_1.0.join("file1.txt"), d_level_1.1.join("file1.txt"));
+ let file2 = (d_level_2.0.join("file2.txt"), d_level_2.1.join("file2.txt"));
+ let file3 = (d_level_3.0.join("file3.txt"), d_level_3.1.join("file3.txt"));
+
+ create_all(&d_level_1.0, true).unwrap();
+ create_all(&d_level_2.0, true).unwrap();
+ create_all(&d_level_3.0, true).unwrap();
+ create_all(&path_to, true).unwrap();
+
+ assert!(path_to.exists());
+ assert!(d_level_1.0.exists());
+ assert!(d_level_2.0.exists());
+ assert!(d_level_3.0.exists());
+
+ assert!(!d_level_2.1.exists());
+ assert!(!d_level_3.1.exists());
+
+ fs_extra::file::write_all(&file1.0, "content1").unwrap();
+ fs_extra::file::write_all(&file2.0, "content2").unwrap();
+ fs_extra::file::write_all(&file3.0, "content3").unwrap();
+
+ assert!(file1.0.exists());
+ assert!(file2.0.exists());
+ assert!(file3.0.exists());
+
+ assert!(!file1.1.exists());
+ assert!(!file2.1.exists());
+ assert!(!file3.1.exists());
+
+ let mut options = CopyOptions::new();
+ options.content_only = true;
+ let (tx, rx) = mpsc::channel();
+ let result = thread::spawn(move || {
+ let func_test = |process_info: TransitProcess| {
+ tx.send(process_info).unwrap();
+ TransitProcessResult::ContinueOrAbort
+ };
+
+ let result = move_dir_with_progress(&d_level_1.0, &path_to, &options, func_test).unwrap();
+
+ assert_eq!(24, result);
+
+ assert!(!d_level_1.0.exists());
+ assert!(!d_level_2.0.exists());
+ assert!(!d_level_3.0.exists());
+
+ assert!(d_level_1.1.exists());
+ assert!(d_level_2.1.exists());
+ assert!(d_level_3.1.exists());
+
+ assert!(!file1.0.exists());
+ assert!(!file2.0.exists());
+ assert!(!file3.0.exists());
+
+ assert!(file1.1.exists());
+ assert!(file2.1.exists());
+ assert!(file3.1.exists());
+ }).join();
+
+ match result {
+ Ok(_) => {}
+ Err(err) => panic!(err),
+ }
+
+ match rx.recv() {
+ Err(_) => panic!("Errors should not be!"),
+ _ => {}
+ }
+}