use once_cell::sync::OnceCell;
use std::path::{Path, PathBuf};
pub fn get_base_test_dir() -> &'static Path {
static DIR: OnceCell<PathBuf> = OnceCell::new();
DIR.get_or_init(|| init_base_test_dir().expect("could not initialize base test data directory"))
.as_path()
}
fn init_base_test_dir() -> std::io::Result<PathBuf> {
let pb = get_target_dir()?.join("test-data");
if pb.is_dir() {
eprintln!("Removing {:?} from previous test run...", pb.display());
std::fs::remove_dir_all(&pb)?;
}
eprintln!("Creating {:?}...", pb.display());
std::fs::create_dir(&pb)?;
Ok(pb)
}
fn get_target_dir() -> std::io::Result<PathBuf> {
for candidate in std::env::current_exe()?.ancestors() {
if candidate.is_dir() && candidate.file_name().and_then(|os| os.to_str()) == Some("target")
{
return Ok(candidate.to_path_buf());
}
}
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Cargo 'target/' directory not found.",
))
}
#[cfg(test)]
mod tests;