From 4db05f702bd660a4d24cb50caf73ea90daa49fd3 Mon Sep 17 00:00:00 2001 From: Bortlesboat Date: Tue, 12 May 2026 02:18:44 -0400 Subject: [PATCH] fix: honor cortex config dir for whoami --- src/cortex-cli/src/agent_cmd/tests.rs | 5 +-- src/cortex-cli/src/cli/handlers.rs | 62 +++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/cortex-cli/src/agent_cmd/tests.rs b/src/cortex-cli/src/agent_cmd/tests.rs index e2ff07f9f..18f7ba753 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/cli/handlers.rs b/src/cortex-cli/src/cli/handlers.rs index e92fa8632..d289e0ee4 100644 --- a/src/cortex-cli/src/cli/handlers.rs +++ b/src/cortex-cli/src/cli/handlers.rs @@ -7,6 +7,7 @@ use anyhow::{Result, bail}; use clap::CommandFactory; use clap_complete::{Shell, generate}; use std::io::{self, BufRead, Write}; +use std::path::PathBuf; use super::args::*; use crate::login::{ @@ -532,9 +533,7 @@ fn install_completions(shell: Shell) -> Result<()> { pub async fn run_whoami() -> Result<()> { use cortex_login::{AuthMode, load_auth_with_fallback, safe_format_key}; - let cortex_home = dirs::home_dir() - .map(|h| h.join(".cortex")) - .unwrap_or_else(|| std::path::PathBuf::from(".cortex")); + let cortex_home = get_whoami_cortex_home(); // Check environment variables first if let Ok(token) = std::env::var("CORTEX_AUTH_TOKEN") @@ -589,6 +588,10 @@ pub async fn run_whoami() -> Result<()> { Ok(()) } +fn get_whoami_cortex_home() -> PathBuf { + cortex_engine::config::find_cortex_home().unwrap_or_else(|_| PathBuf::from(".cortex")) +} + /// Resume a previous session. pub async fn run_resume(resume_cli: ResumeCommand) -> Result<()> { use crate::utils::resolve_session_id; @@ -996,8 +999,50 @@ pub async fn run_history(history_cli: HistoryCommand) -> Result<()> { #[cfg(test)] mod tests { + use super::get_whoami_cortex_home; use clap_complete::Shell; + use serial_test::serial; + use std::env; + use std::ffi::{OsStr, OsString}; use std::io::{self, ErrorKind, Write}; + 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); + unsafe { + // SAFETY: Tests using this guard are serialized with `#[serial]`. + env::set_var(key, value); + } + Self { key, original } + } + + fn remove(key: &'static str) -> Self { + let original = env::var_os(key); + unsafe { + // SAFETY: Tests using this guard are serialized with `#[serial]`. + env::remove_var(key); + } + Self { key, original } + } + } + + impl Drop for EnvVarGuard { + fn drop(&mut self) { + unsafe { + // SAFETY: Tests using this guard are serialized with `#[serial]`. + match &self.original { + Some(value) => env::set_var(self.key, value), + None => env::remove_var(self.key), + } + } + } + } // ========================================================================= // Shell name parsing tests (unit test the parsing logic directly) @@ -1019,6 +1064,17 @@ mod tests { } } + #[test] + #[serial] + fn test_whoami_cortex_home_uses_cortex_config_dir() { + let temp_dir = TempDir::new().unwrap(); + let cortex_config_dir = temp_dir.path().join("custom-config-dir"); + let _cortex_home = EnvVarGuard::remove("CORTEX_HOME"); + let _config_dir = EnvVarGuard::set("CORTEX_CONFIG_DIR", &cortex_config_dir); + + assert_eq!(get_whoami_cortex_home(), cortex_config_dir); + } + /// Helper to extract shell name from path (like the real function does) fn extract_shell_name_from_path(path: &str) -> &str { std::path::Path::new(path)