use std::{
path::{Path, PathBuf},
sync::atomic::{AtomicU32, Ordering},
};
pub fn test_scratch_dir(manifest_dir: &str, tag: &str) -> PathBuf {
static COUNTER: AtomicU32 = AtomicU32::new(0);
let unique = COUNTER.fetch_add(1, Ordering::Relaxed);
let manifest = PathBuf::from(manifest_dir);
let owner = manifest.file_name().map_or_else(
|| "workspace".to_string(),
|name| name.to_string_lossy().into_owned(),
);
let path = workspace_root(&manifest)
.join("target/test-output")
.join(owner)
.join(format!("{tag}-{}-{unique}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).expect("a scratch directory under target/test-output");
path
}
fn workspace_root(manifest: &Path) -> PathBuf {
manifest
.ancestors()
.find(|directory| directory.join("Cargo.lock").is_file())
.unwrap_or(manifest)
.to_path_buf()
}
#[cfg(test)]
#[path = "tests/test_scratch_tests.rs"]
mod tests;