mirror of
https://github.com/PR0M3TH3AN/Marlin.git
synced 2025-09-09 07:38:50 +00:00
test(config): verify fallback when no home
This commit is contained in:
@@ -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"),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user