Files
texrra-engine/tests/native_generated_files.rs

332 lines
12 KiB
Rust

//! 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();
}
#[test]
fn redirected_show_uses_the_open_stream_without_setting_the_cli_error_status() {
let root = directory();
let oracle = directory();
let entry = "show-stream.tex";
let input = r"\catcode123=1\catcode125=2
\immediate\openout5=report.dat
{\showstream=5\def\shown{ABC}\show\shown\showthe\count0\showlists}
\immediate\closeout5
\end";
fs::write(root.join(entry), input).unwrap();
fs::write(oracle.join(entry), input).unwrap();
let expected = reference(&oracle, entry);
let actual = native(&root, entry);
assert!(expected.status.success());
assert!(actual.status.success(), "{actual:?}");
assert_eq!(
fs::read(root.join("report.dat")).unwrap(),
fs::read(oracle.join("report.dat")).unwrap()
);
let log = fs::read_to_string(root.join("job.log")).unwrap();
assert!(!log.contains("> \\shown"));
fs::remove_dir_all(root).unwrap();
fs::remove_dir_all(oracle).unwrap();
}
#[test]
fn automatic_kerns_preserve_raw_ligature_and_hyphen_margin_order() {
let root = directory();
let oracle = directory();
let entry = "automatic-kerns.tex";
let input = r"\catcode123=1 \catcode125=2
\pdfoutput=1
\font\roman=cmr10 \roman \hyphenchar\roman=45
\pdfprependkern=1 \pdfappendkern=1 \pdfprotrudechars=2
\knbccode\roman65=80 \knaccode\roman65=150
\knbccode\roman86=120 \knaccode\roman86=-50 \knbccode\roman12=200
\knbccode\roman45=95 \knaccode\roman45=105 \rpcode\roman45=100
\hsize=32pt \parindent=0pt \pretolerance=-1 \tolerance=10000 \exhyphenpenalty=0
\immediate\openout3=lists.dat \showstream=3
\showboxbreadth=100 \showboxdepth=10
\setbox0=\hbox{AV fi AA\-VV} \showbox0
\setbox1=\vbox{\noindent AA-VV VV\par} \showbox1
\immediate\closeout3
\end";
fs::write(root.join(entry), input).unwrap();
fs::write(oracle.join(entry), input).unwrap();
let expected = reference(&oracle, entry);
let actual = native(&root, entry);
assert!(expected.status.success(), "{expected:?}");
assert!(actual.status.success(), "{actual:?}");
assert_eq!(
fs::read_to_string(root.join("lists.dat")).unwrap(),
fs::read_to_string(oracle.join("lists.dat")).unwrap()
);
fs::remove_dir_all(root).unwrap();
fs::remove_dir_all(oracle).unwrap();
}
#[test]
fn redirected_showlists_preserves_live_nested_math_alignment_and_output() {
let root = directory();
let oracle = directory();
let entry = "activity.tex";
let input = r"\catcode37=14 \catcode38=4
\input math-fonts
\pdfoutput=1
\immediate\openout3=lists.dat \showstream=3
\showboxbreadth=30 \showboxdepth=10
\setbox0=\vbox{\halign{#&\hfil#\cr
A&
\hbox{$x_i^2+{a\over b}\showlists$}\cr}}
\vsize=14pt \output={\showlists\shipout\box255}
\hrule height10pt width4pt
\vskip2pt \penalty-10000
\immediate\closeout3 \end";
let fonts = include_bytes!("../examples/native-bootstrap/math-fonts.tex");
for path in [&root, &oracle] {
fs::write(path.join(entry), input).unwrap();
fs::write(path.join("math-fonts.tex"), fonts).unwrap();
}
let expected = reference(&oracle, entry);
let actual = native(&root, entry);
assert!(expected.status.success(), "{expected:?}");
assert!(actual.status.success(), "{actual:?}");
assert_eq!(
fs::read_to_string(root.join("lists.dat")).unwrap(),
fs::read_to_string(oracle.join("lists.dat")).unwrap()
);
fs::remove_dir_all(root).unwrap();
fs::remove_dir_all(oracle).unwrap();
}