use std::sync::Mutex;
pub static ENV_LOCK: Mutex<()> = Mutex::new(());
pub fn with_fake_xdg<R>(f: impl FnOnce() -> R) -> R {
let _guard = ENV_LOCK.lock().unwrap_or_else(|poisoned| {
poisoned.into_inner()
});
let base = std::env::temp_dir().join(format!("nmrs-test-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&base).expect("failed to create temp directory for test");
unsafe {
std::env::set_var("XDG_DATA_HOME", &base);
}
struct Cleanup {
base: std::path::PathBuf,
}
impl Drop for Cleanup {
fn drop(&mut self) {
unsafe {
std::env::remove_var("XDG_DATA_HOME");
}
let _ = std::fs::remove_dir_all(&self.base);
}
}
let _cleanup = Cleanup { base };
f()
}