summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Booth <pmbooth@gmail.com>2018-09-26 06:49:21 +0100
committerPhil Booth <pmbooth@gmail.com>2018-09-26 07:02:39 +0100
commita1cd868c0224f16d921a25f04cbc39cb03a5471d (patch)
tree41159d19af67c8bacc1917b0ddac193a8e6bf416
parent4475925dc2136ae139245164cbb77d59b3d84dae (diff)
Treat empty environment variables as unset
-rw-r--r--Cargo.toml1
-rw-r--r--src/env.rs5
-rw-r--r--tests/env.rs12
3 files changed, 18 insertions, 0 deletions
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");
+}