Expand description
Lock environment variables to prevent simultaneous access. Use lock_env to set values for whatever environment variables you intend to access in your test. This will return a guard that, when dropped, will revert the environment to its initial state. The guard uses a Mutex underneath to ensure that multiple tests within the same process can’t access it at the same time.
use std::env;
let var = "ENV_LOCK_TEST_VARIABLE";
assert!(env::var(var).is_err());
let guard = env_lock::lock_env([(var, Some("hello!"))]);
assert_eq!(env::var(var).unwrap(), "hello!");
drop(guard);
assert!(env::var(var).is_err());Structs§
- A guard used to indicate that the current process environment is locked. Returned by lock_env. This will restore and unlock the environment on drop.
Functions§
- Lock the environment and set each given variable to its corresponding value. If the environment is already locked, this will block until the lock can be acquired. The returned guard will keep the environment locked so the calling test has exclusive access to it. Upon being dropped, the old environment values will be restored and then the environment will be unlocked.