11//! Tests for DAG command functionality.
22
3- use super :: helpers:: convert_specs;
3+ use super :: helpers:: { convert_specs, get_dag_store_path } ;
44use super :: types:: { DagSpecInput , TaskSpecInput } ;
55use cortex_agents:: task:: { DagHydrator , Task , TaskId , TaskSpec } ;
6+ use serial_test:: serial;
67use std:: collections:: HashMap ;
8+ use std:: env;
9+ use std:: ffi:: { OsStr , OsString } ;
10+ use tempfile:: TempDir ;
711
812use super :: executor:: TaskExecutor ;
913
14+ struct EnvVarGuard {
15+ key : & ' static str ,
16+ original : Option < OsString > ,
17+ }
18+
19+ impl EnvVarGuard {
20+ fn set ( key : & ' static str , value : impl AsRef < OsStr > ) -> Self {
21+ let original = env:: var_os ( key) ;
22+ // SAFETY: These tests are serialized and restore the environment on drop.
23+ unsafe {
24+ env:: set_var ( key, value) ;
25+ }
26+ Self { key, original }
27+ }
28+
29+ fn remove ( key : & ' static str ) -> Self {
30+ let original = env:: var_os ( key) ;
31+ // SAFETY: These tests are serialized and restore the environment on drop.
32+ unsafe {
33+ env:: remove_var ( key) ;
34+ }
35+ Self { key, original }
36+ }
37+ }
38+
39+ impl Drop for EnvVarGuard {
40+ fn drop ( & mut self ) {
41+ // SAFETY: These tests are serialized and restore the environment before returning.
42+ unsafe {
43+ match & self . original {
44+ Some ( value) => env:: set_var ( self . key , value) ,
45+ None => env:: remove_var ( self . key ) ,
46+ }
47+ }
48+ }
49+ }
50+
1051#[ test]
1152fn test_load_yaml_spec ( ) {
1253 let yaml = r#"
@@ -31,6 +72,33 @@ tasks:
3172 assert_eq ! ( spec. tasks[ 1 ] . depends_on, vec![ "setup" ] ) ;
3273}
3374
75+ #[ test]
76+ #[ serial]
77+ fn dag_store_path_uses_cortex_home ( ) {
78+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
79+ let cortex_home = temp_dir. path ( ) . join ( "custom-cortex-home" ) ;
80+ let _config_dir = EnvVarGuard :: remove ( "CORTEX_CONFIG_DIR" ) ;
81+ let _cortex_home = EnvVarGuard :: set ( "CORTEX_HOME" , & cortex_home) ;
82+
83+ let path = get_dag_store_path ( ) . unwrap ( ) ;
84+
85+ assert_eq ! ( path, cortex_home. join( "dags" ) ) ;
86+ }
87+
88+ #[ test]
89+ #[ serial]
90+ fn dag_store_path_prefers_cortex_config_dir ( ) {
91+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
92+ let cortex_home = temp_dir. path ( ) . join ( "cortex-home" ) ;
93+ let cortex_config_dir = temp_dir. path ( ) . join ( "cortex-config-dir" ) ;
94+ let _cortex_home = EnvVarGuard :: set ( "CORTEX_HOME" , & cortex_home) ;
95+ let _config_dir = EnvVarGuard :: set ( "CORTEX_CONFIG_DIR" , & cortex_config_dir) ;
96+
97+ let path = get_dag_store_path ( ) . unwrap ( ) ;
98+
99+ assert_eq ! ( path, cortex_config_dir. join( "dags" ) ) ;
100+ }
101+
34102#[ test]
35103fn test_convert_specs ( ) {
36104 let input = DagSpecInput {
0 commit comments