initial commit
This commit is contained in:
BIN
tests/expected/simple.tar
Normal file
BIN
tests/expected/simple.tar
Normal file
Binary file not shown.
78
tests/pack.rs
Normal file
78
tests/pack.rs
Normal file
@ -0,0 +1,78 @@
|
||||
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");
|
||||
}
|
BIN
tests/samples/simple.tar.bz2
Normal file
BIN
tests/samples/simple.tar.bz2
Normal file
Binary file not shown.
BIN
tests/samples/simple.tar.gz
Normal file
BIN
tests/samples/simple.tar.gz
Normal file
Binary file not shown.
1
tests/samples/simple/a.txt
Normal file
1
tests/samples/simple/a.txt
Normal file
@ -0,0 +1 @@
|
||||
this is file A
|
1
tests/samples/simple/b.txt
Normal file
1
tests/samples/simple/b.txt
Normal file
@ -0,0 +1 @@
|
||||
this is file B
|
BIN
tests/samples/symlinks.tar.gz
Normal file
BIN
tests/samples/symlinks.tar.gz
Normal file
Binary file not shown.
1
tests/samples/symlinks/one.txt
Normal file
1
tests/samples/symlinks/one.txt
Normal file
@ -0,0 +1 @@
|
||||
this is file one
|
1
tests/samples/symlinks/somedir/two.txt
Normal file
1
tests/samples/symlinks/somedir/two.txt
Normal file
@ -0,0 +1 @@
|
||||
this is file two
|
1
tests/samples/symlinks/two.txt
Symbolic link
1
tests/samples/symlinks/two.txt
Symbolic link
@ -0,0 +1 @@
|
||||
somedir/two.txt
|
50
tests/unpack.rs
Normal file
50
tests/unpack.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tempdir::TempDir;
|
||||
|
||||
#[test]
|
||||
fn test_unpack_simple() {
|
||||
let unpack_dir = TempDir::new("ark_unpack").unwrap();
|
||||
ark::unpack("tests/samples/simple.tar.gz", unpack_dir.path()).unwrap();
|
||||
let unpack_path = unpack_dir.path().join("tests/samples/simple");
|
||||
compare_trees("tests/samples/simple".into(), unpack_path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unpack_symlinks() {
|
||||
let unpack_dir = TempDir::new("ark_unpack").unwrap();
|
||||
ark::unpack("tests/samples/symlinks.tar.gz", unpack_dir.path()).unwrap();
|
||||
let unpack_path = unpack_dir.path().join("tests/samples/symlinks");
|
||||
compare_trees("tests/samples/symlinks".into(), unpack_path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unpack_bz2() {
|
||||
let unpack_dir = TempDir::new("ark_unpack").unwrap();
|
||||
ark::unpack("tests/samples/simple.tar.bz2", unpack_dir.path()).unwrap();
|
||||
let unpack_path = unpack_dir.path().join("tests/samples/simple");
|
||||
compare_trees("tests/samples/simple".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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user