From f2db4bd8d392a65ae85ed035b643624bbb20c4c6 Mon Sep 17 00:00:00 2001 From: guenhter Date: Wed, 18 Sep 2024 16:54:59 +0200 Subject: [PATCH 1/4] Fix macro import --- .github/workflows/ci.yml | 7 ------- README.md | 5 +++++ src/lib.rs | 8 ++++++++ .../tests/integration_test.rs => tests/macro_test.rs | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) rename temp_env_vars_macro/tests/integration_test.rs => tests/macro_test.rs (96%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2012801..0ab0211 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,10 +17,3 @@ jobs: - uses: actions/checkout@v4 - name: Run tests run: cargo test --verbose - - name: Run core tests - run: | - cargo test --verbose - - name: Run macro tests - run: | - cd temp_env_vars_macro - cargo test --verbose diff --git a/README.md b/README.md index 0620e36..12ae232 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,8 @@ If more tests are used with this macro, those tests will be executed sequentiall avoid an enviornment variable mixup. ```rust +use temp_env_vars::temp_env_vars; + #[test] #[temp_env_vars] fn test_some() { @@ -58,6 +60,9 @@ used to have better control. Whenever the created `TempEnvScope` goes out of scope, all env vars are reset. ```rust +use serial_test::serial; +use temp_env_vars::TempEnvScope; + #[test] #[serial] // Use external "serial" crate as parallel tests mix up envs fn test_some() { diff --git a/src/lib.rs b/src/lib.rs index b790e41..f1ebf43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,8 @@ //! to avoid an enviornment variable mixup. //! //! ```rust +//! use temp_env_vars::temp_env_vars; +//! //! #[test] //! #[temp_env_vars] //! fn test_some() { @@ -40,6 +42,9 @@ //! Whenever the created `TempEnvScope` goes out of scope, all env vars are reset. //! //! ```rust +//! use serial_test::serial; +//! use temp_env_vars::TempEnvScope; +//! //! #[test] //! #[serial] // Use external "serial" crate as parallel tests mix up envs //! fn test_some() { @@ -62,6 +67,9 @@ //! // "FOO" is not longer set here. //! } //! ``` + +pub use temp_env_vars_macro::temp_env_vars; + use std::{ collections::HashMap, sync::{Arc, LazyLock, Mutex}, diff --git a/temp_env_vars_macro/tests/integration_test.rs b/tests/macro_test.rs similarity index 96% rename from temp_env_vars_macro/tests/integration_test.rs rename to tests/macro_test.rs index d588774..261bc0d 100644 --- a/temp_env_vars_macro/tests/integration_test.rs +++ b/tests/macro_test.rs @@ -2,7 +2,7 @@ use core::time; use std::thread::sleep; use assertor::{assert_that, ResultAssertion}; -use temp_env_vars_macro::temp_env_vars; +use temp_env_vars::temp_env_vars; #[test] #[temp_env_vars] From 39efa49ac3d32d514e3ecb4da32f387b8e2d56b4 Mon Sep 17 00:00:00 2001 From: guenhter Date: Wed, 18 Sep 2024 16:58:24 +0200 Subject: [PATCH 2/4] Move restore logic --- src/lib.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f1ebf43..b5ede34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,16 +91,16 @@ impl TempEnvScope { original_vars: std::env::vars().collect(), } } -} -impl Drop for TempEnvScope { - fn drop(&mut self) { - let mut after: HashMap = std::env::vars().collect(); + /// Sets the environment variables to the state as they were + /// when this `TempEnvScope` was created. + fn restore(&self) { + let mut now: HashMap = std::env::vars().collect(); self.original_vars.keys().for_each(|key| { - after.remove(key); + now.remove(key); }); - after.keys().for_each(|key| { + now.keys().for_each(|key| { std::env::remove_var(key); }); self.original_vars.iter().for_each(|(k, v)| { @@ -109,6 +109,12 @@ impl Drop for TempEnvScope { } } +impl Drop for TempEnvScope { + fn drop(&mut self) { + self.restore(); + } +} + #[cfg(test)] mod tests { use std::collections::HashMap; From d4ca24fe8f4818f71a64a64f4ef7121d28792af2 Mon Sep 17 00:00:00 2001 From: guenhter Date: Wed, 18 Sep 2024 17:01:46 +0200 Subject: [PATCH 3/4] Update publishing script --- PUBLISHING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PUBLISHING.md b/PUBLISHING.md index b213042..bf62dd9 100644 --- a/PUBLISHING.md +++ b/PUBLISHING.md @@ -8,7 +8,7 @@ Here are some notes when publishing a new version of the crate ```bash # Publish the macro first cd temp_env_vars_macro -# Comment out the dev-dependency to the temp_env_vars +sed -i 's/^temp_env_vars/# temp_env_vars/' Cargo.toml cargo publish --allow-dirty git checkout -- . From 3b40bed56a43151c7954e76ccb8d1e65f1d42de2 Mon Sep 17 00:00:00 2001 From: guenhter Date: Wed, 18 Sep 2024 16:59:05 +0200 Subject: [PATCH 4/4] Bump version to 0.2.1 --- Cargo.toml | 4 ++-- temp_env_vars_macro/Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6ee2f9d..05b730e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "temp_env_vars" -version = "0.2.0" +version = "0.2.1" 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.2.0", path = "./temp_env_vars_macro" } +temp_env_vars_macro = { version = "0.2.1", path = "./temp_env_vars_macro" } [dev-dependencies] assertor = "0.0.2" diff --git a/temp_env_vars_macro/Cargo.toml b/temp_env_vars_macro/Cargo.toml index 869fee9..cc3ce17 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.2.0" +version = "0.2.1" 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.2.0", path = ".." } +temp_env_vars = { version = "0.2.1", path = ".." } [lib] proc-macro = true