env-lock

Set and lock environment variables for tests

MIT License

Downloads
1.5K
Stars
1

env-lock

A process's environment is a form of global mutable state. In Rust, tests are run in a shared process. This means tests that modify environment variables can inadvertently affect each other. env-lock provides a simple interface to safely modify and lock the process environment, to prevent simultaneous access. This provides two forms of isolation: isolation from the external environment, and isolation between tests that read/modify the environment.

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());