Skip to content

Commit

Permalink
Merge pull request #1 from guenhter/prepare-for-release
Browse files Browse the repository at this point in the history
Move sources of core folder to root
  • Loading branch information
guenhter authored Sep 14, 2024
2 parents f419c5e + 8576bf1 commit ad90568
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 267 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
run: cargo test --verbose
- name: Run core tests
run: |
cd temp_env_vars_core
cargo test --verbose
- name: Run macro tests
run: |
Expand Down
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "temp_env_vars"
version = "0.1.0"
version = "0.1.1"
authors = ["Günther Grill <[email protected]>"]
edition = "2021"
rust-version = "1.80"
description = "Resets all environment variables changed in a defined scope"
readme = "README.adoc"
readme = "README.md"
homepage = "https://github.com/guenhter/temp_env_vars"
repository = "https://github.com/guenhter/temp_env_vars"
license = "MIT"
Expand All @@ -14,14 +14,12 @@ categories = ["development-tools::testing"]
exclude = [".github/", ".vscode/", ".editorconfig", ".gitignore"]

[dependencies]
temp_env_vars_macro = { version = "0.1.0", path = "./temp_env_vars_macro" }
temp_env_vars_core = { version = "0.1.0", path = "./temp_env_vars_core" }
temp_env_vars_macro = { version = "0.1.1", path = "./temp_env_vars_macro" }

[dev-dependencies]
assertor = "0.0.2"
serial_test = "3.1.1"
anyhow = "1.0.86"


[workspace]
members = ["temp_env_vars_core", "temp_env_vars_macro"]
members = ["temp_env_vars_macro"]
18 changes: 18 additions & 0 deletions PUBLISHING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Publishing crate

Here are some notes when publishing a new version of the crate

1. Ensure all versions are updated
2. Run this

```bash
# Publish the macro first
cd temp_env_vars_macro
# Comment out the dev-dependency to the temp_env_vars
cargo publish --allow-dirty
git checkout -- .

# Publish the "root" crate
cd ..
cargo publish
```
46 changes: 18 additions & 28 deletions README.adoc → README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
= Temporary Environment Variables
# Temporary Environment Variables

:caution-caption: :fire:
:status:
:url-repo: https://github.com/guenhter/temp_env_vars
[![Version](https://img.shields.io/crates/v/temp_env_vars.svg)](https://crates.io/crates/temp_env_vars)
[![Downloads](https://img.shields.io/crates/d/temp_env_vars)](https://crates.io/crates/temp_env_vars)
[![MIT license](https://img.shields.io/crates/l/temp_env_vars.svg)](./LICENSE)
[![Build Status](https://github.com/guenhter/temp_env_vars/workflows/CI/badge.svg?branch=main)](https://github.com/guenhter/temp_env_vars/actions)
[![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)


ifdef::status[]
image:https://img.shields.io/crates/l/serial_test.svg[MIT license,link={url-repo}/blob/main/LICENSE]
image:{url-repo}/workflows/CI/badge.svg[Build Status (GitHub Actions),link={url-repo}/actions]
endif::[]

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

[CAUTION]
====
The software currently in the starting phase and will change
====
[!WARNING]

== Installation
The software currently in the starting phase and will change


[CAUTION]
====
This crate is not yet available via crates.io (comming soon)
====
## Installation

```toml
temp_env_vars = { git = "https://github.com/guenhter/temp_env_vars.git", branch = "main" }
temp_env_vars_core = { git = "https://github.com/guenhter/temp_env_vars.git", branch = "main" }
```bash
cargo add temp_env_vars
```


== Usage
## Usage

`temp_env_vars` can be used in two different forms:

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


=== Use as macro
### 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
Expand All @@ -59,7 +49,7 @@ fn test_some() {
```


=== Use with TestEnvScope
### Use with TestEnvScope

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.

Expand Down Expand Up @@ -91,12 +81,12 @@ fn test_bar() {
```


== Contribution
## 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
## License

This project is licensed under the https://github.com/guenhter/temp_env_vars/blob/main/LICENSE[MIT license].
145 changes: 129 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,150 @@
//! // "FOO" is not longer set here.
//! }
//! ```
pub use temp_env_vars_core::TestEnvScope;
pub use temp_env_vars_macro::temp_env_vars;
use std::{
collections::HashMap,
sync::{Arc, LazyLock, Mutex},
};

// Makes the mutex available for the `temp_env_vars` macro. Unfortunately, Macro traits cannot
// export other types than macros, so this is the least bad place to export this.
#[doc(hidden)]
pub static TEMP_ENV_VAR_MACRO_MUTEX: LazyLock<Arc<Mutex<()>>> = LazyLock::new(Arc::default);

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

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

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

self.original_vars.keys().for_each(|key| {
after.remove(key);
});
after.keys().for_each(|key| {
std::env::remove_var(key);
});
self.original_vars.iter().for_each(|(k, v)| {
std::env::set_var(k, v);
});
}
}

#[cfg(test)]
mod test {
use super::temp_env_vars;
mod tests {
use std::collections::HashMap;

use assertor::{assert_that, ResultAssertion};
use assertor::{assert_that, EqualityAssertion, ResultAssertion};
use serial_test::serial;
use temp_env_vars_core::TestEnvScope;

use super::TestEnvScope;

#[test]
#[temp_env_vars]
#[serial] // Advised to use serial_test if other env-tests are not used with annotated with "temp_env_vars"
fn test_with_macro() {
#[serial]
fn test_nothing_is_changed() {
let original: HashMap<String, String> = std::env::vars().collect();

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

let after: HashMap<String, String> = std::env::vars().collect();
assert_that!(after).is_equal_to(original);
}

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

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

let after: HashMap<String, String> = std::env::vars().collect();
assert_that!(std::env::var("FOO")).is_err();
std::env::set_var("FOO", "MACRO_TEST");
assert_that!(after).is_equal_to(original);
}

#[test]
#[serial] // Advised to use the serial_test create to avoid env mixup
fn test_with_env_scope() {
#[serial]
fn test_changed_vars_are_reset() {
std::env::set_var("FOO", "BAR2");
let original: HashMap<String, String> = std::env::vars().collect();

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

let after: HashMap<String, String> = std::env::vars().collect();
assert_that!(std::env::var("FOO")).has_ok("BAR2".to_string());
assert_that!(after).is_equal_to(original);
}

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

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

let after: HashMap<String, String> = std::env::vars().collect();
assert_that!(std::env::var("FOO")).has_ok("BAR3".to_string());
assert_that!(after).is_equal_to(original);
}

#[test]
#[serial]
fn test_two_scopes_active_at_same_time() {
std::env::remove_var("FOO");

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

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

assert_that!(std::env::var("FOO")).is_err();
}

#[test]
#[serial]
fn test_sequential_test_scopes() {
std::env::remove_var("FOO");

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

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

drop(env_scope); // Env vars should be cleaned here
{
let _env_scope = TestEnvScope::new();

std::env::set_var("FOO", "BAR6");
assert_that!(std::env::var("FOO")).is_ok();
}
assert_that!(std::env::var("FOO")).is_err();
}
}
20 changes: 0 additions & 20 deletions temp_env_vars_core/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion temp_env_vars_core/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion temp_env_vars_core/README.adoc

This file was deleted.

Loading

0 comments on commit ad90568

Please sign in to comment.