diff options
author | Phil Booth <pmbooth@gmail.com> | 2018-09-26 06:49:21 +0100 |
---|---|---|
committer | Phil Booth <pmbooth@gmail.com> | 2018-09-26 07:02:39 +0100 |
commit | a1cd868c0224f16d921a25f04cbc39cb03a5471d (patch) | |
tree | 41159d19af67c8bacc1917b0ddac193a8e6bf416 | |
parent | 4475925dc2136ae139245164cbb77d59b3d84dae (diff) |
Treat empty environment variables as unset
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/env.rs | 5 | ||||
-rw-r--r-- | tests/env.rs | 12 |
3 files changed, 18 insertions, 0 deletions
@@ -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" @@ -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"); +} |