Skip to content

Commit

Permalink
feat: allow users not to provide config option with default values
Browse files Browse the repository at this point in the history
Now, you are able to run the tracler like this:

```
TORRUST_TRACKER_CONFIG="" cargo run
```

Default values will be used for the missing values in the provided
configuration. In that case, none of the values have been provided, so
it will use default values for all options.
  • Loading branch information
josecelano committed May 9, 2024
1 parent 43942ce commit 0252f30
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ use std::fs;
use std::net::IpAddr;
use std::str::FromStr;

use figment::providers::{Env, Format, Toml};
use figment::providers::{Env, Format, Serialized, Toml};
use figment::Figment;
use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};
Expand Down Expand Up @@ -393,7 +393,7 @@ impl Configuration {
///
/// Will return `Err` if the environment variable does not exist or has a bad configuration.
pub fn load(info: &Info) -> Result<Configuration, Error> {
let figment = Figment::new()
let figment = Figment::from(Serialized::defaults(Configuration::default()))
.merge(Toml::string(&info.tracker_toml))
.merge(Env::prefixed("TORRUST_TRACKER__").split("__"));

Expand Down Expand Up @@ -523,6 +523,24 @@ mod tests {
assert_eq!(contents, default_config_toml());
}

#[test]
fn configuration_should_use_the_default_values_when_an_empty_configuration_is_provided_by_the_user() {
figment::Jail::expect_with(|_jail| {
let empty_configuration = String::new();

let info = Info {
tracker_toml: empty_configuration,
api_admin_token: None,
};

let configuration = Configuration::load(&info).expect("Could not load configuration from file");

assert_eq!(configuration, Configuration::default());

Ok(())
});
}

#[test]
fn configuration_should_be_loaded_from_a_toml_config_file() {
figment::Jail::expect_with(|_jail| {
Expand Down

0 comments on commit 0252f30

Please sign in to comment.