test(config): verify fallback when no home

This commit is contained in:
thePR0M3TH3AN
2025-05-21 08:32:25 -04:00
parent 4453bbe746
commit 9fa57aa1e7
2 changed files with 48 additions and 6 deletions

View File

@@ -20,3 +20,42 @@ fn load_xdg_or_fallback() {
let cfg = Config::load().unwrap();
assert!(cfg.db_path.to_string_lossy().ends_with(".db"));
}
#[test]
fn load_fallback_current_dir() {
// Save and clear HOME & XDG_DATA_HOME
let orig_home = env::var_os("HOME");
let orig_xdg = env::var_os("XDG_DATA_HOME");
env::remove_var("HOME");
env::remove_var("XDG_DATA_HOME");
env::remove_var("MARLIN_DB_PATH");
let cfg = Config::load().unwrap();
// Compute expected file name based on current directory hash
let cwd = env::current_dir().unwrap();
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut h = DefaultHasher::new();
cwd.hash(&mut h);
let digest = h.finish();
let expected_name = format!("index_{:016x}.db", digest);
assert_eq!(cfg.db_path, std::path::PathBuf::from(&expected_name));
assert!(cfg
.db_path
.file_name()
.unwrap()
.to_string_lossy()
.starts_with("index_"));
// Restore environment variables
match orig_home {
Some(val) => env::set_var("HOME", val),
None => env::remove_var("HOME"),
}
match orig_xdg {
Some(val) => env::set_var("XDG_DATA_HOME", val),
None => env::remove_var("XDG_DATA_HOME"),
}
}