diff --git a/src/cortex-cli/src/agent_cmd/tests.rs b/src/cortex-cli/src/agent_cmd/tests.rs index e2ff07f9..18f7ba75 100644 --- a/src/cortex-cli/src/agent_cmd/tests.rs +++ b/src/cortex-cli/src/agent_cmd/tests.rs @@ -3,10 +3,9 @@ #[cfg(test)] mod tests { use crate::agent_cmd::cli::{CopyArgs, ExportArgs}; - use crate::agent_cmd::loader::{ - load_builtin_agents, parse_frontmatter, read_file_with_encoding, - }; + use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter}; use crate::agent_cmd::types::AgentMode; + use crate::utils::file::read_file_with_encoding; #[test] fn test_read_file_with_utf8() { diff --git a/src/cortex-cli/src/lock_cmd.rs b/src/cortex-cli/src/lock_cmd.rs index dc652cad..253d06b6 100644 --- a/src/cortex-cli/src/lock_cmd.rs +++ b/src/cortex-cli/src/lock_cmd.rs @@ -116,9 +116,9 @@ fn validate_session_id(session_id: &str) -> Result<()> { /// Get the lock file path. fn get_lock_file_path() -> PathBuf { - dirs::home_dir() - .map(|h| h.join(".cortex").join("session_locks.json")) - .unwrap_or_else(|| PathBuf::from(".cortex/session_locks.json")) + cortex_engine::config::find_cortex_home() + .unwrap_or_else(|_| PathBuf::from(".cortex")) + .join("session_locks.json") } /// Load the lock file. @@ -357,6 +357,50 @@ async fn run_check(args: LockCheckArgs) -> Result<()> { #[cfg(test)] mod tests { use super::*; + use serial_test::serial; + use std::env; + use std::ffi::{OsStr, OsString}; + use tempfile::TempDir; + + struct EnvVarGuard { + key: &'static str, + original: Option, + } + + impl EnvVarGuard { + fn set(key: &'static str, value: impl AsRef) -> Self { + let original = env::var_os(key); + // SAFETY: This serial test guard mutates process environment in a + // #[serial] test and restores the original value on drop. + unsafe { + env::set_var(key, value); + } + Self { key, original } + } + + fn remove(key: &'static str) -> Self { + let original = env::var_os(key); + // SAFETY: This serial test guard mutates process environment in a + // #[serial] test and restores the original value on drop. + unsafe { + env::remove_var(key); + } + Self { key, original } + } + } + + impl Drop for EnvVarGuard { + fn drop(&mut self) { + // SAFETY: This serial test guard restores the process environment + // to the value captured before the test mutation. + unsafe { + match &self.original { + Some(value) => env::set_var(self.key, value), + None => env::remove_var(self.key), + } + } + } + } #[test] fn test_lock_entry_serialization_with_reason() { @@ -508,4 +552,15 @@ mod tests { let path_str = path.to_string_lossy(); assert!(path_str.contains(".cortex")); } + + #[test] + #[serial] + fn test_get_lock_file_path_uses_cortex_home() { + let temp_dir = TempDir::new().unwrap(); + let cortex_home = temp_dir.path().join("custom-cortex-home"); + let _config_dir = EnvVarGuard::remove("CORTEX_CONFIG_DIR"); + let _cortex_home = EnvVarGuard::set("CORTEX_HOME", &cortex_home); + + assert_eq!(get_lock_file_path(), cortex_home.join("session_locks.json")); + } }