Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(hooks): handle mixed formats in serialization + simplify (#848)
Maximilian Roos32c724fc777d
diff --git a/src/config/commands.rs b/src/config/commands.rsindex 4958c7150..24c877ce3 100644--- a/src/config/commands.rs+++ b/src/config/commands.rs@@ -140,8 +140,9 @@ impl Serialize for CommandConfig {return self.commands[0].template.serialize(serializer);}- // Serialize as named map. Auto-generate names for unnamed commands- // (can happen when merging unnamed global hooks with named project hooks).+ // Serialize as named map. Generate keys for unnamed commands (can happen+ // when merging unnamed global hooks with named project hooks, though+ // merged configs are only used for execution, never serialized in production).let mut map = serializer.serialize_map(Some(self.commands.len()))?;let mut unnamed_counter = 0u32;for cmd in &self.commands {@@ -149,7 +150,7 @@ impl Serialize for CommandConfig {Some(name) => name.clone(),None => {unnamed_counter += 1;- format!("_{}", unnamed_counter)+ format!("_{unnamed_counter}")}};map.serialize_entry(&key, &cmd.template)?;@@ -441,39 +442,40 @@ third = "echo 3"assert_eq!(merged.commands[0].template, "echo base");}+ // ============================================================================+ // Serialization Edge Cases (merged configs)+ // ============================================================================+ //+ // Note: Merged configs (from merge_append) are only used for execution,+ // never serialized in production. These tests verify serialization doesn't+ // panic, but round-trip fidelity isn't required.++ /// Serializing a merged config with mixed named/unnamed commands doesn't panic.#[test]- fn test_serialize_mixed_named_unnamed_commands() {- // When merging unnamed global hooks with named project hooks,- // the result has both named and unnamed commands. Serialization- // must handle this without panicking.+ fn test_serialize_mixed_named_unnamed_succeeds() {#[derive(Serialize)]struct Wrapper {cmd: CommandConfig,}- // Simulate merge of unnamed global + named project+ // Simulate merge of unnamed global + named project hookslet global = CommandConfig {- commands: vec![Command::new(None, "echo global".to_string())],+ commands: vec![Command::new(None, "npm install".to_string())],};- let project = CommandConfig {+ let per_project = CommandConfig {commands: vec![Command::new(- Some("build".to_string()),- "cargo build".to_string(),+ Some("setup".to_string()),+ "echo setup".to_string(),)],};- let merged = global.merge_append(&project);- // This should NOT panic- let wrapper = Wrapper { cmd: merged };- let result = toml::to_string(&wrapper);+ let merged = global.merge_append(&per_project);+ assert_eq!(merged.commands.len(), 2);- // Should succeed and contain both commands- assert!(result.is_ok(), "Serialization panicked or failed");- let serialized = result.unwrap();- assert!(serialized.contains("echo global"), "Missing global command");- assert!(- serialized.contains("cargo build"),- "Missing project command"- );+ // Should not panic - generates "_1" for unnamed command+ let wrapper = Wrapper { cmd: merged };+ let serialized = toml::to_string(&wrapper).unwrap();+ assert!(serialized.contains("npm install"), "{serialized}");+ assert!(serialized.contains("echo setup"), "{serialized}");}}diff --git a/src/config/user.rs b/src/config/user.rsindex d1a4994c4..715abfb31 100644--- a/src/config/user.rs+++ b/src/config/user.rs@@ -2905,4 +2905,98 @@ squash-template-file = "path/to/file"result.err());}++ // =========================================================================+ // Hooks Merge Behavior Tests+ // =========================================================================+ //+ // Note: Merged configs are only used for execution, never serialized in+ // production. These tests verify merge semantics for execution order.++ /// Merging string-format global hooks with table-format per-project hooks+ /// preserves both and maintains correct execution order.+ #[test]+ fn test_hooks_merge_mixed_formats_preserves_order() {+ // Global uses string format (unnamed command)+ let global_hooks = parse_hooks(r#"post-start = "npm install""#);++ // Per-project uses table format (named commands)+ let project_hooks = parse_hooks(+ r#"+[post-start]+setup = "echo setup"+"#,+ );++ let mut config = UserConfig {+ configs: OverridableConfig {+ hooks: global_hooks,+ ..Default::default()+ },+ ..Default::default()+ };++ config.projects.insert(+ "github.com/user/repo".to_string(),+ UserProjectOverrides {+ overrides: OverridableConfig {+ hooks: project_hooks,+ ..Default::default()+ },+ ..Default::default()+ },+ );++ // Verify merge preserves order: global first, then project+ let effective = config.hooks(Some("github.com/user/repo"));+ let commands = effective.post_start.as_ref().unwrap().commands();+ assert_eq!(commands.len(), 2);+ assert_eq!(commands[0].template, "npm install"); // Global first+ assert_eq!(commands[1].template, "echo setup"); // Project second+ }++ /// When global and per-project both define same hook type, both run in order.+ #[test]+ fn test_hooks_merge_same_names_both_run() {+ // Both define "test" command - both should execute+ let global_hooks = parse_hooks(+ r#"+[post-start]+test = "cargo test"+"#,+ );++ let project_hooks = parse_hooks(+ r#"+[post-start]+test = "npm test"+"#,+ );++ let mut config = UserConfig {+ configs: OverridableConfig {+ hooks: global_hooks,+ ..Default::default()+ },+ ..Default::default()+ };++ config.projects.insert(+ "github.com/user/repo".to_string(),+ UserProjectOverrides {+ overrides: OverridableConfig {+ hooks: project_hooks,+ ..Default::default()+ },+ ..Default::default()+ },+ );++ // Both commands present, global first+ let effective = config.hooks(Some("github.com/user/repo"));+ let commands = effective.post_start.as_ref().unwrap().commands();+ assert_eq!(commands.len(), 2);+ assert_eq!(commands[0].template, "cargo test");+ assert_eq!(commands[1].template, "npm test");+ }}