Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Add non-snapshot test for {{ branch }} expansion in detached HEAD (#112)

Maximilian Roosdd1e6c277939
diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rs
index 62b915d1e..79b09b14b 100644
--- a/tests/integration_tests/remove.rs
+++ b/tests/integration_tests/remove.rs
@@ -1115,3 +1115,61 @@ approved-commands = ["touch {marker_path}"]
"Pre-remove hook should run for detached HEAD worktrees in background mode"
);
}
+
+/// Test that {{ branch }} template variable expands to empty string for detached HEAD.
+///
+/// This is a non-snapshot test to avoid cross-platform line-wrapping differences
+/// (macOS temp paths are ~60 chars vs Linux ~20 chars). The snapshot version
+/// of this test (`test_pre_remove_hook_runs_for_detached_head`) verifies the hook runs;
+/// this test verifies the specific template expansion behavior.
+#[test]
+fn test_pre_remove_hook_branch_expansion_detached_head() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create a file where the hook will write the branch template expansion
+ let branch_file = repo.root_path().join("branch-expansion.txt");
+ let branch_path = branch_file.to_string_lossy().replace('\\', "/");
+
+ // Create project config with hook that writes {{ branch }} to file
+ repo.write_project_config(&format!(
+ r#"pre-remove = "echo 'branch={{{{ branch }}}}' > {branch_path}""#,
+ ));
+ repo.commit("Add config");
+
+ // Pre-approve the command
+ repo.write_test_config(&format!(
+ r#"worktree-path = "../{{{{ main_worktree }}}}.{{{{ branch }}}}"
+
+[projects."repo"]
+approved-commands = ["echo 'branch={{{{ branch }}}}' > {branch_path}"]
+"#,
+ ));
+
+ // Create a worktree and detach HEAD
+ let worktree_path = repo.add_worktree("feature-branch-test");
+ repo.detach_head_in_worktree("feature-branch-test");
+
+ // Run wt remove (not a snapshot test - just verify behavior)
+ let output = wt_command()
+ .args(["remove", "--no-background"])
+ .current_dir(&worktree_path)
+ .env("WORKTRUNK_CONFIG_PATH", repo.test_config_path())
+ .output()
+ .expect("Failed to execute wt remove");
+
+ assert!(
+ output.status.success(),
+ "wt remove should succeed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ // Verify {{ branch }} expanded to empty string
+ let content =
+ std::fs::read_to_string(&branch_file).expect("Hook should have created the branch file");
+ assert_eq!(
+ content.trim(),
+ "branch=",
+ "{{ branch }} should expand to empty string for detached HEAD worktrees"
+ );
+}