diff --git a/crates/print/src/spool.rs b/crates/print/src/spool.rs index 3771149551..a940822087 100644 --- a/crates/print/src/spool.rs +++ b/crates/print/src/spool.rs @@ -19,11 +19,22 @@ pub(crate) fn missing_print_dll_in(system32: &std::path::Path) -> Option<&'stati PRINT_DLLS.into_iter().find(|dll| !system32.join(dll).is_file()) } +#[cfg(all(windows, test))] +thread_local! { + /// Tests: the folder the print DLLs are looked for in, instead of the system's, so the + /// public entry points can be checked against a Windows that lacks them. + pub(crate) static SYSTEM32_FOR_TEST: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; +} + /// The first print DLL this Windows installation lacks. `None` when all are there, and when the /// system folder is unknown (the loader then decides, as before). fn missing_print_dll() -> Option<&'static str> { #[cfg(windows)] { + #[cfg(test)] + if let Some(dir) = SYSTEM32_FOR_TEST.with(|d| d.borrow().clone()) { + return missing_print_dll_in(&dir); + } let root = std::env::var_os("SystemRoot").or_else(|| std::env::var_os("windir"))?; missing_print_dll_in(&std::path::Path::new(&root).join("System32")) } diff --git a/crates/print/src/tests.rs b/crates/print/src/tests.rs index a6c0585aeb..aa6e184e50 100644 --- a/crates/print/src/tests.rs +++ b/crates/print/src/tests.rs @@ -1160,7 +1160,30 @@ fn missing_print_dlls_are_named() { std::fs::write(dir.join(PRINT_DLLS[1]), b"").unwrap(); assert_eq!(missing_print_dll_in(&dir), None); let _ = std::fs::remove_dir_all(&dir); - // This machine's own Windows has them. - let system32 = std::path::Path::new(&std::env::var_os("SystemRoot").unwrap()).join("System32"); - assert_eq!(missing_print_dll_in(&system32), None); +} + +/// #2630: on a Windows without the print DLLs, the public entry points answer before any call +/// into the delay-loaded DLLs: no printers, and printing refused naming the DLL. The job names a +/// printer that doesn't exist and the bytes aren't a PDF, so a missing guard fails here with +/// another error rather than reaching a real printer. +#[cfg(windows)] +#[test] +fn printing_without_the_print_dlls_is_refused_before_the_spooler() { + use crate::spool::{PRINT_DLLS, SYSTEM32_FOR_TEST, printers, submit, submit_with_progress}; + let dir = std::env::temp_dir().join(format!("pdfcraft-print-no-dlls-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + SYSTEM32_FOR_TEST.with(|d| *d.borrow_mut() = Some(dir.clone())); + let job = crate::spool::Job { printer: Some("PdfCraft test: no such printer".into()), ..Default::default() }; + let named = |r: Result| match r { + Err(crate::PrintError::Spool(m)) => m.contains(PRINT_DLLS[0]), + _ => false, + }; + assert!(named(submit(b"not a PDF", &job)), "submit names the missing DLL"); + let mut called = false; + assert!(named(submit_with_progress(b"not a PDF", &job, &mut |_, _| called = true)), "submit_with_progress names it"); + assert!(!called, "no sheet reached a spooler"); + assert!(printers().is_empty(), "no printers are listed"); + SYSTEM32_FOR_TEST.with(|d| *d.borrow_mut() = None); + let _ = std::fs::remove_dir_all(&dir); }