Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,233 @@
|
||||
use texrra_tex_core::execution::*;
|
||||
|
||||
struct Host {
|
||||
cancellation: Cancellation,
|
||||
messages: Vec<String>,
|
||||
}
|
||||
|
||||
impl SourceProvider for Host {
|
||||
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
|
||||
Err(format!("unexpected input {name}"))
|
||||
}
|
||||
|
||||
fn observe_message(&mut self, text: &str) -> Result<(), String> {
|
||||
self.messages.push(text.to_owned());
|
||||
self.cancellation.cancel();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancellation_stops_a_running_job_before_later_effects() {
|
||||
let cancellation = Cancellation::default();
|
||||
let mut host = Host {
|
||||
cancellation: cancellation.clone(),
|
||||
messages: Vec::new(),
|
||||
};
|
||||
let source = SourceFile {
|
||||
name: "probe.tex".into(),
|
||||
data: br"\catcode123=1 \catcode125=2 \message{FIRST}\message{SECOND}\end".to_vec(),
|
||||
};
|
||||
let job = || {
|
||||
Executor::new(State::initex(), Limits::default()).with_cancellation(cancellation.clone())
|
||||
};
|
||||
let interrupted = job().run(source.clone(), &mut host).unwrap_err();
|
||||
assert!(
|
||||
interrupted.message.contains("compilation cancelled"),
|
||||
"{interrupted}"
|
||||
);
|
||||
assert_eq!(host.messages, ["FIRST"]);
|
||||
let cancelled_before_start = job().run(source, &mut host).unwrap_err();
|
||||
assert!(
|
||||
cancelled_before_start
|
||||
.message
|
||||
.contains("compilation cancelled")
|
||||
);
|
||||
assert_eq!(host.messages, ["FIRST"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn obsolete_revision_cannot_publish_over_a_newer_job() {
|
||||
struct RevisingHost {
|
||||
session: NativeSession,
|
||||
new_revision: Option<RevisionJob>,
|
||||
}
|
||||
impl SourceProvider for RevisingHost {
|
||||
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
|
||||
Err(format!("unexpected input {name}"))
|
||||
}
|
||||
fn observe_message(&mut self, text: &str) -> Result<(), String> {
|
||||
if text == "REVISE" {
|
||||
self.new_revision = Some(self.session.start());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let session = NativeSession::default();
|
||||
let executor = || Executor::new(State::initex(), Limits::default());
|
||||
let source = |text: &str| SourceFile {
|
||||
name: "doc.tex".into(),
|
||||
data: text.as_bytes().into(),
|
||||
};
|
||||
let mut host = RevisingHost {
|
||||
session: session.clone(),
|
||||
new_revision: None,
|
||||
};
|
||||
let first = session.start();
|
||||
let first_number = first.revision();
|
||||
let first_result = first.run(
|
||||
executor(),
|
||||
source(r"\catcode123=1 \catcode125=2 \message{REVISE}\end"),
|
||||
&mut host,
|
||||
);
|
||||
assert!(matches!(first_result, Err(RevisionError::Cancelled)));
|
||||
assert!(session.latest().is_none());
|
||||
let second = host.new_revision.take().unwrap();
|
||||
assert!(second.revision() > first_number);
|
||||
session.cancel(first_number);
|
||||
let second = second
|
||||
.run(executor(), source(r"\count0=24\end"), &mut host)
|
||||
.unwrap();
|
||||
assert_eq!(session.latest().unwrap().revision, second.revision);
|
||||
assert_eq!(second.execution.state.count(0), 24);
|
||||
let mut artifact = "previous";
|
||||
assert_eq!(
|
||||
session.publish_current(second.revision, || {
|
||||
artifact = "current";
|
||||
Ok::<_, ()>(())
|
||||
}),
|
||||
Ok(Some(()))
|
||||
);
|
||||
let third = session.start();
|
||||
session.cancel(third.revision());
|
||||
assert!(matches!(
|
||||
third.run(executor(), source(r"\end"), &mut host),
|
||||
Err(RevisionError::Cancelled)
|
||||
));
|
||||
assert_eq!(
|
||||
session.publish_current(second.revision, || {
|
||||
artifact = "obsolete";
|
||||
Ok::<_, ()>(())
|
||||
}),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(artifact, "current");
|
||||
assert_eq!(session.latest().unwrap().revision, second.revision);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edited_source_versions_and_pages_match_independent_full_jobs() {
|
||||
use std::{collections::BTreeMap, path::Path};
|
||||
use texrra_tex_core::resources::{
|
||||
FileWorld, LoadedResource, ResourceKind, ResourceTrace, SearchPath, WorldConfig,
|
||||
};
|
||||
|
||||
struct Snapshot {
|
||||
sources: BTreeMap<&'static str, SourceFile>,
|
||||
world: FileWorld,
|
||||
}
|
||||
impl SourceProvider for Snapshot {
|
||||
fn execution_resource_trace(&self) -> Option<&ResourceTrace> {
|
||||
Some(self.world.trace())
|
||||
}
|
||||
|
||||
fn resource(
|
||||
&mut self,
|
||||
kind: ResourceKind,
|
||||
name: &str,
|
||||
from: &str,
|
||||
) -> Result<LoadedResource, String> {
|
||||
self.world.read(kind, name, Path::new(from))
|
||||
}
|
||||
|
||||
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
|
||||
self.sources
|
||||
.get(name)
|
||||
.cloned()
|
||||
.ok_or_else(|| name.to_owned())
|
||||
}
|
||||
}
|
||||
let source = |name: &str, text: &str| SourceFile {
|
||||
name: name.into(),
|
||||
data: text.as_bytes().into(),
|
||||
};
|
||||
let session = NativeSession::default();
|
||||
let executor = || Executor::new(State::default(), Limits::default()).with_native_pages();
|
||||
let mut prior: Option<std::sync::Arc<RevisionExecution>> = None;
|
||||
for (index, (body, macro_body, input, size, aux)) in [
|
||||
("Body", "Macro", "Input", 10, "Reference"),
|
||||
("Edited", "Macro", "Input", 10, "Reference"),
|
||||
("Edited", "Changed", "Input", 10, "Reference"),
|
||||
("Edited", "Changed", "Replacement", 10, "Reference"),
|
||||
("Edited", "Changed", "Replacement", 12, "Reference"),
|
||||
("Edited", "Changed", "Replacement", 12, "Resolved"),
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let entry = source(
|
||||
"doc.tex",
|
||||
&format!(
|
||||
r"\font\body=cmr10 at {size}pt \body
|
||||
\def\phrase{{{macro_body}}}
|
||||
\shipout\hbox{{{body} \phrase\input part.tex \input doc.aux }}
|
||||
\end"
|
||||
),
|
||||
);
|
||||
let snapshot = || {
|
||||
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
|
||||
let mut config = WorldConfig::new(root);
|
||||
config.paths.insert(
|
||||
ResourceKind::Tfm,
|
||||
vec![SearchPath::directory(root.join("assets"), false)],
|
||||
);
|
||||
Snapshot {
|
||||
sources: BTreeMap::from([
|
||||
("part.tex", source("part.tex", input)),
|
||||
("doc.aux", source("doc.aux", aux)),
|
||||
]),
|
||||
world: FileWorld::new(config).unwrap(),
|
||||
}
|
||||
};
|
||||
let mut clean_files = snapshot();
|
||||
let clean = executor().run(entry.clone(), &mut clean_files).unwrap();
|
||||
let result = session
|
||||
.start()
|
||||
.run(executor(), entry, &mut snapshot())
|
||||
.unwrap();
|
||||
assert_eq!(result.execution, clean);
|
||||
let resources = result.execution_resources.as_ref().unwrap();
|
||||
assert_eq!(resources, clean_files.world.trace());
|
||||
assert!(resources.reads.iter().any(|resource| {
|
||||
resource.path.ends_with("cmr10.tfm")
|
||||
&& resource.bytes > 0
|
||||
&& resource.sha256.len() == 64
|
||||
}));
|
||||
assert_eq!(result.revision, index as u64 + 1);
|
||||
assert_eq!(result.source_versions.len(), 3);
|
||||
assert_eq!(result.execution.shipout_display_lists().unwrap().len(), 1);
|
||||
if let Some(prior) = prior {
|
||||
let changed: Vec<_> = prior
|
||||
.source_versions
|
||||
.iter()
|
||||
.zip(&result.source_versions)
|
||||
.enumerate()
|
||||
.filter_map(|(i, (a, b))| (a != b).then_some(i))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
changed,
|
||||
[match index {
|
||||
3 => 1,
|
||||
5 => 2,
|
||||
_ => 0,
|
||||
}]
|
||||
);
|
||||
assert_ne!(
|
||||
prior.execution.shipout_display_lists().unwrap(),
|
||||
result.execution.shipout_display_lists().unwrap()
|
||||
);
|
||||
}
|
||||
prior = Some(result);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user