@@ -457,9 +457,13 @@ impl CodebookConfig {
457
457
let content = toml:: to_string_pretty ( & * self . global_settings . read ( ) . unwrap ( ) )
458
458
. map_err ( |e| io:: Error :: new ( io:: ErrorKind :: Other , e) ) ?;
459
459
info ! (
460
- "Saving project configuration to {}" ,
460
+ "Saving global configuration to {}" ,
461
461
global_config_path. display( )
462
462
) ;
463
+ // Create parent directories if they don't exist
464
+ if let Some ( parent) = global_config_path. parent ( ) {
465
+ fs:: create_dir_all ( parent) ?;
466
+ }
463
467
fs:: write ( global_config_path, content)
464
468
}
465
469
@@ -611,6 +615,32 @@ mod tests {
611
615
Ok ( config)
612
616
}
613
617
618
+ #[ test]
619
+ fn test_save_global_creates_directories ( ) -> Result < ( ) , io:: Error > {
620
+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
621
+ let global_dir = temp_dir. path ( ) . join ( "deep" ) . join ( "nested" ) . join ( "dir" ) ;
622
+ let config_path = global_dir. join ( "codebook.toml" ) ;
623
+
624
+ // Create config with a path that doesn't exist yet
625
+ let config = CodebookConfig {
626
+ global_config_path : Some ( config_path. clone ( ) ) ,
627
+ global_settings : RwLock :: new ( Some ( ConfigSettings :: default ( ) ) ) ,
628
+ ..Default :: default ( )
629
+ } ;
630
+
631
+ // Directory doesn't exist yet
632
+ assert ! ( !global_dir. exists( ) ) ;
633
+
634
+ // Save should create directories
635
+ config. save_global ( ) ?;
636
+
637
+ // Now directory and file should exist
638
+ assert ! ( global_dir. exists( ) ) ;
639
+ assert ! ( config_path. exists( ) ) ;
640
+
641
+ Ok ( ( ) )
642
+ }
643
+
614
644
#[ test]
615
645
fn test_add_word ( ) -> Result < ( ) , io:: Error > {
616
646
let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
0 commit comments