mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-06 18:52:36 +00:00
test(ui): stabilize Reduce metadata and isolate output directories
This commit is contained in:
1 parent
4ee79bd40c
commit
e2be8db7fb
1 file changed
+29
-6
@@ -69,12 +69,37 @@ fn opening_images_and_text_converts_them_to_new_pdfs() {
|
||||
assert!(app.open_bytes("junk.png", None, b"\x89PNG\r\n\x1a\nnot really".to_vec()).is_err());
|
||||
}
|
||||
|
||||
// Use a directory claimed exclusively by this run, including after process-ID reuse.
|
||||
struct ReduceDir(std::path::PathBuf);
|
||||
impl ReduceDir {
|
||||
fn new() -> Self {
|
||||
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
for _ in 0..128 {
|
||||
let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let path = std::env::temp_dir().join(format!("pdfcraft-reduce-{}-{n}", std::process::id()));
|
||||
match std::fs::create_dir(&path) {
|
||||
Ok(()) => return Self(path),
|
||||
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
|
||||
Err(error) => panic!("creating Reduce test directory: {error}"),
|
||||
}
|
||||
}
|
||||
panic!("no unused Reduce test directory after 128 attempts");
|
||||
}
|
||||
}
|
||||
impl Drop for ReduceDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Open `name` and run Reduce File Size on it, saving to `out` without a dialog.
|
||||
fn reduce(name: &'static str, bytes: Vec<u8>, out: &std::path::Path) -> egui_kittest::Harness<'static, PdfCraftApp> {
|
||||
let out2 = out.to_path_buf();
|
||||
let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(move |_cc| {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.set_option("language", "en").unwrap();
|
||||
// Keep creation and optimization metadata identical across wall-clock seconds.
|
||||
app.session = std::mem::take(&mut app.session).with_clock(|| 1_700_000_000);
|
||||
app.open_bytes(name, None, bytes).unwrap();
|
||||
app.save_override = Some(out2.to_string_lossy().into_owned());
|
||||
app
|
||||
@@ -105,9 +130,8 @@ fn reduce_file_size_writes_a_compact_copy() {
|
||||
enc.set_depth(png::BitDepth::Eight);
|
||||
enc.write_header().unwrap().write_image_data(&px).unwrap();
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("pdfcraft-reduce-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let out = dir.join("reduced.pdf");
|
||||
let dir = ReduceDir::new();
|
||||
let out = dir.0.join("reduced.pdf");
|
||||
let h = reduce("photo.png", photo, &out);
|
||||
let bytes = std::fs::read(&out).unwrap();
|
||||
assert!(bytes.starts_with(b"%PDF-"));
|
||||
@@ -119,9 +143,8 @@ fn reduce_file_size_writes_a_compact_copy() {
|
||||
#[test]
|
||||
fn reduce_file_size_saves_nothing_when_the_file_is_already_small() {
|
||||
use egui_kittest::kittest::Queryable;
|
||||
let dir = std::env::temp_dir().join(format!("pdfcraft-reduce-small-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let out = dir.join("reduced.pdf");
|
||||
let dir = ReduceDir::new();
|
||||
let out = dir.0.join("reduced.pdf");
|
||||
// A new text PDF is already compact: a copy would be no smaller (#490).
|
||||
let h = reduce("notes.txt", "lorem ipsum ".repeat(500).into_bytes(), &out);
|
||||
assert!(!out.exists(), "no copy was written");
|
||||
|
||||
Reference in new issue
Block a user