ephemeral-env 0.1.0

A utility for creating ephemeral environments which are reverted on Drop
Documentation
  • Coverage
  • 80%
    4 out of 5 items documented1 out of 5 items with examples
  • Size
  • Source code size: 21.74 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.43 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • grahambinns

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();
}