#env-var #env

macro temp_env_vars_macro

Helper crate for temp_env_vars to enable macro support

4 releases

0.2.1 Sep 18, 2024
0.2.0 Sep 18, 2024
0.1.1 Sep 14, 2024
0.1.0 Sep 14, 2024

#909 in Testing

Download history 224/week @ 2024-09-09 247/week @ 2024-09-16 78/week @ 2024-09-23 105/week @ 2024-09-30 36/week @ 2024-10-07 71/week @ 2024-10-14 305/week @ 2024-10-21 342/week @ 2024-10-28 249/week @ 2024-11-04 428/week @ 2024-11-11 204/week @ 2024-11-18 241/week @ 2024-11-25 16/week @ 2024-12-02 252/week @ 2024-12-09

714 downloads per month
Used in temp_env_vars

MIT license

8KB
54 lines

Temporary Environment Variables

Version Downloads MIT license Build Status MSRV: 1.80.0

temp_env_vars allows to reset all changes to environment variables changed within the execution of a certain function.

[!WARNING] This crate is under active development and will probably have braking changes

Installation

cargo add temp_env_vars

Usage

temp_env_vars can be used in two different forms:

  1. as macro #[temp_env_vars]
  2. with TempEnvScope::new()

Use as macro

#[temp_env_vars] is the preferred way to use the temp_env_vars crate. Every change to envionrment variables within the execution of the annotated function will be reset after the function has ended.

If more tests are used with this macro, those tests will be executed sequentially to avoid an enviornment variable mixup.

use temp_env_vars::temp_env_vars;

#[test]
#[temp_env_vars]
fn test_some() {
    std::env::set_var("FOO", "BAR");
    assert_eq!(std::env::var("FOO").unwrap(), "BAR");

    // Env vars get reset when this test is done
}

Use with TempEnvScope

If resetting the environment variables after the function execution is not sufficient, but the reset must happen somewhere within the function, the TempEnvScope can be used to have better control.

Whenever the created TempEnvScope goes out of scope, all env vars are reset.

use serial_test::serial;
use temp_env_vars::TempEnvScope;

#[test]
#[serial] // Use external "serial" crate as parallel tests mix up envs
fn test_some() {
    let _env_scope = TempEnvScope::new();
    std::env::set_var("FOO", "BAR");
    assert_eq!(std::env::var("FOO").unwrap(), "BAR");

    // After "_env_scope" goes out of scope, all vars are restored
}

#[test]
#[serial] // Use external "serial" crate as parallel tests mix up envs
fn test_bar() {
    let _env_scope = TempEnvScope::new();
    std::env::set_var("FOO", "BAR");
    assert_eq!(std::env::var("FOO").unwrap(), "BAR");

    drop(_env_scope); // After "_env_scope" goes out of scope, all vars are restored

    // "FOO" is not longer set here.
}

Contribution

Contribution are always welcome in any form.

You acknowledge and agree that the owner reserve the right to change the license of the Work, including but not limited to all Contributions previously submitted by You, at any time without the need for approval from You or any other contributor.

License

This project is licensed under the MIT license.

Dependencies

~220–660KB
~16K SLoC