cbz2pdf/tests/cbz_reader_tests.rs
Marc Plano-Lesay 034f0b142c
All checks were successful
Checking Renovate configuration / validate (pull_request) Successful in 1m5s
Build and test / Clippy (pull_request) Successful in 3m25s
Checking yaml / Run yamllint (pull_request) Successful in 11s
Build and test / Tests (pull_request) Successful in 4m12s
Build and test / Build AMD64 (pull_request) Successful in 4m12s
Build and test / Generate Documentation (pull_request) Successful in 3m52s
chore: refactor ahead of supporting more conversion types
2025-10-10 16:37:21 +11:00

57 lines
1.9 KiB
Rust

use std::fs::File;
use std::io::Write;
use cbz2pdf::formats::cbz::CbzReader;
use cbz2pdf::formats::FormatReader;
#[test]
fn cbz_reader_reads_jpgs_and_sorts_by_name() {
// Build a temporary CBZ with 3 jpgs (including in a subdir) and one non-jpg
let temp_dir = tempfile::tempdir().expect("create temp dir");
let cbz_path = temp_dir.path().join("book.cbz");
{
let file = File::create(&cbz_path).expect("create cbz");
let mut zip = zip::ZipWriter::new(file);
let options = zip::write::SimpleFileOptions::default();
// out of order names
zip.start_file("002.jpg", options).unwrap();
// Create a tiny JPEG using the JPEG encoder
let img = image::DynamicImage::new_rgb8(1, 1).to_rgb8();
let mut buf = Vec::new();
{
let mut cursor = std::io::Cursor::new(&mut buf);
let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut cursor, 80);
enc.encode(&img, 1, 1, image::ColorType::Rgb8.into())
.unwrap();
}
zip.write_all(&buf).unwrap();
zip.start_file("001.jpg", options).unwrap();
zip.write_all(&buf).unwrap();
// nested path should be accepted
zip.start_file("subdir/003.jpg", options).unwrap();
zip.write_all(&buf).unwrap();
// non-jpg should be ignored
zip.start_file("notes.txt", options).unwrap();
zip.write_all(b"hello").unwrap();
zip.finish().unwrap();
}
let reader = CbzReader;
let doc = reader.read(&cbz_path).expect("read cbz");
assert_eq!(doc.pages.len(), 3, "should include only jpg files");
let names: Vec<String> = doc.pages.iter().map(|p| p.name.clone()).collect();
assert_eq!(
names,
vec!["001.jpg", "002.jpg", "003.jpg"],
"pages sorted by name"
);
// temp_dir goes out of scope and cleans up automatically
}