1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
use std::ffi::OsString; use std::path::Path; use std::sync::Mutex; static HOME_LOCK: Mutex<()> = Mutex::new(()); struct HomeGuard(Option<OsString>); impl Drop for HomeGuard { fn drop(&mut self) { match self.0.take() { Some(previous_home) => unsafe { std::env::set_var("HOME", previous_home); }, None => unsafe { std::env::remove_var("HOME"); }, } } } pub(crate) fn with_temp_home<T>(test: impl FnOnce(&Path) -> T) -> T { let _guard = HOME_LOCK.lock().expect("home lock"); let temp = tempfile::tempdir().expect("tempdir"); let previous_home = std::env::var_os("HOME"); let _home_guard = HomeGuard(previous_home); unsafe { std::env::set_var("HOME", temp.path()); } test(temp.path()) }