use std::{
path::{Path, PathBuf},
str::FromStr,
};
use tempfile::TempDir;
use once_cell::sync::Lazy;
use std::fs::File;
use zip::ZipArchive;
const ZIP_NAME: &str = "test-repos.zip";
static MAIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
fn zip_path() -> PathBuf {
PathBuf::from_str(MAIFEST_DIR).unwrap().join(ZIP_NAME)
}
fn unzip_archive(zip_path: &Path, dest: &Path) {
let file = File::open(zip_path).expect("Expected a valid zip");
let mut archive = ZipArchive::new(file).expect("Expected to read zip");
for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let out_path = dest.join(
file.mangled_name()
.components()
.skip(1)
.collect::<PathBuf>(),
);
if file.is_dir() {
std::fs::create_dir_all(&out_path).unwrap();
} else {
if let Some(parent) = out_path.parent() {
std::fs::create_dir_all(parent).unwrap();
}
let mut out = std::fs::File::create(&out_path).unwrap();
std::io::copy(&mut file, &mut out).unwrap();
}
}
}
static TEST_DATA_DIR: Lazy<TempDir> = Lazy::new(|| {
let dir = TempDir::new().expect("Create temp dir");
let zp = zip_path();
unzip_archive(&zp, dir.path());
dir
});
pub fn test_data_dir() -> &'static Path {
TEST_DATA_DIR.path()
}