You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to load configuration variables from various sources using figment. The module looks like this:
#[derive(Deserialize)]pubstructConfig{pubport:u16,pubhost:String,}pubfnload_config() -> Config{let file_name = "config";Figment::new().join(("port",8080)).join(("host","localhost")).merge(Env::raw()).extract().expect("Fail to load config")}
When I'm tried to test the load_condig function, it seems that the environment variables don't get loaded. Here is my test module
#[cfg(test)]mod tests {usesuper::*;use std::env;#[test]fnloads_defaults(){let res = load_config();assert_eq!(res.host, "localhost");assert_eq!(res.port, 8080);}#[test]fnenvironment_variables_override_defaults(){
env::set_var("port","8888");let res = load_config();assert_eq!(res.port, 8888);}}
The first test run successfully. But the second test fails - the port stays unchanged although I expected it to be overwritten in the figment with 8888.
What I am doing wrong? It seems that the env vars don't get loaded or not overwritten correctly. I also tested with Jail as follows, but with the same result with the port variables don't get overwritten.
#[test]fnenvironment_variables_override_defaults_in_jail(){
figment::Jail::expect_with(|jail| {
jail.set_env("port",8888);let res = load_config();assert_eq!(res.port, 8888);Ok(())});}
The text was updated successfully, but these errors were encountered:
jaads
changed the title
Figment doesn't load env variables
merge() doesn't overwrite config values
Sep 26, 2024
I also wanted to load configuration variables from a file which should overwrite the defaults. But this doesn't seem to work either, although I basically used this example from the docs.
so I think the merge function does not work as expected.
I'm trying to load configuration variables from various sources using figment. The module looks like this:
When I'm tried to test the
load_condig
function, it seems that the environment variables don't get loaded. Here is my test moduleThe first test run successfully. But the second test fails - the port stays unchanged although I expected it to be overwritten in the figment with 8888.
What I am doing wrong? It seems that the env vars don't get loaded or not overwritten correctly. I also tested with Jail as follows, but with the same result with the port variables don't get overwritten.
The text was updated successfully, but these errors were encountered: