Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge() with a leading join() doesn't overwrite #128

Open
jaads opened this issue Sep 25, 2024 · 1 comment
Open

merge() with a leading join() doesn't overwrite #128

jaads opened this issue Sep 25, 2024 · 1 comment

Comments

@jaads
Copy link

jaads commented Sep 25, 2024

I'm trying to load configuration variables from various sources using figment. The module looks like this:

#[derive(Deserialize)]
pub struct Config {
    pub port: u16,
    pub host: String,
}

pub fn load_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 {
    use super::*;
    use std::env;

    #[test]
    fn loads_defaults() {
        let res = load_config();
        assert_eq!(res.host, "localhost");
        assert_eq!(res.port, 8080);
    }

    #[test]
    fn environment_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]
  fn environment_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(())
      });
  }
@jaads jaads changed the title Figment doesn't load env variables merge() doesn't overwrite config values Sep 26, 2024
@jaads
Copy link
Author

jaads commented 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.

here is a test which is failing:

  #[test]
    fn file_overwrites_defaults_with_jail() {

  #[derive(Debug, Deserialize, PartialEq)]
        pub struct Config {
            pub port: u16,
        }

        figment::Jail::expect_with(|jail| {
            jail.create_file("config.toml", r#"
        port = 9999
    "#)?;

            let config: Config = Figment::new()
                .join(("port", 8080))
                .merge(Toml::file("config.toml"))
                .extract().expect("extract result");

            assert_eq!(config, Config {
                port: 9999,
            });

            Ok(())
        });
    }

why is the port not overwritten by merge() with the value 9999?

@jaads jaads changed the title merge() doesn't overwrite config values merge() doesn't overwrite Sep 26, 2024
@jaads jaads changed the title merge() doesn't overwrite merge() doesn't overwrite, with a leading join() Sep 26, 2024
@jaads jaads changed the title merge() doesn't overwrite, with a leading join() merge() with a leading join() doesn't overwrite Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant