use kpathsea::Kpaths;
use std::io::Write;
#[cfg(not(windows))]
fn normalized(path: &str) -> String {
path.replace('\\', "/").to_lowercase()
}
#[test]
fn texinputs_honored_without_kpsewhich() {
let in_process = match Kpaths::new() {
Ok(kpse) => kpse.is_in_process(),
Err(_) => return,
};
if !in_process {
return;
}
let dir = std::env::temp_dir().join(format!("kpse_texinputs_probe_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("lxo_texinputs_probe.tex");
writeln!(std::fs::File::create(&file).unwrap(), "% probe").unwrap();
let sep = if cfg!(windows) { ';' } else { ':' };
unsafe { std::env::set_var("TEXINPUTS", format!("{}{sep}", dir.display())) };
let not_executable = dir.join("not_executable_kpsewhich");
std::fs::write(¬_executable, b"#!/bin/sh\n").unwrap();
for bogus in [
"/nonexistent/definitely-not-kpsewhich",
"definitely-not-a-command-on-path",
"",
not_executable.to_string_lossy().as_ref(),
] {
unsafe { std::env::set_var("KPSEWHICH", bogus) };
let kpse = Kpaths::new()
.unwrap_or_else(|e| panic!("linked Kpaths::new must not fail for KPSEWHICH={bogus:?}: {e}"));
assert!(
kpse.is_in_process(),
"expected the in-process backend to survive KPSEWHICH={bogus:?}"
);
#[cfg(not(windows))]
assert_eq!(
kpse
.find_file("lxo_texinputs_probe.tex")
.as_deref()
.map(normalized),
Some(normalized(&file.to_string_lossy())),
"a TEXINPUTS-only file must resolve through the fallback-anchored libkpathsea \
with KPSEWHICH={bogus:?}"
);
}
let _ = std::fs::remove_dir_all(&dir);
}