Skip to content

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

test: Add integration tests for project hooks

Maximilian Roos77ab5f012182
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 629398147..7d1ec9971 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -147,11 +147,21 @@ fn exec_through_wrapper(
repo: &TestRepo,
subcommand: &str,
args: &[&str],
+) -> ShellOutput {
+ exec_through_wrapper_from(shell, repo, subcommand, args, repo.root_path())
+}
+
+fn exec_through_wrapper_from(
+ shell: &str,
+ repo: &TestRepo,
+ subcommand: &str,
+ args: &[&str],
+ working_dir: &std::path::Path,
) -> ShellOutput {
let script = build_shell_script(shell, repo, subcommand, args);
let mut cmd = Command::new(shell);
- cmd.arg("-c").arg(&script).current_dir(repo.root_path());
+ cmd.arg("-c").arg(&script).current_dir(working_dir);
// Configure git environment
repo.clean_cli_env(&mut cmd);
@@ -330,6 +340,232 @@ mod tests {
);
}
+ /// Test switch --create with post-create-command (blocking) and post-start-command (background)
+ #[rstest]
+ #[case("bash")]
+ #[case("fish")]
+ fn test_wrapper_switch_with_hooks(#[case] shell: &str) {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create project config with both post-create and post-start hooks
+ let config_dir = repo.root_path().join(".config");
+ fs::create_dir_all(&config_dir).expect("Failed to create .config dir");
+ fs::write(
+ config_dir.join("wt.toml"),
+ r#"# Blocking command that runs before worktree is ready
+post-create-command = [
+ "echo 'Installing dependencies...'",
+ "echo 'Building project...'"
+]
+
+# Background command that runs in parallel
+[post-start-command]
+server = "echo 'Starting dev server on port 3000'"
+watch = "echo 'Watching for file changes'"
+"#,
+ )
+ .expect("Failed to write project config");
+
+ repo.commit("Add hooks");
+
+ // Pre-approve the commands in user config
+ fs::write(
+ repo.test_config_path(),
+ r#"worktree-path = "../{main-worktree}.{branch}"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo 'Installing dependencies...'"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo 'Building project...'"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo 'Starting dev server on port 3000'"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo 'Watching for file changes'"
+"#,
+ )
+ .expect("Failed to write user config");
+
+ let output = exec_through_wrapper(shell, &repo, "switch", &["--create", "feature-hooks"]);
+
+ assert_eq!(output.exit_code, 0, "{}: Command should succeed", shell);
+ output.assert_no_directive_leaks();
+
+ assert_snapshot!(
+ format!("switch_with_hooks_{}", shell),
+ output.normalized().as_ref()
+ );
+ }
+
+ /// Test merge with successful pre-merge-command validation
+ #[rstest]
+ #[case("bash")]
+ #[case("fish")]
+ fn test_wrapper_merge_with_pre_merge_success(#[case] shell: &str) {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Create project config with pre-merge validation
+ let config_dir = repo.root_path().join(".config");
+ fs::create_dir_all(&config_dir).expect("Failed to create .config dir");
+ fs::write(
+ config_dir.join("wt.toml"),
+ r#"[pre-merge-command]
+format = "echo '✓ Code formatting check passed'"
+lint = "echo '✓ Linting passed - no warnings'"
+test = "echo '✓ All 47 tests passed in 2.3s'"
+"#,
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add pre-merge validation");
+
+ // Create a main worktree
+ let main_wt = repo.root_path().parent().unwrap().join("test-repo.main-wt");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add main worktree");
+
+ // Create feature worktree with a commit
+ let feature_wt = repo.add_worktree("feature", "feature");
+ fs::write(feature_wt.join("feature.txt"), "feature content").expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "feature.txt"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to add file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Add feature"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Pre-approve commands
+ fs::write(
+ repo.test_config_path(),
+ r#"worktree-path = "../{main-worktree}.{branch}"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo '✓ Code formatting check passed'"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo '✓ Linting passed - no warnings'"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo '✓ All 47 tests passed in 2.3s'"
+"#,
+ )
+ .expect("Failed to write user config");
+
+ // Run merge from the feature worktree
+ let output =
+ exec_through_wrapper_from(shell, &repo, "merge", &["main", "--force"], &feature_wt);
+
+ assert_eq!(output.exit_code, 0, "{}: Merge should succeed", shell);
+ output.assert_no_directive_leaks();
+
+ assert_snapshot!(
+ format!("merge_with_pre_merge_success_{}", shell),
+ output.normalized().as_ref()
+ );
+ }
+
+ /// Test merge with failing pre-merge-command that aborts the merge
+ #[rstest]
+ #[case("bash")]
+ #[case("fish")]
+ fn test_wrapper_merge_with_pre_merge_failure(#[case] shell: &str) {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Create project config with failing pre-merge validation
+ let config_dir = repo.root_path().join(".config");
+ fs::create_dir_all(&config_dir).expect("Failed to create .config dir");
+ fs::write(
+ config_dir.join("wt.toml"),
+ r#"[pre-merge-command]
+format = "echo '✓ Code formatting check passed'"
+test = "echo '✗ Test suite failed: 3 tests failing' && exit 1"
+"#,
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add failing pre-merge validation");
+
+ // Create a main worktree
+ let main_wt = repo.root_path().parent().unwrap().join("test-repo.main-wt");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add main worktree");
+
+ // Create feature worktree with a commit
+ let feature_wt = repo.add_worktree("feature-fail", "feature-fail");
+ fs::write(feature_wt.join("feature.txt"), "feature content").expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "feature.txt"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to add file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Add feature"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Pre-approve the commands
+ fs::write(
+ repo.test_config_path(),
+ r#"worktree-path = "../{main-worktree}.{branch}"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo '✓ Code formatting check passed'"
+
+[[approved-commands]]
+project = "test-repo"
+command = "echo '✗ Test suite failed: 3 tests failing' && exit 1"
+"#,
+ )
+ .expect("Failed to write user config");
+
+ // Run merge from the feature worktree
+ let output =
+ exec_through_wrapper_from(shell, &repo, "merge", &["main", "--force"], &feature_wt);
+
+ output.assert_no_directive_leaks();
+
+ assert_snapshot!(
+ format!("merge_with_pre_merge_failure_{}", shell),
+ output.normalized().as_ref()
+ );
+ }
+
// ========================================================================
// Bash Shell Wrapper Tests
// ========================================================================
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_bash.snap
index e6cb439b3..a40888224 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_bash.snap
@@ -3,4 +3,4 @@ source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
🔄 Validating merge...
-Already on main, nothing to merge
+✅ Already on main, nothing to merge
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_fish.snap
index e6cb439b3..a40888224 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_fish.snap
@@ -3,4 +3,4 @@ source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
🔄 Validating merge...
-Already on main, nothing to merge
+✅ Already on main, nothing to merge
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
new file mode 100644
index 000000000..96d8b4f3b
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
@@ -0,0 +1,19 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+🔄 Validating merge...
+🔄 Running pre-merge command format:
+  echo '✓ Code formatting check passed'
+
+
+🔄 Running pre-merge command test:
+  echo '✗ Test suite failed: 3 tests failing' && exit 1
+
+✓ Code formatting check passed
+✗ Test suite failed: 3 tests failing
+❌ Pre-merge command failed: test
+
+Command failed with exit code: exit status: 1
+
+💡 Use --no-hooks to skip pre-merge commands
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
new file mode 100644
index 000000000..8e873bebb
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
@@ -0,0 +1,18 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+🔄 Validating merge...
+🔄 Running pre-merge command format:
+  echo '✓ Code formatting check passed'
+
+🔄 Running pre-merge command test:
+  echo '✗ Test suite failed: 3 tests failing' && exit 1
+
+✓ Code formatting check passed
+✗ Test suite failed: 3 tests failing
+❌ Pre-merge command failed: test
+
+Command failed with exit code: exit status: 1
+
+💡 Use --no-hooks to skip pre-merge commands
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_bash.snap
new file mode 100644
index 000000000..f985b23ec
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_bash.snap
@@ -0,0 +1,32 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+🔄 Validating merge...
+🔄 Running pre-merge command format:
+  echo '✓ Code formatting check passed'
+
+
+🔄 Running pre-merge command lint:
+  echo '✓ Linting passed - no warnings'
+
+
+🔄 Running pre-merge command test:
+  echo '✓ All 47 tests passed in 2.3s'
+
+
+🔄 Rebasing onto main...
+🔄 Pushing 1 commit to main @ d067f01
+
+* d067f01 (HEAD -> feature) Add feature
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+✅ Pushed to main (1 commit, 1 file, +1) 
+🔄 Cleaning up worktree...
+
+✅ Returned to primary at [TMPDIR]/test-repo
+✓ Code formatting check passed
+✓ Linting passed - no warnings
+✓ All 47 tests passed in 2.3s
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_fish.snap
new file mode 100644
index 000000000..16c6bf991
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_fish.snap
@@ -0,0 +1,28 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+🔄 Validating merge...
+🔄 Running pre-merge command format:
+  echo '✓ Code formatting check passed'
+
+🔄 Running pre-merge command lint:
+  echo '✓ Linting passed - no warnings'
+
+🔄 Running pre-merge command test:
+  echo '✓ All 47 tests passed in 2.3s'
+
+🔄 Rebasing onto main...
+🔄 Pushing 1 commit to main @ d067f01
+
+* d067f01 (HEAD -> feature) Add feature
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+✅ Pushed to main (1 commit, 1 file, +1) 
+🔄 Cleaning up worktree...
+✅ Returned to primary at [TMPDIR]/test-repo
+✓ Code formatting check passed
+✓ Linting passed - no warnings
+✓ All 47 tests passed in 2.3s
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_bash.snap
index 7fb4e8d21..8b824a4bb 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_bash.snap
@@ -2,4 +2,5 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-Removed worktree for to-remove
+🔄 Removing worktree for to-remove...
+✅ Removed worktree for to-remove
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_fish.snap
index 7fb4e8d21..8b824a4bb 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_fish.snap
@@ -2,4 +2,5 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-Removed worktree for to-remove
+🔄 Removing worktree for to-remove...
+✅ Removed worktree for to-remove
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_bash.snap
index 41bb642cf..6d6577aca 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_bash.snap
@@ -2,4 +2,4 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-Created new worktree for feature at [TMPDIR]/test-repo.feature
+✅ Created new worktree for feature at [TMPDIR]/test-repo.feature
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_fish.snap
index 41bb642cf..6d6577aca 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_fish.snap
@@ -2,4 +2,4 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-Created new worktree for feature at [TMPDIR]/test-repo.feature
+✅ Created new worktree for feature at [TMPDIR]/test-repo.feature
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_bash.snap
index f3cf2b023..9bb4c47c2 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_bash.snap
@@ -2,5 +2,5 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-Created new worktree for test-exec at [TMPDIR]/test-repo.test-exec
+✅ Created new worktree for test-exec at [TMPDIR]/test-repo.test-exec
executed
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_fish.snap
index f3cf2b023..9bb4c47c2 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_fish.snap
@@ -2,5 +2,5 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-Created new worktree for test-exec at [TMPDIR]/test-repo.test-exec
+✅ Created new worktree for test-exec at [TMPDIR]/test-repo.test-exec
executed
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_bash.snap
new file mode 100644
index 000000000..1ede37f78
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_bash.snap
@@ -0,0 +1,21 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+🔄 Executing (post-create):
+  echo 'Installing dependencies...'
+
+
+🔄 Executing (post-create):
+  echo 'Building project...'
+
+
+🔄 Starting (background):
+  echo 'Starting dev server on port 3000'
+
+🔄 Starting (background):
+  echo 'Watching for file changes'
+
+✅ Created new worktree for feature-hooks at [TMPDIR]/test-repo.feature-hooks
+Installing dependencies...
+Building project...
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_fish.snap
new file mode 100644
index 000000000..a83a945fa
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_fish.snap
@@ -0,0 +1,19 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+🔄 Executing (post-create):
+  echo 'Installing dependencies...'
+
+🔄 Executing (post-create):
+  echo 'Building project...'
+
+🔄 Starting (background):
+  echo 'Starting dev server on port 3000'
+
+🔄 Starting (background):
+  echo 'Watching for file changes'
+
+✅ Created new worktree for feature-hooks at [TMPDIR]/test-repo.feature-hooks
+Installing dependencies...
+Building project...