Selaa lähdekoodia

Config: ignore an empty VICTORY_CONFIG

(cherry picked from commit fd44561ea1180aff5380285527fb8cac8195f4e8)
Enzo Souza Duarte 2 päivää sitten
vanhempi
sitoutus
41dc5da4c9
1 muutettua tiedostoa jossa 20 lisäystä ja 1 poistoa
  1. 20 1
      crates/victory-core/src/config.rs

+ 20 - 1
crates/victory-core/src/config.rs

@@ -46,7 +46,7 @@ struct FilePaths {
 impl Config {
     /// Loads configuration from the environment and the config file.
     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
             .clone()
             .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> {
     let dir = root.join(sub);
     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]
     fn example_config_parses() {
         let text = include_str!("../../../victory.example.toml");