Skip to content

Commit

Permalink
Merge pull request #3 from guenhter/rename-scope
Browse files Browse the repository at this point in the history
Rename TestEnvScope to TempEnvScope
  • Loading branch information
guenhter authored Sep 18, 2024
2 parents 8ba0ca7 + 779cfb3 commit 6159061
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "temp_env_vars"
version = "0.1.2"
version = "0.2.0"
authors = ["Günther Grill <[email protected]>"]
edition = "2021"
rust-version = "1.80"
Expand All @@ -14,7 +14,7 @@ categories = ["development-tools::testing"]
exclude = [".github/", ".vscode/", ".editorconfig", ".gitignore"]

[dependencies]
temp_env_vars_macro = { version = "0.1.2", path = "./temp_env_vars_macro" }
temp_env_vars_macro = { version = "0.2.0", path = "./temp_env_vars_macro" }

[dev-dependencies]
assertor = "0.0.2"
Expand Down
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
[![MSRV: 1.80.0](https://flat.badgen.net/badge/MSRV/1.80.0/purple)](https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html)


`temp_env_vars` allows to to manipulate enviornment variables during a test and reset all changes when the test is done.

[!WARNING]

The software currently in the starting phase and will change
`temp_env_vars` allows to rest 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

Expand All @@ -26,16 +25,17 @@ cargo add temp_env_vars
`temp_env_vars` can be used in two different forms:

1. as macro `#[temp_env_vars]`
2. with `TestEnvScope::new()`
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 test function
will be reset after the test has ended.
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.
If more tests are used with this macro, those tests will be executed sequentially to
avoid an enviornment variable mixup.

```rust
#[test]
Expand All @@ -44,38 +44,39 @@ fn test_some() {
std::env::set_var("FOO", "BAR");
assert_eq!(std::env::var("FOO").unwrap(), "BAR");

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


### Use with TestEnvScope
### Use with TempEnvScope

If resetting the environment variables after the test execution is not sufficient, but the reset must happen somewhere within the test, the `TestEnvScope` can be used to have better control.
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 `TestEnvScope` goes out of scope, all env vars are reset.
Whenever the created `TempEnvScope` goes out of scope, all env vars are reset.

```rust
#[test]
#[serial] // Use "serial" (external crate), as parallel tests could mix up envs
#[serial] // Use external "serial" crate as parallel tests mix up envs
fn test_some() {
let _env_scope = TestEnvScope::new();
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 "serial" (external crate), as parallel tests could mix up envs
#[serial] // Use external "serial" crate as parallel tests mix up envs
fn test_bar() {
let _env_scope = TestEnvScope::new();
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.
}
```
Expand Down
57 changes: 28 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! # temp_env_vars
//!
//! `temp_env_vars` allows to to manipulate enviornment variables during a test and reset
//! all changes when the test is done.
//! `temp_env_vars` allows to rest all changes to environment variables changed
//! within the execution of a certain function.
//!
//! ## Usage
//!
//! `temp_env_vars` can be used in two different forms:
//!
//! 1. as macro `#[temp_env_vars]`
//! 2. with `TestEnvScope::new()`
//! 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 test function
//! will be reset after the test has ended.
//! 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.
Expand All @@ -26,40 +26,39 @@
//! std::env::set_var("FOO", "BAR");
//! assert_eq!(std::env::var("FOO").unwrap(), "BAR");
//!
//! // Env vars get reset when test is done
//! // Env vars get reset when this test is done
//! }
//! ```
//!
//! ### Use with TestEnvScope
//! ### Use with TempEnvScope
//!
//!
//! If resetting the environment variables after the test execution is not sufficient,
//! but the reset must happen somewhere within the test, the `TestEnvScope` can be
//! 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 `TestEnvScope` goes out of scope, all env vars are reset.
//! Whenever the created `TempEnvScope` goes out of scope, all env vars are reset.
//!
//! ```rust
//! #[test]
//! #[serial] // Advices to use serial, alse parallel tests could mix up envs
//! #[serial] // Use external "serial" crate as parallel tests mix up envs
//! fn test_some() {
//! let _env_scope = TestEnvScope::new();
//! 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] // Advices to use serial, alse parallel tests could mix up envs
//! #[serial] // Use external "serial" crate as parallel tests mix up envs
//! fn test_bar() {
//! let _env_scope = TestEnvScope::new();
//! 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.
//! }
//! ```
Expand All @@ -74,19 +73,19 @@ use std::{
pub static TEMP_ENV_VAR_MACRO_MUTEX: LazyLock<Arc<Mutex<()>>> = LazyLock::new(Arc::default);

#[derive(Debug)]
pub struct TestEnvScope {
pub struct TempEnvScope {
original_vars: HashMap<String, String>,
}

impl TestEnvScope {
pub fn new() -> TestEnvScope {
TestEnvScope {
impl TempEnvScope {
pub fn new() -> TempEnvScope {
TempEnvScope {
original_vars: std::env::vars().collect(),
}
}
}

impl Drop for TestEnvScope {
impl Drop for TempEnvScope {
fn drop(&mut self) {
let mut after: HashMap<String, String> = std::env::vars().collect();

Expand All @@ -109,15 +108,15 @@ mod tests {
use assertor::{assert_that, EqualityAssertion, ResultAssertion};
use serial_test::serial;

use super::TestEnvScope;
use super::TempEnvScope;

#[test]
#[serial]
fn test_nothing_is_changed() {
let original: HashMap<String, String> = std::env::vars().collect();

{
let _env_scope = TestEnvScope::new();
let _env_scope = TempEnvScope::new();
}

let after: HashMap<String, String> = std::env::vars().collect();
Expand All @@ -131,7 +130,7 @@ mod tests {
let original: HashMap<String, String> = std::env::vars().collect();

{
let _env_scope = TestEnvScope::new();
let _env_scope = TempEnvScope::new();
std::env::set_var("FOO", "BAR1");
}

Expand All @@ -147,7 +146,7 @@ mod tests {
let original: HashMap<String, String> = std::env::vars().collect();

{
let _env_scope = TestEnvScope::new();
let _env_scope = TempEnvScope::new();
std::env::set_var("FOO", "123");
}

Expand All @@ -163,7 +162,7 @@ mod tests {
let original: HashMap<String, String> = std::env::vars().collect();

{
let _env_scope = TestEnvScope::new();
let _env_scope = TempEnvScope::new();
std::env::remove_var("FOO");
}

Expand All @@ -178,8 +177,8 @@ mod tests {
std::env::remove_var("FOO");

{
let _env_scope_1 = TestEnvScope::new();
let _env_scope_2 = TestEnvScope::new();
let _env_scope_1 = TempEnvScope::new();
let _env_scope_2 = TempEnvScope::new();

std::env::set_var("FOO", "BAR4");
assert_that!(std::env::var("FOO")).is_ok();
Expand All @@ -194,15 +193,15 @@ mod tests {
std::env::remove_var("FOO");

{
let _env_scope = TestEnvScope::new();
let _env_scope = TempEnvScope::new();

std::env::set_var("FOO", "BAR5");
assert_that!(std::env::var("FOO")).is_ok();
}
assert_that!(std::env::var("FOO")).is_err();

{
let _env_scope = TestEnvScope::new();
let _env_scope = TempEnvScope::new();

std::env::set_var("FOO", "BAR6");
assert_that!(std::env::var("FOO")).is_ok();
Expand Down
4 changes: 2 additions & 2 deletions temp_env_vars_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "temp_env_vars_macro"
version = "0.1.2"
version = "0.2.0"
authors = ["Günther Grill <[email protected]>"]
edition = "2021"
rust-version = "1.80"
Expand All @@ -21,7 +21,7 @@ syn = { version = "2.0.74", features = ["full"] }
assertor = "0.0.2"
serial_test = "3.1.1"
anyhow = "1.0.86"
temp_env_vars = { version = "0.1.2", path = ".." }
temp_env_vars = { version = "0.2.0", path = ".." }

[lib]
proc-macro = true
Expand Down
2 changes: 1 addition & 1 deletion temp_env_vars_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn temp_env_vars(
*
#vis #asynciness fn #name () #returning {
let _temp_env_vars_scope_lock = temp_env_vars::TEMP_ENV_VAR_MACRO_MUTEX.lock();
let _temp_env_vars_scope = temp_env_vars::TestEnvScope::new();
let _temp_env_vars_scope = temp_env_vars::TempEnvScope::new();
#block
}
};
Expand Down

0 comments on commit 6159061

Please sign in to comment.