-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
110 lines (99 loc) · 3.17 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![feature(try_from)]
extern crate vidar;
#[macro_use]
mod lifecycle;
use std::collections::HashMap;
use std::convert::TryFrom;
use vidar::{Config, ConfigBuilder, Environment, Error, ErrorKind, Kind};
#[test]
fn no_file() {
let mut most = HashMap::new();
most.insert(Kind::Common, lifecycle::COMMON);
most.insert(Kind::Development, lifecycle::DEV);
most.insert(Kind::Test, lifecycle::TEST);
most.insert(Kind::Staging, lifecycle::STAGE);
most.insert(Kind::Production, lifecycle::PROD);
let config = ConfigBuilder::default()
.app_name("no_file")
.kind(Kind::Integration)
.build()
.expect("Unable to build `Config`");
wrap!("no_file", Some(most), {
match Environment::try_from(config) {
Ok(_) => assert!(false),
Err(e) => match e {
Error(ErrorKind::Io(_), _) => assert!(true),
_ => assert!(false),
},
}
});
}
#[test]
fn invalid_property() {
let config = ConfigBuilder::default()
.app_name("invalid_property")
.kind(Kind::Staging)
.build()
.expect("Unable to build `Config`");
wrap!("invalid_property", None, {
match Environment::try_from(config) {
Ok(_) => assert!(false),
Err(e) => match e {
Error(ErrorKind::InvalidProperty, _) => assert!(true),
_ => assert!(false),
},
}
});
}
fn check_test_props(props: &HashMap<String, String>) {
assert!(props.contains_key("key1"));
assert_eq!(props.get(&"key1".to_string()), Some(&"val1".to_string()));
assert!(props.contains_key("key2"));
assert_eq!(props.get(&"key2".to_string()), Some(&"val2".to_string()));
assert!(props.contains_key("key3"));
assert_eq!(props.get(&"key3".to_string()), Some(&"val3".to_string()));
assert!(props.contains_key("url"));
}
fn check_env_config(folder_name: &str, config: Config, url_value: &str) {
wrap!(folder_name, None, {
match Environment::try_from(config) {
Ok(env) => {
let props = env.props();
check_test_props(props);
assert_eq!(props.get(&"url".to_string()), Some(&url_value.to_string()));
}
Err(_e) => assert!(false),
}
});
}
#[test]
fn dev_config_env() {
let config = ConfigBuilder::default()
.app_name("dev_config")
.common(true)
.build()
.expect("Unable to build Config");
check_env_config("dev_config", config, "https://localhost");
}
#[test]
fn test_with_comments_env() {
let config = ConfigBuilder::default()
.app_name("test_with_comments")
.kind(Kind::Test)
.common(true)
.comments(true)
.comment_char('#')
.build()
.expect("Unable to build Config");
check_env_config("test_with_comments", config, "https://testurl.vidar.com");
}
#[test]
fn prod_config_env() {
let config = ConfigBuilder::default()
.app_name("prod_config")
.common(true)
.kind(Kind::Production)
.build()
.expect("Unable to build Config");
check_env_config("prod_config", config, "https://produrl.vidar.com");
}