Added my first test!! :) For copy_files_except_ext function
This commit is contained in:
parent
1799ed9ed3
commit
5960050676
|
@ -136,17 +136,21 @@ pub fn remove_dir_content(dir: &Path) -> Result<(), Box<Error>> {
|
||||||
/// Copies all files of a directory to another one except the files with the extensions given in the
|
/// Copies all files of a directory to another one except the files with the extensions given in the
|
||||||
/// `ext_blacklist` array
|
/// `ext_blacklist` array
|
||||||
|
|
||||||
pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blacklist: &[String]) -> Result<(), Box<Error>> {
|
pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blacklist: &[&str]) -> Result<(), Box<Error>> {
|
||||||
|
|
||||||
// Check that from and to are different
|
// Check that from and to are different
|
||||||
if from == to { return Ok(()) }
|
if from == to { return Ok(()) }
|
||||||
|
println!("[*] Loop");
|
||||||
for entry in try!(fs::read_dir(from)) {
|
for entry in try!(fs::read_dir(from)) {
|
||||||
let entry = try!(entry);
|
let entry = try!(entry);
|
||||||
|
println!("[*] {:?}", entry.path());
|
||||||
let metadata = try!(entry.metadata());
|
let metadata = try!(entry.metadata());
|
||||||
|
|
||||||
// If the entry is a dir and the recursive option is enabled, call itself
|
// If the entry is a dir and the recursive option is enabled, call itself
|
||||||
if metadata.is_dir() && recursive {
|
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(
|
try!(copy_files_except_ext(
|
||||||
&from.join(entry.file_name()),
|
&from.join(entry.file_name()),
|
||||||
&to.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
|
// Check if it is in the blacklist
|
||||||
if let Some(ext) = entry.path().extension() {
|
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..."))));
|
try!(fs::copy(entry.path(), &to.join(entry.path().file_name().expect("a file should have a file name..."))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
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") }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue