pub struct EnvGuard { /* private fields */ }Expand description
Holds the process’s environment lock and restores every variable it touched when dropped — including back to absent.
Implementations§
Source§impl EnvGuard
impl EnvGuard
Sourcepub fn hold() -> Self
pub fn hold() -> Self
Take the lock without changing anything — for a test that only needs
to be alone with the environment, or that will set_var later.
Sourcepub fn set<K, V>(vars: impl IntoIterator<Item = (K, V)>) -> Self
pub fn set<K, V>(vars: impl IntoIterator<Item = (K, V)>) -> Self
Take the lock and set these variables.
Sourcepub fn unset<K: AsRef<OsStr>>(vars: impl IntoIterator<Item = K>) -> Self
pub fn unset<K: AsRef<OsStr>>(vars: impl IntoIterator<Item = K>) -> Self
Take the lock and remove these variables.
Sourcepub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(
&mut self,
key: K,
value: V,
) -> &mut Self
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>( &mut self, key: K, value: V, ) -> &mut Self
Set one variable inside this guard’s critical section.
Sourcepub fn unset_var<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self
pub fn unset_var<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self
Remove one variable inside this guard’s critical section.
Sourcepub fn track_var<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self
pub fn track_var<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self
Restore this variable when the guard drops, without changing it now.
For a variable the test does not set but the code under test does. Some
production paths use the environment as a hidden parameter and do not
put it back — deep_research::provision sets MUR_HOME for the
helpers it calls and says so in its own # Concurrency note. A test
calling that leaks the value to every later test in the process, and no
lock can help: the leak is not a race. Run the suite with
--test-threads=1 and it still happens, which is how this was found.