Crates.io | env-lock |
lib.rs | env-lock |
version | 0.1.2 |
source | src |
created_at | 2024-08-15 14:10:53.355355 |
updated_at | 2024-08-19 13:53:48.487749 |
description | Set and lock environment variables for tests |
homepage | |
repository | https://github.com/LucasPickering/env-lock |
max_upload_size | |
id | 1338821 |
size | 13,858 |
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());