Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/path-cli/tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,38 @@ pub fn env_lock() -> std::sync::MutexGuard<'static, ()> {
.unwrap_or_else(|e| e.into_inner())
}

/// RAII guard that pins `$HOME` and `$TOOLPATH_CONFIG_DIR` to a tempdir.
/// RAII guard that pins `$HOME`, `$TOOLPATH_CONFIG_DIR`, and
/// `$XDG_DATA_HOME` to a tempdir.
///
/// `$XDG_DATA_HOME` matters because the opencode `PathResolver` prefers it
/// over `$HOME` when locating `opencode.db`. Without pinning it, a machine
/// that sets `$XDG_DATA_HOME` (common on Linux; also set on this dev box)
/// would send the opencode projector to the *real* user database — the
/// test would fail with "table … already exists" and, worse, mutate the
/// user's live opencode data. Pinning it keeps every harness sandboxed.
pub struct ScopedHome {
_td: tempfile::TempDir,
prev_home: Option<OsString>,
prev_config: Option<OsString>,
prev_xdg_data: Option<OsString>,
}

impl ScopedHome {
pub fn new() -> Self {
let td = tempfile::tempdir().unwrap();
let prev_home = std::env::var_os("HOME");
let prev_config = std::env::var_os("TOOLPATH_CONFIG_DIR");
let prev_xdg_data = std::env::var_os("XDG_DATA_HOME");
unsafe {
std::env::set_var("HOME", td.path());
std::env::set_var("TOOLPATH_CONFIG_DIR", td.path().join(".toolpath"));
std::env::set_var("XDG_DATA_HOME", td.path().join(".local/share"));
}
Self {
_td: td,
prev_home,
prev_config,
prev_xdg_data,
}
}

Expand All @@ -65,6 +77,10 @@ impl Drop for ScopedHome {
Some(v) => std::env::set_var("TOOLPATH_CONFIG_DIR", v),
None => std::env::remove_var("TOOLPATH_CONFIG_DIR"),
}
match &self.prev_xdg_data {
Some(v) => std::env::set_var("XDG_DATA_HOME", v),
None => std::env::remove_var("XDG_DATA_HOME"),
}
}
}
}
Expand Down