Skip to content

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

refactor(tests): simplify test helpers and remove unnecessary worktrees (#326)

Maximilian Roosd13eb0c1d2cf
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 36f34cb3e..963e3a798 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -169,22 +169,22 @@ pub fn repo_with_remote(mut repo: TestRepo) -> TestRepo {
repo
}
-/// Repo with a separate worktree for the main branch.
+/// Repo with main branch available for merge operations.
///
-/// Many merge/push tests need the main branch in a separate worktree so the
-/// primary worktree can be on a feature branch. This fixture sets that up.
+/// The primary worktree is already on main, so no separate worktree is needed.
+/// This fixture exists for compatibility with tests that expect it.
///
/// Use `#[from(repo_with_main_worktree)]` in rstest:
/// ```ignore
/// #[rstest]
/// fn test_merge(#[from(repo_with_main_worktree)] mut repo: TestRepo) {
/// let feature_wt = repo.add_worktree("feature");
-/// // main is already in a separate worktree
+/// // primary is on main, ready for merge
/// }
/// ```
#[rstest::fixture]
pub fn repo_with_main_worktree(repo: TestRepo) -> TestRepo {
- repo.add_main_worktree();
+ // Primary is already on main - no separate worktree needed
repo
}
@@ -213,9 +213,9 @@ pub fn repo_with_feature_worktree(mut repo_with_main_worktree: TestRepo) -> Test
repo_with_main_worktree
}
-/// Repo with remote, main worktree, and a feature branch with one commit.
+/// Repo with remote and a feature branch with one commit.
///
-/// Combines `repo_with_remote` with a main worktree and feature worktree setup.
+/// Combines `repo_with_remote` with a feature worktree setup.
/// Access the feature worktree path via `repo.worktrees["feature"]`.
///
/// Use for tests that need remote tracking AND a feature branch ready to merge/push.
@@ -224,12 +224,12 @@ pub fn repo_with_feature_worktree(mut repo_with_main_worktree: TestRepo) -> Test
/// fn test_push(mut repo_with_remote_and_feature: TestRepo) {
/// let repo = &mut repo_with_remote_and_feature;
/// let feature_wt = &repo.worktrees["feature"];
-/// // Has remote, main worktree, and feature with one commit
+/// // Has remote and feature with one commit
/// }
/// ```
#[rstest::fixture]
pub fn repo_with_remote_and_feature(mut repo_with_remote: TestRepo) -> TestRepo {
- repo_with_remote.add_main_worktree();
+ // Primary is already on main - no separate worktree needed
repo_with_remote.add_worktree_with_commit(
"feature",
"feature.txt",
@@ -296,7 +296,7 @@ pub fn repo_with_multi_commit_feature(mut repo_with_main_worktree: TestRepo) ->
/// Merge test setup with a single commit on feature branch.
///
/// Creates a repo with:
-/// - A worktree for main at `repo.main-wt`
+/// - Primary worktree on main (unchanged)
/// - A feature worktree with one commit adding `feature.txt`
///
/// Returns `(repo, feature_worktree_path)`.
@@ -311,32 +311,12 @@ pub fn repo_with_multi_commit_feature(mut repo_with_main_worktree: TestRepo) ->
/// ```
#[rstest::fixture]
pub fn merge_scenario(mut repo: TestRepo) -> (TestRepo, PathBuf) {
- // Create a worktree for main
- let main_wt = repo.root_path().parent().unwrap().join("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()
- .unwrap();
-
// Create a feature worktree and make a commit
+ // Primary stays on main - no need for separate main worktree
let feature_wt = repo.add_worktree("feature");
std::fs::write(feature_wt.join("feature.txt"), "feature content").unwrap();
-
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["add", "feature.txt"])
- .current_dir(&feature_wt)
- .output()
- .unwrap();
-
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["commit", "-m", "Add feature file"])
- .current_dir(&feature_wt)
- .output()
- .unwrap();
+ repo.run_git_in(&feature_wt, &["add", "feature.txt"]);
+ repo.run_git_in(&feature_wt, &["commit", "-m", "Add feature file"]);
(repo, feature_wt)
}
@@ -344,7 +324,7 @@ pub fn merge_scenario(mut repo: TestRepo) -> (TestRepo, PathBuf) {
/// Merge test setup with multiple commits on feature branch.
///
/// Creates a repo with:
-/// - A worktree for main at `repo.main-wt`
+/// - Primary worktree on main (unchanged)
/// - A feature worktree with two commits: `file1.txt` and `file2.txt`
///
/// Returns `(repo, feature_worktree_path)`.
@@ -359,45 +339,11 @@ pub fn merge_scenario(mut repo: TestRepo) -> (TestRepo, PathBuf) {
/// ```
#[rstest::fixture]
pub fn merge_scenario_multi_commit(mut repo: TestRepo) -> (TestRepo, PathBuf) {
- // Create a worktree for main
- let main_wt = repo.root_path().parent().unwrap().join("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()
- .unwrap();
-
// Create a feature worktree and make multiple commits
+ // Primary stays on main - no need for separate main worktree
let feature_wt = repo.add_worktree("feature");
-
- std::fs::write(feature_wt.join("file1.txt"), "content 1").unwrap();
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["add", "file1.txt"])
- .current_dir(&feature_wt)
- .output()
- .unwrap();
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["commit", "-m", "feat: add file 1"])
- .current_dir(&feature_wt)
- .output()
- .unwrap();
-
- std::fs::write(feature_wt.join("file2.txt"), "content 2").unwrap();
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["add", "file2.txt"])
- .current_dir(&feature_wt)
- .output()
- .unwrap();
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["commit", "-m", "feat: add file 2"])
- .current_dir(&feature_wt)
- .output()
- .unwrap();
+ repo.commit_in_worktree(&feature_wt, "file1.txt", "content 1", "feat: add file 1");
+ repo.commit_in_worktree(&feature_wt, "file2.txt", "content 2", "feat: add file 2");
(repo, feature_wt)
}
@@ -806,6 +752,26 @@ pub fn set_temp_home_env(cmd: &mut Command, home: &Path) {
cmd.env("APPDATA", home.join(".config"));
}
+/// Check that a git command succeeded, panicking with diagnostics if not.
+///
+/// Use this after `git_command().output()` to ensure the command succeeded.
+///
+/// # Example
+/// ```ignore
+/// let output = repo.git_command().args(["add", "."]).current_dir(&dir).output().unwrap();
+/// check_git_status(&output, "add");
+/// ```
+pub fn check_git_status(output: &std::process::Output, cmd_desc: &str) {
+ if !output.status.success() {
+ panic!(
+ "git {} failed:\nstdout: {}\nstderr: {}",
+ cmd_desc,
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ }
+}
+
pub struct TestRepo {
temp_dir: TempDir, // Must keep to ensure cleanup on drop
root: PathBuf,
@@ -891,12 +857,7 @@ impl TestRepo {
};
// Run git init (can't avoid this for empty repos)
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["init", "-q"])
- .current_dir(&repo.root)
- .output()
- .unwrap();
+ repo.run_git(&["init", "-q"]);
repo
}
@@ -962,6 +923,64 @@ impl TestRepo {
cmd
}
+ /// Run a git command in the repo root, panicking on failure.
+ ///
+ /// Thin wrapper around `git_command()` that runs the command and checks status.
+ pub fn run_git(&self, args: &[&str]) {
+ let output = self.git_command().args(args).output().unwrap();
+ check_git_status(&output, &args.join(" "));
+ }
+
+ /// Run a git command in a specific directory, panicking on failure.
+ ///
+ /// Thin wrapper around `git_command()` that runs in `dir` and checks status.
+ pub fn run_git_in(&self, dir: &Path, args: &[&str]) {
+ let output = self
+ .git_command()
+ .args(args)
+ .current_dir(dir)
+ .output()
+ .unwrap();
+ check_git_status(&output, &args.join(" "));
+ }
+
+ /// Run a git command and return stdout as a trimmed string.
+ ///
+ /// Thin wrapper around `git_command()` for commands that return output.
+ pub fn git_output(&self, args: &[&str]) -> String {
+ let output = self.git_command().args(args).output().unwrap();
+ check_git_status(&output, &args.join(" "));
+ String::from_utf8_lossy(&output.stdout).trim().to_string()
+ }
+
+ /// Stage all changes in a directory.
+ pub fn stage_all(&self, dir: &Path) {
+ self.run_git_in(dir, &["add", "."]);
+ }
+
+ /// Get the HEAD commit SHA.
+ pub fn head_sha(&self) -> String {
+ let output = self
+ .git_command()
+ .args(["rev-parse", "HEAD"])
+ .output()
+ .unwrap();
+ check_git_status(&output, "rev-parse HEAD");
+ String::from_utf8_lossy(&output.stdout).trim().to_string()
+ }
+
+ /// Get the HEAD commit SHA in a specific directory.
+ pub fn head_sha_in(&self, dir: &Path) -> String {
+ let output = self
+ .git_command()
+ .args(["rev-parse", "HEAD"])
+ .current_dir(dir)
+ .output()
+ .unwrap();
+ check_git_status(&output, "rev-parse HEAD");
+ String::from_utf8_lossy(&output.stdout).trim().to_string()
+ }
+
/// Configure command for CLI tests with isolated environment.
///
/// Sets `WORKTRUNK_CONFIG_PATH`, `HOME`, and mock gh/glab commands.
@@ -1172,28 +1191,9 @@ impl TestRepo {
// Use default template path format: ../{{ main_worktree }}.{{ branch }}
// From {temp_dir}/repo, this resolves to {temp_dir}/repo.{branch}
let worktree_path = self.temp_dir.path().join(format!("repo.{}", safe_branch));
+ let worktree_str = worktree_path.to_str().unwrap();
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- let output = cmd
- .args([
- "worktree",
- "add",
- "-b",
- branch,
- worktree_path.to_str().unwrap(),
- ])
- .current_dir(&self.root)
- .output()
- .unwrap();
-
- if !output.status.success() {
- panic!(
- "Failed to add worktree:\nstdout: {}\nstderr: {}",
- String::from_utf8_lossy(&output.stdout),
- String::from_utf8_lossy(&output.stderr)
- );
- }
+ self.run_git(&["worktree", "add", "-b", branch, worktree_str]);
// Canonicalize worktree path to match what git returns
let canonical_path = canonicalize(&worktree_path).unwrap();
@@ -1208,14 +1208,18 @@ impl TestRepo {
/// This is a convenience method that creates a worktree for the main branch
/// in the standard location expected by merge tests. Returns the path to the
/// created worktree.
+ ///
+ /// If the primary worktree is currently on "main", this method detaches HEAD
+ /// first so the worktree can be created.
pub fn add_main_worktree(&self) -> PathBuf {
+ // If primary is on main, detach HEAD first so we can create a worktree for it
+ if self.current_branch() == "main" {
+ self.detach_head();
+ }
+
let main_wt = self.root_path().parent().unwrap().join("repo.main-wt");
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])
- .current_dir(self.root_path())
- .output()
- .unwrap();
+ let main_wt_str = main_wt.to_str().unwrap();
+ self.run_git(&["worktree", "add", main_wt_str, "main"]);
main_wt
}
@@ -1243,23 +1247,9 @@ impl TestRepo {
message: &str,
) -> PathBuf {
let worktree_path = self.add_worktree(branch);
-
std::fs::write(worktree_path.join(filename), content).unwrap();
-
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["add", filename])
- .current_dir(&worktree_path)
- .output()
- .unwrap();
-
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["commit", "-m", message])
- .current_dir(&worktree_path)
- .output()
- .unwrap();
-
+ self.run_git_in(&worktree_path, &["add", filename]);
+ self.run_git_in(&worktree_path, &["commit", "-m", message]);
worktree_path
}
@@ -1305,20 +1295,8 @@ impl TestRepo {
message: &str,
) {
std::fs::write(worktree_path.join(filename), content).unwrap();
-
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["add", filename])
- .current_dir(worktree_path)
- .output()
- .unwrap();
-
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["commit", "-m", message])
- .current_dir(worktree_path)
- .output()
- .unwrap();
+ self.run_git_in(worktree_path, &["add", filename]);
+ self.run_git_in(worktree_path, &["commit", "-m", message]);
}
/// Creates a branch without a worktree.
@@ -1326,12 +1304,7 @@ impl TestRepo {
/// This creates a local branch pointing to HEAD without checking it out.
/// Useful for testing branch listing without creating worktrees.
pub fn create_branch(&self, branch_name: &str) {
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["branch", branch_name])
- .current_dir(self.root_path())
- .output()
- .unwrap();
+ self.run_git(&["branch", branch_name]);
}
/// Pushes a branch to origin.
@@ -1339,12 +1312,7 @@ impl TestRepo {
/// Creates a remote tracking branch on origin. Requires `setup_remote()`
/// to have been called first.
pub fn push_branch(&self, branch_name: &str) {
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["push", "origin", branch_name])
- .current_dir(self.root_path())
- .output()
- .unwrap();
+ self.run_git(&["push", "origin", branch_name]);
}
/// Detach HEAD in the main repository
@@ -1359,39 +1327,19 @@ impl TestRepo {
}
fn detach_head_at(&self, path: &Path) {
- // Get current commit SHA
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- let output = cmd
- .args(["rev-parse", "HEAD"])
- .current_dir(path)
- .output()
- .unwrap();
-
- let sha = String::from_utf8_lossy(&output.stdout).trim().to_string();
-
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["checkout", "--detach", &sha])
- .current_dir(path)
- .output()
- .unwrap();
+ let sha = self.head_sha_in(path);
+ self.run_git_in(path, &["checkout", "--detach", &sha]);
}
/// Lock a worktree with an optional reason
pub fn lock_worktree(&self, name: &str, reason: Option<&str>) {
let worktree_path = self.worktree_path(name);
+ let worktree_str = worktree_path.to_str().unwrap();
- let mut args = vec!["worktree", "lock"];
- if let Some(r) = reason {
- args.push("--reason");
- args.push(r);
+ match reason {
+ Some(r) => self.run_git(&["worktree", "lock", "--reason", r, worktree_str]),
+ None => self.run_git(&["worktree", "lock", worktree_str]),
}
- args.push(worktree_path.to_str().unwrap());
-
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(&args).current_dir(&self.root).output().unwrap();
}
/// Create a bare remote repository and set it as origin
@@ -1413,39 +1361,19 @@ impl TestRepo {
let remote_path = self.temp_dir.path().join(format!("{}.git", remote_name));
std::fs::create_dir(&remote_path).unwrap();
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["init", "--bare", "--initial-branch", default_branch])
- .current_dir(&remote_path)
- .output()
- .unwrap();
+ self.run_git_in(
+ &remote_path,
+ &["init", "--bare", "--initial-branch", default_branch],
+ );
// Canonicalize remote path
let remote_path = canonicalize(&remote_path).unwrap();
+ let remote_path_str = remote_path.to_str().unwrap();
- // Add as remote
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["remote", "add", remote_name, remote_path.to_str().unwrap()])
- .current_dir(&self.root)
- .output()
- .unwrap();
-
- // Push current branch to remote
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["push", "-u", remote_name, default_branch])
- .current_dir(&self.root)
- .output()
- .unwrap();
-
- // Set remote/HEAD to point to the default branch
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["remote", "set-head", remote_name, default_branch])
- .current_dir(&self.root)
- .output()
- .unwrap_or_else(|_| panic!("Failed to set {}/HEAD", remote_name));
+ // Add as remote, push, and set HEAD
+ self.run_git(&["remote", "add", remote_name, remote_path_str]);
+ self.run_git(&["push", "-u", remote_name, default_branch]);
+ self.run_git(&["remote", "set-head", remote_name, default_branch]);
self.remote = Some(remote_path);
}
@@ -1455,12 +1383,7 @@ impl TestRepo {
/// This forces git to not have a cached default branch, useful for testing
/// the fallback path that queries the remote.
pub fn clear_origin_head(&self) {
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["remote", "set-head", "origin", "--delete"])
- .current_dir(&self.root)
- .output()
- .unwrap();
+ self.run_git(&["remote", "set-head", "origin", "--delete"]);
}
/// Check if origin/HEAD is set
@@ -1480,26 +1403,14 @@ impl TestRepo {
/// Creates a new branch and switches to it in the primary worktree.
/// This is useful for testing scenarios where the primary worktree is not on the main branch.
pub fn switch_primary_to(&self, branch: &str) {
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- cmd.args(["switch", "-c", branch])
- .current_dir(&self.root)
- .output()
- .unwrap_or_else(|_| panic!("Failed to create {} branch", branch));
+ self.run_git(&["switch", "-c", branch]);
}
/// Get the current branch of the primary worktree
///
/// Returns the name of the current branch, or panics if HEAD is detached.
pub fn current_branch(&self) -> String {
- let mut cmd = Command::new("git");
- self.configure_git_cmd(&mut cmd);
- let output = cmd
- .args(["branch", "--show-current"])
- .current_dir(&self.root)
- .output()
- .unwrap();
- String::from_utf8_lossy(&output.stdout).trim().to_string()
+ self.git_output(&["branch", "--show-current"])
}
/// Setup mock `gh` and `glab` commands that return immediately without network calls
diff --git a/tests/integration_tests/approval_ui.rs b/tests/integration_tests/approval_ui.rs
index 7420b9fcd..a2cfdda26 100644
--- a/tests/integration_tests/approval_ui.rs
+++ b/tests/integration_tests/approval_ui.rs
@@ -332,10 +332,7 @@ fn test_hook_post_merge_target_is_current_branch(repo: TestRepo) {
repo.commit("Add post-merge hook");
// Create and switch to a feature branch
- repo.git_command()
- .args(["checkout", "-b", "my-feature-branch"])
- .output()
- .unwrap();
+ repo.run_git(&["checkout", "-b", "my-feature-branch"]);
// Run the hook with --yes to skip approval
let output = repo
@@ -370,10 +367,7 @@ fn test_hook_pre_merge_target_is_current_branch(repo: TestRepo) {
repo.commit("Add pre-merge hook");
// Create and switch to a feature branch
- repo.git_command()
- .args(["checkout", "-b", "my-feature-branch"])
- .output()
- .unwrap();
+ repo.run_git(&["checkout", "-b", "my-feature-branch"]);
// Run the hook with --yes to skip approval
let output = repo
diff --git a/tests/integration_tests/ci_status.rs b/tests/integration_tests/ci_status.rs
index 2c091640f..14415e597 100644
--- a/tests/integration_tests/ci_status.rs
+++ b/tests/integration_tests/ci_status.rs
@@ -14,35 +14,25 @@
use crate::common::{TestRepo, make_snapshot_cmd, repo, setup_snapshot_settings};
use insta_cmd::assert_cmd_snapshot;
use rstest::rstest;
-use std::process::Command;
/// Get the HEAD commit SHA for a branch
fn get_branch_sha(repo: &TestRepo, branch: &str) -> String {
- let output = repo
- .git_command()
- .args(["rev-parse", branch])
- .output()
- .expect("Failed to get commit SHA");
- String::from_utf8_lossy(&output.stdout).trim().to_string()
+ repo.git_output(&["rev-parse", branch])
}
/// Test CI status detection with GitHub PR showing passed checks
#[rstest]
fn test_list_full_with_github_pr_passed(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -75,19 +65,15 @@ fn test_list_full_with_github_pr_passed(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_github_pr_failed(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -120,19 +106,15 @@ fn test_list_full_with_github_pr_failed(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_github_pr_running(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -165,19 +147,15 @@ fn test_list_full_with_github_pr_running(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_github_pr_conflicts(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -210,19 +188,15 @@ fn test_list_full_with_github_pr_conflicts(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_status_context_pending(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -255,19 +229,15 @@ fn test_list_full_with_status_context_pending(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_status_context_failure(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -300,19 +270,15 @@ fn test_list_full_with_status_context_failure(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_github_workflow_run(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it (so has_upstream is true)
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so workflow run isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -341,19 +307,15 @@ fn test_list_full_with_github_workflow_run(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_github_workflow_running(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so workflow run isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -382,35 +344,21 @@ fn test_list_full_with_github_workflow_running(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_stale_pr(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Make additional commit locally (not pushed)
let worktree_path = repo.worktrees.get("feature").unwrap();
std::fs::write(worktree_path.join("new_file.txt"), "new content").unwrap();
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["add", "."])
- .current_dir(worktree_path)
- .output()
- .unwrap();
- let mut cmd = Command::new("git");
- repo.configure_git_cmd(&mut cmd);
- cmd.args(["commit", "-m", "Local commit"])
- .current_dir(worktree_path)
- .output()
- .unwrap();
+ repo.stage_all(worktree_path);
+ repo.run_git_in(worktree_path, &["commit", "-m", "Local commit"]);
// Setup mock gh with PR data - use fake SHA to simulate stale PR
// (PR was created before the local commit, so PR HEAD differs from local HEAD)
@@ -438,19 +386,15 @@ fn test_list_full_with_stale_pr(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_mixed_check_types(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -485,19 +429,15 @@ fn test_list_full_with_mixed_check_types(mut repo: TestRepo) {
#[rstest]
fn test_list_full_with_no_ci_checks(mut repo: TestRepo) {
// Add GitHub remote
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/test-owner/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/test-owner/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
@@ -528,19 +468,15 @@ fn test_list_full_with_no_ci_checks(mut repo: TestRepo) {
#[rstest]
fn test_list_full_filters_by_repo_owner(mut repo: TestRepo) {
// Add GitHub remote with specific owner
- repo.git_command()
- .args([
- "remote",
- "add",
- "origin",
- "https://github.com/my-org/test-repo.git",
- ])
- .output()
- .unwrap();
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://github.com/my-org/test-repo.git",
+ ]);
// Create a feature branch and push it
repo.add_worktree("feature");
- repo.push_branch("feature");
// Get actual commit SHA so PR isn't marked as stale
let head_sha = get_branch_sha(&repo, "feature");
diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rs
index c7d2af692..879ea7f78 100644
--- a/tests/integration_tests/config_state.rs
+++ b/tests/integration_tests/config_state.rs
@@ -123,10 +123,7 @@ fn test_state_clear_default_branch(mut repo: TestRepo) {
#[rstest]
fn test_state_clear_default_branch_empty(repo: TestRepo) {
// Set up remote but don't set default branch cache
- repo.git_command()
- .args(["remote", "add", "origin", "https://example.com/repo.git"])
- .output()
- .unwrap();
+ repo.run_git(&["remote", "add", "origin", "https://example.com/repo.git"]);
let output = wt_state_cmd(&repo, "default-branch", "clear", &[])
.output()
diff --git a/tests/integration_tests/default_branch.rs b/tests/integration_tests/default_branch.rs
index 582dc9e0f..0b3d7142d 100644
--- a/tests/integration_tests/default_branch.rs
+++ b/tests/integration_tests/default_branch.rs
@@ -79,7 +79,8 @@ fn test_get_default_branch_with_custom_remote(mut repo: TestRepo) {
#[rstest]
fn test_primary_remote_detects_custom_remote(mut repo: TestRepo) {
- repo.setup_custom_remote("upstream", "develop");
+ // Use "main" since that's the local branch - the test only cares about remote name detection
+ repo.setup_custom_remote("upstream", "main");
// Test that primary_remote detects the custom remote name
let git_repo = Repository::at(repo.root_path());
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 19a644e2b..c477253c4 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -225,9 +225,6 @@ fn test_merge_rebase_conflict(repo: TestRepo) {
repo.commit("Add shared file");
- // Create a worktree for main
- repo.add_main_worktree();
-
// Modify shared.txt in main branch (from the base commit)
std::fs::write(repo.root_path().join("shared.txt"), "main version\n").unwrap();
@@ -595,8 +592,6 @@ fn test_merge_pre_merge_command_success(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --yes to skip approval prompts
@@ -617,8 +612,6 @@ fn test_merge_pre_merge_command_failure(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --yes - pre-merge command should fail and block merge
@@ -639,8 +632,6 @@ fn test_merge_pre_merge_command_no_hooks(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --no-verify - should skip pre-merge commands and succeed
@@ -670,8 +661,6 @@ test = "exit 0"
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --yes - all pre-merge commands should pass
@@ -696,8 +685,6 @@ fn test_merge_post_merge_command_success(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --yes
@@ -735,8 +722,6 @@ fn test_merge_post_merge_command_skipped_with_no_verify(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --no-verify - hook should be skipped entirely
@@ -764,8 +749,6 @@ fn test_merge_post_merge_command_failure(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --yes - post-merge command should fail but merge should complete
@@ -794,8 +777,6 @@ deploy = "echo 'Deploying branch {{ branch }}' > deploy.txt"
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --yes
@@ -835,7 +816,6 @@ fn test_merge_post_merge_runs_with_nothing_to_merge(mut repo: TestRepo) {
repo.commit("Add config");
// Create a worktree for main (destination for post-merge commands)
- repo.add_main_worktree();
// Create a feature worktree with NO commits (already up-to-date with main)
let feature_wt = repo.add_worktree("feature");
@@ -900,9 +880,6 @@ fn test_merge_pre_commit_command_success(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
-
// Create a feature worktree and make a change
let feature_wt = repo.add_worktree("feature");
fs::write(feature_wt.join("feature.txt"), "feature content").unwrap();
@@ -925,9 +902,6 @@ fn test_merge_pre_commit_command_failure(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
-
// Create a feature worktree and make a change
let feature_wt = repo.add_worktree("feature");
fs::write(feature_wt.join("feature.txt"), "feature content").unwrap();
@@ -954,9 +928,6 @@ fn test_merge_pre_squash_command_success(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
-
// Create a feature worktree and make commits
let feature_wt = repo.add_feature();
@@ -978,8 +949,6 @@ fn test_merge_pre_squash_command_failure(mut repo: TestRepo) {
repo.commit("Add config");
- // Create a worktree for main
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Merge with --yes (squashing is default) - pre-commit command should fail and block merge
@@ -1168,9 +1137,6 @@ EOF
.output()
.unwrap();
- // Create a worktree for main
- repo.add_main_worktree();
-
// Create a feature worktree and make multiple commits
let feature_wt = repo.add_worktree("feature-auth");
@@ -1483,9 +1449,6 @@ fi
.output()
.unwrap();
- // Create a worktree for main
- repo.add_main_worktree();
-
// Create a feature worktree and make multiple commits
let feature_wt = repo.add_worktree("feature-auth");
diff --git a/tests/integration_tests/post_start_commands.rs b/tests/integration_tests/post_start_commands.rs
index fd8e20703..e0ae5ad2c 100644
--- a/tests/integration_tests/post_start_commands.rs
+++ b/tests/integration_tests/post_start_commands.rs
@@ -1,5 +1,5 @@
use crate::common::{
- TestRepo, make_snapshot_cmd, repo, repo_with_remote, resolve_git_common_dir,
+ TestRepo, make_snapshot_cmd, repo, repo_with_remote, resolve_git_common_dir, set_temp_home_env,
setup_snapshot_settings, wait_for_file, wait_for_file_content, wait_for_file_count,
wait_for_file_lines, wait_for_valid_json,
};
@@ -27,9 +27,7 @@ fn snapshot_switch(test_name: &str, repo: &TestRepo, args: &[&str]) {
let settings = setup_snapshot_settings(repo);
settings.bind(|| {
let mut cmd = make_snapshot_cmd(repo, "switch", args, None);
- cmd.env("HOME", temp_home.path());
- // Windows: the `home` crate uses USERPROFILE for home_dir()
- cmd.env("USERPROFILE", temp_home.path());
+ set_temp_home_env(&mut cmd, temp_home.path());
assert_cmd_snapshot!(test_name, cmd);
});
}
@@ -382,15 +380,12 @@ approved-commands = ["cat > context.json"]
// Create worktree - this should pipe JSON to the hook's stdin
let temp_home = TempDir::new().unwrap();
- let output = wt_command()
- .args(["switch", "--create", "feature-json"])
+ let mut cmd = wt_command();
+ cmd.args(["switch", "--create", "feature-json"])
.current_dir(repo.root_path())
- .env("HOME", temp_home.path())
- // Windows: the `home` crate uses USERPROFILE for home_dir()
- .env("USERPROFILE", temp_home.path())
- .env("WORKTRUNK_CONFIG_PATH", repo.test_config_path())
- .output()
- .expect("failed to run wt switch");
+ .env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+ let output = cmd.output().expect("failed to run wt switch");
assert!(
output.status.success(),
@@ -487,15 +482,12 @@ approved-commands = ["./scripts/setup.py"]
// Create worktree
let temp_home = TempDir::new().unwrap();
- let output = wt_command()
- .args(["switch", "--create", "feature-script"])
+ let mut cmd = wt_command();
+ cmd.args(["switch", "--create", "feature-script"])
.current_dir(repo.root_path())
- .env("HOME", temp_home.path())
- // Windows: the `home` crate uses USERPROFILE for home_dir()
- .env("USERPROFILE", temp_home.path())
- .env("WORKTRUNK_CONFIG_PATH", repo.test_config_path())
- .output()
- .expect("failed to run wt switch");
+ .env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+ let output = cmd.output().expect("failed to run wt switch");
assert!(
output.status.success(),
@@ -560,15 +552,12 @@ approved-commands = ["cat > context.json"]
// Create worktree
let temp_home = TempDir::new().unwrap();
- let output = wt_command()
- .args(["switch", "--create", "bg-json"])
+ let mut cmd = wt_command();
+ cmd.args(["switch", "--create", "bg-json"])
.current_dir(repo.root_path())
- .env("HOME", temp_home.path())
- // Windows: the `home` crate uses USERPROFILE for home_dir()
- .env("USERPROFILE", temp_home.path())
- .env("WORKTRUNK_CONFIG_PATH", repo.test_config_path())
- .output()
- .expect("failed to run wt switch");
+ .env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+ let output = cmd.output().expect("failed to run wt switch");
assert!(
output.status.success(),
diff --git a/tests/integration_tests/push.rs b/tests/integration_tests/push.rs
index 2e7d3801b..ab91c057f 100644
--- a/tests/integration_tests/push.rs
+++ b/tests/integration_tests/push.rs
@@ -1,5 +1,6 @@
use crate::common::{
- TestRepo, make_snapshot_cmd, repo, repo_with_feature_worktree, setup_snapshot_settings,
+ TestRepo, make_snapshot_cmd, repo, repo_with_feature_worktree, repo_with_remote,
+ setup_snapshot_settings,
};
use insta_cmd::assert_cmd_snapshot;
use rstest::rstest;
@@ -197,7 +198,7 @@ fn test_push_dirty_target_overlap_renamed_file(mut repo: TestRepo) {
}
#[rstest]
-fn test_push_error_not_fast_forward(mut repo: TestRepo) {
+fn test_push_error_not_fast_forward(#[from(repo_with_remote)] mut repo: TestRepo) {
// Create feature branch from initial commit
let feature_wt = repo.add_worktree("feature");
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 0e3916f91..d813ea15d 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -1007,7 +1007,6 @@ test = "echo '✓ All 47 tests passed in 2.3s'"
.unwrap();
repo.commit("Add pre-merge validation");
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Pre-approve commands
@@ -1061,7 +1060,6 @@ test = "echo '✗ Test suite failed: 3 tests failing' && exit 1"
.unwrap();
repo.commit("Add failing pre-merge validation");
- repo.add_main_worktree();
// Create feature worktree with a commit
let feature_wt = repo.add_worktree_with_commit(
@@ -1138,7 +1136,6 @@ check2 = "{} check2 3"
.unwrap();
repo.commit("Add pre-merge validation with mixed output");
- repo.add_main_worktree();
let feature_wt = repo.add_feature();
// Pre-approve commands
@@ -2197,8 +2194,6 @@ fi
.output()
.unwrap();
- repo.add_main_worktree();
-
// Create a feature worktree and make multiple commits
let feature_wt = repo.add_worktree("feature-auth");