Add test for creating, storing and loading hardlink nodes

This commit is contained in:
Nathan Fisher 2023-12-15 19:04:44 -05:00
parent 46c41e1c13
commit d26a5c5805
1 changed files with 30 additions and 1 deletions

View File

@ -313,6 +313,7 @@ impl Node {
#[cfg(test)]
mod tests {
use super::*;
use core::panic;
use std::{fmt::Write, fs::remove_file};
static LI: &[u8] = include_bytes!("../test/li.txt");
@ -456,7 +457,7 @@ mod tests {
assert_eq!(tgt, "li.txt");
node.write(&mut writer).unwrap();
}
let fd = std::fs::File::open("test/lilnk.node").unwrap();
let fd = fs::File::open("test/lilnk.node").unwrap();
let mut reader = BufReader::new(fd);
let node = Node::read(&mut reader).unwrap();
let FileType::SoftLink(ref tgt) = node.filetype else {
@ -466,6 +467,34 @@ mod tests {
assert_eq!(tgt, "li.txt");
}
#[test]
fn load_store_hardlink() {
{
let _res = remove_file("test/lihlnk.txt");
fs::hard_link("test/li.txt", "test/lihlnk.txt").unwrap();
let _res = remove_file("test/lihlnk.node");
let fd = std::fs::File::create("test/lihlnk.node").unwrap();
let mut writer = std::io::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();
let mut reader = BufReader::new(fd);
let node = Node::read(&mut reader).unwrap();
let FileType::HardLink(ref tgt) = node.filetype else {
eprintln!("Read incorrect filetype: {:?}", node.filetype);
panic!();
};
assert_eq!(tgt, "test/li.txt");
}
#[test]
fn extract_file() {
let links = Mutex::new(HashMap::new());