Compare commits

...

4 commits

Author SHA1 Message Date
910d95230f Merge branch 'develop' into feature/unit_tests 2025-07-12 01:33:17 +00:00
117faee583 use devcontainers-extra repo 2025-07-12 01:33:08 +00:00
2900715a74 [wip] unit testing
- styling, `test_util::make_check_trait_panic`
2025-07-12 01:32:10 +00:00
6167ebc98a [wip] unit tests for file module
- testing for `Uploading`
2025-07-12 01:31:28 +00:00
4 changed files with 79 additions and 18 deletions

View file

@ -18,7 +18,7 @@
"ghcr.io/lee-orr/rusty-dev-containers/cargo-binstall:0": { "ghcr.io/lee-orr/rusty-dev-containers/cargo-binstall:0": {
"packages": "cargo-llvm-cov" "packages": "cargo-llvm-cov"
}, },
"ghcr.io/devcontainers-contrib/features/apt-get-packages:1": { "ghcr.io/devcontainers-extra/features/apt-get-packages:1": {
"packages": "git-flow, musl-tools" "packages": "git-flow, musl-tools"
} }
}, },

View file

@ -112,7 +112,7 @@ mod tests {
} }
#[test] #[test]
#[should_panic(expected = "did not fit into \"u32\"")] #[should_panic = "did not fit into \"u32\""]
fn test_usize_overflow_panics() { fn test_usize_overflow_panics() {
// works // works
assert_eq!(from_usize_or_panic::<u64>(usize::MAX), u64::MAX); assert_eq!(from_usize_or_panic::<u64>(usize::MAX), u64::MAX);

View file

@ -66,7 +66,7 @@ impl Uploading {
Some(self) Some(self)
} else { } else {
warn!("attempted to rewind twice"); warn!("attempted to rewind with no `previous_offset`");
None None
} }
} }
@ -142,7 +142,10 @@ mod tests {
use crate::{ use crate::{
sharry::{Client, json::NewShareRequest}, sharry::{Client, json::NewShareRequest},
test_util::{MockClient, create_file, data::cases}, test_util::{
MockClient, create_file,
data::{DATA_LENGTHS_BAD, cases, cases_with},
},
}; };
use super::*; use super::*;
@ -169,7 +172,7 @@ mod tests {
#[test] #[test]
fn basic_tests() { fn basic_tests() {
fn check_members(upl: &Uploading, path: &PathBuf, size: u64) { fn check_members(upl: &Uploading, path: &PathBuf, size: u64) {
assert_eq!(&upl.path, path); assert_eq!(upl.path, *path);
assert_eq!(upl.size, size); assert_eq!(upl.size, size);
assert_eq!(upl.offset, 0); assert_eq!(upl.offset, 0);
assert!(upl.previous_offset.is_none()); assert!(upl.previous_offset.is_none());
@ -194,23 +197,75 @@ mod tests {
unsafe { Uploading::new_unchecked(upl.path, upl.size, upl.hash, upl.file_id) }; unsafe { Uploading::new_unchecked(upl.path, upl.size, upl.hash, upl.file_id) };
check_members(&upl, &path, size); check_members(&upl, &path, size);
// TODO into separate test
// // `check_eof`
// let upl = if size == 0 {
// upl
// } else {
// let eof = upl.check_eof();
// assert!(eof.is_ok());
// let upl = eof.unwrap();
// check_members(&upl, &path, size);
// upl
// };
// `stop` // `stop`
let chk = upl.stop(); let chk = upl.stop();
assert_eq!(chk.get_name(), path.file_name().unwrap().to_str().unwrap()); assert_eq!(chk.get_name(), path.file_name().unwrap().to_str().unwrap());
assert_eq!(chk.get_size(), size); assert_eq!(chk.get_size(), size);
} }
} }
#[test]
fn check_eof_works() {
for (content, size) in cases() {
let (upl, _share_id, file) = create_uploading(content);
let path = file.path().canonicalize().unwrap();
let eof = upl.check_eof();
if size > 0 {
assert!(eof.is_ok());
} else {
assert!(eof.is_err());
assert_eq!(eof.unwrap_err(), path);
};
}
}
#[test]
fn check_read_works() {
for (content, size, extra) in cases_with(DATA_LENGTHS_BAD) {
let (mut upl, _share_id, _file) = create_uploading(content);
// use oversized buffer
let mut buf = vec![0; (size + extra) as usize];
let chunk_res = upl.read(&mut buf);
if size > 0 {
assert_eq!(upl.previous_offset, Some(0));
assert_eq!(upl.offset, size);
let chunk = chunk_res.unwrap();
assert_eq!(chunk.get_offset(), 0);
assert_eq!(chunk.get_length(), size);
assert_eq!(chunk.get_file_id().to_string(), upl.file_id.to_string());
assert_eq!(chunk.get_data(), content);
} else {
assert!(chunk_res.is_err());
};
}
}
#[test]
fn check_rewind_works() {
let cases = cases()
// ignore "empty" testcase
.filter(|&(_, size)| size > 0)
// remove "size"
.map(|(content, _)| content);
for content in cases {
let (mut upl, _share_id, _file) = create_uploading(content);
// read 1 byte and rewind
upl.read(&mut [0]).unwrap();
upl = upl.rewind().unwrap();
// check: read has been rewound
assert_eq!(upl.offset, 0);
assert_eq!(upl.previous_offset, None);
// check: attempting to rewind again will destroy
assert!(upl.rewind().is_none());
}
}
} }

View file

@ -28,3 +28,9 @@ pub fn create_file(data: &[u8]) -> NamedTempFile {
tmp.write_all(data).unwrap(); tmp.write_all(data).unwrap();
tmp tmp
} }
#[test]
#[should_panic = "`impl foo for bar` expected: 1, actual: 0"]
fn make_check_trait_panic() {
check_trait(0, 1, "foo", "bar");
}