Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion crates/viport-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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("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"));
Expand Down Expand Up @@ -122,6 +122,14 @@ fn read_config_file(path: &Path, required: bool) -> Result<Option<String>> {
}
}

/// 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<std::ffi::OsString>) -> Option<PathBuf> {
env("VIPORT_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)?;
Expand Down Expand Up @@ -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");
Expand Down