fix(spaceshd): isolate instance-lock test via SPACESH_LOCK

lock_is_exclusive_within_process acquired the global ~/.spacesh/daemon.lock,
so it flaked whenever a real daemon was running. Add a SPACESH_LOCK env
override to lock_path() and point the test at a private temp file under a
serial() guard, making the suite deterministic regardless of a live daemon.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-14 08:00:35 +07:00
parent cd44f0b263
commit 21180ae9e0
+12
View File
@@ -19,6 +19,11 @@ pub fn socket_path() -> Result<PathBuf> {
} }
pub fn lock_path() -> Result<PathBuf> { pub fn lock_path() -> Result<PathBuf> {
if let Ok(p) = std::env::var("SPACESH_LOCK") {
if !p.is_empty() {
return Ok(PathBuf::from(p));
}
}
Ok(spacesh_dir()?.join("daemon.lock")) Ok(spacesh_dir()?.join("daemon.lock"))
} }
@@ -64,6 +69,11 @@ mod tests {
#[test] #[test]
fn lock_is_exclusive_within_process() { fn lock_is_exclusive_within_process() {
let _serial = crate::test_support::serial();
// Use a private lock path so a real running daemon (which holds the
// global ~/.spacesh/daemon.lock) can't make this test flake.
let tmp = std::env::temp_dir().join("spacesh-lock-exclusive-test.lock");
std::env::set_var("SPACESH_LOCK", &tmp);
let first = acquire_instance_lock().unwrap(); let first = acquire_instance_lock().unwrap();
assert!(first.is_some(), "first acquire should succeed"); assert!(first.is_some(), "first acquire should succeed");
// A second attempt from the same process on the same fd path: // A second attempt from the same process on the same fd path:
@@ -72,6 +82,8 @@ mod tests {
let second = acquire_instance_lock().unwrap(); let second = acquire_instance_lock().unwrap();
assert!(second.is_none(), "second acquire should be blocked"); assert!(second.is_none(), "second acquire should be blocked");
drop(first); drop(first);
std::env::remove_var("SPACESH_LOCK");
let _ = std::fs::remove_file(&tmp);
} }
#[test] #[test]