Expand description
This crate is for setting environment variables temporarily.
It is useful for testing with different environment variables that should not interfere.
Examples
temp_env::with_var("MY_ENV_VAR", Some("production"), || {
// Run some code where `MY_ENV_VAR` set to `"production"`.
});
temp_env::with_vars(
[
("FIRST_VAR", Some("Hello")),
("SECOND_VAR", Some("World!")),
],
|| {
// Run some code where `FIRST_VAR` is set to `"Hello"` and `SECOND_VAR` is set to
// `"World!"`.
}
);
temp_env::with_vars(
[
("FIRST_VAR", Some("Hello")),
("SECOND_VAR", None),
],
|| {
// Run some code where `FIRST_VAR` is set to `"Hello"` and `SECOND_VAR` is unset (even if
// it was set before)
}
);
It’s possible the closure returns a value:
let s = temp_env::with_var("INNER_ENV_VAR", Some("inner value"), || {
std::env::var("INNER_ENV_VAR").unwrap()
});
println!("{}", s);
Functions
- Sets a single environment variable for the duration of the closure.
- Unsets a single environment variable for the duration of the closure.
- Sets environment variables for the duration of the closure.
- Unsets environment variables for the duration of the closure.