Show native preview during durable file publication
This commit is contained in:
1 parent
75d156f0cf
commit
eda01fb689
5 files changed
+439
-58
No files matched your search
@@ -417,6 +417,142 @@ fn resumed_suffix_reuses_search_for_unchanged_paragraphs() {
|
||||
fs::remove_dir_all(p).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provisional_preview_matches_published_pages_and_cancellation_keeps_artifacts() {
|
||||
#[derive(Clone, Copy)]
|
||||
enum Action {
|
||||
Supersede,
|
||||
#[cfg(unix)]
|
||||
Conflict,
|
||||
Publish,
|
||||
}
|
||||
|
||||
struct WatchDraft {
|
||||
compiler: NativeCompiler,
|
||||
p: PathBuf,
|
||||
previous: Arc<CompiledDocument>,
|
||||
artifacts: [Vec<u8>; 3],
|
||||
update: Mutex<Option<PreviewUpdate>>,
|
||||
action: Action,
|
||||
}
|
||||
impl CompileObserver for WatchDraft {
|
||||
fn stage(&self, _: u64, _: CompileStage) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draft_ready(&self, draft: DraftPreview<'_>) -> Result<(), String> {
|
||||
assert!(Arc::ptr_eq(
|
||||
&self.previous,
|
||||
&self.compiler.latest().unwrap()
|
||||
));
|
||||
for (name, expected) in ["job.pdf", "job.log", "state.dat"]
|
||||
.into_iter()
|
||||
.zip(&self.artifacts)
|
||||
{
|
||||
assert_eq!(&fs::read(self.p.join(name)).unwrap(), expected);
|
||||
}
|
||||
let preview = draft.preview_since(Some(self.previous.revision()))?;
|
||||
assert_eq!(
|
||||
preview.pages.iter().map(|p| p.index).collect::<Vec<_>>(),
|
||||
[1]
|
||||
);
|
||||
*self.update.lock().unwrap() = Some(preview);
|
||||
match self.action {
|
||||
Action::Supersede => {
|
||||
self.compiler.start();
|
||||
}
|
||||
#[cfg(unix)]
|
||||
Action::Conflict => {
|
||||
fs::hard_link(self.p.join("job.pdf"), self.p.join("conflicting.pdf")).unwrap();
|
||||
}
|
||||
Action::Publish => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let p = directory("provisional-preview");
|
||||
let body = r"
|
||||
\immediate\openout1=state.dat
|
||||
\immediate\write1{ORIGINAL}
|
||||
\immediate\closeout1
|
||||
\shipout\hbox{Stable first page}
|
||||
\shipout\hbox{Previous second page}
|
||||
\end
|
||||
";
|
||||
let mut definition = atomic_definition(&p, 1, body);
|
||||
definition.generated.insert(p.join("state.dat"));
|
||||
let project = ProjectSnapshot::new(definition).unwrap();
|
||||
let request = request(&p)
|
||||
.with_project(project.clone())
|
||||
.observe_generated_initial();
|
||||
let compiler = NativeCompiler::default().with_automatic_entry_checkpoints();
|
||||
let previous = compiler.compile(request.clone()).unwrap();
|
||||
let artifacts = ["job.pdf", "job.log", "state.dat"].map(|name| fs::read(p.join(name)).unwrap());
|
||||
let edit = format!(
|
||||
"{HEADER}{}",
|
||||
body.replace("ORIGINAL", "EDITED")
|
||||
.replace("Previous second page", "Current second page")
|
||||
);
|
||||
let changed = project
|
||||
.with_file_edits(
|
||||
2,
|
||||
BTreeMap::from([(p.join("main.tex"), Arc::from(edit.into_bytes()))]),
|
||||
)
|
||||
.unwrap();
|
||||
let mut request = request.with_project(changed);
|
||||
let mut cancelled = None;
|
||||
for action in [
|
||||
Action::Supersede,
|
||||
#[cfg(unix)]
|
||||
Action::Conflict,
|
||||
Action::Publish,
|
||||
] {
|
||||
let observer = Arc::new(WatchDraft {
|
||||
compiler: compiler.clone(),
|
||||
p: p.clone(),
|
||||
previous: previous.clone(),
|
||||
artifacts: artifacts.clone(),
|
||||
update: Mutex::new(None),
|
||||
action,
|
||||
});
|
||||
request.observer = Some(observer.clone());
|
||||
let result = compiler.compile_detailed(request.clone());
|
||||
let update = observer.update.lock().unwrap().take().unwrap();
|
||||
if !matches!(action, Action::Publish) {
|
||||
let failure = result.err().unwrap();
|
||||
if matches!(action, Action::Supersede) {
|
||||
assert!(matches!(failure.error, CompileError::Cancelled));
|
||||
assert_eq!(failure.phase, AttemptPhase::ReadyToPublish);
|
||||
} else {
|
||||
assert!(failure.error.to_string().contains("aliases"), "{failure:?}");
|
||||
assert_eq!(failure.phase, AttemptPhase::Publishing);
|
||||
fs::remove_file(p.join("conflicting.pdf")).unwrap();
|
||||
}
|
||||
assert!(Arc::ptr_eq(&previous, &compiler.latest().unwrap()));
|
||||
assert_eq!(
|
||||
["job.pdf", "job.log", "state.dat"].map(|name| fs::read(p.join(name)).unwrap()),
|
||||
artifacts,
|
||||
);
|
||||
cancelled = Some(update);
|
||||
} else {
|
||||
let document = result.unwrap();
|
||||
assert_eq!(
|
||||
update,
|
||||
document.preview_since(Some(previous.revision())).unwrap()
|
||||
);
|
||||
assert_eq!(update.pages, cancelled.as_ref().unwrap().pages);
|
||||
assert_eq!(
|
||||
document.pdf.as_deref(),
|
||||
Some(fs::read(p.join("job.pdf")).unwrap().as_slice())
|
||||
);
|
||||
assert_eq!(fs::read(p.join("state.dat")).unwrap(), b"EDITED\n");
|
||||
assert!(Arc::ptr_eq(&document, &compiler.latest().unwrap()));
|
||||
}
|
||||
}
|
||||
fs::remove_dir_all(p).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn library_cli_bytes_final_resource_closure_and_immutable_revision_match() {
|
||||
let p = directory("closure");
|
||||
|
||||
Reference in new issue
Block a user