summaryrefslogtreecommitdiffstats
path: root/tests/overwrite_key.rs
blob: ae6be3772fb5046054e63a2964ee4cca8fc5e458 (plain)
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
use config::{Config, File, Environment};
use serde::{Serialize,  Deserialize};

const CONFIG: &str = r#"
name = "foo"
[v4]
ca_path = "ca"
cert_path = "bar"
key_path = "baz"
"#;

#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
    pub name: String,
    pub v4: TlsConfig,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TlsConfig {
    pub ca_path: String,
    #[serde(alias = "certpath")]
    pub cert_path: String,
    // #[serde(alias = "keypath")]
    pub key_path: String,
}

#[test]
fn overwrite_key() {
    std::env::set_var("V4_CERTPATH", "Hello World");

    let s = Config::builder()
        .add_source(File::from_str(CONFIG, config::FileFormat::Toml))
        .add_source(Environment::default().separator("_"))
        .build();

    assert!(s.is_ok(), "build failed: {:?}", s);
    let s = s.unwrap();

    let v: Result<Settings, _> = s.try_deserialize();

    // This is expected to error because the key `certpath` is specified by ENV and `cert_path`
    // from the TOML.
    // This should work, but does not because of the way this crate deserializes into T.
    //
    // The fix is to name the "TlsConfig::cert_path" field "TlsConfig::certpath".
    assert!(v.is_err(), "accidentially ok: {:?}", v);
}