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,238 @@
|
||||
//! Real repeated jobs and edits; generated-file ownership is never inferred from suffixes.
|
||||
#[path = "support/native_fonts.rs"]
|
||||
mod native_fonts;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, Output},
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
};
|
||||
fn directory() -> PathBuf {
|
||||
static NEXT: AtomicU64 = AtomicU64::new(0);
|
||||
let p = std::env::temp_dir().join(format!(
|
||||
"texrra-generated-cli-{}-{}",
|
||||
std::process::id(),
|
||||
NEXT.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
fs::create_dir_all(&p).unwrap();
|
||||
p
|
||||
}
|
||||
fn hash(bytes: &[u8]) -> String {
|
||||
format!("{:x}", Sha256::digest(bytes))
|
||||
}
|
||||
fn native(root: &Path, entry: &str) -> Output {
|
||||
native_fonts::command()
|
||||
.env("TEXRRA_STATE_DIR", root.join("policy"))
|
||||
.args([
|
||||
"native",
|
||||
"--initex",
|
||||
"--etex",
|
||||
"--job",
|
||||
"--resource-manifest",
|
||||
"trace.json",
|
||||
entry,
|
||||
"job.pdf",
|
||||
])
|
||||
.current_dir(root)
|
||||
.output()
|
||||
.unwrap()
|
||||
}
|
||||
fn reference(root: &Path, entry: &str) -> Output {
|
||||
Command::new("pdftex")
|
||||
.args([
|
||||
"-ini",
|
||||
"-etex",
|
||||
"-interaction=nonstopmode",
|
||||
"-halt-on-error",
|
||||
entry,
|
||||
])
|
||||
.current_dir(root)
|
||||
.output()
|
||||
.unwrap()
|
||||
}
|
||||
fn source(name: &str, tail: &str) -> String {
|
||||
format!(
|
||||
r"\catcode123=1\catcode125=2\catcode35=6
|
||||
\pdfoutput=1\pdfpagewidth=320bp\pdfpageheight=240bp
|
||||
\output={{\shipout\box255}}\hsize=250pt\vsize=160pt\parindent=0pt
|
||||
\openin1={name} \ifeof1\count0=0\else\closein1\input {name}\relax\fi
|
||||
\immediate\openout0={name}\advance\count0 by1
|
||||
\immediate\write0{{\string\count0=\the\count0\string\relax}}\immediate\closeout0
|
||||
{tail}
|
||||
\font\rm=cmr10\rm\noindent Revision \the\count0.\par\end"
|
||||
)
|
||||
}
|
||||
#[test]
|
||||
fn repeated_jobs_external_artifact_edit_and_sibling_entry_match_real_initex() {
|
||||
let root = directory();
|
||||
for (i, name) in ["memo.dat", "generated.source", "not-special.aux"]
|
||||
.iter()
|
||||
.enumerate()
|
||||
{
|
||||
let n = root.join(format!("native-{i}"));
|
||||
let r = root.join(format!("reference-{i}"));
|
||||
fs::create_dir_all(&n).unwrap();
|
||||
fs::create_dir_all(&r).unwrap();
|
||||
for dir in [&n, &r] {
|
||||
fs::write(dir.join("main.tex"), source(name, "")).unwrap();
|
||||
}
|
||||
for pass in 0..4 {
|
||||
if pass == 2 {
|
||||
for dir in [&n, &r] {
|
||||
fs::write(dir.join(name), b"\\count0=40\\relax\n").unwrap();
|
||||
}
|
||||
}
|
||||
let entry = if pass == 3 { "sibling.tex" } else { "main.tex" };
|
||||
if pass == 3 {
|
||||
for dir in [&n, &r] {
|
||||
fs::write(dir.join(entry), source(name, "")).unwrap();
|
||||
}
|
||||
}
|
||||
let before = fs::read(n.join(name)).ok();
|
||||
let actual = native(&n, entry);
|
||||
let expected = reference(&r, entry);
|
||||
assert!(
|
||||
actual.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&actual.stderr)
|
||||
);
|
||||
assert!(
|
||||
expected.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&expected.stdout)
|
||||
);
|
||||
let bytes = fs::read(n.join(name)).unwrap();
|
||||
assert_eq!(bytes, fs::read(r.join(name)).unwrap());
|
||||
let trace: serde_json::Value =
|
||||
serde_json::from_slice(&fs::read(n.join("trace.json")).unwrap()).unwrap();
|
||||
let receipts = trace["generated_artifacts"]["writes"].as_array().unwrap();
|
||||
assert_eq!(receipts.len(), 1);
|
||||
assert_eq!(receipts[0]["after"]["sha256"], hash(&bytes));
|
||||
if let Some(previous) = before {
|
||||
assert_eq!(receipts[0]["before"]["sha256"], hash(&previous));
|
||||
assert!(
|
||||
trace["reads"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|v| v["sha256"] == hash(&previous)
|
||||
&& v["path"].as_str().unwrap().ends_with(name))
|
||||
);
|
||||
} else {
|
||||
assert!(receipts[0]["before"].is_null());
|
||||
}
|
||||
let ledger = fs::read(
|
||||
trace["generated_artifacts"]["ledger_path"]
|
||||
.as_str()
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
trace["generated_artifacts"]["ledger_current"]["sha256"],
|
||||
hash(&ledger)
|
||||
);
|
||||
assert!(fs::metadata(n.join("job.pdf")).unwrap().len() > 1000);
|
||||
}
|
||||
}
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn unknown_aux_and_internal_policy_remain_protected_with_existing_pdf() {
|
||||
let root = directory();
|
||||
fs::write(root.join("unowned.aux"), b"\\relax\n").unwrap();
|
||||
fs::write(
|
||||
root.join("main.tex"),
|
||||
r"\input unowned.aux\immediate\openout0=unowned.aux \end",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("job.pdf"), b"KEEP").unwrap();
|
||||
let initial = native(&root, "main.tex");
|
||||
assert!(!initial.status.success());
|
||||
let trace: serde_json::Value =
|
||||
serde_json::from_slice(&fs::read(root.join("trace.json")).unwrap()).unwrap();
|
||||
let ledger = PathBuf::from(
|
||||
trace["generated_artifacts"]["ledger_path"]
|
||||
.as_str()
|
||||
.unwrap(),
|
||||
);
|
||||
for body in [
|
||||
r"\input unowned.aux\immediate\openout0=unowned.aux".into(),
|
||||
format!(r#"\immediate\openout0="{}""#, ledger.display()),
|
||||
format!(
|
||||
r#"\immediate\openout0="{}""#,
|
||||
ledger.with_extension("lock").display()
|
||||
),
|
||||
] {
|
||||
fs::write(root.join("main.tex"), format!("{body} \\end")).unwrap();
|
||||
fs::write(root.join("job.pdf"), b"KEEP").unwrap();
|
||||
let output = native(&root, "main.tex");
|
||||
assert!(!output.status.success());
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("overwrite"));
|
||||
assert_eq!(fs::read(root.join("job.pdf")).unwrap(), b"KEEP");
|
||||
assert_eq!(fs::read(root.join("unowned.aux")).unwrap(), b"\\relax\n");
|
||||
}
|
||||
fs::write(root.join("main.tex"),r"\catcode123=1\catcode125=2\immediate\openout0=trace.json \immediate\write0{STREAM}\immediate\closeout0\end").unwrap();
|
||||
let collision = native(&root, "main.tex");
|
||||
assert!(
|
||||
!collision.status.success()
|
||||
&& String::from_utf8_lossy(&collision.stderr).contains("collides with a TeX stream")
|
||||
);
|
||||
assert_eq!(fs::read(root.join("trace.json")).unwrap(), b"STREAM\n");
|
||||
assert_eq!(fs::read(root.join("job.pdf")).unwrap(), b"KEEP");
|
||||
let lock = fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(ledger.with_extension("lock"))
|
||||
.unwrap();
|
||||
lock.try_lock().unwrap();
|
||||
fs::write(root.join("main.tex"), r"\end").unwrap();
|
||||
let log = fs::read(root.join("job.log")).unwrap();
|
||||
let blocked = native(&root, "main.tex");
|
||||
assert!(
|
||||
!blocked.status.success()
|
||||
&& String::from_utf8_lossy(&blocked.stderr).contains("already in use")
|
||||
);
|
||||
assert_eq!(fs::read(root.join("job.pdf")).unwrap(), b"KEEP");
|
||||
assert_eq!(fs::read(root.join("job.log")).unwrap(), log);
|
||||
drop(lock);
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn error_recovery_preserves_real_side_effect_ownership_and_recompiles() {
|
||||
let root = directory();
|
||||
let n = root.join("native");
|
||||
let r = root.join("reference");
|
||||
fs::create_dir_all(&n).unwrap();
|
||||
fs::create_dir_all(&r).unwrap();
|
||||
for dir in [&n, &r] {
|
||||
fs::write(
|
||||
dir.join("main.tex"),
|
||||
source("state.dat", r"\errmessage{STOP AFTER REAL WRITE}"),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
fs::write(n.join("job.pdf"), b"KEEP").unwrap();
|
||||
assert!(!native(&n, "main.tex").status.success());
|
||||
assert!(!reference(&r, "main.tex").status.success());
|
||||
assert_eq!(fs::read(n.join("job.pdf")).unwrap(), b"KEEP");
|
||||
assert_eq!(
|
||||
fs::read(n.join("state.dat")).unwrap(),
|
||||
fs::read(r.join("state.dat")).unwrap()
|
||||
);
|
||||
for dir in [&n, &r] {
|
||||
fs::write(dir.join("main.tex"), source("state.dat", "")).unwrap();
|
||||
}
|
||||
let result = native(&n, "main.tex");
|
||||
assert!(
|
||||
result.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&result.stderr)
|
||||
);
|
||||
assert!(reference(&r, "main.tex").status.success());
|
||||
assert_eq!(
|
||||
fs::read(n.join("state.dat")).unwrap(),
|
||||
fs::read(r.join("state.dat")).unwrap()
|
||||
);
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user