@@ -7,6 +7,7 @@ use anyhow::{Result, bail};
77use clap:: CommandFactory ;
88use clap_complete:: { Shell , generate} ;
99use std:: io:: { self , BufRead , Write } ;
10+ use std:: path:: PathBuf ;
1011
1112use super :: args:: * ;
1213use crate :: login:: {
@@ -532,9 +533,7 @@ fn install_completions(shell: Shell) -> Result<()> {
532533pub async fn run_whoami ( ) -> Result < ( ) > {
533534 use cortex_login:: { AuthMode , load_auth_with_fallback, safe_format_key} ;
534535
535- let cortex_home = dirs:: home_dir ( )
536- . map ( |h| h. join ( ".cortex" ) )
537- . unwrap_or_else ( || std:: path:: PathBuf :: from ( ".cortex" ) ) ;
536+ let cortex_home = get_whoami_cortex_home ( ) ;
538537
539538 // Check environment variables first
540539 if let Ok ( token) = std:: env:: var ( "CORTEX_AUTH_TOKEN" )
@@ -589,6 +588,10 @@ pub async fn run_whoami() -> Result<()> {
589588 Ok ( ( ) )
590589}
591590
591+ fn get_whoami_cortex_home ( ) -> PathBuf {
592+ cortex_engine:: config:: find_cortex_home ( ) . unwrap_or_else ( |_| PathBuf :: from ( ".cortex" ) )
593+ }
594+
592595/// Resume a previous session.
593596pub async fn run_resume ( resume_cli : ResumeCommand ) -> Result < ( ) > {
594597 use crate :: utils:: resolve_session_id;
@@ -996,8 +999,50 @@ pub async fn run_history(history_cli: HistoryCommand) -> Result<()> {
996999
9971000#[ cfg( test) ]
9981001mod tests {
1002+ use super :: get_whoami_cortex_home;
9991003 use clap_complete:: Shell ;
1004+ use serial_test:: serial;
1005+ use std:: env;
1006+ use std:: ffi:: { OsStr , OsString } ;
10001007 use std:: io:: { self , ErrorKind , Write } ;
1008+ use tempfile:: TempDir ;
1009+
1010+ struct EnvVarGuard {
1011+ key : & ' static str ,
1012+ original : Option < OsString > ,
1013+ }
1014+
1015+ impl EnvVarGuard {
1016+ fn set ( key : & ' static str , value : impl AsRef < OsStr > ) -> Self {
1017+ let original = env:: var_os ( key) ;
1018+ unsafe {
1019+ // SAFETY: Tests using this guard are serialized with `#[serial]`.
1020+ env:: set_var ( key, value) ;
1021+ }
1022+ Self { key, original }
1023+ }
1024+
1025+ fn remove ( key : & ' static str ) -> Self {
1026+ let original = env:: var_os ( key) ;
1027+ unsafe {
1028+ // SAFETY: Tests using this guard are serialized with `#[serial]`.
1029+ env:: remove_var ( key) ;
1030+ }
1031+ Self { key, original }
1032+ }
1033+ }
1034+
1035+ impl Drop for EnvVarGuard {
1036+ fn drop ( & mut self ) {
1037+ unsafe {
1038+ // SAFETY: Tests using this guard are serialized with `#[serial]`.
1039+ match & self . original {
1040+ Some ( value) => env:: set_var ( self . key , value) ,
1041+ None => env:: remove_var ( self . key ) ,
1042+ }
1043+ }
1044+ }
1045+ }
10011046
10021047 // =========================================================================
10031048 // Shell name parsing tests (unit test the parsing logic directly)
@@ -1019,6 +1064,17 @@ mod tests {
10191064 }
10201065 }
10211066
1067+ #[ test]
1068+ #[ serial]
1069+ fn test_whoami_cortex_home_uses_cortex_config_dir ( ) {
1070+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
1071+ let cortex_config_dir = temp_dir. path ( ) . join ( "custom-config-dir" ) ;
1072+ let _cortex_home = EnvVarGuard :: remove ( "CORTEX_HOME" ) ;
1073+ let _config_dir = EnvVarGuard :: set ( "CORTEX_CONFIG_DIR" , & cortex_config_dir) ;
1074+
1075+ assert_eq ! ( get_whoami_cortex_home( ) , cortex_config_dir) ;
1076+ }
1077+
10221078 /// Helper to extract shell name from path (like the real function does)
10231079 fn extract_shell_name_from_path ( path : & str ) -> & str {
10241080 std:: path:: Path :: new ( path)
0 commit comments