From 389e33e512474391b178e0dd26f2ec778e49e66a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Fri, 4 Jul 2025 23:55:26 +0000 Subject: [PATCH] [wip] unit tests for `file` module - testing for `Checked` - better place for test-only utilities --- src/file/checked.rs | 12 ++++++++---- src/file/mod.rs | 2 +- src/lib.rs | 23 +---------------------- src/sharry/ids.rs | 2 +- src/sharry/uri.rs | 2 +- src/test_util/mod.rs | 23 +++++++++++++++++++++++ 6 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 src/test_util/mod.rs diff --git a/src/file/checked.rs b/src/file/checked.rs index 9015101..8bbe215 100644 --- a/src/file/checked.rs +++ b/src/file/checked.rs @@ -66,12 +66,12 @@ impl Checked { /// - Mismatch if file already hashed /// /// TODO this could use an error variant like `IllegalInvocation` - pub fn hash(&mut self, f: impl Fn(u64)) -> crate::Result<()> { + pub fn hash(&mut self, on_progress: impl FnMut(u64)) -> crate::Result<()> { if self.hash.is_some() { return Err(crate::Error::mismatch("unhashed file", self.path.display())); } - self.hash = Some(super::compute_hash(&self.path, self.size, f)?); + self.hash = Some(super::compute_hash(&self.path, self.size, on_progress)?); Ok(()) } @@ -115,11 +115,13 @@ impl FileTrait for Checked { #[cfg(test)] mod tests { + // tests for `Checked::start_upload` omitted, as they require a `sharry::Client` + use tempfile::TempDir; use crate::{ - create_file, file::tests::{CASES, HASHES}, + test_util::create_file, }; use super::*; @@ -128,12 +130,13 @@ mod tests { 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 path = file .path() .canonicalize() .expect("the file should have a canonical path"); - let chk = Checked::new(file.path()).expect("creating `Checked` should succeed"); assert_eq!(chk.path, path); assert_eq!(chk.size, size); assert!(chk.hash.is_none()); @@ -182,6 +185,7 @@ mod tests { let mut chk = Checked::new(file.path()).expect("creating `Checked` should succeed"); chk.hash(drop).expect("`hash` should succeed"); + // `FileTrait` chk.check_hash(drop).expect("`check_hash` should succeed"); assert_eq!(chk.hash, Some(hash.to_string())); diff --git a/src/file/mod.rs b/src/file/mod.rs index b8dd45b..3d42b8a 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -124,7 +124,7 @@ pub trait FileTrait { #[cfg(test)] mod tests { - use crate::create_file; + use crate::test_util::create_file; use super::*; diff --git a/src/lib.rs b/src/lib.rs index 2efbcd5..6dc030f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,29 +9,8 @@ mod file; mod impl_ureq; pub mod output; mod sharry; +mod test_util; pub use appstate::AppState; pub use cli::Cli; pub use error::{Error, Parameter, Result}; - -#[cfg(test)] -pub fn check_trait(expected: &E, actual: &A, tr: &'static str, ty: &'static str) -where - E: std::fmt::Debug + PartialEq, - A: std::fmt::Debug, -{ - assert_eq!( - expected, actual, - "`impl {tr} for {ty}` expected: {expected:?}, actual: {actual:?}", - ); -} - -/// Helper to create a temp file from `data` -#[cfg(test)] -fn create_file(data: &[u8]) -> tempfile::NamedTempFile { - use std::io::Write; - - let mut tmp = tempfile::NamedTempFile::new().expect("creating temp file"); - tmp.write_all(data).expect("writing to tempfile"); - tmp -} diff --git a/src/sharry/ids.rs b/src/sharry/ids.rs index e9eae98..3705dcd 100644 --- a/src/sharry/ids.rs +++ b/src/sharry/ids.rs @@ -102,7 +102,7 @@ impl TryFrom for FileID { #[cfg(test)] mod tests { use super::*; - use crate::check_trait; + use crate::test_util::check_trait; #[test] fn basic_traits_working() { diff --git a/src/sharry/uri.rs b/src/sharry/uri.rs index 20eb1d8..84564f1 100644 --- a/src/sharry/uri.rs +++ b/src/sharry/uri.rs @@ -98,8 +98,8 @@ impl Uri { mod tests { use super::*; use crate::{ - check_trait, sharry::{FileID, ShareID}, + test_util::check_trait, }; #[test] diff --git a/src/test_util/mod.rs b/src/test_util/mod.rs new file mode 100644 index 0000000..3687825 --- /dev/null +++ b/src/test_util/mod.rs @@ -0,0 +1,23 @@ +#![cfg(test)] + +use std::{fmt, io::Write}; + +use tempfile::NamedTempFile; + +pub fn check_trait(expected: &E, actual: &A, tr: &'static str, ty: &'static str) +where + E: fmt::Debug + PartialEq, + A: fmt::Debug, +{ + assert_eq!( + expected, actual, + "`impl {tr} for {ty}` expected: {expected:?}, actual: {actual:?}", + ); +} + +/// Helper to create a temp file from `data` +pub fn create_file(data: &[u8]) -> NamedTempFile { + let mut tmp = NamedTempFile::new().expect("creating temp file"); + tmp.write_all(data).expect("writing to tempfile"); + tmp +}