From 596005067630185e9ed1ae7fad4b6ccde50c7356 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Wed, 16 Sep 2015 23:35:16 +0200 Subject: [PATCH] Added my first test!! :) For copy_files_except_ext function --- src/utils/mod.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a3ed5603..ca1ff1bb 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -136,17 +136,21 @@ pub fn remove_dir_content(dir: &Path) -> Result<(), Box> { /// Copies all files of a directory to another one except the files with the extensions given in the /// `ext_blacklist` array -pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blacklist: &[String]) -> Result<(), Box> { +pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blacklist: &[&str]) -> Result<(), Box> { // Check that from and to are different if from == to { return Ok(()) } - + println!("[*] Loop"); for entry in try!(fs::read_dir(from)) { let entry = try!(entry); + println!("[*] {:?}", entry.path()); let metadata = try!(entry.metadata()); // If the entry is a dir and the recursive option is enabled, call itself if metadata.is_dir() && recursive { + if entry.path() == to.to_path_buf() { continue } + println!("[*] is dir"); + try!(fs::create_dir(&to.join(entry.file_name()))); try!(copy_files_except_ext( &from.join(entry.file_name()), &to.join(entry.file_name()), @@ -157,11 +161,63 @@ pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blackl // Check if it is in the blacklist if let Some(ext) = entry.path().extension() { - if ext_blacklist.contains(&String::from(ext.to_str().unwrap())) { continue } - + if ext_blacklist.contains(&ext.to_str().unwrap()) { continue } + println!("[*] creating path for file: {:?}", &to.join(entry.path().file_name().expect("a file should have a file name..."))); + //try!(create_path(&to.join(entry.path()))); + println!("[*] creating file: {:?}", &to.join(entry.path().file_name().expect("a file should have a file name..."))); try!(fs::copy(entry.path(), &to.join(entry.path().file_name().expect("a file should have a file name...")))); } } } Ok(()) } + + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ + +// tests + +#[cfg(test)] +mod tests { + extern crate tempdir; + + use super::copy_files_except_ext; + use super::PathExt; + use std::fs; + + #[test] + fn copy_files_except_ext_test() { + let tmp = match tempdir::TempDir::new("") { + Ok(t) => t, + Err(_) => panic!("Could not create a temp dir"), + }; + + // Create a couple of files + if let Err(_) = fs::File::create(&tmp.path().join("file.txt")) { panic!("Could not create file.txt") } + if let Err(_) = fs::File::create(&tmp.path().join("file.md")) { panic!("Could not create file.md") } + if let Err(_) = fs::File::create(&tmp.path().join("file.png")) { panic!("Could not create file.png") } + if let Err(_) = fs::create_dir(&tmp.path().join("sub_dir")) { panic!("Could not create sub_dir") } + if let Err(_) = fs::File::create(&tmp.path().join("sub_dir/file.png")) { panic!("Could not create sub_dir/file.png") } + + // Create output dir + if let Err(_) = fs::create_dir(&tmp.path().join("output")) { panic!("Could not create output") } + + match copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, &["md"]) { + Err(e) => panic!("Error while executing the function:\n{:?}", e), + Ok(_) => {}, + } + + // Check if the correct files where created + for entry in fs::read_dir(&tmp.path().join("output")).unwrap() { + println!("{:?}", entry.ok().unwrap().path()) + } + + if !(&tmp.path().join("output/file.txt")).exists() { panic!("output/file.txt should exist") } + if (&tmp.path().join("output/file.md")).exists() { panic!("output/file.md should not exist") } + if !(&tmp.path().join("output/file.png")).exists() { panic!("output/file.png should exist") } + if !(&tmp.path().join("output/sub_dir/file.png")).exists() { panic!("output/sub_dir/file.png should exist") } + + } +}