use std::path::{Path, PathBuf};
use std::sync::LazyLock;
#[cfg(not(test))]
static CONF_DIR: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::config_dir);
#[cfg(test)]
static CONF_DIR: LazyLock<Option<PathBuf>> = LazyLock::new(|| Some(std::env::temp_dir()));
#[cfg(not(test))]
static CACHE_DIR: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::cache_dir);
#[cfg(test)]
static CACHE_DIR: LazyLock<Option<PathBuf>> = LazyLock::new(|| Some(std::env::temp_dir()));
pub fn init_config_dir() -> Result<Option<PathBuf>, String> {
if let Some(dir) = CONF_DIR.as_deref() {
init_dir(dir).map(Option::Some)
} else {
Ok(None)
}
}
pub fn init_cache_dir() -> Result<Option<PathBuf>, String> {
if let Some(dir) = CACHE_DIR.as_deref() {
init_dir(dir).map(Option::Some)
} else {
Ok(None)
}
}
fn init_dir(p: &Path) -> Result<PathBuf, String> {
let mut p: PathBuf = p.to_path_buf();
p.push("termscp/");
if p.exists() {
return Ok(p);
}
match std::fs::create_dir_all(p.as_path()) {
Ok(_) => Ok(p),
Err(err) => Err(err.to_string()),
}
}
pub fn get_bookmarks_paths(config_dir: &Path) -> PathBuf {
let mut bookmarks_file: PathBuf = PathBuf::from(config_dir);
bookmarks_file.push("bookmarks.toml");
bookmarks_file
}
pub fn get_config_paths(config_dir: &Path) -> (PathBuf, PathBuf) {
let mut bookmarks_file: PathBuf = PathBuf::from(config_dir);
bookmarks_file.push("config.toml");
let mut keys_dir: PathBuf = PathBuf::from(config_dir);
keys_dir.push(".ssh/"); (bookmarks_file, keys_dir)
}
pub fn get_log_paths(cache_dir: &Path) -> PathBuf {
let mut log_file: PathBuf = PathBuf::from(cache_dir);
log_file.push("termscp.log");
log_file
}
pub fn get_theme_path(config_dir: &Path) -> PathBuf {
let mut theme_file: PathBuf = PathBuf::from(config_dir);
theme_file.push("theme.toml");
theme_file
}
#[cfg(test)]
mod tests {
use std::fs::{File, OpenOptions};
use std::io::Write;
use pretty_assertions::assert_eq;
use serial_test::serial;
use super::*;
#[test]
#[serial]
fn test_system_environment_get_config_dir() {
let conf_dir: PathBuf = init_config_dir().ok().unwrap().unwrap();
assert!(std::fs::remove_dir_all(conf_dir.as_path()).is_ok());
}
#[test]
#[serial]
fn should_get_cache_dir() {
let cache_dir: PathBuf = init_cache_dir().ok().unwrap().unwrap();
assert!(std::fs::remove_dir_all(cache_dir.as_path()).is_ok());
}
#[test]
#[serial]
fn test_system_environment_get_config_dir_err() {
let mut conf_dir: PathBuf = std::env::temp_dir();
conf_dir.push("termscp");
let mut f: File = OpenOptions::new()
.create(true)
.write(true)
.open(conf_dir.as_path())
.ok()
.unwrap();
assert!(writeln!(f, "Hello world!").is_ok());
drop(f);
assert!(init_config_dir().is_err());
assert!(std::fs::remove_file(conf_dir.as_path()).is_ok());
}
#[test]
#[serial]
fn test_system_environment_get_bookmarks_paths() {
assert_eq!(
get_bookmarks_paths(Path::new("/home/omar/.config/termscp/")),
PathBuf::from("/home/omar/.config/termscp/bookmarks.toml"),
);
}
#[test]
#[serial]
fn test_system_environment_get_config_paths() {
assert_eq!(
get_config_paths(Path::new("/home/omar/.config/termscp/")),
(
PathBuf::from("/home/omar/.config/termscp/config.toml"),
PathBuf::from("/home/omar/.config/termscp/.ssh/")
)
);
}
#[test]
#[serial]
fn test_system_environment_get_log_paths() {
assert_eq!(
get_log_paths(Path::new("/home/omar/.cache/termscp/")),
PathBuf::from("/home/omar/.cache/termscp/termscp.log"),
);
}
#[test]
#[serial]
fn test_system_environment_get_theme_path() {
assert_eq!(
get_theme_path(Path::new("/home/omar/.config/termscp/")),
PathBuf::from("/home/omar/.config/termscp/theme.toml"),
);
}
}