Update test helper function names

This commit is contained in:
Jack Fletcher 2022-01-27 00:12:17 +00:00
parent 161308c673
commit 4cb2067019

View file

@ -14,10 +14,10 @@ fn end_to_end() {
let file = &mut Cursor::new(Vec::new()); let file = &mut Cursor::new(Vec::new());
println!("Writing file with {} compression", method); println!("Writing file with {} compression", method);
write_to_zip(file, method).expect("Couldn't write to test file"); write_test_archive(file, method).expect("Couldn't write test zip archive");
println!("Checking file contents"); println!("Checking file contents");
check_zip_contents(file, ENTRY_NAME, Some(method)); check_archive_file(file, ENTRY_NAME, Some(method));
} }
} }
@ -27,7 +27,7 @@ fn end_to_end() {
fn copy() { fn copy() {
for &method in SUPPORTED_METHODS { for &method in SUPPORTED_METHODS {
let src_file = &mut Cursor::new(Vec::new()); let src_file = &mut Cursor::new(Vec::new());
write_to_zip(src_file, method).expect("Couldn't write to test file"); write_test_archive(src_file, method).expect("Couldn't write to test file");
let mut tgt_file = &mut Cursor::new(Vec::new()); let mut tgt_file = &mut Cursor::new(Vec::new());
@ -55,8 +55,8 @@ fn copy() {
let mut tgt_archive = zip::ZipArchive::new(tgt_file).unwrap(); let mut tgt_archive = zip::ZipArchive::new(tgt_file).unwrap();
check_zip_file_contents(&mut tgt_archive, ENTRY_NAME); check_test_file_contents(&mut tgt_archive, ENTRY_NAME);
check_zip_file_contents(&mut tgt_archive, COPY_ENTRY_NAME); check_test_file_contents(&mut tgt_archive, COPY_ENTRY_NAME);
} }
} }
@ -66,7 +66,7 @@ fn copy() {
fn append() { fn append() {
for &method in SUPPORTED_METHODS { for &method in SUPPORTED_METHODS {
let mut file = &mut Cursor::new(Vec::new()); let mut file = &mut Cursor::new(Vec::new());
write_to_zip(file, method).expect("Couldn't write to test file"); write_test_archive(file, method).expect("Couldn't write to test file");
{ {
let mut zip = zip::ZipWriter::new_append(&mut file).unwrap(); let mut zip = zip::ZipWriter::new_append(&mut file).unwrap();
@ -80,13 +80,13 @@ fn append() {
} }
let mut zip = zip::ZipArchive::new(&mut file).unwrap(); let mut zip = zip::ZipArchive::new(&mut file).unwrap();
check_zip_file_contents(&mut zip, ENTRY_NAME); check_test_file_contents(&mut zip, ENTRY_NAME);
check_zip_file_contents(&mut zip, COPY_ENTRY_NAME); check_test_file_contents(&mut zip, COPY_ENTRY_NAME);
} }
} }
// Write a test zip archive to buffer. // Write a test zip archive to buffer.
fn write_to_zip( fn write_test_archive(
file: &mut Cursor<Vec<u8>>, file: &mut Cursor<Vec<u8>>,
method: CompressionMethod, method: CompressionMethod,
) -> zip::result::ZipResult<()> { ) -> zip::result::ZipResult<()> {
@ -115,20 +115,24 @@ fn write_to_zip(
Ok(()) Ok(())
} }
// Load an archive from buffer and check for expected test data. // Load an archive from buffer and check for test data.
fn read_zip<R: Read + Seek>(zip_file: R) -> zip::result::ZipResult<zip::ZipArchive<R>> { fn check_test_archive<R: Read + Seek>(zip_file: R) -> zip::result::ZipResult<zip::ZipArchive<R>> {
let mut archive = zip::ZipArchive::new(zip_file).unwrap(); let mut archive = zip::ZipArchive::new(zip_file).unwrap();
let expected_file_names = [ // Check archive contains expected files names.
"test/", {
"test/☃.txt", let expected_file_names = [
"test_with_extra_data/🐢.txt", "test/",
ENTRY_NAME, "test/☃.txt",
]; "test_with_extra_data/🐢.txt",
let expected_file_names = HashSet::from_iter(expected_file_names.iter().copied()); ENTRY_NAME,
let file_names = archive.file_names().collect::<HashSet<_>>(); ];
assert_eq!(file_names, expected_file_names); let expected_file_names = HashSet::from_iter(expected_file_names.iter().copied());
let file_names = archive.file_names().collect::<HashSet<_>>();
assert_eq!(file_names, expected_file_names);
}
// Check an archive file has expected extra field contents.
{ {
let file_with_extra_data = archive.by_name("test_with_extra_data/🐢.txt")?; let file_with_extra_data = archive.by_name("test_with_extra_data/🐢.txt")?;
let mut extra_data = Vec::new(); let mut extra_data = Vec::new();
@ -142,7 +146,7 @@ fn read_zip<R: Read + Seek>(zip_file: R) -> zip::result::ZipResult<zip::ZipArchi
} }
// Read a file in the archive as a string. // Read a file in the archive as a string.
fn read_zip_file<R: Read + Seek>( fn read_archive_file<R: Read + Seek>(
archive: &mut zip::ZipArchive<R>, archive: &mut zip::ZipArchive<R>,
name: &str, name: &str,
) -> zip::result::ZipResult<String> { ) -> zip::result::ZipResult<String> {
@ -150,11 +154,12 @@ fn read_zip_file<R: Read + Seek>(
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
Ok(contents) Ok(contents)
} }
// Check a file in the archive contains expected data. // Check a file in the archive contains expected data.
fn check_zip_contents( fn check_archive_file(
zip_file: &mut Cursor<Vec<u8>>, zip_file: &mut Cursor<Vec<u8>>,
name: &str, name: &str,
expected_method: Option<CompressionMethod>, expected_method: Option<CompressionMethod>,
@ -177,7 +182,7 @@ fn check_zip_contents(
// Check a file in the archive contains the lorem string. // Check a file in the archive contains the lorem string.
fn check_test_file_contents<R: Read + Seek>(archive: &mut zip::ZipArchive<R>, name: &str) { fn check_test_file_contents<R: Read + Seek>(archive: &mut zip::ZipArchive<R>, name: &str) {
let file_contents: String = read_zip_file(archive, name).unwrap(); let file_contents: String = read_archive_file(archive, name).unwrap();
assert_eq!(file_contents.as_bytes(), LOREM_IPSUM); assert_eq!(file_contents.as_bytes(), LOREM_IPSUM);
} }