use std::fs; use std::process::Command; use std::path::PathBuf; use tempdir::TempDir; fn verify(targets: &[&str], archive_path: &str, compress_flag: &str) { let unpack_dir = TempDir::new("ark_pack").unwrap(); dbg!(Command::new("tar") .arg(compress_flag) .args(["-xf", archive_path, "-C"]) .arg(unpack_dir.path()) .output() .unwrap()); for target in targets { let unpack_path = unpack_dir.path().join(&target); compare_trees(target.into(), unpack_path); } } fn compare_trees(src: PathBuf, dst: PathBuf) { // we don't want to follow symlinks, because if the symlink points // within the archive then we'll be looking at it anyway, and if // points outside then it's out of scope for us let src_meta = src.symlink_metadata().unwrap(); let dst_meta = dst.symlink_metadata().unwrap(); assert_eq!(src_meta.file_type(), dst_meta.file_type()); assert_eq!(src_meta.permissions(), dst_meta.permissions()); if src_meta.is_symlink() { let src_target = fs::read_link(&src).unwrap(); let dst_target = fs::read_link(&dst).unwrap(); assert_eq!(src_target, dst_target); } // eventually compare content of files here else if src_meta.is_dir() { for entry in fs::read_dir(&src).unwrap() { let src_child = entry.unwrap().path(); let dst_child = dst.join(src_child.file_name().unwrap()); compare_trees(src_child, dst_child); } } } #[test] fn test_pack_simple_files() { let targets = [ "tests/samples/simple/a.txt", "tests/samples/simple/b.txt", ]; let archive_path = "tests/output/pack_simple_files.tar.gz"; ark::pack(&targets, &archive_path).unwrap(); verify(&targets, &archive_path, "-z"); } #[test] fn test_pack_simple_dir() { let targets = ["tests/samples/simple"]; let archive_path = "tests/output/pack_simple_dir.tar.gz"; ark::pack(&targets, &archive_path).unwrap(); verify(&targets, &archive_path, "-z"); } #[test] fn test_pack_symlinks() { let targets = ["tests/samples/symlinks"]; let archive_path = "tests/output/pack_symlinks.tar.gz"; ark::pack(&targets, &archive_path).unwrap(); verify(&targets, &archive_path, "-z"); } #[test] fn test_bz2() { let targets = ["tests/samples/simple"]; let archive_path = "tests/output/pack_simple_dir.tar.bz2"; ark::pack(&targets, &archive_path).unwrap(); verify(&targets, &archive_path, "-j"); }