Skip to content

Commit

Permalink
Treat empty environment variables as unset
Browse files Browse the repository at this point in the history
  • Loading branch information
philbooth committed Sep 26, 2018
1 parent 4475925 commit a1cd868
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions src/env.rs
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/env.rs
Expand Up @@ -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");
}

0 comments on commit a1cd868

Please sign in to comment.