From 779cfb3cf7adbcb5b332054e0e83cc8211db92a8 Mon Sep 17 00:00:00 2001 From: guenhter Date: Wed, 18 Sep 2024 04:25:22 +0200 Subject: [PATCH] Rename TestEnvScope to TempEnvScope --- Cargo.toml | 4 +-- README.md | 37 +++++++++++----------- src/lib.rs | 57 +++++++++++++++++----------------- temp_env_vars_macro/Cargo.toml | 4 +-- temp_env_vars_macro/src/lib.rs | 2 +- 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b8b20b1..6ee2f9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "temp_env_vars" -version = "0.1.2" +version = "0.2.0" authors = ["Günther Grill "] edition = "2021" rust-version = "1.80" @@ -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" diff --git a/README.md b/README.md index 211b4e4..ac67d7f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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] @@ -44,22 +44,24 @@ 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"); @@ -67,15 +69,14 @@ fn test_some() { } #[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. } ``` diff --git a/src/lib.rs b/src/lib.rs index 0386caf..74b2b05 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. @@ -26,24 +26,24 @@ //! 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"); //! @@ -51,15 +51,14 @@ //! } //! //! #[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. //! } //! ``` @@ -74,19 +73,19 @@ use std::{ pub static TEMP_ENV_VAR_MACRO_MUTEX: LazyLock>> = LazyLock::new(Arc::default); #[derive(Debug)] -pub struct TestEnvScope { +pub struct TempEnvScope { original_vars: HashMap, } -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 = std::env::vars().collect(); @@ -109,7 +108,7 @@ mod tests { use assertor::{assert_that, EqualityAssertion, ResultAssertion}; use serial_test::serial; - use super::TestEnvScope; + use super::TempEnvScope; #[test] #[serial] @@ -117,7 +116,7 @@ mod tests { let original: HashMap = std::env::vars().collect(); { - let _env_scope = TestEnvScope::new(); + let _env_scope = TempEnvScope::new(); } let after: HashMap = std::env::vars().collect(); @@ -131,7 +130,7 @@ mod tests { let original: HashMap = std::env::vars().collect(); { - let _env_scope = TestEnvScope::new(); + let _env_scope = TempEnvScope::new(); std::env::set_var("FOO", "BAR1"); } @@ -147,7 +146,7 @@ mod tests { let original: HashMap = std::env::vars().collect(); { - let _env_scope = TestEnvScope::new(); + let _env_scope = TempEnvScope::new(); std::env::set_var("FOO", "123"); } @@ -163,7 +162,7 @@ mod tests { let original: HashMap = std::env::vars().collect(); { - let _env_scope = TestEnvScope::new(); + let _env_scope = TempEnvScope::new(); std::env::remove_var("FOO"); } @@ -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(); @@ -194,7 +193,7 @@ 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(); @@ -202,7 +201,7 @@ mod tests { 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(); diff --git a/temp_env_vars_macro/Cargo.toml b/temp_env_vars_macro/Cargo.toml index 59989f3..869fee9 100644 --- a/temp_env_vars_macro/Cargo.toml +++ b/temp_env_vars_macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "temp_env_vars_macro" -version = "0.1.2" +version = "0.2.0" authors = ["Günther Grill "] edition = "2021" rust-version = "1.80" @@ -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 diff --git a/temp_env_vars_macro/src/lib.rs b/temp_env_vars_macro/src/lib.rs index 0e6aebf..0d3f66e 100644 --- a/temp_env_vars_macro/src/lib.rs +++ b/temp_env_vars_macro/src/lib.rs @@ -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 } };