use std::path::{Path, PathBuf};
use tempfile::TempDir;
use wasm_pkg_client::{
Client, Config,
caching::{CachingClient, FileCache},
};
use wasm_pkg_core::wit::WIT_DEPS_DIR;
pub fn fixture_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
}
pub async fn get_client() -> anyhow::Result<(TempDir, CachingClient<FileCache>)> {
let client = Client::new(Config::default());
let cache_temp_dir = tempfile::tempdir()?;
let cache = FileCache::new(cache_temp_dir.path()).await?;
Ok((cache_temp_dir, CachingClient::new(Some(client), cache)))
}
pub async fn load_fixture(fixture: &str) -> anyhow::Result<(TempDir, PathBuf)> {
let temp_dir = tempfile::tempdir()?;
let fixture_path = fixture_dir().join(fixture);
tokio::fs::metadata(&fixture_path).await?;
let copied_path = temp_dir.path().join(fixture_path.file_name().unwrap());
copy_dir(&fixture_path, &copied_path).await?;
Ok((temp_dir, copied_path))
}
async fn copy_dir(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> anyhow::Result<()> {
tokio::fs::create_dir_all(&destination).await?;
let mut entries = tokio::fs::read_dir(source).await?;
while let Some(entry) = entries.next_entry().await? {
let filetype = entry.file_type().await?;
if filetype.is_dir() {
if entry.path().file_name().unwrap_or_default() == WIT_DEPS_DIR {
continue;
}
Box::pin(copy_dir(
entry.path(),
destination.as_ref().join(entry.file_name()),
))
.await?;
} else {
let path = entry.path();
let extension = path.extension().unwrap_or_default();
if extension == "lock" || extension == "wasm" {
continue;
}
tokio::fs::copy(path, destination.as_ref().join(entry.file_name())).await?;
}
}
Ok(())
}