[wip] unit tests for file module

- testing for `compute_hash` and `check_hash`
This commit is contained in:
Jörn-Michael Miehe 2025-07-03 17:20:56 +00:00
parent 6814f74484
commit 61d62d731e
3 changed files with 43 additions and 34 deletions

View file

@ -121,6 +121,18 @@ impl Error {
}
}
pub fn is_mismatch<E, A>(&self, has_expected: E, has_actual: A) -> bool
where
String: PartialEq<E> + PartialEq<A>,
{
matches!(
self,
Self::Mismatch { expected, actual }
if *expected == has_expected
&& *actual == has_actual
)
}
#[must_use]
pub fn get_invalid_param(&self) -> Option<&Parameter> {
if let Self::InvalidParameter(p) = self {

View file

@ -154,26 +154,35 @@ mod tests {
let file = create_file(content);
let callback = drop;
let err_callback = |err| {
// check error
match err {
crate::Error::Mismatch { expected, actual } => {
assert_eq!(expected, bad_size.to_string());
assert_eq!(actual, good_size.to_string());
}
_ => panic!(
"Expected Error::Mismatch for input size `{bad_size}` but got {err:?}"
),
};
};
compute_hash(file.path(), bad_size, callback)
.map_err(err_callback)
{
let err = compute_hash(file.path(), bad_size, callback)
.expect_err("compute_hash should report a mismatch");
check_hash(file.path(), bad_size, Some("foobar"), callback)
.map_err(err_callback)
// check error
assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string()));
}
{
let err = check_hash(file.path(), bad_size, Some("foobar"), callback)
.expect_err("check_hash should report a mismatch");
// check error
assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string()));
}
}
}
#[test]
fn hash_value_none() {
for (content, size) in CASES {
let file = create_file(content);
let callback = drop;
let err = check_hash(file.path(), size, None, callback)
.expect_err("check_hash should report a mismatch");
// check error
assert!(err.is_mismatch("hash", file.path().display().to_string()));
}
}
@ -190,17 +199,11 @@ mod tests {
let file = create_file(content);
let callback = drop;
let err = check_hash(file.path(), size, Some(&bad_hash.to_string()), callback)
let err = check_hash(file.path(), size, Some(bad_hash), callback)
.expect_err("check_hash should report a mismatch");
// check error
match err {
crate::Error::Mismatch { expected, actual } => {
assert_eq!(expected, bad_hash);
assert_eq!(actual, good_hash);
}
_ => panic!("Expected Error::Mismatch but got {err:?}"),
}
assert!(err.is_mismatch(bad_hash, good_hash));
}
}
}

View file

@ -185,16 +185,10 @@ mod tests {
for bad in bad_inputs {
let err = FileID::try_from(bad.to_string()).expect_err("URL should not parse");
// make sure it's the Mismatch variant, and that it contains the original input
match err {
crate::Error::Mismatch { expected, actual } => {
assert_eq!(
expected, "<proto>://<host>/api/v2/alias/upload/<share>/files/tus/<file>",
"Error should output expected format"
);
assert_eq!(actual, bad, "Error should echo back the input");
}
_ => panic!("Expected Error::Mismatch for input `{bad}` but got {err:?}"),
}
assert!(err.is_mismatch(
"<proto>://<host>/api/v2/alias/upload/<share>/files/tus/<file>",
bad
));
}
}
}