Skip to content

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

Refactor: Move pre-merge command output test

Maximilian Roose23d930cd05d
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index fb53ec2ab..4b1e487e9 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -570,44 +570,6 @@ pub fn make_snapshot_cmd(
make_snapshot_cmd_with_global_flags(repo, subcommand, args, cwd, &[])
}
-/// Run a command and capture combined stdout+stderr output for snapshot testing
-///
-/// This mimics real terminal usage where stdout and stderr are interleaved (like `2>&1`).
-/// Returns the combined output as a String with exit code information.
-pub fn run_with_combined_output(
- repo: &TestRepo,
- subcommand: &str,
- args: &[&str],
- cwd: Option<&Path>,
-) -> String {
- use std::process::Stdio;
-
- // Build the command with all arguments
- let wt_bin = insta_cmd::get_cargo_bin("wt");
- let mut cmd_parts = vec![wt_bin.to_str().unwrap(), subcommand];
- cmd_parts.extend(args.iter());
-
- // Run through shell with 2>&1 to get true interleaved output
- let cmd_str = cmd_parts.join(" ");
- let shell_cmd = format!("{} 2>&1", cmd_str);
-
- let mut cmd = Command::new("sh");
- repo.clean_cli_env(&mut cmd);
- cmd.arg("-c")
- .arg(&shell_cmd)
- .current_dir(cwd.unwrap_or(repo.root_path()))
- .stdout(Stdio::piped());
-
- let output = cmd.output().expect("Failed to execute command");
- let combined = String::from_utf8_lossy(&output.stdout).to_string();
-
- format!(
- "Exit code: {}\n{}",
- output.status.code().unwrap_or(-1),
- combined
- )
-}
-
/// Resolve the actual git directory path from a worktree path
///
/// In worktrees, `.git` is a file containing `gitdir: /path/to/git/dir`,
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 9911182a7..fc9a198f6 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -1,7 +1,6 @@
use crate::common::{TestRepo, make_snapshot_cmd, setup_snapshot_settings};
use insta_cmd::assert_cmd_snapshot;
use std::fs;
-use std::path::PathBuf;
use std::process::Command;
/// Helper to create snapshot with normalized paths
@@ -1147,137 +1146,6 @@ test = "exit 0"
);
}
-#[test]
-fn test_merge_pre_merge_command_stdout_stderr_ordering() {
- let mut repo = TestRepo::new();
- repo.commit("Initial commit");
- repo.setup_remote("main");
-
- // Get path to the test fixture script
- let fixtures_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");
- let script_path = fixtures_dir.join("mixed-output.sh");
-
- // Create project config with two named pre-merge commands that both output to stdout and stderr
- 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"),
- format!(
- r#"
-[pre-merge-command]
-check1 = "{} check1 3"
-check2 = "{} check2 3"
-"#,
- script_path.display(),
- script_path.display()
- ),
- )
- .expect("Failed to write config");
-
- repo.commit("Add config");
-
- // Create a worktree for main
- 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 worktree");
-
- // Create a feature worktree and make 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 file"])
- .current_dir(&feature_wt)
- .output()
- .expect("Failed to commit");
-
- // Merge with --force - verify output ordering
- // Expected behavior in real usage (with 2>&1):
- // check1 header → all check1 output → check2 header → all check2 output
- //
- // Note: The snapshot separates stdout/stderr for display, but in practice
- // they're properly interleaved when both go to the terminal.
- // The important verification is that within each stream, the ordering is correct.
- snapshot_merge(
- "merge_pre_merge_command_stdout_stderr_ordering",
- &repo,
- &["main", "--force"],
- Some(&feature_wt),
- );
-}
-
-#[test]
-fn test_merge_pre_merge_command_combined_output() {
- use crate::common::run_with_combined_output;
-
- let mut repo = TestRepo::new();
- repo.commit("Initial commit");
- repo.setup_remote("main");
-
- // Create project config with two named pre-merge commands that output to BOTH stdout and stderr
- // This verifies the ordering is: header1 → command1 → stdout1 → stderr1 → header2 → command2 → stdout2 → stderr2
- 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]
-first = "echo 'STDOUT-FROM-FIRST' && echo 'STDERR-FROM-FIRST' >&2"
-second = "echo 'STDOUT-FROM-SECOND' && echo 'STDERR-FROM-SECOND' >&2"
-"#,
- )
- .expect("Failed to write config");
-
- repo.commit("Add config");
-
- // Create a worktree for main
- 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 worktree");
-
- // Create a feature worktree and make 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 file"])
- .current_dir(&feature_wt)
- .output()
- .expect("Failed to commit");
-
- // Run merge and capture combined output
- let output = run_with_combined_output(&repo, "merge", &["main", "--force"], Some(&feature_wt));
-
- let settings = crate::common::setup_snapshot_settings(&repo);
- settings.bind(|| {
- insta::assert_snapshot!("merge_pre_merge_command_combined_output", output);
- });
-}
-
#[test]
fn test_merge_post_merge_command_success() {
let mut repo = TestRepo::new();
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index abd7dfae9..df3d3f8b1 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -35,6 +35,7 @@ use crate::common::TestRepo;
use insta::assert_snapshot;
use insta_cmd::get_cargo_bin;
use std::fs;
+use std::path::PathBuf;
use std::process::Command;
use std::sync::LazyLock;
@@ -753,6 +754,102 @@ approved-commands = [
);
}
+ /// Test merge with pre-merge commands that output to both stdout and stderr
+ /// Verifies that interleaved stdout/stderr appears in correct temporal order
+ /// Note: fish disabled due to flaky PTY buffering race conditions
+ #[rstest]
+ #[case("bash")]
+ #[case("zsh")]
+ // #[case("fish")] // TODO: Fish shell has non-deterministic PTY output ordering
+ fn test_wrapper_merge_with_mixed_stdout_stderr(#[case] shell: &str) {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Get path to the test fixture script
+ let fixtures_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");
+ let script_path = fixtures_dir.join("mixed-output.sh");
+
+ // Create project config with pre-merge commands that output to both stdout and stderr
+ 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"),
+ format!(
+ r#"[pre-merge-command]
+check1 = "{} check1 3"
+check2 = "{} check2 3"
+"#,
+ script_path.display(),
+ script_path.display()
+ ),
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add pre-merge validation with mixed output");
+
+ // 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(),
+ format!(
+ r#"worktree-path = "../{{main-worktree}}.{{branch}}"
+
+[projects."test-repo"]
+approved-commands = [
+ "{} check1 3",
+ "{} check2 3",
+]
+"#,
+ script_path.display(),
+ script_path.display()
+ ),
+ )
+ .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();
+
+ // Verify output shows proper temporal ordering:
+ // header1 → all check1 output (interleaved stdout/stderr) → header2 → all check2 output
+ // This ensures that stdout/stderr from child processes properly stream through
+ // to the terminal in real-time, maintaining correct ordering
+ assert_snapshot!(
+ format!("merge_with_mixed_stdout_stderr_{}", shell),
+ output.normalized()
+ );
+ }
+
// ========================================================================
// Bash Shell Wrapper Tests
// ========================================================================
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_bash.snap
new file mode 100644
index 000000000..a031ceb84
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_bash.snap
@@ -0,0 +1,38 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized()
+---
+🔄 Running pre-merge: check1:
+  /Users/maximilian/workspace/worktrunk.insta-stdout-err/tests/fixtures/mixed-output.sh check1 3
+
+[check1] stdout line 1
+[check1] stderr line 1
+[check1] stdout line 2
+[check1] stderr line 2
+[check1] stdout line 3
+[check1] stderr line 3
+[check1] stdout complete
+[check1] stderr complete
+
+🔄 Running pre-merge: check2:
+  /Users/maximilian/workspace/worktrunk.insta-stdout-err/tests/fixtures/mixed-output.sh check2 3
+
+[check2] stdout line 1
+[check2] stderr line 1
+[check2] stdout line 2
+[check2] stderr line 2
+[check2] stdout line 3
+[check2] stderr line 3
+[check2] stdout complete
+[check2] stderr complete
+
+🔄 Merging 1 commit to main @ 429b6a2 (no commit/squash/rebase needed)
+
+  * 429b6a2 (HEAD -> feature) Add feature
+
+  feature.txt | 1 +
+  1 file changed, 1 insertion(+)
+
+✅ Merged to main (1 commit, 1 file, +1)
+✅ Returned to primary at [TMPDIR]/test-repo
+🔄 Removing worktree & branch...
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_zsh.snap
new file mode 100644
index 000000000..acc7d1a48
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_zsh.snap
@@ -0,0 +1,38 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized()
+---
+[check1] stdout line 1
+[check1] stderr line 1
+[check1] stdout line 2
+[check1] stderr line 2
+[check1] stdout line 3
+[check1] stderr line 3
+[check1] stdout complete
+[check1] stderr complete
+[check2] stdout line 1
+[check2] stderr line 1
+[check2] stdout line 2
+[check2] stderr line 2
+[check2] stdout line 3
+[check2] stderr line 3
+[check2] stdout complete
+[check2] stderr complete
+🔄 Running pre-merge: check1:
+  /Users/maximilian/workspace/worktrunk.insta-stdout-err/tests/fixtures/mixed-output.sh check1 3
+
+
+🔄 Running pre-merge: check2:
+  /Users/maximilian/workspace/worktrunk.insta-stdout-err/tests/fixtures/mixed-output.sh check2 3
+
+
+🔄 Merging 1 commit to main @ 429b6a2 (no commit/squash/rebase needed)
+
+  * 429b6a2 (HEAD -> feature) Add feature
+
+  feature.txt | 1 +
+  1 file changed, 1 insertion(+)
+
+✅ Merged to main (1 commit, 1 file, +1)
+✅ Returned to primary at [TMPDIR]/test-repo
+🔄 Removing worktree & branch...
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_combined_output.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_combined_output.snap
deleted file mode 100644
index afdefdac4..000000000
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_combined_output.snap
+++ /dev/null
@@ -1,21 +0,0 @@
----
-source: tests/integration_tests/merge.rs
-expression: output
----
-Exit code: 0
-🔄 Running pre-merge: first:
-  echo 'STDOUT-FROM-FIRST' && echo 'STDERR-FROM-FIRST' >&2
-STDOUT-FROM-FIRST
-STDERR-FROM-FIRST
-🔄 Running pre-merge: second:
-  echo 'STDOUT-FROM-SECOND' && echo 'STDERR-FROM-SECOND' >&2
-STDOUT-FROM-SECOND
-STDERR-FROM-SECOND
-🔄 Merging 1 commit to main @ [SHA] (no commit/squash/rebase needed)
-
-  * [SHA] (HEAD -> feature) Add feature file
-  feature.txt | 1 +
-  1 file changed, 1 insertion(+)
-✅ Merged to main (1 commit, 1 file, +1)
-✅ Returned to primary at [REPO]
-🔄 Removing worktree & branch...
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
deleted file mode 100644
index 09bb437c7..000000000
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
+++ /dev/null
@@ -1,54 +0,0 @@
----
-source: tests/integration_tests/merge.rs
-info:
- program: wt
- args:
- - merge
- - main
- - "--force"
- env:
- CLICOLOR_FORCE: "1"
- COLUMNS: "150"
- GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
- GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
- GIT_CONFIG_GLOBAL: /dev/null
- GIT_CONFIG_SYSTEM: /dev/null
- GIT_EDITOR: ""
- LANG: C
- LC_ALL: C
- SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpHm2RXY/test-config.toml
----
-success: true
-exit_code: 0
------ stdout -----
-🔄 Running pre-merge: check1:
-  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check1 3
-🔄 Running pre-merge: check2:
-  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check2 3
-🔄 Merging 1 commit to main @ [SHA] (no commit/squash/rebase needed)
-
-  * [SHA] (HEAD -> feature) Add feature file
-  feature.txt | 1 +
-  1 file changed, 1 insertion(+)
-✅ Merged to main (1 commit, 1 file, +1)
-✅ Returned to primary at [REPO]
-🔄 Removing worktree & branch...
-
------ stderr -----
-[check1] stdout line 1
-[check1] stderr line 1
-[check1] stdout line 2
-[check1] stderr line 2
-[check1] stdout line 3
-[check1] stderr line 3
-[check1] stdout complete
-[check1] stderr complete
-[check2] stdout line 1
-[check2] stderr line 1
-[check2] stdout line 2
-[check2] stderr line 2
-[check2] stdout line 3
-[check2] stderr line 3
-[check2] stdout complete
-[check2] stderr complete