239 lines
8.6 KiB
Rust
239 lines
8.6 KiB
Rust
//! Live terminal/log/status and output protection around native show diagnostics.
|
|
#[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-context-cli-{}-{}",
|
|
std::process::id(),
|
|
ID.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&p).unwrap();
|
|
p
|
|
}
|
|
fn native(dir: &Path) -> std::process::Output {
|
|
native_fonts::command()
|
|
.args(["native", "--initex", "--etex", "probe.tex", "native.pdf"])
|
|
.current_dir(dir)
|
|
.output()
|
|
.unwrap()
|
|
}
|
|
fn reference(dir: &Path, fatal: bool) -> String {
|
|
let mut c = Command::new("pdftex");
|
|
c.args(["-ini", "-etex", "-interaction=nonstopmode"]);
|
|
if fatal {
|
|
c.arg("-halt-on-error");
|
|
}
|
|
let o = c
|
|
.arg("probe.tex")
|
|
.current_dir(dir)
|
|
.output()
|
|
.expect("real INITEX required");
|
|
assert_eq!(o.status.code(), Some(1));
|
|
String::from_utf8(o.stdout).unwrap()
|
|
}
|
|
#[test]
|
|
fn real_show_diagnostics_match_both_channels_and_continue_file_io_and_pdf_with_status_one() {
|
|
let dir = directory();
|
|
let source = "\\catcode123=1\\catcode125=2\\catcode35=6\\errorcontextlines=100\n\\font\\rm=cmr10\\rm\\hsize=200pt\n\\long\\def\\A#1{hello #1}\\show\\A\n\\count0=13\\showthe\\count0\\relax\n\\message{AFTER_DIAGNOSTICS}\\immediate\\openout1=real.aux \\immediate\\write1{ACTUAL=\\the\\count0}\\immediate\\closeout1\n\\noindent Still renders after diagnostics.\\par\\end\n";
|
|
fs::write(dir.join("probe.tex"), source).unwrap();
|
|
let r = reference(&dir, false);
|
|
let n = native(&dir);
|
|
assert_eq!(n.status.code(), Some(1));
|
|
let terminal = String::from_utf8(n.stderr).unwrap();
|
|
assert!(!terminal.contains("error:"), "{terminal}");
|
|
let exact = &r[r.find("> ").unwrap()..r.find("AFTER_DIAGNOSTICS").unwrap()];
|
|
let log = fs::read_to_string(dir.join("native.log")).unwrap();
|
|
assert!(
|
|
terminal.contains(exact),
|
|
"native:\n{terminal}\nreference:\n{r}"
|
|
);
|
|
let reference_log = fs::read_to_string(dir.join("probe.log")).unwrap();
|
|
let exact_log = &reference_log
|
|
[reference_log.find("> ").unwrap()..reference_log.find("AFTER_DIAGNOSTICS").unwrap()];
|
|
assert!(log.contains(exact_log), "{log}\n{reference_log}");
|
|
assert_eq!(
|
|
fs::read_to_string(dir.join("real.aux")).unwrap(),
|
|
"ACTUAL=13\n"
|
|
);
|
|
assert!(
|
|
fs::read(dir.join("native.pdf"))
|
|
.unwrap()
|
|
.starts_with(b"%PDF-")
|
|
);
|
|
let text = Command::new("pdftotext")
|
|
.args(["native.pdf", "-"])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert!(text.status.success());
|
|
assert_eq!(
|
|
String::from_utf8(text.stdout)
|
|
.unwrap()
|
|
.split_whitespace()
|
|
.collect::<Vec<_>>(),
|
|
["Still", "renders", "after", "diagnostics."]
|
|
);
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|
|
#[test]
|
|
fn fatal_after_show_keeps_existing_pdf_and_logs_actual_context_without_becoming_legal_show_status()
|
|
{
|
|
let dir = directory();
|
|
let source = "\\catcode123=1\\catcode125=2\\catcode35=6\\errorcontextlines=100\n\\show\\relax\n\\def\\A#1{#1\\relax}\\A{\\bad\\relax}\\end\n";
|
|
fs::write(dir.join("probe.tex"), source).unwrap();
|
|
fs::write(dir.join("native.pdf"), b"KEEP").unwrap();
|
|
let r = reference(&dir, false);
|
|
let n = native(&dir);
|
|
assert_eq!(n.status.code(), Some(1));
|
|
let terminal = String::from_utf8(n.stderr).unwrap();
|
|
assert!(terminal.contains("error:"));
|
|
assert!(terminal.contains("undefined native control sequence"));
|
|
assert!(!terminal.contains("\nUNREACHABLE\n"));
|
|
let exact = r
|
|
.split_once("! Undefined control sequence.\n")
|
|
.unwrap()
|
|
.1
|
|
.split_once(" )\n")
|
|
.unwrap()
|
|
.0;
|
|
assert!(terminal.contains(exact), "{terminal}\n{r}");
|
|
let log = fs::read_to_string(dir.join("native.log")).unwrap();
|
|
assert!(log.contains(exact));
|
|
assert!(log.contains("! undefined native control sequence"));
|
|
assert_eq!(fs::read(dir.join("native.pdf")).unwrap(), b"KEEP");
|
|
assert_eq!(fs::read_to_string(dir.join("probe.tex")).unwrap(), source);
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn real_read_rejects_outer_tokens_and_readline_keeps_the_same_file_bytes_literal() {
|
|
let dir = directory();
|
|
let data = b"\\A\n";
|
|
fs::write(dir.join("data.tex"), data).unwrap();
|
|
for (primitive, success) in [("read", false), ("readline", true)] {
|
|
let source = format!(
|
|
"\\catcode123=1\\catcode125=2\\endlinechar=-1\n\\outer\\def\\A{{YES}}\\openin0=data.tex \\{primitive}0 to\\B \\message{{READLINE=\\meaning\\B}}\\end\n"
|
|
);
|
|
fs::write(dir.join("probe.tex"), &source).unwrap();
|
|
fs::write(dir.join("native.pdf"), b"KEEP").unwrap();
|
|
let r = Command::new("pdftex")
|
|
.args([
|
|
"-ini",
|
|
"-etex",
|
|
"-interaction=nonstopmode",
|
|
"-halt-on-error",
|
|
"probe.tex",
|
|
])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.expect("real INITEX required");
|
|
let n = native_fonts::command()
|
|
.args([
|
|
"native",
|
|
"--initex",
|
|
"--etex",
|
|
"--resource-manifest",
|
|
"trace.json",
|
|
"probe.tex",
|
|
"native.pdf",
|
|
])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert_eq!(
|
|
r.status.success(),
|
|
success,
|
|
"{}",
|
|
String::from_utf8_lossy(&r.stdout)
|
|
);
|
|
assert_eq!(
|
|
n.status.success(),
|
|
success,
|
|
"{}",
|
|
String::from_utf8_lossy(&n.stderr)
|
|
);
|
|
let terminal = String::from_utf8(n.stderr).unwrap();
|
|
if success {
|
|
assert!(terminal.contains(r"READLINE=macro:->\A"), "{terminal}");
|
|
assert!(
|
|
String::from_utf8(r.stdout)
|
|
.unwrap()
|
|
.contains(r"READLINE=macro:->\A")
|
|
);
|
|
} else {
|
|
assert!(terminal.contains("forbidden outer"), "{terminal}");
|
|
assert!(terminal.contains("data.tex:1:1"), "{terminal}");
|
|
}
|
|
let trace: serde_json::Value =
|
|
serde_json::from_slice(&fs::read(dir.join("trace.json")).unwrap()).unwrap();
|
|
assert_eq!(trace["stream_reads"][0]["bytes"], data.len());
|
|
assert_eq!(fs::read(dir.join("data.tex")).unwrap(), data);
|
|
assert_eq!(fs::read(dir.join("native.pdf")).unwrap(), b"KEEP");
|
|
}
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn interaction_modes_route_actual_messages_like_initex() {
|
|
let dir = directory();
|
|
let source = r"\catcode123=1\catcode125=2
|
|
\message{START=\the\interactionmode}
|
|
{\interactionmode=0\message{HIDDEN=\the\interactionmode}}
|
|
\ifnum\interactionmode=0 \message{AFTERGROUP=0}\fi
|
|
\nonstopmode\message{VISIBLE=\the\interactionmode}
|
|
\def\modereport{\message{ASSIGNED=\the\interactionmode}}
|
|
\afterassignment\modereport\scrollmode\message{SCROLL=\the\interactionmode}
|
|
\errorstopmode\message{ERRORSTOP=\the\interactionmode}
|
|
\global\batchmode\message{BATCH=\the\interactionmode}
|
|
\end";
|
|
fs::write(dir.join("probe.tex"), source).unwrap();
|
|
let oracle = Command::new("pdftex")
|
|
.args(["-ini", "-etex", "-interaction=nonstopmode", "probe.tex"])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.expect("real INITEX required");
|
|
assert!(
|
|
oracle.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&oracle.stdout)
|
|
);
|
|
let actual = native(&dir);
|
|
assert!(
|
|
actual.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&actual.stderr)
|
|
);
|
|
let oracle_terminal = String::from_utf8(oracle.stdout).unwrap();
|
|
let native_terminal = String::from_utf8(actual.stderr).unwrap();
|
|
let oracle_log = fs::read_to_string(dir.join("probe.log")).unwrap();
|
|
let native_log = fs::read_to_string(dir.join("native.log")).unwrap();
|
|
for message in [
|
|
"START=1",
|
|
"HIDDEN=0",
|
|
"AFTERGROUP=0",
|
|
"VISIBLE=1",
|
|
"ASSIGNED=2",
|
|
"SCROLL=2",
|
|
"ERRORSTOP=3",
|
|
"BATCH=0",
|
|
] {
|
|
assert!(
|
|
oracle_log.contains(message) && native_log.contains(message),
|
|
"{message}:\n{native_log}"
|
|
);
|
|
assert_eq!(
|
|
oracle_terminal.contains(message),
|
|
native_terminal.contains(message),
|
|
"{message}:\n{native_terminal}"
|
|
);
|
|
}
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|