diff --git a/src/options.rs b/src/options.rs index 11c5aaa54..0cac29fe6 100644 --- a/src/options.rs +++ b/src/options.rs @@ -60,54 +60,60 @@ macro_rules! is_flag_enabled { }; } -cfg_if::cfg_if! { - if #[cfg(test)] { - const DEFAULT_CONFIG_FILE_PATH: &str = "bottom_this_is_a_test/bottom.toml"; - } else { - const DEFAULT_CONFIG_FILE_PATH: &str = "bottom/bottom.toml"; - } -} +/// The default config file sub-path. +const DEFAULT_CONFIG_FILE_LOCATION: &str = "bottom/bottom.toml"; /// Returns the config path to use. If `override_config_path` is specified, then /// we will use that. If not, then return the "default" config path, which is: +/// /// - If a path already exists at `/bottom/bottom.toml`, then use that for /// legacy reasons. /// - Otherwise, use `/bottom/bottom.toml`. /// /// For more details on this, see [dirs](https://docs.rs/dirs/latest/dirs/fn.config_dir.html)' /// documentation. +/// +/// XXX: For macOS, we additionally will manually check `$XDG_CONFIG_HOME` as well first +/// before falling back to `dirs`. fn get_config_path(override_config_path: Option<&Path>) -> Option { if let Some(conf_loc) = override_config_path { return Some(conf_loc.to_path_buf()); } else if let Some(home_path) = dirs::home_dir() { let mut old_home_path = home_path; old_home_path.push(".config/"); - old_home_path.push(DEFAULT_CONFIG_FILE_PATH); - if old_home_path.exists() { - // We used to create it at `/DEFAULT_CONFIG_FILE_PATH`, but changed it - // to be more correct later. However, for legacy reasons, if it already exists, - // use the old one. - return Some(old_home_path); + old_home_path.push(DEFAULT_CONFIG_FILE_LOCATION); + if let Ok(res) = old_home_path.try_exists() { + if res { + // We used to create it at `/DEFAULT_CONFIG_FILE_PATH`, but changed it + // to be more correct later. However, for legacy reasons, if it already exists, + // use the old one. + return Some(old_home_path); + } } } let config_path = dirs::config_dir().map(|mut path| { - path.push(DEFAULT_CONFIG_FILE_PATH); + path.push(DEFAULT_CONFIG_FILE_LOCATION); path }); if cfg!(target_os = "macos") { - if let Some(old_macos_path) = &config_path { - if old_macos_path.exists() { - return config_path; - } - } - if let Ok(xdg_config_path) = std::env::var("XDG_CONFIG_HOME") { if !xdg_config_path.is_empty() { + // If XDG_CONFIG_HOME exists and is non-empty, _but_ we previously used the Library-based path + // for a config and it exists, then use that instead for backwards-compatibility. + if let Some(old_macos_path) = &config_path { + if let Ok(res) = old_macos_path.try_exists() { + if res { + return config_path; + } + } + } + + // Otherwise, try and use the XDG_CONFIG_HOME-based path. let mut cfg_path = PathBuf::new(); cfg_path.push(xdg_config_path); - cfg_path.push(DEFAULT_CONFIG_FILE_PATH); + cfg_path.push(DEFAULT_CONFIG_FILE_LOCATION); return Some(cfg_path); } @@ -1215,7 +1221,7 @@ mod test { #[test] fn test_get_config_path_macos() { use super::get_config_path; - use super::DEFAULT_CONFIG_FILE_PATH; + use super::DEFAULT_CONFIG_FILE_LOCATION; use std::path::PathBuf; // Case three: no previous config, no XDG var. @@ -1231,7 +1237,10 @@ mod test { }) .unwrap(); - assert_eq!(get_config_path(None), Some(case_1)); + // Skip this test if the file already exists. + if !case_1.exists() { + assert_eq!(get_config_path(None), Some(case_1)); + } // Case two: no previous config, XDG var exists. std::env::set_var("XDG_CONFIG_HOME", "/tmp"); @@ -1239,7 +1248,10 @@ mod test { case_2.push("/tmp"); case_2.push(DEFAULT_CONFIG_FILE_PATH); - assert_eq!(get_config_path(None), Some(case_2)); + // Skip this test if the file already exists. + if !case_2.exists() { + assert_eq!(get_config_path(None), Some(case_2)); + } // Case one: old non-XDG exists already, XDG var exists. // let case_3 = case_1;