Skip to content

Commit 19a23f9

Browse files
committed
Fix global config not being saved due to folders not being created
1 parent 8cb9e4d commit 19a23f9

File tree

1 file changed

+31
-1
lines changed
  • crates/codebook-config/src

1 file changed

+31
-1
lines changed

crates/codebook-config/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,13 @@ impl CodebookConfig {
457457
let content = toml::to_string_pretty(&*self.global_settings.read().unwrap())
458458
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
459459
info!(
460-
"Saving project configuration to {}",
460+
"Saving global configuration to {}",
461461
global_config_path.display()
462462
);
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+
}
463467
fs::write(global_config_path, content)
464468
}
465469

@@ -611,6 +615,32 @@ mod tests {
611615
Ok(config)
612616
}
613617

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+
614644
#[test]
615645
fn test_add_word() -> Result<(), io::Error> {
616646
let temp_dir = TempDir::new().unwrap();

0 commit comments

Comments
 (0)