[wip] unit tests for file module

- testing for `Checked`
- better place for test-only utilities
This commit is contained in:
Jörn-Michael Miehe 2025-07-04 23:55:26 +00:00
parent 1ac110ddfb
commit 389e33e512
6 changed files with 35 additions and 29 deletions

View file

@ -66,12 +66,12 @@ impl Checked {
/// - Mismatch if file already hashed /// - Mismatch if file already hashed
/// ///
/// TODO this could use an error variant like `IllegalInvocation` /// 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() { if self.hash.is_some() {
return Err(crate::Error::mismatch("unhashed file", self.path.display())); 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(()) Ok(())
} }
@ -115,11 +115,13 @@ impl FileTrait for Checked {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// tests for `Checked::start_upload` omitted, as they require a `sharry::Client`
use tempfile::TempDir; use tempfile::TempDir;
use crate::{ use crate::{
create_file,
file::tests::{CASES, HASHES}, file::tests::{CASES, HASHES},
test_util::create_file,
}; };
use super::*; use super::*;
@ -128,12 +130,13 @@ mod tests {
fn new_on_existing_file_works() { fn new_on_existing_file_works() {
for (content, size) in CASES { for (content, size) in CASES {
let file = create_file(content); let file = create_file(content);
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
let path = file let path = file
.path() .path()
.canonicalize() .canonicalize()
.expect("the file should have a canonical path"); .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.path, path);
assert_eq!(chk.size, size); assert_eq!(chk.size, size);
assert!(chk.hash.is_none()); assert!(chk.hash.is_none());
@ -182,6 +185,7 @@ mod tests {
let mut chk = Checked::new(file.path()).expect("creating `Checked` should succeed"); let mut chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
chk.hash(drop).expect("`hash` should succeed"); chk.hash(drop).expect("`hash` should succeed");
// `FileTrait`
chk.check_hash(drop).expect("`check_hash` should succeed"); chk.check_hash(drop).expect("`check_hash` should succeed");
assert_eq!(chk.hash, Some(hash.to_string())); assert_eq!(chk.hash, Some(hash.to_string()));

View file

@ -124,7 +124,7 @@ pub trait FileTrait {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::create_file; use crate::test_util::create_file;
use super::*; use super::*;

View file

@ -9,29 +9,8 @@ mod file;
mod impl_ureq; mod impl_ureq;
pub mod output; pub mod output;
mod sharry; mod sharry;
mod test_util;
pub use appstate::AppState; pub use appstate::AppState;
pub use cli::Cli; pub use cli::Cli;
pub use error::{Error, Parameter, Result}; pub use error::{Error, Parameter, Result};
#[cfg(test)]
pub fn check_trait<E, A>(expected: &E, actual: &A, tr: &'static str, ty: &'static str)
where
E: std::fmt::Debug + PartialEq<A>,
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
}

View file

@ -102,7 +102,7 @@ impl TryFrom<String> for FileID {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::check_trait; use crate::test_util::check_trait;
#[test] #[test]
fn basic_traits_working() { fn basic_traits_working() {

View file

@ -98,8 +98,8 @@ impl Uri {
mod tests { mod tests {
use super::*; use super::*;
use crate::{ use crate::{
check_trait,
sharry::{FileID, ShareID}, sharry::{FileID, ShareID},
test_util::check_trait,
}; };
#[test] #[test]

23
src/test_util/mod.rs Normal file
View file

@ -0,0 +1,23 @@
#![cfg(test)]
use std::{fmt, io::Write};
use tempfile::NamedTempFile;
pub fn check_trait<E, A>(expected: &E, actual: &A, tr: &'static str, ty: &'static str)
where
E: fmt::Debug + PartialEq<A>,
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
}