From 50098edc81455bbc23a17649c73fd4fa682c7ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 12 Jul 2025 02:04:30 +0000 Subject: [PATCH] [wip] unit tests for `file` module - testing/coverage for module --- src/file/mod.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/file/mod.rs b/src/file/mod.rs index 3910991..4fa4b94 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -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) {