Skip to content

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

feat: Add tests for primary worktree on non-main branch

Maximilian Roosd172200b81ee
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 6fb99a9f7..e6a52280b 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -420,6 +420,33 @@ impl TestRepo {
.expect("Failed to check origin/HEAD");
output.status.success()
}
+
+ /// Switch the primary worktree to a different branch
+ ///
+ /// 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));
+ }
+
+ /// 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()
+ .expect("Failed to get current branch");
+ String::from_utf8_lossy(&output.stdout).trim().to_string()
+ }
}
/// Create configured insta Settings for snapshot tests
diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rs
index 4eea58440..07a58fe3c 100644
--- a/tests/integration_tests/list.rs
+++ b/tests/integration_tests/list.rs
@@ -597,3 +597,18 @@ fn test_list_with_upstream_tracking() {
assert_cmd_snapshot!("with_upstream_tracking", cmd);
});
}
+
+#[test]
+fn test_list_primary_on_different_branch() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ repo.switch_primary_to("develop");
+ assert_eq!(repo.current_branch(), "develop");
+
+ repo.add_worktree("feature-a", "feature-a");
+ repo.add_worktree("feature-b", "feature-b");
+
+ snapshot_list("list_primary_on_different_branch", &repo);
+}
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index ec98db44d..99de0d7e7 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -2131,3 +2131,113 @@ fn test_merge_no_commits_with_changes() {
Some(&feature_wt),
);
}
+
+#[test]
+fn test_merge_primary_on_different_branch() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ repo.switch_primary_to("develop");
+ assert_eq!(repo.current_branch(), "develop");
+
+ // Create a feature worktree and make a commit
+ let feature_wt = repo.add_worktree("feature-from-develop", "feature-from-develop");
+ 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");
+
+ snapshot_merge(
+ "merge_primary_on_different_branch",
+ &repo,
+ &["main"],
+ Some(&feature_wt),
+ );
+
+ // Verify primary switched to main after merge
+ assert_eq!(repo.current_branch(), "main");
+}
+
+#[test]
+fn test_merge_primary_on_different_branch_dirty() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Make main and develop diverge - modify file.txt on main
+ fs::write(repo.root_path().join("file.txt"), "main version").expect("Failed to modify file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "file.txt"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Update file on main"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to commit");
+
+ // Create a develop branch from the previous commit (before the main update)
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ let output = cmd
+ .args(["rev-parse", "HEAD~1"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to get previous commit");
+ let base_commit = String::from_utf8_lossy(&output.stdout).trim().to_string();
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["switch", "-c", "develop", &base_commit])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to create develop branch");
+
+ // Modify file.txt in develop (uncommitted) to a different value
+ // This will conflict when trying to switch to main
+ fs::write(repo.root_path().join("file.txt"), "develop local changes")
+ .expect("Failed to modify file");
+
+ // Create a feature worktree and make a commit
+ let feature_wt = repo.add_worktree("feature-dirty-primary", "feature-dirty-primary");
+ 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");
+
+ // Try to merge to main - should fail because primary has uncommitted changes that conflict
+ snapshot_merge(
+ "merge_primary_on_different_branch_dirty",
+ &repo,
+ &["main"],
+ Some(&feature_wt),
+ );
+}
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 4cf4ee45c..d45357e22 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -391,3 +391,29 @@ fn test_switch_create_no_remote() {
// Create a branch without specifying base - should infer default branch locally
snapshot_switch("switch_create_no_remote", &repo, &["--create", "feature"]);
}
+
+#[test]
+fn test_switch_primary_on_different_branch() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ repo.switch_primary_to("develop");
+ assert_eq!(repo.current_branch(), "develop");
+
+ // Create a feature worktree using the default branch (main)
+ // This should work fine even though primary is on develop
+ snapshot_switch(
+ "switch_primary_on_different_branch",
+ &repo,
+ &["--create", "feature-from-main"],
+ );
+
+ // Also test switching to an existing branch
+ repo.add_worktree("existing-branch", "existing-branch");
+ snapshot_switch(
+ "switch_to_existing_primary_on_different_branch",
+ &repo,
+ &["existing-branch"],
+ );
+}
diff --git a/tests/snapshots/integration__integration_tests__list__list_primary_on_different_branch.snap b/tests/snapshots/integration__integration_tests__list__list_primary_on_different_branch.snap
new file mode 100644
index 000000000..a74ad162c
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__list__list_primary_on_different_branch.snap
@@ -0,0 +1,30 @@
+---
+source: tests/integration_tests/list.rs
+info:
+ program: wt
+ args:
+ - list
+ 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/.tmpmgEt2q/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+Branch State Path Commit Age Message
+develop  ./test-repo b834638e 10 months ago Initial commit
+feature-a (matches main) ./feature-a b834638e 10 months ago Initial commit
+feature-b (matches main) ./feature-b b834638e 10 months ago Initial commit
+
+Showing 3 worktrees
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_primary_on_different_branch.snap b/tests/snapshots/integration__integration_tests__merge__merge_primary_on_different_branch.snap
new file mode 100644
index 000000000..6656b9991
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_primary_on_different_branch.snap
@@ -0,0 +1,36 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - merge
+ - main
+ 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/.tmpecpr1s/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+πŸ”„ Merging 1 commit to main @ [SHA] (no commit/squash/rebase needed)
+
+* [SHA] (HEAD -> feature-from-develop) Add feature file
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+βœ… Merged to main (1 commit, 1 file, +1)
+βœ… Returned to primary at [REPO]
+πŸ”„ Switching to main...
+πŸ”„ Removing worktree & branch...
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_primary_on_different_branch_dirty.snap b/tests/snapshots/integration__integration_tests__merge__merge_primary_on_different_branch_dirty.snap
new file mode 100644
index 000000000..92f8a403e
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_primary_on_different_branch_dirty.snap
@@ -0,0 +1,41 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - merge
+ - main
+ 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/.tmp25DBdp/test-config.toml
+---
+success: false
+exit_code: 1
+----- stdout -----
+πŸ”„ Rebasing onto main...
+βœ… Rebased onto main
+πŸ”„ Merging 1 commit to main @ [SHA] (no commit/squash needed)
+
+* [SHA] (HEAD -> feature-dirty-primary) Add feature file
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+βœ… Merged to main (1 commit, 1 file, +1)
+βœ… Returned to primary at [REPO]
+πŸ”„ Switching to main...
+
+----- stderr -----
+Failed to switch to 'main': error: Your local changes to the following files would be overwritten by checkout:
+ file.txt
+Please commit your changes or stash them before you switch branches.
+Aborting
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_primary_on_different_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_primary_on_different_branch.snap
new file mode 100644
index 000000000..da1649db3
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_primary_on_different_branch.snap
@@ -0,0 +1,29 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - feature-from-main
+ 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/.tmpYEQ4Tj/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+βœ… Created new worktree for feature-from-main from main at [REPO].feature-from-main
+
+To enable automatic cd, run: wt config shell
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_to_existing_primary_on_different_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_to_existing_primary_on_different_branch.snap
new file mode 100644
index 000000000..e55d72aa9
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_to_existing_primary_on_different_branch.snap
@@ -0,0 +1,28 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - existing-branch
+ 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/.tmpJZ0ptu/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+βœ… Switched to worktree for existing-branch at [WORKTREE_EXISTING_BRANCH]
+
+To enable automatic cd, run: wt config shell
+
+----- stderr -----