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.rsindex 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 environmentrepo.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.snapindex 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.rsexpression: output.normalized().as_ref()---🔄 [36mValidating merge...[0m-Already on [1mmain[0m, nothing to merge+✅ [32mAlready on [1m[32mmain[0m, nothing to merge[0mdiff --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.snapindex 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.rsexpression: output.normalized().as_ref()---🔄 [36mValidating merge...[0m-Already on [1mmain[0m, nothing to merge+✅ [32mAlready on [1m[32mmain[0m, nothing to merge[0mdiff --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.snapnew file mode 100644index 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()+---+🔄 [36mValidating merge...[0m+🔄 [36mRunning pre-merge command [1m[36mformat[0m:[0m+[40m [0m echo '✓ Code formatting check passed'+++🔄 [36mRunning pre-merge command [1m[36mtest[0m:[0m+[40m [0m echo '✗ Test suite failed: 3 tests failing' && exit 1++✓ Code formatting check passed+✗ Test suite failed: 3 tests failing+❌ [31mPre-merge command failed: [1m[31mtest[0m[0m++Command failed with exit code: exit status: 1++💡 [2mUse --no-hooks to skip pre-merge commands[0mdiff --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.snapnew file mode 100644index 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()+---+🔄 [36mValidating merge...[0m+🔄 [36mRunning pre-merge command [1m[36mformat[0m:[0m+[40m [0m echo '✓ Code formatting check passed'++🔄 [36mRunning pre-merge command [1m[36mtest[0m:[0m+[40m [0m echo '✗ Test suite failed: 3 tests failing' && exit 1++✓ Code formatting check passed+✗ Test suite failed: 3 tests failing+❌ [31mPre-merge command failed: [1m[31mtest[0m[0m++Command failed with exit code: exit status: 1++💡 [2mUse --no-hooks to skip pre-merge commands[0mdiff --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.snapnew file mode 100644index 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()+---+🔄 [36mValidating merge...[0m+🔄 [36mRunning pre-merge command [1m[36mformat[0m:[0m+[40m [0m echo '✓ Code formatting check passed'+++🔄 [36mRunning pre-merge command [1m[36mlint[0m:[0m+[40m [0m echo '✓ Linting passed - no warnings'+++🔄 [36mRunning pre-merge command [1m[36mtest[0m:[0m+[40m [0m echo '✓ All 47 tests passed in 2.3s'+++🔄 [36mRebasing onto [1m[36mmain[0m...[0m+🔄 [36mPushing 1 commit to [1m[36mmain[0m @ d067f01[0m++* [33md067f01[m[33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Add feature++ feature.txt | 1 [32m+[m+ 1 file changed, 1 insertion(+)++✅ [32mPushed to [1m[32mmain[0m (1 commit, 1 file, [32m+1[0m) [0m+🔄 [36mCleaning up worktree...[0m++✅ [32mReturned to primary at [TMPDIR]/test-repo[0m+✓ Code formatting check passed+✓ Linting passed - no warnings+✓ All 47 tests passed in 2.3sdiff --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.snapnew file mode 100644index 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()+---+🔄 [36mValidating merge...[0m+🔄 [36mRunning pre-merge command [1m[36mformat[0m:[0m+[40m [0m echo '✓ Code formatting check passed'++🔄 [36mRunning pre-merge command [1m[36mlint[0m:[0m+[40m [0m echo '✓ Linting passed - no warnings'++🔄 [36mRunning pre-merge command [1m[36mtest[0m:[0m+[40m [0m echo '✓ All 47 tests passed in 2.3s'++🔄 [36mRebasing onto [1m[36mmain[0m...[0m+🔄 [36mPushing 1 commit to [1m[36mmain[0m @ d067f01[0m++* [33md067f01[m[33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Add feature++ feature.txt | 1 [32m+[m+ 1 file changed, 1 insertion(+)++✅ [32mPushed to [1m[32mmain[0m (1 commit, 1 file, [32m+1[0m) [0m+🔄 [36mCleaning up worktree...[0m+✅ [32mReturned to primary at [TMPDIR]/test-repo[0m+✓ Code formatting check passed+✓ Linting passed - no warnings+✓ All 47 tests passed in 2.3sdiff --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.snapindex 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.rsexpression: output.normalized().as_ref()----Removed worktree for [1mto-remove[0m+🔄 [36mRemoving worktree for [1mto-remove[0m...[0m+✅ [32mRemoved worktree for [1m[32mto-remove[0m[0mdiff --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.snapindex 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.rsexpression: output.normalized().as_ref()----Removed worktree for [1mto-remove[0m+🔄 [36mRemoving worktree for [1mto-remove[0m...[0m+✅ [32mRemoved worktree for [1m[32mto-remove[0m[0mdiff --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.snapindex 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.rsexpression: output.normalized().as_ref()----Created new worktree for [1mfeature[0m at [TMPDIR]/test-repo.feature+✅ [32mCreated new worktree for [1m[32mfeature[0m at [TMPDIR]/test-repo.feature[0mdiff --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.snapindex 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.rsexpression: output.normalized().as_ref()----Created new worktree for [1mfeature[0m at [TMPDIR]/test-repo.feature+✅ [32mCreated new worktree for [1m[32mfeature[0m at [TMPDIR]/test-repo.feature[0mdiff --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.snapindex 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.rsexpression: output.normalized().as_ref()----Created new worktree for [1mtest-exec[0m at [TMPDIR]/test-repo.test-exec+✅ [32mCreated new worktree for [1m[32mtest-exec[0m at [TMPDIR]/test-repo.test-exec[0mexecuteddiff --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.snapindex 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.rsexpression: output.normalized().as_ref()----Created new worktree for [1mtest-exec[0m at [TMPDIR]/test-repo.test-exec+✅ [32mCreated new worktree for [1m[32mtest-exec[0m at [TMPDIR]/test-repo.test-exec[0mexecuteddiff --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.snapnew file mode 100644index 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()+---+🔄 [36mExecuting (post-create):[0m+[40m [0m echo 'Installing dependencies...'+++🔄 [36mExecuting (post-create):[0m+[40m [0m echo 'Building project...'+++🔄 [36mStarting (background):[0m+[40m [0m echo 'Starting dev server on port 3000'++🔄 [36mStarting (background):[0m+[40m [0m echo 'Watching for file changes'++✅ [32mCreated new worktree for [1m[32mfeature-hooks[0m at [TMPDIR]/test-repo.feature-hooks[0m+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.snapnew file mode 100644index 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()+---+🔄 [36mExecuting (post-create):[0m+[40m [0m echo 'Installing dependencies...'++🔄 [36mExecuting (post-create):[0m+[40m [0m echo 'Building project...'++🔄 [36mStarting (background):[0m+[40m [0m echo 'Starting dev server on port 3000'++🔄 [36mStarting (background):[0m+[40m [0m echo 'Watching for file changes'++✅ [32mCreated new worktree for [1m[32mfeature-hooks[0m at [TMPDIR]/test-repo.feature-hooks[0m+Installing dependencies...+Building project...