Files
texrra-engine/tests/native_splitting.rs
T

180 lines
5.8 KiB
Rust

//! Real native split output/resources and strict protection at page boundaries.
#[path = "support/native_fonts.rs"]
mod native_fonts;
use std::{
fs,
path::PathBuf,
process::Command,
sync::atomic::{AtomicU64, Ordering},
};
fn directory() -> PathBuf {
static N: AtomicU64 = AtomicU64::new(0);
let p = std::env::temp_dir().join(format!(
"texrra-native-splitting-{}-{}",
std::process::id(),
N.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&p).unwrap();
p
}
#[test]
fn original_split_lists_reach_real_pdf_fonts_and_resource_trace() {
let p = directory();
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let source = root.join("examples/native-bootstrap/splitting.tex");
let before = fs::read(&source).unwrap();
let r = native_fonts::command()
.args([
"native",
"--initex",
"--etex",
"--resource-manifest",
"resources.json",
])
.arg(&source)
.arg("native.pdf")
.current_dir(&p)
.output()
.unwrap();
assert!(
r.status.success(),
"{p:?}: {}",
String::from_utf8_lossy(&r.stderr)
);
let log = fs::read_to_string(p.join("native.log")).unwrap();
assert!(log.contains("SPLIT-FIRST=FIRST/FIRST/SEVEN"), "{log}");
let pdf = fs::read(p.join("native.pdf")).unwrap();
assert!(pdf.starts_with(b"%PDF-"));
let text = Command::new("pdftotext")
.args(["-layout", "native.pdf", "-"])
.current_dir(&p)
.output()
.unwrap();
assert!(text.status.success());
let text = String::from_utf8(text.stdout).unwrap();
for expected in [
"Native splitting",
"First fragment:",
"Second fragment:",
"FIRST.",
"SEVEN.",
"The final material",
] {
assert!(text.contains(expected), "{text}");
}
let fonts = Command::new("pdffonts")
.arg("native.pdf")
.current_dir(&p)
.output()
.unwrap();
assert!(fonts.status.success());
let fonts = String::from_utf8(fonts.stdout).unwrap();
for f in ["CMR10", "CMMI10", "CMMI7"] {
assert!(fonts.contains(f), "{fonts}");
}
let trace: serde_json::Value =
serde_json::from_slice(&fs::read(p.join("resources.json")).unwrap()).unwrap();
let reads = trace["reads"].as_array().unwrap();
for suffix in [
"splitting.tex",
"math-fonts.tex",
"cmr10.tfm",
"cmmi7.tfm",
"cmr10.pfb",
"cmmi7.pfb",
] {
assert!(
reads
.iter()
.any(|r| r["path"].as_str().unwrap().ends_with(suffix)),
"missing actual resource {suffix}: {trace}"
);
}
assert_eq!(fs::read(&source).unwrap(), before);
fs::remove_dir_all(p).unwrap();
}
#[test]
fn outer_marks_and_invalid_splits_protect_existing_pdf_and_original_source() {
let p = directory();
for (body, diagnostic) in [
(
r"\mark{OUTER}\end",
"outer marks require native page ownership",
),
(
r"\setbox1=\hbox{}\setbox0=\vsplit1 to0pt\end",
"native vsplit needs a vbox",
),
(
r"\setbox1=\vbox{\mark{\noexpand\bad x}\hbox{}}\setbox0=\vsplit1 to0pt\relax\splitfirstmark\end",
"undefined native control sequence",
),
] {
let source = format!("\\catcode123=1\\catcode125=2\n{body}\n");
fs::write(p.join("probe.tex"), &source).unwrap();
fs::write(p.join("native.pdf"), b"KEEP").unwrap();
let r = native_fonts::command()
.args(["native", "--initex", "--etex", "probe.tex", "native.pdf"])
.current_dir(&p)
.output()
.unwrap();
assert!(!r.status.success());
assert!(
String::from_utf8_lossy(&r.stderr).contains(diagnostic),
"{}",
String::from_utf8_lossy(&r.stderr)
);
assert_eq!(fs::read(p.join("native.pdf")).unwrap(), b"KEEP");
assert_eq!(fs::read_to_string(p.join("probe.tex")).unwrap(), source);
}
fs::remove_dir_all(p).unwrap();
}
#[test]
fn actual_high_byte_mark_diagnostics_match_initex_in_both_channels() {
let p = directory();
let source = format!(
r"\catcode123=1\catcode125=2\catcode94=7\catcode200=11\showboxbreadth=100\showboxdepth=30\tracingonline=1\setbox0=\vbox{{\mark{{{}}}\hbox{{}}}}\showbox0\end",
r"\noexpand\^^c8 ".repeat(30)
);
fs::write(p.join("probe.tex"), &source).unwrap();
fs::write(p.join("native.pdf"), b"KEEP").unwrap();
let n = native_fonts::command()
.args(["native", "--initex", "--etex", "probe.tex", "native.pdf"])
.current_dir(&p)
.output()
.unwrap();
assert_eq!(
n.status.code(),
Some(1),
"{}",
String::from_utf8_lossy(&n.stderr)
);
assert!(!String::from_utf8_lossy(&n.stderr).contains("error:"));
let r = Command::new("pdftex")
.args(["-ini", "-etex", "-interaction=nonstopmode", "probe.tex"])
.current_dir(&p)
.output()
.unwrap();
assert_eq!(r.status.code(), Some(1));
fn body(s: &str) -> &str {
s.split_once("> \\box0=")
.unwrap()
.1
.split_once("! OK.")
.unwrap()
.0
}
let log = fs::read_to_string(p.join("native.log")).unwrap();
let reference = fs::read_to_string(p.join("probe.log")).unwrap();
assert_eq!(body(&log), body(&reference));
assert_eq!(
body(std::str::from_utf8(&n.stderr).unwrap()),
body(std::str::from_utf8(&r.stdout).unwrap())
);
assert_eq!(body(&log).matches(r"\^^c8 ").count(), 12);
assert_eq!(fs::read(p.join("native.pdf")).unwrap(), b"KEEP");
assert_eq!(fs::read_to_string(p.join("probe.tex")).unwrap(), source);
fs::remove_dir_all(p).unwrap();
}