#![allow(dead_code)]
use flate2::read::GzDecoder;
use std::{
collections::HashSet,
default::Default,
env,
fs::File,
io::{Read, Write},
path::{Path, PathBuf},
};
use tectonic::{errors::Result, io::memory::MemoryFileCollection};
use tectonic_bridge_core::{CoreBridgeLauncher, MinimalDriver};
pub use tectonic::test_util::{test_path, TestBundle};
pub fn set_test_root() {
::tectonic::test_util::set_test_root_augmented(env!("CARGO_MANIFEST_DIR"));
}
pub fn cargo_dir() -> PathBuf {
env::var_os("CARGO_BIN_PATH")
.map(PathBuf::from)
.or_else(|| {
env::current_exe().ok().map(|mut path| {
path.pop();
if path.ends_with("deps") {
path.pop();
}
path
})
})
.unwrap_or_else(|| panic!("CARGO_BIN_PATH wasn't set. Cannot continue running test"))
}
pub fn ensure_plain_format() -> Result<PathBuf> {
use tectonic::{
io::{try_open_file, FilesystemIo, FilesystemPrimaryInputIo, IoStack, MemoryIo},
TexEngine,
};
use tectonic_status_base::NoopStatusBackend;
let fmt_path = test_path(&["plain.fmt"]);
if try_open_file(&fmt_path).is_not_available() {
let mut mem = MemoryIo::new(true);
let mut assets_dir = test_path(&["assets"]);
let mut fs_support = FilesystemIo::new(&assets_dir, false, false, HashSet::new());
assets_dir.push("plain");
assets_dir.set_extension("tex");
let mut fs_primary = FilesystemPrimaryInputIo::new(&assets_dir);
{
let io = IoStack::new(vec![&mut mem, &mut fs_primary, &mut fs_support]);
let mut hooks = MinimalDriver::new(io);
let mut status = NoopStatusBackend::default();
let mut launcher = CoreBridgeLauncher::new(&mut hooks, &mut status);
TexEngine::default()
.halt_on_error_mode(true)
.initex_mode(true)
.process(&mut launcher, "UNUSED.fmt", "plain.tex")?;
}
let mut temp_fmt = tempfile::Builder::new()
.prefix("plain_fmt")
.rand_bytes(6)
.tempfile_in(test_path(&[]))?;
temp_fmt.write_all(&mem.files.borrow().get("plain.fmt").unwrap().data)?;
temp_fmt.persist(&fmt_path)?;
}
Ok(fmt_path)
}
pub struct ExpectedInfo {
name: String,
contents: Vec<u8>,
gzipped: bool,
}
impl ExpectedInfo {
pub fn read<P: AsRef<Path>>(path: P) -> Self {
let path = path.as_ref();
let name = path
.file_name()
.unwrap_or_else(|| panic!("couldn't get file name of {:?}", path))
.to_str()
.unwrap_or_else(|| panic!("couldn't Unicode-ify file name of {:?}", path))
.to_owned();
let mut f = File::open(path).unwrap_or_else(|_| panic!("failed to open {:?}", path));
let mut contents = Vec::new();
f.read_to_end(&mut contents).unwrap();
ExpectedInfo {
name,
contents,
gzipped: false,
}
}
pub fn read_with_extension(pbase: &mut PathBuf, extension: &str) -> Self {
pbase.set_extension(extension);
Self::read(pbase)
}
pub fn read_with_extension_rooted_gz(pbase: &mut PathBuf, extension: &str) -> Self {
pbase.set_extension(extension);
let name = pbase.file_name().unwrap().to_str().unwrap().to_owned();
let mut dec = GzDecoder::new(File::open(&pbase).unwrap());
let mut contents = Vec::new();
dec.read_to_end(&mut contents).unwrap();
let root = format!(
"{}{}",
pbase.parent().unwrap().to_str().unwrap(),
std::path::MAIN_SEPARATOR
);
let contents = String::from_utf8(contents)
.unwrap()
.replace("${ROOT}", &root)
.replace(
"${len(ROOT)+106}",
&(root.as_bytes().len() + 106).to_string(),
)
.into_bytes();
ExpectedInfo {
name,
contents,
gzipped: true,
}
}
pub fn test_data(&self, observed: &[u8]) {
if self.contents == observed {
return;
}
{
let mut n = self.name.clone();
n.push_str(".expected");
let mut f = File::create(&n)
.unwrap_or_else(|_| panic!("failed to create {} for test failure diagnosis", n));
f.write_all(&self.contents)
.unwrap_or_else(|_| panic!("failed to write {} for test failure diagnosis", n));
}
{
let mut n = self.name.clone();
n.push_str(".observed");
let mut f = File::create(&n)
.unwrap_or_else(|_| panic!("failed to create {} for test failure diagnosis", n));
f.write_all(observed)
.unwrap_or_else(|_| panic!("failed to write {} for test failure diagnosis", n));
}
panic!("difference in {}; contents saved to disk", self.name);
}
pub fn test_from_collection(&self, files: &MemoryFileCollection) {
if !self.gzipped {
if let Some(file) = files.get(&self.name) {
self.test_data(&file.data)
} else {
panic!(
"{:?} not in {:?}",
self.name,
files.keys().collect::<Vec<_>>()
)
}
} else {
let mut buf = Vec::new();
let mut dec = GzDecoder::new(&files.get(&self.name).unwrap().data[..]);
dec.read_to_end(&mut buf).unwrap();
self.test_data(&buf);
}
}
}