From a1cd868c0224f16d921a25f04cbc39cb03a5471d Mon Sep 17 00:00:00 2001 From: Phil Booth Date: Wed, 26 Sep 2018 06:49:21 +0100 Subject: Treat empty environment variables as unset --- Cargo.toml | 1 + src/env.rs | 5 +++++ tests/env.rs | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 7b783d6..cdca1dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ json = ["serde_json"] yaml = ["yaml-rust"] hjson = ["serde-hjson"] ini = ["rust-ini"] +ignore-empty-env-vars = [] [dependencies] lazy_static = "1.0" diff --git a/src/env.rs b/src/env.rs index 51e91a9..d570932 100644 --- a/src/env.rs +++ b/src/env.rs @@ -74,6 +74,11 @@ impl Source for Environment { }; for (key, value) in env::vars() { + // Treat empty environment variables as unset + if cfg!(feature = "ignore-empty-env-vars") && value == "" { + continue; + } + let mut key = key.to_string(); // Check for prefix diff --git a/tests/env.rs b/tests/env.rs index 932d2fc..d1cb11a 100644 --- a/tests/env.rs +++ b/tests/env.rs @@ -60,3 +60,15 @@ fn test_separator_behavior() { env::remove_var("C_B_A"); } + +#[test] +#[cfg(feature = "ignore-empty-env-vars")] +fn test_empty_value_is_ignored() { + env::set_var("C_A_B", ""); + + let environment = Environment::new(); + + assert!(!environment.collect().unwrap().contains_key("c_a_b")); + + env::remove_var("C_A_B"); +} -- cgit v1.2.3