use std::collections::{HashMap, HashSet};
use std::default::Default;
use std::ffi::{OsStr, OsString};
use std::str::FromStr;
use tectonic::digest::DigestData;
use tectonic::engines::IoEventBackend;
use tectonic::io::filesystem::{FilesystemIo, FilesystemPrimaryInputIo};
use tectonic::io::{IoStack, MemoryIo};
use tectonic::status::NoopStatusBackend;
use tectonic::TexEngine;
mod util;
use crate::util::test_path;
const DEBUG: bool = false;
#[derive(Clone, Debug, Eq, PartialEq)]
struct FileSummary {
write_digest: Option<DigestData>,
}
impl FileSummary {
fn new() -> FileSummary {
FileSummary { write_digest: None }
}
}
struct FormatTestEvents(HashMap<OsString, FileSummary>);
impl FormatTestEvents {
fn new() -> FormatTestEvents {
FormatTestEvents(HashMap::new())
}
}
impl IoEventBackend for FormatTestEvents {
fn output_opened(&mut self, name: &OsStr) {
self.0.insert(name.to_os_string(), FileSummary::new());
}
fn output_closed(&mut self, name: OsString, digest: DigestData) {
let summ = self
.0
.get_mut(&name)
.expect("closing file that wasn't opened?");
summ.write_digest = Some(digest);
}
}
fn test_format_generation(texname: &str, fmtname: &str, sha256: &str) {
util::set_test_root();
let mut p = test_path(&["assets"]);
let fs_allow_writes = DEBUG;
let mut fs_support = FilesystemIo::new(&p, fs_allow_writes, false, HashSet::new());
p.push(texname);
let mut fs_primary = FilesystemPrimaryInputIo::new(&p);
let mem_stdout_allowed = !DEBUG;
let mut mem = MemoryIo::new(mem_stdout_allowed);
let mut events = FormatTestEvents::new();
use tectonic::io::GenuineStdoutIo;
let mut stdout = GenuineStdoutIo::new();
{
let mut io = if DEBUG {
IoStack::new(vec![&mut stdout, &mut fs_primary, &mut fs_support])
} else {
IoStack::new(vec![&mut mem, &mut fs_primary, &mut fs_support])
};
TexEngine::new()
.initex_mode(true)
.process(
&mut io,
&mut events,
&mut NoopStatusBackend::new(),
"unused.fmt",
texname,
&Default::default(),
)
.unwrap();
}
let want_digest = DigestData::from_str(sha256).unwrap();
for (name, info) in &events.0 {
if name.to_string_lossy() == fmtname {
let observed = info.write_digest.unwrap();
if observed != want_digest {
println!(
"expected {} to have SHA256 = {}",
fmtname,
want_digest.to_string()
);
println!("instead, got {}", observed.to_string());
panic!();
}
}
}
}
#[test]
fn plain_format() {
test_format_generation(
"plain.tex",
"plain.fmt",
"8e33c4c9af66ddb064a36749db1e0ba681bbebd1a896d2886745a0efa9a745a1",
)
}