Skip to content

Commit 1ffaa91

Browse files
committed
Add tests for XDG global config lookup
1 parent bd72bdf commit 1ffaa91

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/config.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ pub fn load_config(project_root: &std::path::Path) -> Config {
9696
#[cfg(test)]
9797
mod tests {
9898
use super::*;
99+
use std::sync::{Mutex, OnceLock};
100+
101+
fn env_lock() -> &'static Mutex<()> {
102+
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
103+
LOCK.get_or_init(|| Mutex::new(()))
104+
}
99105

100106
#[test]
101107
fn test_parse_full_config() {
@@ -225,4 +231,53 @@ mod tests {
225231
assert_eq!(config.top_k, Some(42));
226232
assert_eq!(config.hidden, Some(true));
227233
}
234+
235+
#[test]
236+
fn test_global_config_path_prefers_xdg_config_home() {
237+
let _guard = env_lock().lock().unwrap();
238+
let xdg = tempfile::TempDir::new().unwrap();
239+
let home = tempfile::TempDir::new().unwrap();
240+
241+
unsafe {
242+
std::env::set_var("XDG_CONFIG_HOME", xdg.path());
243+
std::env::set_var("HOME", home.path());
244+
}
245+
246+
let path = global_config_path().unwrap();
247+
assert_eq!(path, xdg.path().join("vecgrep").join("config.toml"));
248+
}
249+
250+
#[test]
251+
fn test_global_config_path_falls_back_to_home_dot_config() {
252+
let _guard = env_lock().lock().unwrap();
253+
let home = tempfile::TempDir::new().unwrap();
254+
255+
unsafe {
256+
std::env::remove_var("XDG_CONFIG_HOME");
257+
std::env::set_var("HOME", home.path());
258+
}
259+
260+
let path = global_config_path().unwrap();
261+
assert_eq!(path, home.path().join(".config/vecgrep/config.toml"));
262+
}
263+
264+
#[test]
265+
fn test_load_config_reads_global_from_xdg_config_home() {
266+
let _guard = env_lock().lock().unwrap();
267+
let xdg = tempfile::TempDir::new().unwrap();
268+
let home = tempfile::TempDir::new().unwrap();
269+
let project = tempfile::TempDir::new().unwrap();
270+
let global_dir = xdg.path().join("vecgrep");
271+
std::fs::create_dir_all(&global_dir).unwrap();
272+
std::fs::write(global_dir.join("config.toml"), "top_k = 17\nquiet = true\n").unwrap();
273+
274+
unsafe {
275+
std::env::set_var("XDG_CONFIG_HOME", xdg.path());
276+
std::env::set_var("HOME", home.path());
277+
}
278+
279+
let config = load_config(project.path());
280+
assert_eq!(config.top_k, Some(17));
281+
assert_eq!(config.quiet, Some(true));
282+
}
228283
}

0 commit comments

Comments
 (0)