use std::collections::HashSet;
use std::fs::{DirBuilder, File};
use std::io::{self, Write};
use std::path::Path;
use std::process::Command;
use tempdir::TempDir;
use duplicate_destroyer;
use duplicate_destroyer::DuplicateObject;
fn write_file(path: &Path, contents: &str) -> io::Result<()> {
let mut file = File::create(path).expect("Could not create a file.");
writeln!(file, "{}", contents)?;
Ok(())
}
#[test]
fn duplicate_dirs_test() -> io::Result<()> {
let tmp_dir = TempDir::new("add_directories_success_test").expect("Failed creating temp dir.");
let tmp_dir_str = tmp_dir.path().to_owned().into_os_string();
let tmp_dir_path = tmp_dir.path();
for topdir in ["A", "B"] {
DirBuilder::new().create(tmp_dir_path.join(topdir))?;
let a_file_path = tmp_dir_path.join(topdir).join("a.txt");
write_file(&a_file_path, "test_text_a")?;
let b_folder_path = tmp_dir_path.join(topdir).join("b");
DirBuilder::new().create(&b_folder_path)?;
write_file(&b_folder_path.join("alpha.txt"), "test_text_alpha")?;
write_file(&b_folder_path.join("beta.txt"), "test_text_beta")?;
}
let mut options: duplicate_destroyer::Config = Default::default();
options.set_minimum_size(0);
let paths = vec![tmp_dir_str.clone()];
let duplicates = duplicate_destroyer::get_duplicates(paths, &options);
let expected_duplicate = DuplicateObject::new(
8235,
HashSet::from([
tmp_dir_path.join("A").into_os_string(),
tmp_dir_path.join("B").into_os_string(),
]),
);
assert_eq!(Ok(vec![expected_duplicate]), duplicates);
tmp_dir.close()?;
Ok(())
}
#[test]
fn duplicate_files_test() -> io::Result<()>{
let tmp_dir = TempDir::new("add_directories_success_test").expect("Failed creating temp dir.");
let tmp_dir_str = tmp_dir.path().to_owned().into_os_string();
let tmp_dir_path = tmp_dir.path();
for topdir in ["A", "B"] {
DirBuilder::new().create(tmp_dir_path.join(topdir))?;
let a_file_path = tmp_dir_path.join(topdir).join("a.txt");
write_file(&a_file_path, "test_text_a")?;
let differing_file_path = tmp_dir_path.join(topdir).join("diff.txt");
write_file(&differing_file_path, &["test_text_", topdir].join(""))?;
}
let mut options: duplicate_destroyer::Config = Default::default();
options.set_minimum_size(0);
let paths = vec![tmp_dir_str.clone()];
let duplicates = duplicate_destroyer::get_duplicates(paths, &options);
let expected_duplicate = DuplicateObject::new(
12,
HashSet::from([
tmp_dir_path.join("A/a.txt").into_os_string(),
tmp_dir_path.join("B/a.txt").into_os_string(),
]),
);
assert_eq!(Ok(vec![expected_duplicate]), duplicates);
tmp_dir.close()?;
Ok(())
}
#[test]
fn contained_duplicate_test() -> io::Result<()> {
let tmp_dir = TempDir::new("add_directories_success_test").expect("Failed creating temp dir.");
let tmp_dir_path = tmp_dir.path();
for topdir in ["A", "B", "C"] {
DirBuilder::new().create(tmp_dir_path.join(topdir))?;
let a_file_path = tmp_dir_path.join(topdir).join("a.txt");
write_file(&a_file_path, "test_text_a")?;
let b_folder_path = tmp_dir_path.join(topdir).join("b");
DirBuilder::new().create(&b_folder_path)?;
write_file(&b_folder_path.join("alpha.txt"), "test_text_alpha")?;
write_file(&b_folder_path.join("beta.txt"), "test_text_beta")?;
}
let diff_file_path = tmp_dir_path.join("C").join("diff.txt");
write_file(&diff_file_path, "test_text_diff")?;
let mut options: duplicate_destroyer::Config = Default::default();
options.set_minimum_size(0);
let mut paths = vec![
tmp_dir_path.join("A").to_owned().into_os_string(),
tmp_dir_path.join("B").to_owned().into_os_string(),
tmp_dir_path.join("C").to_owned().into_os_string(),
];
let expected_duplicate = DuplicateObject::new(
8235,
HashSet::from([
tmp_dir_path.join("A").into_os_string(),
tmp_dir_path.join("B").into_os_string(),
]),
);
let duplicates = duplicate_destroyer::get_duplicates(paths.clone(), &options);
assert_eq!(Ok(vec![expected_duplicate.clone()]), duplicates);
paths.reverse();
let reversed_duplicates = duplicate_destroyer::get_duplicates(paths, &options);
assert_eq!(Ok(vec![expected_duplicate]), reversed_duplicates);
tmp_dir.close()?;
Ok(())
}