Skip to content

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

refactor: replace raw git commands with repo.run_command() in tests (#1714)

Maximilian Roos54d73d89a013
diff --git a/src/commands/picker/summary.rs b/src/commands/picker/summary.rs
index 48884a5bb..42513a1aa 100644
--- a/src/commands/picker/summary.rs
+++ b/src/commands/picker/summary.rs
@@ -58,64 +58,67 @@ mod tests {
use super::*;
use crate::commands::list::model::{ItemKind, WorktreeData};
use std::fs;
-
- fn git_command(dir: &std::path::Path) -> std::process::Command {
- let mut cmd = std::process::Command::new("git");
- cmd.current_dir(dir)
- .env("GIT_AUTHOR_NAME", "Test")
- .env("GIT_AUTHOR_EMAIL", "[email protected]")
- .env("GIT_COMMITTER_NAME", "Test")
- .env("GIT_COMMITTER_EMAIL", "[email protected]");
- cmd
- }
-
- fn git(dir: &std::path::Path, args: &[&str]) {
- let output = git_command(dir).args(args).output().unwrap();
- assert!(
- output.status.success(),
- "git {:?} failed: {}",
- args,
- String::from_utf8_lossy(&output.stderr)
- );
+ use worktrunk::shell_exec::Cmd;
+
+ fn git_init(dir: &std::path::Path, args: &[&str]) {
+ Cmd::new("git")
+ .args(args.iter().copied())
+ .current_dir(dir)
+ .run()
+ .unwrap();
}
- fn git_output(dir: &std::path::Path, args: &[&str]) -> String {
- let output = git_command(dir).args(args).output().unwrap();
- String::from_utf8(output.stdout).unwrap().trim().to_string()
+ fn configure_test_identity(repo: &Repository) {
+ repo.run_command(&["config", "user.name", "Test"]).unwrap();
+ repo.run_command(&["config", "user.email", "[email protected]"])
+ .unwrap();
}
/// Create a minimal temp git repo (for cache-only tests that don't need branches).
fn temp_repo() -> (tempfile::TempDir, Repository) {
let dir = tempfile::tempdir().unwrap();
- git(dir.path(), &["init", "--initial-branch=main"]);
- git(dir.path(), &["commit", "--allow-empty", "-m", "init"]);
+ git_init(dir.path(), &["init", "--initial-branch=main"]);
let repo = Repository::at(dir.path()).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
+ .unwrap();
(dir, repo)
}
/// Create a temp repo with main branch, default-branch config, and a real commit.
fn temp_repo_configured() -> (tempfile::TempDir, Repository, String) {
let dir = tempfile::tempdir().unwrap();
- git(dir.path(), &["init", "--initial-branch=main"]);
- git(dir.path(), &["config", "worktrunk.default-branch", "main"]);
- fs::write(dir.path().join("README.md"), "# Project\n").unwrap();
- git(dir.path(), &["add", "README.md"]);
- git(dir.path(), &["commit", "-m", "initial commit"]);
- let head = git_output(dir.path(), &["rev-parse", "HEAD"]);
+ git_init(dir.path(), &["init", "--initial-branch=main"]);
let repo = Repository::at(dir.path()).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["config", "worktrunk.default-branch", "main"])
+ .unwrap();
+ fs::write(dir.path().join("README.md"), "# Project\n").unwrap();
+ repo.run_command(&["add", "README.md"]).unwrap();
+ repo.run_command(&["commit", "-m", "initial commit"])
+ .unwrap();
+ let head = repo
+ .run_command(&["rev-parse", "HEAD"])
+ .unwrap()
+ .trim()
+ .to_string();
(dir, repo, head)
}
/// Create a temp repo with main + feature branch that has real changes.
fn temp_repo_with_feature() -> (tempfile::TempDir, Repository, String) {
- let (dir, _, _) = temp_repo_configured();
+ let (dir, repo, _) = temp_repo_configured();
- git(dir.path(), &["checkout", "-b", "feature"]);
+ repo.run_command(&["checkout", "-b", "feature"]).unwrap();
fs::write(dir.path().join("new.txt"), "new content\n").unwrap();
- git(dir.path(), &["add", "new.txt"]);
- git(dir.path(), &["commit", "-m", "add new file"]);
-
- let head = git_output(dir.path(), &["rev-parse", "HEAD"]);
+ repo.run_command(&["add", "new.txt"]).unwrap();
+ repo.run_command(&["commit", "-m", "add new file"]).unwrap();
+
+ let head = repo
+ .run_command(&["rev-parse", "HEAD"])
+ .unwrap()
+ .trim()
+ .to_string();
let repo = Repository::at(dir.path()).unwrap();
(dir, repo, head)
}
@@ -360,7 +363,7 @@ mod tests {
let (dir, repo, head) = temp_repo_with_feature();
// Add uncommitted changes
fs::write(dir.path().join("uncommitted.txt"), "wip\n").unwrap();
- git(dir.path(), &["add", "uncommitted.txt"]);
+ repo.run_command(&["add", "uncommitted.txt"]).unwrap();
let result = compute_combined_diff("feature", &head, Some(dir.path()), &repo);
assert!(result.is_some());
@@ -389,21 +392,30 @@ mod tests {
// "master", "develop", "trunk"). This ensures default_branch() returns
// None, exercising the code path where branch diff is skipped.
let dir = tempfile::tempdir().unwrap();
- git(dir.path(), &["init", "--initial-branch=init-branch"]);
+ git_init(dir.path(), &["init", "--initial-branch=init-branch"]);
+ let setup_repo = Repository::at(dir.path()).unwrap();
+ configure_test_identity(&setup_repo);
fs::write(dir.path().join("README.md"), "# Project\n").unwrap();
- git(dir.path(), &["add", "README.md"]);
- git(dir.path(), &["commit", "-m", "initial commit"]);
- git(dir.path(), &["checkout", "-b", "feature"]);
- git(
- dir.path(),
- &["commit", "--allow-empty", "-m", "feature commit"],
- );
+ setup_repo.run_command(&["add", "README.md"]).unwrap();
+ setup_repo
+ .run_command(&["commit", "-m", "initial commit"])
+ .unwrap();
+ setup_repo
+ .run_command(&["checkout", "-b", "feature"])
+ .unwrap();
+ setup_repo
+ .run_command(&["commit", "--allow-empty", "-m", "feature commit"])
+ .unwrap();
// Add uncommitted changes
fs::write(dir.path().join("wip.txt"), "work in progress\n").unwrap();
- git(dir.path(), &["add", "wip.txt"]);
+ setup_repo.run_command(&["add", "wip.txt"]).unwrap();
- let head = git_output(dir.path(), &["rev-parse", "HEAD"]);
+ let head = setup_repo
+ .run_command(&["rev-parse", "HEAD"])
+ .unwrap()
+ .trim()
+ .to_string();
let repo = Repository::at(dir.path()).unwrap();
// Verify default_branch() actually returns None with these branch names
diff --git a/src/git/recover.rs b/src/git/recover.rs
index a89ebce86..820bbd8bb 100644
--- a/src/git/recover.rs
+++ b/src/git/recover.rs
@@ -218,6 +218,12 @@ mod tests {
.unwrap();
}
+ fn configure_test_identity(repo: &Repository) {
+ repo.run_command(&["config", "user.name", "Test"]).unwrap();
+ repo.run_command(&["config", "user.email", "[email protected]"])
+ .unwrap();
+ }
+
#[test]
fn test_try_repo_at_rejects_git_file() {
let tmp = tempfile::tempdir().unwrap();
@@ -281,28 +287,18 @@ mod tests {
let repo_dir = base.join("repo");
std::fs::create_dir(&repo_dir).unwrap();
git_init(&repo_dir);
+ let repo = Repository::at(&repo_dir).unwrap();
+ configure_test_identity(&repo);
// Create an initial commit so worktree add works
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(&repo_dir)
- .run()
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
// Add a linked worktree
let wt_path = base.join("feature-wt");
- Cmd::new("git")
- .args([
- "worktree",
- "add",
- &wt_path.to_string_lossy(),
- "-b",
- "feature",
- ])
- .current_dir(&repo_dir)
- .run()
+ let wt_str = wt_path.to_string_lossy();
+ repo.run_command(&["worktree", "add", &wt_str, "-b", "feature"])
.unwrap();
- let repo = Repository::at(&repo_dir).unwrap();
assert!(was_worktree_of(&repo, &wt_path));
}
@@ -310,13 +306,11 @@ mod tests {
fn test_was_worktree_of_rejects_unknown_path() {
let tmp = tempfile::tempdir().unwrap();
git_init(tmp.path());
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(tmp.path())
- .run()
+ let repo = Repository::at(tmp.path()).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
- let repo = Repository::at(tmp.path()).unwrap();
let unknown = PathBuf::from("/nonexistent/unknown");
assert!(!was_worktree_of(&repo, &unknown));
}
@@ -338,24 +332,15 @@ mod tests {
let repo_dir = base.join("repo");
std::fs::create_dir(&repo_dir).unwrap();
git_init(&repo_dir);
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(&repo_dir)
- .run()
+ let repo = Repository::at(&repo_dir).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
// Add a linked worktree
let wt_path = base.join("feature-wt");
- Cmd::new("git")
- .args([
- "worktree",
- "add",
- &wt_path.to_string_lossy(),
- "-b",
- "feature",
- ])
- .current_dir(&repo_dir)
- .run()
+ let wt_str = wt_path.to_string_lossy();
+ repo.run_command(&["worktree", "add", &wt_str, "-b", "feature"])
.unwrap();
// Delete the worktree directory (simulating external removal)
@@ -372,10 +357,9 @@ mod tests {
let repo_dir = base.join("repo");
std::fs::create_dir(&repo_dir).unwrap();
git_init(&repo_dir);
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(&repo_dir)
- .run()
+ let repo = Repository::at(&repo_dir).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
// Try to recover from a path that was never a worktree
@@ -395,26 +379,24 @@ mod tests {
std::fs::create_dir(&repo_b).unwrap();
git_init(&repo_a);
git_init(&repo_b);
- for repo in [&repo_a, &repo_b] {
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(repo)
- .run()
+ let repo_a_handle = Repository::at(&repo_a).unwrap();
+ let repo_b_handle = Repository::at(&repo_b).unwrap();
+ for repo in [&repo_a_handle, &repo_b_handle] {
+ configure_test_identity(repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
}
// Add worktrees for both repos as siblings
let wt_a = base.join("alpha.feature");
let wt_b = base.join("beta.feature");
- Cmd::new("git")
- .args(["worktree", "add", &wt_a.to_string_lossy(), "-b", "feature"])
- .current_dir(&repo_a)
- .run()
+ let wt_a_str = wt_a.to_string_lossy();
+ let wt_b_str = wt_b.to_string_lossy();
+ repo_a_handle
+ .run_command(&["worktree", "add", &wt_a_str, "-b", "feature"])
.unwrap();
- Cmd::new("git")
- .args(["worktree", "add", &wt_b.to_string_lossy(), "-b", "feature"])
- .current_dir(&repo_b)
- .run()
+ repo_b_handle
+ .run_command(&["worktree", "add", &wt_b_str, "-b", "feature"])
.unwrap();
// Delete beta's worktree (simulating wt merge from another terminal)
@@ -436,25 +418,16 @@ mod tests {
let repo_dir = base.join("myrepo");
std::fs::create_dir(&repo_dir).unwrap();
git_init(&repo_dir);
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(&repo_dir)
- .run()
+ let repo = Repository::at(&repo_dir).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
// Add a worktree nested under the repo
let wt_path = repo_dir.join(".worktrees").join("feature");
std::fs::create_dir_all(wt_path.parent().unwrap()).unwrap();
- Cmd::new("git")
- .args([
- "worktree",
- "add",
- &wt_path.to_string_lossy(),
- "-b",
- "feature",
- ])
- .current_dir(&repo_dir)
- .run()
+ let wt_str = wt_path.to_string_lossy();
+ repo.run_command(&["worktree", "add", &wt_str, "-b", "feature"])
.unwrap();
// Delete the worktree
@@ -471,23 +444,14 @@ mod tests {
let repo_dir = base.join("repo");
std::fs::create_dir(&repo_dir).unwrap();
git_init(&repo_dir);
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(&repo_dir)
- .run()
+ let repo = Repository::at(&repo_dir).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
let wt_path = base.join("feature-wt");
- Cmd::new("git")
- .args([
- "worktree",
- "add",
- &wt_path.to_string_lossy(),
- "-b",
- "feature",
- ])
- .current_dir(&repo_dir)
- .run()
+ let wt_str = wt_path.to_string_lossy();
+ repo.run_command(&["worktree", "add", &wt_str, "-b", "feature"])
.unwrap();
// Delete the worktree
@@ -504,12 +468,10 @@ mod tests {
// A normal repo with a main worktree should suggest `wt switch ^`.
let tmp = tempfile::tempdir().unwrap();
git_init(tmp.path());
- Cmd::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(tmp.path())
- .run()
- .unwrap();
let repo = Repository::at(tmp.path()).unwrap();
+ configure_test_identity(&repo);
+ repo.run_command(&["commit", "--allow-empty", "-m", "init"])
+ .unwrap();
let hint = hint_for_repo(&repo);
insta::assert_snapshot!(hint.ansi_strip(), @"Current directory was removed. Try: wt switch ^");
}
diff --git a/tests/integration_tests/step_promote.rs b/tests/integration_tests/step_promote.rs
index 94860c548..b15050db4 100644
--- a/tests/integration_tests/step_promote.rs
+++ b/tests/integration_tests/step_promote.rs
@@ -4,7 +4,8 @@ use crate::common::{TestRepo, make_snapshot_cmd, repo, setup_snapshot_settings,
use insta_cmd::assert_cmd_snapshot;
use rstest::rstest;
use std::fs;
-use std::process::Command;
+use worktrunk::git::Repository;
+use worktrunk::shell_exec::Cmd;
/// Helper to get the current branch in a directory
fn branch_name(repo: &TestRepo, dir: &std::path::Path) -> String {
@@ -270,10 +271,9 @@ fn test_promote_bare_repo_no_worktrees() {
let bare_repo = temp_dir.path().join("bare.git");
// Create a bare repository
- Command::new("git")
- .args(["init", "--bare"])
- .arg(&bare_repo)
- .output()
+ Cmd::new("git")
+ .args(["init", "--bare", bare_repo.to_str().unwrap()])
+ .run()
.unwrap();
// Try to run promote in the bare repo - fails with "No worktrees found"
@@ -300,57 +300,42 @@ fn test_promote_bare_repo_with_worktrees() {
let temp_clone = temp_dir.path().join("temp");
// Create a bare repository
- Command::new("git")
- .args(["init", "--bare", "--initial-branch=main"])
- .arg(&bare_repo)
- .output()
+ Cmd::new("git")
+ .args([
+ "init",
+ "--bare",
+ "--initial-branch=main",
+ bare_repo.to_str().unwrap(),
+ ])
+ .run()
.unwrap();
// Create a commit via a temporary clone
- Command::new("git")
+ Cmd::new("git")
.args([
"clone",
bare_repo.to_str().unwrap(),
temp_clone.to_str().unwrap(),
])
- .output()
- .unwrap();
-
- Command::new("git")
- .args(["config", "user.email", "[email protected]"])
- .current_dir(&temp_clone)
- .output()
+ .run()
.unwrap();
- Command::new("git")
- .args(["config", "user.name", "Test"])
- .current_dir(&temp_clone)
- .output()
+ let clone_repo = Repository::at(&temp_clone).unwrap();
+ clone_repo
+ .run_command(&["config", "user.email", "[email protected]"])
.unwrap();
-
- Command::new("git")
- .args(["commit", "--allow-empty", "-m", "init"])
- .current_dir(&temp_clone)
- .output()
+ clone_repo
+ .run_command(&["config", "user.name", "Test"])
.unwrap();
-
- Command::new("git")
- .args(["push", "origin", "main"])
- .current_dir(&temp_clone)
- .output()
+ clone_repo
+ .run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
+ clone_repo.run_command(&["push", "origin", "main"]).unwrap();
// Add a worktree to the bare repo
- Command::new("git")
- .args([
- "--git-dir",
- bare_repo.to_str().unwrap(),
- "worktree",
- "add",
- worktree_path.to_str().unwrap(),
- "main",
- ])
- .output()
+ let bare_repo_handle = Repository::at(&bare_repo).unwrap();
+ bare_repo_handle
+ .run_command(&["worktree", "add", worktree_path.to_str().unwrap(), "main"])
.unwrap();
// Try to run promote in the bare repo - should fail with bare repo error