# ephemeral-env: A managed ephemeral environment for testing
If your code is driven by environment variables you'll often find yourself
writing tests like this:
```rs no-run
#[test]
fn test_something_works() {
unsafe {
std::env::set_var("THATS_NOT_MY_COW", "It goes baa");
}
do_the_thing();
unsafe {
std::env::remove_var("THATS_NOT_MY_COW");
}
}
```
The trouble with this is twofold:
1. You need to remember to undo whatever changes you made at the end of your
test, lest you pollute the environment for other tests.
2. You're at the mercy of tests racing and changing the environment from under
one another.
`ephemeral-env` solves this by:
1. Creating a copy of the current environment
2. Reverting to that original state when the ephemeral env is dropped
3. Providing you with a convenience function to avoid having to pepper your tests with `unsafe {...}`.
Rewriting the example above with `ephemeral-env` would give you:
```rs no-run
#[test]
fn test_something_works() {
let test_env = ephemeral_env::EphemeralEnv::from_env_sync().unwrap();
test_env::set_var("THATS_NOT_MY_COW", "It goes baa");
do_the_thing();
}
```