From 56491d7086a58146639cf1d9c35e4944b733c9e8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 23 Aug 2022 13:29:11 +0200 Subject: [PATCH 1/3] Add test for overwriting key from env Signed-off-by: Matthias Beyer --- tests/overwrite_key.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/overwrite_key.rs diff --git a/tests/overwrite_key.rs b/tests/overwrite_key.rs new file mode 100644 index 00000000..9f1283f5 --- /dev/null +++ b/tests/overwrite_key.rs @@ -0,0 +1,40 @@ +use config::{Config, File, Environment}; +use serde::{Serialize, Deserialize}; + +const CONFIG: &str = r#" +name = "foo" +[v4] +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 = s.try_deserialize(); + assert!(v.is_ok(), "not ok: {:?}", v); +} From a324d20a54a4086a46edcaf4b643a809eb7932ae Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 23 Aug 2022 13:34:36 +0200 Subject: [PATCH 2/3] fixup! Add test for overwriting key from env --- tests/overwrite_key.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/overwrite_key.rs b/tests/overwrite_key.rs index 9f1283f5..059e3b2b 100644 --- a/tests/overwrite_key.rs +++ b/tests/overwrite_key.rs @@ -4,6 +4,7 @@ use serde::{Serialize, Deserialize}; const CONFIG: &str = r#" name = "foo" [v4] +ca_path = "ca" cert_path = "bar" key_path = "baz" "#; From 9bb454ed6b68e3b95b19aa47c86cc04d8636a567 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 23 Aug 2022 13:37:28 +0200 Subject: [PATCH 3/3] Make test expect error The comment on the assert!() explains why this is not expecting an Error. Signed-off-by: Matthias Beyer --- tests/overwrite_key.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/overwrite_key.rs b/tests/overwrite_key.rs index 059e3b2b..ae6be377 100644 --- a/tests/overwrite_key.rs +++ b/tests/overwrite_key.rs @@ -37,5 +37,11 @@ fn overwrite_key() { let s = s.unwrap(); let v: Result = s.try_deserialize(); - assert!(v.is_ok(), "not ok: {:?}", v); + + // 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); }