Honor PDF draft mode in native execution and publication

This commit is contained in:
gloamxun committed 2026-11-27 22:48:48 +08:00
1 parent af19a26c23
commit f14669ae39
13 files changed
+206 -30

No files matched your search

+93
View File
@@ -18,6 +18,99 @@ fn directory() -> PathBuf {
p
}
#[test]
fn pdf_draft_preserves_existing_output_and_writes_real_auxiliary_data() {
let p = directory();
let source = r"\catcode123=1 \catcode125=2
\pdfoutput=1 \pdfdraftmode=1 \pdfminorversion=5
\pdfobjcompresslevel=3 \pdfcompresslevel=8
\immediate\openout0=probe.aux
\shipout\hbox{\vrule width1pt height2pt
\write0{MODE=\the\pdfdraftmode;ZIP=\the\pdfcompresslevel;}}
\immediate\closeout0
\end";
fs::write(p.join("probe.tex"), source).unwrap();
let published = p.join("probe.pdf");
fs::write(&published, b"previously published bytes").unwrap();
let output = native_fonts::command()
.args(["native", "--job", "--initex", "--etex", "probe.tex", "probe.pdf"])
.current_dir(&p)
.output()
.unwrap();
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
let log = fs::read_to_string(p.join("probe.log")).unwrap();
assert!(log.contains(r"\pdfdraftmode enabled, not changing output pdf"));
assert_eq!(fs::read(&published).unwrap(), b"previously published bytes");
assert_eq!(fs::read(p.join("probe.aux")).unwrap(), b"MODE=1;ZIP=0;\n");
fs::remove_dir_all(p).unwrap();
}
#[test]
fn live_pdf_draft_updates_auxiliary_files_without_replacing_the_published_pdf() {
use std::sync::Arc;
use texrra_engine::native::{
CompileRequest, Entry, NativeCompiler, ProjectDefinition, ProjectSnapshot,
};
use texrra_tex_core::{execution::JobTime, resources::WorldConfig};
let root = directory();
let entry = root.join("probe.tex");
let settings = root.join("settings.tex");
let configuration = WorldConfig::new(&root);
let prefix = r"\catcode123=1 \catcode125=2
\pdfoutput=1 \pdfminorversion=5 \pdfobjcompresslevel=1
";
let line = format!("{}\n", "\\relax ".repeat(26));
let body = r"\input settings.tex
\immediate\openout0=probe.aux
\shipout\hbox{\vrule width4pt height5pt \write0{DRAFT=\the\pdfdraftmode;}}
\immediate\closeout0
\end";
let source = format!("{prefix}{}{body}", line.repeat(40));
let project = ProjectSnapshot::new(ProjectDefinition {
configuration: configuration.clone(),
revision: 1,
roots: [root.clone()].into(),
files: [
(entry.clone(), Arc::<[u8]>::from(source.into_bytes())),
(settings.clone(), Arc::from(br"\pdfdraftmode=0".as_slice())),
].into(),
directories: Default::default(),
aliases: Default::default(),
generated_roots: [root.clone()].into(),
generated: Default::default(),
}).unwrap();
let draft_input = project.with_file_edits(2, [(settings.clone(), Arc::from(br"\pdfdraftmode=1".as_slice()))].into()).unwrap();
let restored_input = draft_input.with_file_edits(3, [(settings, Arc::from(br"\pdfdraftmode=0".as_slice()))].into()).unwrap();
let time = JobTime::new(2026, 11, 27, 600).unwrap().with_pdf_clock(0, 0).unwrap();
let mut request = CompileRequest::new(configuration, entry, "probe.pdf", time);
request.entry = Entry::Initex;
request.etex = true;
let live = NativeCompiler::default().with_automatic_entry_checkpoints();
let first = live.compile(request.clone().with_project(project.clone())).unwrap();
// The first round establishes ownership of the newly generated aux file.
let settled = live.compile(request.clone().with_project(project)).unwrap();
assert_eq!(first.pdf, settled.pdf);
let previous_pdf = settled.pdf.as_deref().unwrap();
let draft = live.compile(request.clone().with_project(draft_input.clone())).unwrap();
assert!(draft.entry_checkpoint_work.reused_steps > 0,
"settled {:?}; draft {:?}", settled.entry_checkpoint_work, draft.entry_checkpoint_work);
assert_eq!(draft.pages.len(), 1);
assert!(draft.execution.execution.state.pdf_draft_mode());
assert!(draft.pdf.is_none() && draft.output.is_none());
assert_eq!(fs::read(root.join("probe.pdf")).unwrap(), previous_pdf);
let aux = fs::read(root.join("probe.aux")).unwrap();
assert_eq!(aux, b"DRAFT=1;\n");
let cold = NativeCompiler::default().compile(request.clone().with_project(draft_input)).unwrap();
assert_eq!(draft.pdf, cold.pdf);
assert_eq!(draft.source_map.pages, cold.source_map.pages);
assert_eq!(fs::read(root.join("probe.aux")).unwrap(), aux);
let output = live.compile(request.with_project(restored_input)).unwrap();
assert_eq!(output.pdf.as_deref(), Some(previous_pdf));
assert_eq!(fs::read(root.join("probe.aux")).unwrap(), b"DRAFT=0;\n");
fs::remove_dir_all(root).unwrap();
}
#[test]
fn reproducible_pdf_dates_keep_register_policy_and_reject_invalid_epochs() {
use chrono::{Datelike, Local};