Files
texrra-engine/tests/native_resources.rs

319 lines
10 KiB
Rust

//! Real native resource lookup versus an independently configured INITEX path.
#[path = "support/native_fonts.rs"]
mod native_fonts;
use std::{
fs,
path::{Path, PathBuf},
process::Command,
sync::atomic::{AtomicU64, Ordering},
};
fn directory() -> PathBuf {
static ID: AtomicU64 = AtomicU64::new(0);
let p = std::env::temp_dir().join(format!(
"texrra-resource-cli-{}-{}",
std::process::id(),
ID.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&p).unwrap();
p
}
fn put(path: impl AsRef<Path>, data: impl AsRef<[u8]>) {
let p = path.as_ref();
fs::create_dir_all(p.parent().unwrap()).unwrap();
fs::write(p, data).unwrap();
}
fn native(dir: &Path, paths: &str, args: &[&str]) -> std::process::Output {
native_fonts::command()
.arg("native")
.args(args)
.current_dir(dir)
.env("TEXINPUTS", paths)
.env_remove("TEXRRA_TEXMF")
.output()
.unwrap()
}
#[test]
fn configured_native_paths_and_recorder_choices_match_real_initex() {
let root = directory();
for case in 0..5 {
let dir = root.join(case.to_string());
let a = dir.join("a");
let b = dir.join("b");
fs::create_dir_all(&a).unwrap();
fs::create_dir_all(&b).unwrap();
let (paths, chosen) = match case {
0 => {
put(a.join("choice"), br"\message{CHOSEN=BARE}\endinput");
put(a.join("choice.tex"), br"\message{CHOSEN=TEX}\endinput");
(a.display().to_string(), a.join("choice.tex"))
}
1 => {
put(a.join("choice"), br"\message{CHOSEN=BARE}\endinput");
put(b.join("choice.tex"), br"\message{CHOSEN=TEX}\endinput");
(format!("{}:{}", a.display(), b.display()), a.join("choice"))
}
2 => {
put(a.join("first/choice"), br"\message{CHOSEN=BARE}\endinput");
put(
a.join("second/choice.tex"),
br"\message{CHOSEN=TEX}\endinput",
);
(format!("{}//", a.display()), a.join("second/choice.tex"))
}
3 => {
put(a.join("choice.tex"), br"\message{CHOSEN=WRONG}\endinput");
put(
a.join("pkg/suffix/choice.tex"),
br"\message{CHOSEN=FILTERED}\endinput",
);
(
format!("{}//suffix", a.display()),
a.join("pkg/suffix/choice.tex"),
)
}
_ => {
put(
dir.join("choice.tex"),
br"\message{CHOSEN=DEFAULT}\endinput",
);
(format!("{}:", a.display()), dir.join("choice.tex"))
}
};
put(
dir.join("probe.tex"),
br"\catcode123=1\catcode125=2\input choice \end",
);
let reference = Command::new("pdftex")
.args([
"-ini",
"-etex",
"-interaction=nonstopmode",
"-halt-on-error",
"-recorder",
"./probe.tex",
])
.current_dir(&dir)
.env("TEXINPUTS", &paths)
.output()
.expect("real INITEX required");
assert!(
reference.status.success(),
"{}",
String::from_utf8_lossy(&reference.stdout)
);
let output = native(
&dir,
&paths,
&[
"--initex",
"--etex",
"--input-policy",
"tex",
"--resource-manifest",
"resources.json",
"./probe.tex",
"native.pdf",
],
);
assert!(
output.status.success(),
"case {case}: {}",
String::from_utf8_lossy(&output.stderr)
);
let manifest: serde_json::Value =
serde_json::from_slice(&fs::read(dir.join("resources.json")).unwrap()).unwrap();
assert_eq!(
manifest["reads"][1]["path"].as_str().unwrap(),
chosen.canonicalize().unwrap().to_str().unwrap()
);
let fls = fs::read_to_string(dir.join("probe.fls")).unwrap();
let reference_paths: Vec<_> = fls
.lines()
.filter_map(|line| line.strip_prefix("INPUT "))
.filter_map(|name| dir.join(name).canonicalize().ok())
.collect();
assert!(
reference_paths.contains(&chosen.canonicalize().unwrap()),
"{fls}"
);
let text = fs::read_to_string(&chosen).unwrap();
let marker = text
.strip_prefix("\\message{")
.unwrap()
.split('}')
.next()
.unwrap();
assert!(String::from_utf8_lossy(&output.stderr).contains(marker));
assert!(String::from_utf8_lossy(&reference.stdout).contains(marker));
}
fs::remove_dir_all(root).unwrap();
}
#[test]
fn execution_failure_keeps_real_lookup_trace_and_protects_sources_pdf_and_log() {
let dir = directory();
let source = br"\catcode123=1\catcode125=2\input absent \end";
put(dir.join("probe.tex"), source);
put(dir.join("existing.pdf"), b"KEEP");
let o = native(
&dir,
".",
&[
"--initex",
"--etex",
"--resource-manifest",
"resources.json",
"./probe.tex",
"existing.pdf",
],
);
assert!(!o.status.success());
assert_eq!(fs::read(dir.join("existing.pdf")).unwrap(), b"KEEP");
let a: serde_json::Value =
serde_json::from_slice(&fs::read(dir.join("resources.json")).unwrap()).unwrap();
assert_eq!(a["reads"].as_array().unwrap().len(), 1);
assert!(a["lookups"].as_array().unwrap().last().unwrap()["resolved"].is_null());
put(dir.join("probe.tex"), br"\end");
let source = fs::read(dir.join("probe.tex")).unwrap();
for target in ["probe.tex", "existing.pdf", "existing.log"] {
let o = native(
&dir,
".",
&[
"--initex",
"--resource-manifest",
target,
"./probe.tex",
"existing.pdf",
],
);
assert!(!o.status.success(), "{target}");
assert!(String::from_utf8_lossy(&o.stderr).contains("overwrite"));
assert_eq!(fs::read(dir.join("probe.tex")).unwrap(), source);
assert_eq!(fs::read(dir.join("existing.pdf")).unwrap(), b"KEEP");
}
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn original_distribution_entry_resolves_config_then_stops_at_real_pdf_consumer() {
let root = Command::new("kpsewhich")
.arg("--var-value=TEXMFDIST")
.output()
.expect("reference installation required");
assert!(root.status.success());
let root = String::from_utf8(root.stdout).unwrap();
let root = root.trim();
let dir = directory();
put(dir.join("existing.pdf"), b"KEEP");
let o = native(
&dir,
"",
&[
"--initex",
"--etex",
"--input-policy",
"tex",
"--texmf-root",
root,
"--resource-manifest",
"resources.json",
"pdflatex.ini",
"existing.pdf",
],
);
assert_eq!(o.status.code(), Some(1));
let error = String::from_utf8_lossy(&o.stderr);
assert!(
error.contains("native execution step limit exceeded"),
"{error}"
);
assert!(error.contains("expl3-code.tex:3183:1:"), "{error}");
assert!(!error.contains("not found"), "{error}");
assert_eq!(fs::read(dir.join("existing.pdf")).unwrap(), b"KEEP");
let a: serde_json::Value =
serde_json::from_slice(&fs::read(dir.join("resources.json")).unwrap()).unwrap();
let reads = a["reads"].as_array().unwrap();
assert!(
reads
.iter()
.any(|r| r["path"].as_str().unwrap().ends_with("/pdflatex.ini"))
);
assert!(
reads
.iter()
.any(|r| r["path"].as_str().unwrap().ends_with("/pdftexconfig.tex"))
);
for r in reads {
assert_eq!(r["sha256"].as_str().unwrap().len(), 64);
assert_eq!(
r["bytes"].as_u64().unwrap(),
fs::read(r["path"].as_str().unwrap()).unwrap().len() as u64
);
}
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn stream_trace_hashes_only_delivered_physical_bytes_and_records_actual_eof() {
let dir = directory();
put(dir.join("data.tex"), b"ONE\r\nTWO");
put(
dir.join("probe.tex"),
br"\openin0=data.tex \read0 to\a \read0 to\b \read0 to\c \end",
);
let o = native(
&dir,
".",
&[
"--initex",
"--resource-manifest",
"resources.json",
"./probe.tex",
"native.pdf",
],
);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
let a: serde_json::Value =
serde_json::from_slice(&fs::read(dir.join("resources.json")).unwrap()).unwrap();
let reads = a["stream_reads"].as_array().unwrap();
assert_eq!(reads.len(), 3);
for (range, offset, bytes, eof, digest) in [
(
&reads[0],
0,
5,
false,
"48048b6ec9ff8ea234ec030af5858ea87ea8583ee3a92e0457d8e0d1e965baa7",
),
(
&reads[1],
5,
3,
false,
"a1a8a8cbbed4eb53ae62ee4fb0787504087232c29aa4d817757d06b68d0501ca",
),
(
&reads[2],
8,
0,
true,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
),
] {
assert_eq!(
range["path"].as_str().unwrap(),
dir.join("data.tex")
.canonicalize()
.unwrap()
.to_str()
.unwrap()
);
assert_eq!(range["offset"], offset);
assert_eq!(range["bytes"], bytes);
assert_eq!(range["eof"], eof);
assert_eq!(range["sha256"], digest);
}
// Opening a stream must not invent a full-file version or read its future bytes.
assert_eq!(a["reads"].as_array().unwrap().len(), 1);
fs::remove_dir_all(dir).unwrap();
}