Set environment variables temporarily.
This crate is useful for testing with different environment variables that should not interfere.
This code started as a small test helper written by [@fabian-braun] and [@nbaztec] and published by [@fabian-braun] on [StackOverflow]. [@vmx] found it useful and took the time to make it a proper crate.
``rust
temp_env::with_var("MY_ENV_VAR", Some("production"), || {
// Run some code where
MYENVVARset to
"production"`.
});
tempenv::withvars(
vec![
("FIRSTVAR", Some("Hello")),
("SECONDVAR", Some("World!")),
],
|| {
// Run some code where FIRST_VAR
is set to "Hello"
and SECOND_VAR
is set to
// "World!"
.
}
);
tempenv::withvars(
vec![
("FIRSTVAR", Some("Hello")),
("SECONDVAR", None),
],
|| {
// Run some code where FIRST_VAR
is set to "Hello"
and SECOND_VAR
is unset (even if
// it was set before)
}
);
```
This project is licensed under either of
at your option.