use std::{fs, path::Path, process::Command}; use texrra_tex_core::execution::*; #[derive(Default)] struct Transcript(String); impl SourceProvider for Transcript { fn read(&mut self, name: &str, from: &str) -> Result { let path = Path::new(from).with_file_name(name); let data = fs::read(&path).map_err(|error| error.to_string())?; Ok(SourceFile { name: path.display().to_string(), data, }) } fn observe_diagnostic( &mut self, destination: WriteDestination, text: &str, ) -> Result<(), String> { assert_eq!(destination, WriteDestination::TerminalAndLog); self.0.push_str(text); Ok(()) } } fn compact(text: &str) -> String { text.chars().filter(|c| !c.is_whitespace()).collect() } #[test] fn file_boundary_warnings_follow_actual_save_and_condition_stacks() { let dir = std::env::temp_dir().join(format!("texrra-nesting-{}", std::process::id())); fs::create_dir_all(&dir).unwrap(); let cases = [ ( 1, r"\begingroup\iftrue\input child.tex\relax", "\\fi\n\\endgroup\n", vec![ "end of \\iftrue entered on line 3 of a different file", "end of semi simple group (level 1) entered at line 3 of a different file", ], ), ( 2, r"\input child.tex\relax\fi\endgroup", "\\begingroup\n\\iffalse\\else\n", vec![ "end of file when semi simple group (level 1) entered at line 1 is incomplete", "end of file when \\iffalse\\else entered on line 2 is incomplete", ], ), ( 1, r"\begingroup\iftrue\input child.tex\relax\fi\endgroup", "\\tracingnesting=0\\relax\\fi\\endgroup\n\\begingroup\\tracingnesting=1\\relax\\iftrue\n", vec![ "end of semi simple group (level 1) entered at line 3 of a different file", "end of file when semi simple group (level 1) entered at line 2 is incomplete", "end of file when \\iftrue entered on line 2 is incomplete", ], ), ( 0, r"\input child.tex\relax\fi\endgroup", "\\begingroup\\iftrue\n", vec![], ), ( 1, r"\scantokens{\begingroup\unless\iffalse}\fi\endgroup", "", vec![ "end of file when semi simple group (level 1) entered at line 1 is incomplete", "end of file when \\unless\\iffalse entered on line 1 is incomplete", ], ), ( 1, r"\everyeof{\fi\endgroup}\input child.tex\relax", "\\begingroup\\iftrue\n", vec![], ), ]; for (tracing, body, child, expected) in cases { let source = format!( "\\catcode123=1\\catcode125=2\n\\tracingnesting={tracing}\\relax\n{body}\n\\end\n" ); let file = dir.join("probe.tex"); fs::write(&file, &source).unwrap(); fs::write(dir.join("child.tex"), child).unwrap(); let reference = Command::new("pdftex") .args([ "-ini", "-etex", "-interaction=nonstopmode", "-halt-on-error", "probe.tex", ]) .current_dir(&dir) .output() .unwrap(); let terminal = String::from_utf8_lossy(&reference.stdout); assert!(reference.status.success(), "{terminal}"); let log = fs::read_to_string(dir.join("probe.log")).unwrap(); let mut native = Transcript::default(); Executor::new(State::initex().with_etex(), Limits::default()) .run( SourceFile { name: file.display().to_string(), data: source.into_bytes(), }, &mut native, ) .unwrap(); for text in [&*terminal, &log, &native.0] { let compact = compact(text); let warnings = compact.split("Warning:").skip(1).collect::>(); assert_eq!(warnings.len(), expected.len(), "{body}: {text}"); for (actual, expected) in warnings.into_iter().zip(&expected) { assert!( actual.starts_with(&self::compact(expected)), "{body}: {text}" ); } if tracing == 2 { assert!(compact.contains(r"l.3\iffalse\else"), "{body}: {text}"); } } } fs::remove_dir_all(dir).unwrap(); }