Skip to content

Commit

Permalink
feat: Run one-byte-read tests in CI in release
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Mar 19, 2024
1 parent 7bac68a commit 2877343
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ ci-test:
source <(cargo llvm-cov show-env --export-prefix)
cargo llvm-cov clean --workspace
cargo nextest run --all-features --profile ci
ONE_BYTE_READ=1 cargo nextest run --all-features --release --profile ci
cargo llvm-cov report --lcov --output-path coverage.lcov
42 changes: 38 additions & 4 deletions rc-zip-sync/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use rc_zip::{
error::Error,
parse::Archive,
};
use rc_zip_sync::{ArchiveHandle, HasCursor, ReadZip, ReadZipStreaming};
use rc_zip_sync::{ArchiveHandle, HasCursor, ReadZip, ReadZipStreaming, ReadZipWithSize};

use std::{fs::File, io::Read};
use std::{
fs::File,
io::{self, Read},
};

fn check_case<F: HasCursor>(test: &Case, archive: Result<ArchiveHandle<'_, F>, Error>) {
corpus::check_case(test, archive.as_ref().map(|ar| -> &Archive { ar }));
Expand Down Expand Up @@ -49,8 +52,15 @@ fn real_world_files() {

let guarded_path = case.absolute_path();
let file = File::open(&guarded_path.path).unwrap();
let archive = file.read_zip().map_err(Error::from);
check_case(&case, archive);
if let Ok("1") = std::env::var("ONE_BYTE_READ").as_deref() {
let size = file.metadata().unwrap().len();
let file = OneByteReadWrapper(file);
let archive = file.read_zip_with_size(size).map_err(Error::from);
check_case(&case, archive);
} else {
let archive = file.read_zip().map_err(Error::from);
check_case(&case, archive);
};
drop(guarded_path)
}
}
Expand Down Expand Up @@ -78,3 +88,27 @@ fn streaming() {
drop(guarded_path)
}
}

// This helps find bug in state machines!

struct OneByteReadWrapper<R>(R);

impl<R> io::Read for OneByteReadWrapper<R>
where
R: io::Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(&mut buf[..1])
}
}

impl<R> HasCursor for OneByteReadWrapper<R>
where
R: HasCursor,
{
type Cursor<'a> = OneByteReadWrapper<R::Cursor<'a>> where R: 'a;

fn cursor_at(&self, offset: u64) -> Self::Cursor<'_> {
OneByteReadWrapper(self.0.cursor_at(offset))
}
}

0 comments on commit 2877343

Please sign in to comment.