Skip to content

Instantly share code, notes, and snippets.

@killercup
Last active Mar 23, 2018
Embed
What would you like to do?
/// just some api ideas
#[test]
fn config_loading() {
let fs = MockFS::new()
.file("/usr/local/etc/foo", "fancy = 10")
.file("~/.config/foo", "fancy = 20")
.file(".env", "LE_PREFIX_FANCY=30");
#[derive(Default, Configure, StructOpt)]
struct Args {
fancy: i32,
}
let args = Args::config()
.config_source(ConfigSource::toml().with_mock_fs(fs))
.config_source(ConfigSource::dot_env("LE_PREFIX"))
.config_source(ConfigSource::env("LE_PREFIX"));
assert_eq!(args.clone().load(), Args { fancy: 30 });
std::env::set_var("LE_PREFIX_FANCY", "40");
assert_eq!(args.clone().load(), Args { fancy: 40 });
let args = args.clone()
.config_source(ConfigSource::structopt()
.handle_help_and_version()
.with_mock_args(&["--fancy=50"])
);
assert_eq!(args.clone().load(), Args { fancy: 50 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment