Skip to content

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

test: add unit tests for branch_tracks_ref (#1758)

worktrunk-bote557bf2c4ca5
diff --git a/src/git/mod.rs b/src/git/mod.rs
index 32fe7aa40..c5d5a8647 100644
--- a/src/git/mod.rs
+++ b/src/git/mod.rs
@@ -698,6 +698,153 @@ mod tests {
assert!(branch_ref.is_remote);
}
+ /// Create a git repo with one commit and return the TempDir + repo path.
+ fn init_test_repo() -> (tempfile::TempDir, PathBuf) {
+ use crate::shell_exec::Cmd;
+
+ let tmp = tempfile::tempdir().unwrap();
+ let repo = tmp.path().join("repo");
+ Cmd::new("git")
+ .args(["init", "--initial-branch=main", repo.to_str().unwrap()])
+ .run()
+ .unwrap();
+ Cmd::new("git")
+ .args(["config", "user.email", "[email protected]"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+ Cmd::new("git")
+ .args(["config", "user.name", "Test"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+ std::fs::write(repo.join("file.txt"), "hello").unwrap();
+ Cmd::new("git")
+ .args(["add", "."])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+ Cmd::new("git")
+ .args(["commit", "-m", "init"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+ (tmp, repo)
+ }
+
+ #[test]
+ fn test_branch_tracks_ref_matching() {
+ let (_tmp, repo) = init_test_repo();
+
+ // Create a branch and set its merge config to a PR ref
+ crate::shell_exec::Cmd::new("git")
+ .args(["branch", "pr-branch"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+ crate::shell_exec::Cmd::new("git")
+ .args(["config", "branch.pr-branch.merge", "refs/pull/101/head"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+
+ assert_eq!(
+ branch_tracks_ref(&repo, "pr-branch", "refs/pull/101/head"),
+ Some(true),
+ );
+ }
+
+ #[test]
+ fn test_branch_tracks_ref_different_ref() {
+ let (_tmp, repo) = init_test_repo();
+
+ crate::shell_exec::Cmd::new("git")
+ .args(["branch", "pr-branch"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+ crate::shell_exec::Cmd::new("git")
+ .args(["config", "branch.pr-branch.merge", "refs/pull/101/head"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+
+ // Ask about a different ref — should return Some(false)
+ assert_eq!(
+ branch_tracks_ref(&repo, "pr-branch", "refs/pull/999/head"),
+ Some(false),
+ );
+ }
+
+ #[test]
+ fn test_branch_tracks_ref_no_tracking_config() {
+ let (_tmp, repo) = init_test_repo();
+
+ // Create a branch with no tracking config
+ crate::shell_exec::Cmd::new("git")
+ .args(["branch", "local-only"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+
+ // Branch exists but has no merge config — Some(false)
+ assert_eq!(
+ branch_tracks_ref(&repo, "local-only", "refs/pull/1/head"),
+ Some(false),
+ );
+ }
+
+ #[test]
+ fn test_branch_tracks_ref_nonexistent_branch() {
+ let (_tmp, repo) = init_test_repo();
+
+ // Branch doesn't exist at all — None
+ assert_eq!(
+ branch_tracks_ref(&repo, "no-such-branch", "refs/pull/1/head"),
+ None,
+ );
+ }
+
+ #[test]
+ fn test_branch_tracks_ref_invalid_repo_path() {
+ // Invalid repo path causes Cmd::run() to fail → .ok()? returns None
+ let bad_path = std::path::Path::new("/nonexistent/repo/path");
+ assert_eq!(
+ branch_tracks_ref(bad_path, "main", "refs/pull/1/head"),
+ None,
+ );
+ }
+
+ #[test]
+ fn test_branch_tracks_ref_mr_ref() {
+ let (_tmp, repo) = init_test_repo();
+
+ // Test with GitLab-style MR ref
+ crate::shell_exec::Cmd::new("git")
+ .args(["branch", "mr-branch"])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+ crate::shell_exec::Cmd::new("git")
+ .args([
+ "config",
+ "branch.mr-branch.merge",
+ "refs/merge-requests/42/head",
+ ])
+ .current_dir(&repo)
+ .run()
+ .unwrap();
+
+ assert_eq!(
+ branch_tracks_ref(&repo, "mr-branch", "refs/merge-requests/42/head"),
+ Some(true),
+ );
+ assert_eq!(
+ branch_tracks_ref(&repo, "mr-branch", "refs/pull/42/head"),
+ Some(false),
+ );
+ }
+
#[test]
fn test_branch_ref_detached_head() {
let wt = WorktreeInfo {