@@ -7,6 +7,7 @@ use clap::Parser;
77use serde:: { Deserialize , Serialize } ;
88use std:: path:: PathBuf ;
99
10+ use cortex_engine:: config:: find_cortex_home;
1011use cortex_engine:: list_sessions;
1112use cortex_engine:: rollout:: get_rollout_path;
1213use cortex_engine:: rollout:: reader:: { RolloutItem , get_session_meta, read_rollout} ;
@@ -111,9 +112,7 @@ pub struct ExportToolCall {
111112impl ExportCommand {
112113 /// Run the export command.
113114 pub async fn run ( self ) -> Result < ( ) > {
114- let cortex_home = dirs:: home_dir ( )
115- . map ( |h| h. join ( ".cortex" ) )
116- . ok_or_else ( || anyhow:: anyhow!( "Could not determine home directory" ) ) ?;
115+ let cortex_home = get_cortex_home ( ) ?;
117116
118117 // Get session ID - either from arg or interactive picker
119118 let session_id = match self . session_id {
@@ -251,6 +250,11 @@ fn escape_csv_field(field: &str) -> String {
251250 }
252251}
253252
253+ /// Get the cortex home directory.
254+ fn get_cortex_home ( ) -> Result < PathBuf > {
255+ find_cortex_home ( ) . context ( "Could not determine Cortex home directory" )
256+ }
257+
254258/// Select a session interactively.
255259async fn select_session ( cortex_home : & PathBuf ) -> Result < String > {
256260 let sessions = list_sessions ( cortex_home) ?;
@@ -379,6 +383,58 @@ fn extract_agent_refs(messages: &[ExportMessage]) -> Vec<String> {
379383#[ cfg( test) ]
380384mod tests {
381385 use super :: * ;
386+ use serial_test:: serial;
387+ use std:: env;
388+ use std:: ffi:: { OsStr , OsString } ;
389+ use tempfile:: TempDir ;
390+
391+ struct EnvVarGuard {
392+ key : & ' static str ,
393+ original : Option < OsString > ,
394+ }
395+
396+ impl EnvVarGuard {
397+ fn set ( key : & ' static str , value : impl AsRef < OsStr > ) -> Self {
398+ let original = env:: var_os ( key) ;
399+ // SAFETY: These tests are serialized and restore the environment on drop.
400+ unsafe {
401+ env:: set_var ( key, value) ;
402+ }
403+ Self { key, original }
404+ }
405+
406+ fn remove ( key : & ' static str ) -> Self {
407+ let original = env:: var_os ( key) ;
408+ // SAFETY: These tests are serialized and restore the environment on drop.
409+ unsafe {
410+ env:: remove_var ( key) ;
411+ }
412+ Self { key, original }
413+ }
414+ }
415+
416+ impl Drop for EnvVarGuard {
417+ fn drop ( & mut self ) {
418+ // SAFETY: These tests are serialized and restore the environment before returning.
419+ unsafe {
420+ match & self . original {
421+ Some ( value) => env:: set_var ( self . key , value) ,
422+ None => env:: remove_var ( self . key ) ,
423+ }
424+ }
425+ }
426+ }
427+
428+ #[ test]
429+ #[ serial]
430+ fn test_get_cortex_home_uses_cortex_home_env ( ) {
431+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
432+ let cortex_home = temp_dir. path ( ) . join ( "custom-cortex-home" ) ;
433+ let _config_dir = EnvVarGuard :: remove ( "CORTEX_CONFIG_DIR" ) ;
434+ let _cortex_home = EnvVarGuard :: set ( "CORTEX_HOME" , & cortex_home) ;
435+
436+ assert_eq ! ( get_cortex_home( ) . unwrap( ) , cortex_home) ;
437+ }
382438
383439 #[ test]
384440 fn test_session_export_serialization ( ) {
0 commit comments