use std::ffi::OsString;
use std::path::{Path, PathBuf};
pub const RC_FILE: &str = ".ursarc";
pub const HISTORY_FILE: &str = ".ursa_history";
pub const HISTORY_VAR: &str = "URSA_HISTORY";
pub fn init_file(
init: Option<&Path>,
no_init: bool,
interactive: bool,
home: Option<&Path>,
) -> Option<PathBuf> {
if no_init {
return None;
}
if let Some(init) = init {
return Some(init.to_path_buf());
}
let rc = home?.join(RC_FILE);
(interactive && rc.is_file()).then_some(rc)
}
pub fn history_file(
no_history: bool,
var: Option<OsString>,
home: Option<&Path>,
) -> Option<PathBuf> {
if no_history {
return None;
}
match var {
Some(path) if path.is_empty() => None,
Some(path) => Some(PathBuf::from(path)),
None => home.map(|home| home.join(HISTORY_FILE)),
}
}
pub fn home_dir() -> Option<PathBuf> {
std::env::home_dir().filter(|home| !home.as_os_str().is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn init_file_prefers_the_flag() {
let home = tempfile::tempdir().unwrap();
let home = home.path();
let given = Path::new("setup.sql");
assert_eq!(init_file(None, false, true, Some(home)), None);
assert_eq!(
init_file(Some(given), false, true, Some(home)),
Some(given.to_path_buf())
);
assert_eq!(
init_file(Some(given), false, false, Some(home)),
Some(given.to_path_buf())
);
std::fs::write(home.join(RC_FILE), ".mode csv\n").unwrap();
assert_eq!(
init_file(None, false, true, Some(home)),
Some(home.join(RC_FILE))
);
assert_eq!(
init_file(Some(given), false, true, Some(home)),
Some(given.to_path_buf())
);
assert_eq!(init_file(None, true, true, Some(home)), None);
assert_eq!(init_file(Some(given), true, true, Some(home)), None);
assert_eq!(init_file(None, false, false, Some(home)), None);
assert_eq!(init_file(None, false, true, None), None);
}
#[test]
fn a_directory_named_ursarc_is_not_read() {
let home = tempfile::tempdir().unwrap();
std::fs::create_dir(home.path().join(RC_FILE)).unwrap();
assert_eq!(init_file(None, false, true, Some(home.path())), None);
}
#[test]
fn history_file_follows_the_variable() {
let home = Path::new("/home/someone");
assert_eq!(
history_file(false, None, Some(home)),
Some(home.join(HISTORY_FILE))
);
assert_eq!(
history_file(false, Some("/tmp/h".into()), Some(home)),
Some(PathBuf::from("/tmp/h"))
);
assert_eq!(history_file(false, Some("".into()), Some(home)), None);
assert_eq!(history_file(true, None, Some(home)), None);
assert_eq!(history_file(true, Some("/tmp/h".into()), Some(home)), None);
assert_eq!(history_file(false, None, None), None);
}
}