[wip] unit tests for file module

- testing for `Checked`
This commit is contained in:
Jörn-Michael Miehe 2025-07-10 02:04:29 +00:00
parent cb5873b732
commit b2c032d846

View file

@ -115,20 +115,27 @@ impl FileTrait for Checked {
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use tempfile::{NamedTempFile, TempDir};
use crate::test_util::{
MockClient, create_file,
MockClient, check_trait, create_file,
data::{HASHES_STD_GOOD, cases, data},
};
use super::*;
fn create_checked(content: &[u8]) -> (Checked, NamedTempFile) {
let file = create_file(content);
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
// return both, so the `NamedTempFile` is not auto-deleted here
(chk, file)
}
#[test]
fn new_on_existing_file_works() {
for (content, size) in cases() {
let file = create_file(content);
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
let (chk, file) = create_checked(content);
let path = file
.path()
@ -145,6 +152,19 @@ mod tests {
file.path().file_name().expect("`file_name` should succeed")
);
assert_eq!(chk.get_size(), size);
check_trait(
chk.as_ref(),
path.as_os_str().as_encoded_bytes(),
"AsRef<u8>",
"Checked",
);
// new_direct
let chk = Checked::new_direct(chk.path, chk.size, chk.hash);
assert_eq!(chk.path, path);
assert_eq!(chk.size, size);
assert!(chk.hash.is_none());
}
}
@ -179,8 +199,7 @@ mod tests {
#[test]
fn hashing_works() {
for (content, hash) in data().zip(HASHES_STD_GOOD) {
let file = create_file(content);
let mut chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
let (mut chk, _file) = create_checked(content);
chk.hash(drop).expect("`hash` should succeed");
// `FileTrait`
@ -193,10 +212,10 @@ mod tests {
#[test]
fn hashing_again_errors() {
for content in data() {
let file = create_file(content);
let mut chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
let (mut chk, _file) = create_checked(content);
chk.hash(drop).expect("`hash` should succeed");
// fake hash
chk.hash = Some(String::default());
let err = chk.hash(drop).expect_err("`hash` twice should fail");
assert!(err.is_mismatch("unhashed file", chk.path.display().to_string()));
@ -209,8 +228,7 @@ mod tests {
let share_id = client.add_share();
for content in data() {
let file = create_file(content);
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
let (chk, _file) = create_checked(content);
assert!(
chk.start_upload(