ephemeral-env

Crates.ioephemeral-env
lib.rsephemeral-env
version1.0.0
created_at2025-11-26 11:47:38.297555+00
updated_at2025-12-16 23:18:26.591225+00
descriptionA utility for creating ephemeral environments which are reverted on Drop
homepage
repositoryhttps://gitlab.com/gmb/ephemeral-env-rs
max_upload_size
id1951308
size22,810
Graham Binns (grahambinns)

documentation

README

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:

#[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:

#[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();
}
Commit count: 0

cargo fmt