use std::path::PathBuf;
use crate::config::EnvironmentConfig;
#[allow(clippy::disallowed_methods)] pub fn auths_home_with_config(config: &EnvironmentConfig) -> Result<PathBuf, AuthsHomeError> {
if let Some(ref home) = config.auths_home {
return Ok(home.clone());
}
let home = dirs::home_dir().ok_or(AuthsHomeError::NoHomeDir)?;
Ok(home.join(".auths"))
}
pub fn auths_home() -> Result<PathBuf, AuthsHomeError> {
auths_home_with_config(&EnvironmentConfig::from_env())
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum AuthsHomeError {
#[error("Could not determine home directory")]
NoHomeDir,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn explicit_home_overrides_default() {
let env = EnvironmentConfig::builder()
.auths_home(std::path::PathBuf::from("/tmp/custom-auths"))
.build();
assert_eq!(
auths_home_with_config(&env).unwrap(),
std::path::PathBuf::from("/tmp/custom-auths")
);
}
#[test]
fn no_override_falls_back_to_default() {
let env = EnvironmentConfig::builder().build();
let path = auths_home_with_config(&env).unwrap();
assert!(path.ends_with(".auths"));
}
}