[wip] unit tests for file module

- testing/coverage for module
This commit is contained in:
Jörn-Michael Miehe 2025-07-12 02:04:30 +00:00
parent 910d95230f
commit 50098edc81

View file

@ -13,6 +13,8 @@ use log::{debug, warn};
pub use uploading::Uploading;
/// how many bytes to hash at once (default: 4 MiB)
///
/// size must never exceed 2 EiB
const HASH_CHUNK_SIZE: usize = 4 * 1024 * 1024;
/// compute hash for a file given its path.
@ -45,7 +47,6 @@ fn compute_hash(path: &Path, size: u64, mut on_progress: impl FnMut(u64)) -> cra
}
hasher.update(&buffer[..n]);
// `buf` size must be < 2 EiB
bytes_read += n as u64;
on_progress(n as u64);
}
@ -124,6 +125,8 @@ pub trait FileTrait {
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::test_util::{
create_file,
data::{DATA_LENGTHS_BAD, HASHES_STD_BAD, HASHES_STD_GOOD, cases, cases_with},
@ -146,6 +149,28 @@ mod tests {
}
}
#[test]
fn compute_hash_nonexistent_file() {
let nex_path = {
// this is deleted at the end of this block, so will stop to exist
let nex_file = create_file(&[]);
nex_file.path().to_owned()
};
let err = compute_hash(&nex_path, 0, drop).unwrap_err();
assert!(matches!(err, crate::Error::StdIo(e) if e.kind() == std::io::ErrorKind::NotFound));
}
#[test]
fn compute_hash_directory() {
let dir = TempDir::new().unwrap();
let err = compute_hash(dir.path(), 0, drop).unwrap_err();
assert!(
matches!(err, crate::Error::StdIo(e) if e.kind() == std::io::ErrorKind::IsADirectory)
);
}
#[test]
fn hash_size_mismatch() {
for (content, good_size, bad_size) in cases_with(DATA_LENGTHS_BAD) {