[wip] unit tests
- use `unwrap` while testing
This commit is contained in:
parent
bd9aa77d6c
commit
53fdb6cc97
6 changed files with 37 additions and 60 deletions
|
|
@ -118,17 +118,17 @@ mod tests {
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
sharry::{Client, json::NewShareRequest},
|
sharry::{Client, json::NewShareRequest},
|
||||||
test_util::{
|
test_util::{
|
||||||
MockClient, check_trait, create_file,
|
MockClient, check_trait, create_file,
|
||||||
data::{HASHES_STD_GOOD, cases, data},
|
data::{HASHES_STD_GOOD, cases, data},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn create_checked(content: &[u8]) -> (Checked, NamedTempFile) {
|
fn create_checked(content: &[u8]) -> (Checked, NamedTempFile) {
|
||||||
let file = create_file(content);
|
let file = create_file(content);
|
||||||
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
|
let chk = Checked::new(file.path()).unwrap();
|
||||||
|
|
||||||
// return both, so the `NamedTempFile` is not auto-deleted here
|
// return both, so the `NamedTempFile` is not auto-deleted here
|
||||||
(chk, file)
|
(chk, file)
|
||||||
|
|
@ -138,21 +138,14 @@ test_util::{
|
||||||
fn new_on_existing_file_works() {
|
fn new_on_existing_file_works() {
|
||||||
for (content, size) in cases() {
|
for (content, size) in cases() {
|
||||||
let (chk, file) = create_checked(content);
|
let (chk, file) = create_checked(content);
|
||||||
|
let path = file.path().canonicalize().unwrap();
|
||||||
let path = file
|
|
||||||
.path()
|
|
||||||
.canonicalize()
|
|
||||||
.expect("the file should have a canonical path");
|
|
||||||
|
|
||||||
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());
|
||||||
|
|
||||||
// `FileTrait`
|
// `FileTrait`
|
||||||
assert_eq!(
|
assert_eq!(chk.get_name(), file.path().file_name().unwrap());
|
||||||
chk.get_name(),
|
|
||||||
file.path().file_name().expect("`file_name` should succeed")
|
|
||||||
);
|
|
||||||
assert_eq!(chk.get_size(), size);
|
assert_eq!(chk.get_size(), size);
|
||||||
|
|
||||||
check_trait(
|
check_trait(
|
||||||
|
|
@ -162,7 +155,7 @@ test_util::{
|
||||||
"Checked",
|
"Checked",
|
||||||
);
|
);
|
||||||
|
|
||||||
// new unchecked
|
// new_unchecked
|
||||||
let chk = unsafe { Checked::new_unchecked(chk.path, chk.size, chk.hash) };
|
let chk = unsafe { Checked::new_unchecked(chk.path, chk.size, chk.hash) };
|
||||||
assert_eq!(chk.path, path);
|
assert_eq!(chk.path, path);
|
||||||
assert_eq!(chk.size, size);
|
assert_eq!(chk.size, size);
|
||||||
|
|
@ -172,13 +165,13 @@ test_util::{
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new_on_dir_errors() {
|
fn new_on_dir_errors() {
|
||||||
let tempdir = TempDir::new().expect("creating temp dir");
|
let tempdir = TempDir::new().unwrap();
|
||||||
let fs_root = PathBuf::from("/");
|
let fs_root = PathBuf::from("/");
|
||||||
|
|
||||||
let dirs = [tempdir.path(), fs_root.as_path()];
|
let dirs = [tempdir.path(), fs_root.as_path()];
|
||||||
|
|
||||||
for p in dirs {
|
for p in dirs {
|
||||||
let err = Checked::new(p).expect_err("creating `Checked` should fail");
|
let err = Checked::new(p).unwrap_err();
|
||||||
assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
|
assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
assert_eq!(err.to_string(), "Not a regular file");
|
assert_eq!(err.to_string(), "Not a regular file");
|
||||||
|
|
@ -187,11 +180,11 @@ test_util::{
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new_on_nex_errors() {
|
fn new_on_nex_errors() {
|
||||||
let tempdir = TempDir::new().expect("creating temp dir");
|
let tempdir = TempDir::new().unwrap();
|
||||||
let nex_paths = [0, 1, 2, 3, 4].map(|i| tempdir.path().join(format!("nex_{i}.ext")));
|
let nex_paths = [0, 1, 2, 3, 4].map(|i| tempdir.path().join(format!("nex_{i}.ext")));
|
||||||
|
|
||||||
for p in nex_paths {
|
for p in nex_paths {
|
||||||
let err = Checked::new(p).expect_err("creating `Checked` should fail");
|
let err = Checked::new(p).unwrap_err();
|
||||||
assert_eq!(err.kind(), io::ErrorKind::NotFound);
|
assert_eq!(err.kind(), io::ErrorKind::NotFound);
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
assert_eq!(err.to_string(), "No such file or directory (os error 2)");
|
assert_eq!(err.to_string(), "No such file or directory (os error 2)");
|
||||||
|
|
@ -203,9 +196,9 @@ test_util::{
|
||||||
for (content, hash) in data().zip(HASHES_STD_GOOD) {
|
for (content, hash) in data().zip(HASHES_STD_GOOD) {
|
||||||
let (mut chk, _file) = create_checked(content);
|
let (mut chk, _file) = create_checked(content);
|
||||||
|
|
||||||
chk.hash(drop).expect("`hash` should succeed");
|
chk.hash(drop).unwrap();
|
||||||
// `FileTrait`
|
// `FileTrait`
|
||||||
chk.check_hash(drop).expect("`check_hash` should succeed");
|
chk.check_hash(drop).unwrap();
|
||||||
|
|
||||||
assert_eq!(chk.hash, Some(hash.to_string()));
|
assert_eq!(chk.hash, Some(hash.to_string()));
|
||||||
}
|
}
|
||||||
|
|
@ -218,7 +211,7 @@ test_util::{
|
||||||
|
|
||||||
// fake hash
|
// fake hash
|
||||||
chk.hash = Some(String::default());
|
chk.hash = Some(String::default());
|
||||||
let err = chk.hash(drop).expect_err("`hash` twice should fail");
|
let err = chk.hash(drop).unwrap_err();
|
||||||
|
|
||||||
assert!(err.is_mismatch("unhashed file", chk.path.display().to_string()));
|
assert!(err.is_mismatch("unhashed file", chk.path.display().to_string()));
|
||||||
}
|
}
|
||||||
|
|
@ -227,19 +220,19 @@ test_util::{
|
||||||
#[test]
|
#[test]
|
||||||
fn start_upload_works() {
|
fn start_upload_works() {
|
||||||
let client = MockClient::default();
|
let client = MockClient::default();
|
||||||
let uri = sharry::Uri::from(true);
|
let uri = sharry::Uri::from(true);
|
||||||
let alias_id = sharry::AliasID::from(true);
|
let alias_id = sharry::AliasID::from(true);
|
||||||
|
|
||||||
let share_id = client
|
let share_id = client
|
||||||
.share_create(&uri, &alias_id, NewShareRequest::new("share", 0))
|
.share_create(&uri, &alias_id, NewShareRequest::new("share", 0))
|
||||||
.expect("");
|
.unwrap();
|
||||||
|
|
||||||
for content in data() {
|
for content in data() {
|
||||||
let (chk, _file) = create_checked(content);
|
let (chk, _file) = create_checked(content);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
chk.start_upload( &client, &uri, &alias_id, &share_id )
|
chk.start_upload(&client, &uri, &alias_id, &share_id)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -139,8 +139,7 @@ mod tests {
|
||||||
// to capture progress updates from `compute_hash`
|
// to capture progress updates from `compute_hash`
|
||||||
let mut read_total = 0;
|
let mut read_total = 0;
|
||||||
let callback = |n| read_total += n;
|
let callback = |n| read_total += n;
|
||||||
|
let hash = compute_hash(file.path(), size, callback).unwrap();
|
||||||
let hash = compute_hash(file.path(), size, callback).expect("hash should succeed");
|
|
||||||
|
|
||||||
assert_eq!(hash, expected_hash);
|
assert_eq!(hash, expected_hash);
|
||||||
assert_eq!(read_total, size);
|
assert_eq!(read_total, size);
|
||||||
|
|
@ -151,21 +150,16 @@ mod tests {
|
||||||
fn hash_size_mismatch() {
|
fn hash_size_mismatch() {
|
||||||
for (content, good_size, bad_size) in cases_with(DATA_LENGTHS_BAD) {
|
for (content, good_size, bad_size) in cases_with(DATA_LENGTHS_BAD) {
|
||||||
let file = create_file(content);
|
let file = create_file(content);
|
||||||
let callback = drop;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let err = compute_hash(file.path(), bad_size, callback)
|
// `compute_hash` with bad size
|
||||||
.expect_err("compute_hash should report a mismatch");
|
let err = compute_hash(file.path(), bad_size, drop).unwrap_err();
|
||||||
|
|
||||||
// check error
|
|
||||||
assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string()));
|
assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let err = check_hash(file.path(), bad_size, Some("foobar"), callback)
|
// `check_hash` with bad size
|
||||||
.expect_err("check_hash should report a mismatch");
|
let err = check_hash(file.path(), bad_size, Some("foobar"), drop).unwrap_err();
|
||||||
|
|
||||||
// check error
|
|
||||||
assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string()));
|
assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -175,12 +169,9 @@ mod tests {
|
||||||
fn hash_value_none() {
|
fn hash_value_none() {
|
||||||
for (content, size) in cases() {
|
for (content, size) in cases() {
|
||||||
let file = create_file(content);
|
let file = create_file(content);
|
||||||
let callback = drop;
|
|
||||||
|
|
||||||
let err = check_hash(file.path(), size, None, callback)
|
// `check_hash` with no hash
|
||||||
.expect_err("check_hash should report a mismatch");
|
let err = check_hash(file.path(), size, None, drop).unwrap_err();
|
||||||
|
|
||||||
// check error
|
|
||||||
assert!(err.is_mismatch("hash", file.path().display().to_string()));
|
assert!(err.is_mismatch("hash", file.path().display().to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -191,12 +182,9 @@ mod tests {
|
||||||
cases_with(HASHES_STD_GOOD).zip(HASHES_STD_BAD)
|
cases_with(HASHES_STD_GOOD).zip(HASHES_STD_BAD)
|
||||||
{
|
{
|
||||||
let file = create_file(content);
|
let file = create_file(content);
|
||||||
let callback = drop;
|
|
||||||
|
|
||||||
let err = check_hash(file.path(), size, Some(bad_hash), callback)
|
// `check_hash` with bad hash
|
||||||
.expect_err("check_hash should report a mismatch");
|
let err = check_hash(file.path(), size, Some(bad_hash), drop).unwrap_err();
|
||||||
|
|
||||||
// check error
|
|
||||||
assert!(err.is_mismatch(bad_hash, good_hash));
|
assert!(err.is_mismatch(bad_hash, good_hash));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -171,12 +171,8 @@ mod tests {
|
||||||
];
|
];
|
||||||
|
|
||||||
for (good, expected_fid) in cases {
|
for (good, expected_fid) in cases {
|
||||||
let file_id =
|
let file_id = FileID::try_from(good.to_string()).unwrap();
|
||||||
FileID::try_from(good.to_string()).expect("URL should parse successfully");
|
assert_eq!(file_id.0, expected_fid);
|
||||||
assert_eq!(
|
|
||||||
file_id.0, expected_fid,
|
|
||||||
"Expected `{good}` → FileID({expected_fid}), got {file_id:?}",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -196,7 +192,7 @@ mod tests {
|
||||||
];
|
];
|
||||||
|
|
||||||
for bad in bad_inputs {
|
for bad in bad_inputs {
|
||||||
let err = FileID::try_from(bad.to_string()).expect_err("URL should not parse");
|
let err = FileID::try_from(bad.to_string()).unwrap_err();
|
||||||
// make sure it's the Mismatch variant, and that it contains the original input
|
// make sure it's the Mismatch variant, and that it contains the original input
|
||||||
assert!(err.is_mismatch(
|
assert!(err.is_mismatch(
|
||||||
"<proto>://<host>/api/v2/alias/upload/<share>/files/tus/<file>",
|
"<proto>://<host>/api/v2/alias/upload/<share>/files/tus/<file>",
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ impl MockClient {
|
||||||
.ok_or_else(|| error_response!("can't find share {share_id:?}!"))?;
|
.ok_or_else(|| error_response!("can't find share {share_id:?}!"))?;
|
||||||
|
|
||||||
Ok(RefMut::map(shares, |shares| {
|
Ok(RefMut::map(shares, |shares| {
|
||||||
shares.get_mut(share_id).expect("checked but None!")
|
shares.get_mut(share_id).unwrap()
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,7 +90,7 @@ impl MockClient {
|
||||||
.ok_or_else(|| error_response!("can't find file {file_id:?}!"))?;
|
.ok_or_else(|| error_response!("can't find file {file_id:?}!"))?;
|
||||||
|
|
||||||
Ok(RefMut::map(share, move |share| {
|
Ok(RefMut::map(share, move |share| {
|
||||||
share.files.get_mut(file_id).expect("checked but None!")
|
share.files.get_mut(file_id).unwrap()
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,8 +142,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn false_makes_invalids() {
|
fn false_makes_invalids() {
|
||||||
fn test_check(value: impl CheckID, callback: impl FnOnce(&Parameter) -> bool) {
|
fn test_check(value: impl CheckID, callback: impl FnOnce(&Parameter) -> bool) {
|
||||||
let check = value.check().expect_err("should be invalid");
|
let check = value.check().unwrap_err();
|
||||||
let p = check.get_invalid_param().expect("should be InvalidParam");
|
let p = check.get_invalid_param().unwrap();
|
||||||
assert!(callback(p));
|
assert!(callback(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ where
|
||||||
|
|
||||||
/// Helper to create a temp file from `data`
|
/// Helper to create a temp file from `data`
|
||||||
pub fn create_file(data: &[u8]) -> NamedTempFile {
|
pub fn create_file(data: &[u8]) -> NamedTempFile {
|
||||||
let mut tmp = NamedTempFile::new().expect("creating temp file");
|
let mut tmp = NamedTempFile::new().unwrap();
|
||||||
tmp.write_all(data).expect("writing to tempfile");
|
tmp.write_all(data).unwrap();
|
||||||
tmp
|
tmp
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue