[wip] unit tests for file module
- testing for `Checked` - better place for test-only utilities
This commit is contained in:
parent
1ac110ddfb
commit
389e33e512
6 changed files with 35 additions and 29 deletions
|
|
@ -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()));
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ pub trait FileTrait {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::create_file;
|
||||
use crate::test_util::create_file;
|
||||
|
||||
use super::*;
|
||||
|
||||
|
|
|
|||
23
src/lib.rs
23
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<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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ impl TryFrom<String> for FileID {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::check_trait;
|
||||
use crate::test_util::check_trait;
|
||||
|
||||
#[test]
|
||||
fn basic_traits_working() {
|
||||
|
|
|
|||
|
|
@ -98,8 +98,8 @@ impl Uri {
|
|||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
check_trait,
|
||||
sharry::{FileID, ShareID},
|
||||
test_util::check_trait,
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
23
src/test_util/mod.rs
Normal file
23
src/test_util/mod.rs
Normal 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
|
||||
}
|
||||
Loading…
Reference in a new issue