diff --git a/crates/viport-core/src/config.rs b/crates/viport-core/src/config.rs index b6d43d60..9c7cd111 100644 --- a/crates/viport-core/src/config.rs +++ b/crates/viport-core/src/config.rs @@ -50,7 +50,7 @@ struct FilePaths { impl Config { /// Loads configuration from the environment and the config file. pub fn load() -> Result { - let explicit = std::env::var_os("VIPORT_CONFIG").map(PathBuf::from); + let explicit = explicit_config_path(|k| std::env::var_os(k)); let path = explicit .clone() .unwrap_or_else(|| PathBuf::from("viport.toml")); @@ -122,6 +122,14 @@ fn read_config_file(path: &Path, required: bool) -> Result> { } } +/// The config file explicitly named by the environment, if any. An empty +/// `VIPORT_CONFIG` means unset, like the other path variables. +fn explicit_config_path(env: impl Fn(&str) -> Option) -> Option { + env("VIPORT_CONFIG") + .filter(|v| !v.is_empty()) + .map(PathBuf::from) +} + fn subdir(root: &Path, sub: &str) -> Result { let dir = root.join(sub); ensure_outside_repo(&dir)?; @@ -213,6 +221,17 @@ mod tests { ); } + #[test] + fn empty_config_env_is_ignored() { + let empty = |k: &str| (k == "VIPORT_CONFIG").then(OsString::new); + assert_eq!(explicit_config_path(empty), None); + let set = |k: &str| (k == "VIPORT_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!("../../../viport.example.toml");