Skip to content

Commit

Permalink
Merge pull request #8 from guenhter/fix-importing
Browse files Browse the repository at this point in the history
Fix importing
  • Loading branch information
guenhter authored Sep 18, 2024
2 parents 23bbd47 + 3b40bed commit b39b1ae
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.2.0"
version = "0.2.1"
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.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"
Expand Down
2 changes: 1 addition & 1 deletion PUBLISHING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -- .

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
26 changes: 20 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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},
Expand All @@ -83,16 +91,16 @@ impl TempEnvScope {
original_vars: std::env::vars().collect(),
}
}
}

impl Drop for TempEnvScope {
fn drop(&mut self) {
let mut after: HashMap<String, String> = 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<String, String> = 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)| {
Expand All @@ -101,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;
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.2.0"
version = "0.2.1"
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.2.0", path = ".." }
temp_env_vars = { version = "0.2.1", path = ".." }

[lib]
proc-macro = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b39b1ae

Please sign in to comment.