3 releases

0.1.2 Aug 19, 2024
0.1.1 Aug 15, 2024
0.1.0 Aug 15, 2024

#439 in Concurrency

Download history 167/week @ 2024-08-19 117/week @ 2024-08-26 57/week @ 2024-09-02 8/week @ 2024-09-09 60/week @ 2024-09-16 61/week @ 2024-09-23 40/week @ 2024-09-30 14/week @ 2024-10-07 22/week @ 2024-10-14 55/week @ 2024-10-21 28/week @ 2024-10-28 20/week @ 2024-11-04 19/week @ 2024-11-11 14/week @ 2024-11-18 48/week @ 2024-11-25 19/week @ 2024-12-02

101 downloads per month
Used in 4 crates

MIT license

8KB
92 lines

env-lock

Test CI crates.io docs.rs

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 an interface to safely modify and lock the process environment, to prevent simultaneous access.

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

lib.rs:

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

No runtime deps