|
@@ -46,7 +46,7 @@ struct FilePaths {
|
|
|
impl Config {
|
|
impl Config {
|
|
|
/// Loads configuration from the environment and the config file.
|
|
/// Loads configuration from the environment and the config file.
|
|
|
pub fn load() -> Result<Self> {
|
|
pub fn load() -> Result<Self> {
|
|
|
- let explicit = std::env::var_os("VICTORY_CONFIG").map(PathBuf::from);
|
|
|
|
|
|
|
+ let explicit = explicit_config_path(|k| std::env::var_os(k));
|
|
|
let path = explicit
|
|
let path = explicit
|
|
|
.clone()
|
|
.clone()
|
|
|
.unwrap_or_else(|| PathBuf::from("victory.toml"));
|
|
.unwrap_or_else(|| PathBuf::from("victory.toml"));
|
|
@@ -119,6 +119,14 @@ impl Config {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// The config file explicitly named by the environment, if any. An empty
|
|
|
|
|
+/// `VICTORY_CONFIG` means unset, like the other path variables.
|
|
|
|
|
+fn explicit_config_path(env: impl Fn(&str) -> Option<std::ffi::OsString>) -> Option<PathBuf> {
|
|
|
|
|
+ env("VICTORY_CONFIG")
|
|
|
|
|
+ .filter(|v| !v.is_empty())
|
|
|
|
|
+ .map(PathBuf::from)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
fn subdir(root: &Path, sub: &str) -> Result<PathBuf> {
|
|
fn subdir(root: &Path, sub: &str) -> Result<PathBuf> {
|
|
|
let dir = root.join(sub);
|
|
let dir = root.join(sub);
|
|
|
ensure_outside_repo(&dir)?;
|
|
ensure_outside_repo(&dir)?;
|
|
@@ -188,6 +196,17 @@ mod tests {
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn empty_config_env_is_ignored() {
|
|
|
|
|
+ let empty = |k: &str| (k == "VICTORY_CONFIG").then(OsString::new);
|
|
|
|
|
+ assert_eq!(explicit_config_path(empty), None);
|
|
|
|
|
+ let set = |k: &str| (k == "VICTORY_CONFIG").then(|| OsString::from("custom.toml"));
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ explicit_config_path(set),
|
|
|
|
|
+ Some(PathBuf::from("custom.toml"))
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
#[test]
|
|
#[test]
|
|
|
fn example_config_parses() {
|
|
fn example_config_parses() {
|
|
|
let text = include_str!("../../../victory.example.toml");
|
|
let text = include_str!("../../../victory.example.toml");
|