Combine multiple tests to avoid io collisions when tests run in parallel

This commit is contained in:
Nathan Fisher 2023-12-18 00:09:26 -05:00
parent 71e0ba30d1
commit 03486321ed

View File

@ -374,11 +374,27 @@ mod tests {
} }
#[test] #[test]
fn from_file_path() { fn from_fifo_path() {
let _res = remove_file("test/fifo");
nix::mkfifo("test/fifo", 0o644).unwrap();
let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/fifo", Algorithm::Skip, &links).unwrap();
let FileType::Fifo = node.filetype else {
eprintln!("Incorrect filetype: {:?}", node.filetype);
panic!();
};
}
#[test]
fn file_node() {
{
let fd = fs::File::create("test/li.node").unwrap();
let mut writer = BufWriter::new(fd);
let links = Mutex::new(HashMap::new()); let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap(); let node = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap();
let FileType::Normal(f) = node.filetype else { let FileType::Normal(ref f) = node.filetype else {
panic!() eprintln!("Created wrong filetype: {:?}", node.filetype);
panic!();
}; };
assert_eq!(f.len, 1005); assert_eq!(f.len, 1005);
let Checksum::Sha256(sum) = f.checksum else { let Checksum::Sha256(sum) = f.checksum else {
@ -392,57 +408,6 @@ mod tests {
s, s,
"5f1b6e6e31682fb6683db2e78db11e624527c897618f1a5b0a0b5256f557c22d" "5f1b6e6e31682fb6683db2e78db11e624527c897618f1a5b0a0b5256f557c22d"
); );
}
#[test]
fn from_symlink_path() {
let _res = remove_file("test/lilnk.txt");
symlink("li.txt", "test/lilnk.txt").unwrap();
let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/lilnk.txt", Algorithm::Skip, &links).unwrap();
let FileType::SoftLink(tgt) = node.filetype else {
eprintln!("Incorrect filetype: {:?}", node.filetype);
panic!();
};
assert_eq!(tgt, "li.txt");
}
#[test]
fn from_hardlink_path() {
let _res = remove_file("test/lorem.txt");
fs::hard_link("test/li.txt", "test/lorem.txt").unwrap();
let links = Mutex::new(HashMap::new());
let _node0 = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap();
let node1 = Node::from_path("test/lorem.txt", Algorithm::Sha256, &links).unwrap();
match node1.filetype {
FileType::HardLink(s) => assert_eq!(s, "test/li.txt"),
_ => panic!(),
}
}
#[test]
fn from_fifo_path() {
let _res = remove_file("test/fifo");
nix::mkfifo("test/fifo", 0o644).unwrap();
let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/fifo", Algorithm::Skip, &links).unwrap();
let FileType::Fifo = node.filetype else {
eprintln!("Incorrect filetype: {:?}", node.filetype);
panic!();
};
}
#[test]
fn load_store_file() {
{
let fd = fs::File::create("test/li.node").unwrap();
let mut writer = BufWriter::new(fd);
let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/li.txt", Algorithm::Sha1, &links).unwrap();
let FileType::Normal(_) = node.filetype else {
eprintln!("Created wrong filetype: {:?}", node.filetype);
panic!();
};
node.write(&mut writer).unwrap(); node.write(&mut writer).unwrap();
} }
let meta = fs::metadata("test/li.txt").unwrap(); let meta = fs::metadata("test/li.txt").unwrap();
@ -453,27 +418,35 @@ mod tests {
assert_eq!(meta.uid(), node.uid); assert_eq!(meta.uid(), node.uid);
assert_eq!(meta.gid(), node.gid); assert_eq!(meta.gid(), node.gid);
assert_eq!(meta.mtime(), node.mtime as i64); assert_eq!(meta.mtime(), node.mtime as i64);
let FileType::Normal(f) = node.filetype else { let FileType::Normal(ref f) = node.filetype else {
eprintln!("Read incorrect filetype: {:?}", node.filetype); eprintln!("Read incorrect filetype: {:?}", node.filetype);
panic!() panic!()
}; };
let Checksum::Sha1(sum) = f.checksum else { let Checksum::Sha256(sum) = f.checksum else {
panic!() panic!()
}; };
let mut s = String::new(); let mut s = String::new();
for c in &sum { for c in &sum {
write!(s, "{c:02x}").unwrap(); write!(s, "{c:02x}").unwrap();
} }
assert_eq!(s, "9bf3e5b5efd22f932e100b86c83482787e82a682"); assert_eq!(
s,
"5f1b6e6e31682fb6683db2e78db11e624527c897618f1a5b0a0b5256f557c22d"
);
assert_eq!(LI, f.data); assert_eq!(LI, f.data);
node.extract(Some("test/output")).unwrap();
let f = fs::read_to_string("test/output/test/li.txt").unwrap();
assert_eq!(f.as_bytes(), LI);
} }
#[test] #[test]
fn load_store_symlink() { fn symlink_node() {
{ {
let _res = remove_file("test/lilnk.txt"); let _res = remove_file("test/lilnk.txt");
symlink("li.txt", "test/lilnk.txt").unwrap(); symlink("li.txt", "test/lilnk.txt").unwrap();
if PathBuf::from("test/lilnk.node").exists() {
let _res = remove_file("test/lilnk.node"); let _res = remove_file("test/lilnk.node");
}
let fd = fs::File::create("test/lilnk.node").unwrap(); let fd = fs::File::create("test/lilnk.node").unwrap();
let mut writer = BufWriter::new(fd); let mut writer = BufWriter::new(fd);
let links = Mutex::new(HashMap::new()); let links = Mutex::new(HashMap::new());
@ -493,40 +466,60 @@ mod tests {
panic!(); panic!();
}; };
assert_eq!(tgt, "li.txt"); assert_eq!(tgt, "li.txt");
node.extract(Some("test/output")).unwrap();
let tgt = fs::read_link("test/output/test/lilnk.txt").unwrap();
assert_eq!(tgt, PathBuf::from("li.txt"));
} }
#[test] #[test]
fn load_store_hardlink() { fn hardlink_node() {
{ {
let _res = remove_file("test/lihlnk.txt"); if PathBuf::from("test/lorem.txt").exists() {
fs::hard_link("test/li.txt", "test/lihlnk.txt").unwrap(); let _res = remove_file("test/lorem.txt");
let _res = remove_file("test/lihlnk.node");
let fd = fs::File::create("test/lihlnk.node").unwrap();
let mut writer = BufWriter::new(fd);
let links = Mutex::new(HashMap::new());
let _node = Node::from_path("test/li.txt", Algorithm::Sha1, &links).unwrap();
let node = Node::from_path("test/lihlnk.txt", Algorithm::Sha1, &links).unwrap();
let FileType::HardLink(ref tgt) = node.filetype else {
eprintln!("Created wrong filetype: {:?}", node.filetype);
panic!();
};
assert_eq!(tgt, "test/li.txt");
node.write(&mut writer).unwrap();
} }
let fd = fs::File::open("test/lihlnk.node").unwrap(); fs::hard_link("test/li.txt", "test/lorem.txt").unwrap();
let mut reader = BufReader::new(fd); if PathBuf::from("test/lorem.node").exists() {
let node = Node::read(&mut reader).unwrap(); let _res = remove_file("test/lorem.node");
let FileType::HardLink(ref tgt) = node.filetype else { }
eprintln!("Read incorrect filetype: {:?}", node.filetype); if PathBuf::from("test/li.node").exists() {
let _res = remove_file("test/li.node");
}
let links = Mutex::new(HashMap::new());
let node0 = Node::from_path("test/li.txt", Algorithm::Sha1, &links).unwrap();
let node1 = Node::from_path("test/lorem.txt", Algorithm::Sha1, &links).unwrap();
let FileType::HardLink(ref tgt) = node1.filetype else {
eprintln!("Created wrong filetype: {:?}", node1.filetype);
panic!(); panic!();
}; };
assert_eq!(tgt, "test/li.txt"); assert_eq!(tgt, "test/li.txt");
let fd = fs::File::create("test/li.node").unwrap();
let mut writer = BufWriter::new(fd);
node0.write(&mut writer).unwrap();
let fd = fs::File::create("test/lorem.node").unwrap();
let mut writer = BufWriter::new(fd);
node1.write(&mut writer).unwrap();
}
let fd = fs::File::open("test/lorem.node").unwrap();
let mut reader = BufReader::new(fd);
let node1 = Node::read(&mut reader).unwrap();
let FileType::HardLink(ref tgt) = node1.filetype else {
eprintln!("Read incorrect filetype: {:?}", node1.filetype);
panic!();
};
assert_eq!(tgt, "test/li.txt");
let fd = fs::File::open("test/li.node").unwrap();
let mut reader = BufReader::new(fd);
let node0 = Node::read(&mut reader).unwrap();
node1.extract(Some("test/output")).unwrap();
node0.extract(Some("test/output")).unwrap();
} }
#[test] #[test]
fn load_store_fifo() { fn load_store_fifo() {
{ {
if PathBuf::from("test/fifo").exists() {
let _res = remove_file("test/fifo"); let _res = remove_file("test/fifo");
}
nix::mkfifo("test/fifo", 0o644).unwrap(); nix::mkfifo("test/fifo", 0o644).unwrap();
let links = Mutex::new(HashMap::new()); let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/fifo", Algorithm::Skip, &links).unwrap(); let node = Node::from_path("test/fifo", Algorithm::Skip, &links).unwrap();
@ -534,7 +527,9 @@ mod tests {
eprintln!("Incorrect filetype: {:?}", node.filetype); eprintln!("Incorrect filetype: {:?}", node.filetype);
panic!(); panic!();
}; };
if PathBuf::from("test/fifo.node").exists() {
let _res = remove_file("test/fifo.node"); let _res = remove_file("test/fifo.node");
}
let fd = fs::File::create("test/fifo.node").unwrap(); let fd = fs::File::create("test/fifo.node").unwrap();
let mut writer = BufWriter::new(fd); let mut writer = BufWriter::new(fd);
node.write(&mut writer).unwrap(); node.write(&mut writer).unwrap();
@ -547,45 +542,4 @@ mod tests {
panic!(); panic!();
}; };
} }
#[test]
fn extract_file() {
let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap();
node.extract(Some("test/output")).unwrap();
let f = fs::read_to_string("test/output/test/li.txt").unwrap();
assert_eq!(f.as_bytes(), LI);
}
#[test]
fn extract_symlink() {
let links = Mutex::new(HashMap::new());
let _res = remove_file("test/lilnk.txt");
symlink("li.txt", "test/lilnk.txt").unwrap();
let node = Node::from_path("test/lilnk.txt", Algorithm::Md5, &links).unwrap();
node.extract(Some("test/output")).unwrap();
let tgt = fs::read_link("test/output/test/lilnk.txt").unwrap();
assert_eq!(tgt, PathBuf::from("li.txt"));
}
#[test]
fn extract_hardlink() {
let links = Mutex::new(HashMap::new());
if !PathBuf::from("test/lihlnk.txt").exists() {
fs::hard_link("test/li.txt", "test/lihlnk.txt").unwrap();
}
let node0 = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap();
let FileType::Normal(_) = node0.filetype else {
eprintln!("Created wrong filetype for node0: {:?}", node0.filetype);
panic!();
};
let node1 = Node::from_path("test/lihlnk.txt", Algorithm::Sha256, &links).unwrap();
let FileType::HardLink(ref tgt) = node1.filetype else {
eprintln!("Created wrong filetype for node1: {:?}", node1.filetype);
panic!();
};
assert_eq!(tgt, "test/li.txt");
node1.extract(Some("test/output")).unwrap();
node0.extract(Some("test/output")).unwrap();
}
} }