summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2018-03-12 11:10:01 -0700
committerGitHub <noreply@github.com>2018-03-12 11:10:01 -0700
commite8fa9fee96185ddd18ebcef8a925c75459111edb (patch)
tree02c7f66517f697d749653727611a74149dc49a32
parent1e16a3bfb7adab7f5e642759e94fb17cba1ea0bd (diff)
parent2fe18bbe8716402f6c56101ccbd55f5f1879d884 (diff)
Merge pull request #59 from ChriFo/fix/env
Adjust environment variable handling
-rw-r--r--src/env.rs36
-rw-r--r--tests/env.rs63
2 files changed, 83 insertions, 16 deletions
diff --git a/src/env.rs b/src/env.rs
index aec4de4..b26dc56 100644
--- a/src/env.rs
+++ b/src/env.rs
@@ -9,19 +9,16 @@ pub struct Environment {
/// Optional prefix that will limit access to the environment to only keys that
/// begin with the defined prefix.
///
- /// A prefix, followed by `_` (the seperator),
- /// is tested to be present on each key before its considered
+ /// A prefix with a separator of `_` is tested to be present on each key before its considered
/// to be part of the source environment.
///
/// For example, the key `CONFIG_DEBUG` would become `DEBUG` with a prefix of `config`.
prefix: Option<String>,
- /// The character sequence that separates each key segment in an environment key pattern.
+ /// Optional character sequence that separates each key segment in an environment key pattern.
/// Consider a nested configuration such as `redis.password`, a separator of `_` would allow
/// an environment key of `REDIS_PASSWORD` to match.
- ///
- /// The default separator is `_`.
- separator: String,
+ separator: Option<String>,
}
impl Environment {
@@ -36,13 +33,13 @@ impl Environment {
}
}
- pub fn prefix(&mut self, s: String) -> &mut Self {
- self.prefix = s.into();
+ pub fn prefix(&mut self, s: &str) -> &mut Self {
+ self.prefix = Some(s.into());
self
}
- pub fn separator(&mut self, s: String) -> &mut Self {
- self.separator = s;
+ pub fn separator(&mut self, s: &str) -> &mut Self {
+ self.separator = Some(s.into());
self
}
}
@@ -51,7 +48,7 @@ impl Default for Environment {
fn default() -> Environment {
Environment {
prefix: None,
- separator: "_".into(),
+ separator: None,
}
}
}
@@ -65,9 +62,14 @@ impl Source for Environment {
let mut m = HashMap::new();
let uri: String = "the environment".into();
- // Define a prefiux pattern to test and exclude from keys
+ let separator = match self.separator {
+ Some(ref separator) => separator,
+ _ => ""
+ };
+
+ // Define a prefix pattern to test and exclude from keys
let prefix_pattern = match self.prefix {
- Some(ref prefix) => Some(prefix.clone() + &self.separator),
+ Some(ref prefix) => Some(prefix.clone() + "_"),
_ => None,
};
@@ -76,7 +78,7 @@ impl Source for Environment {
// Check for prefix
if let Some(ref prefix_pattern) = prefix_pattern {
- if key.to_lowercase().starts_with(prefix_pattern) {
+ if key.to_lowercase().starts_with(&prefix_pattern.to_lowercase()) {
// Remove this prefix from the key
key = key[prefix_pattern.len()..].to_string();
} else {
@@ -85,8 +87,10 @@ impl Source for Environment {
}
}
- // Replace `separator` with `.`
- key = key.replace(&self.separator, ".");
+ // If separator is given replace with `.`
+ if !separator.is_empty() {
+ key = key.replace(separator, ".");
+ }
m.insert(
key.to_lowercase(),
diff --git a/tests/env.rs b/tests/env.rs
new file mode 100644
index 0000000..b204118
--- /dev/null
+++ b/tests/env.rs
@@ -0,0 +1,63 @@
+extern crate config;
+
+use config::*;
+use std::env;
+
+#[test]
+fn test_default() {
+ env::set_var("A_B_C", "abc");
+
+ let environment = Environment::new();
+
+ assert!(environment.collect().unwrap().contains_key("a_b_c"));
+
+ env::remove_var("A_B_C");
+}
+
+#[test]
+fn test_prefix_is_removed_from_key() {
+ env::set_var("B_A_C", "abc");
+
+ let environment = Environment::with_prefix("B");
+
+ assert!(environment.collect().unwrap().contains_key("a_c"));
+
+ env::remove_var("B_A_C");
+}
+
+#[test]
+fn test_prefix_with_variant_forms_of_spelling() {
+ env::set_var("a_A_C", "abc");
+
+ let environment = Environment::with_prefix("a");
+
+ assert!(environment.collect().unwrap().contains_key("a_c"));
+
+ env::remove_var("a_A_C");
+ env::set_var("aB_A_C", "abc");
+
+ let environment = Environment::with_prefix("aB");
+
+ assert!(environment.collect().unwrap().contains_key("a_c"));
+
+ env::remove_var("aB_A_C");
+ env::set_var("Ab_A_C", "abc");
+
+ let environment = Environment::with_prefix("ab");
+
+ assert!(environment.collect().unwrap().contains_key("a_c"));
+
+ env::remove_var("Ab_A_C");
+}
+
+#[test]
+fn test_separator_behavior() {
+ env::set_var("C_B_A", "abc");
+
+ let mut environment = Environment::with_prefix("C");
+ environment.separator("_");
+
+ assert!(environment.collect().unwrap().contains_key("b.a"));
+
+ env::remove_var("C_B_A");
+}