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.rsindex 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 testsdiff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rsindex 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.rsindex 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.rsindex 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 locallysnapshot_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.snapnew file mode 100644index 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+[1m[35mdevelop [0m [1m[35m./test-repo[0m [2mb834638e[0m [2m10 months ago[0m [2mInitial commit[0m+[2mfeature-a[0m [2m(matches main)[0m [2m./feature-a[0m [2mb834638e[0m [2m10 months ago[0m [2mInitial commit[0m+[2mfeature-b[0m [2m(matches main)[0m [2m./feature-b[0m [2mb834638e[0m [2m10 months ago[0m [2mInitial commit[0m++[2mShowing 3 worktrees[0m++----- 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.snapnew file mode 100644index 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 -----+π [36mMerging 1 commit to [1m[36mmain[0m @ [2m[SHA][0m (no commit/squash/rebase needed)++* [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature-from-develop[m[33m)[m Add feature file++ feature.txt | 1 [32m+[m+ 1 file changed, 1 insertion(+)++β [32mMerged to [1m[32mmain[0m[0m (1 commit, 1 file, [32m+1[0m)+β [32mReturned to primary at [1m[32m[REPO][0m[0m+π [36mSwitching to [1m[36mmain[0m[36m...[0m+π [36mRemoving worktree & branch...[0m++----- 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.snapnew file mode 100644index 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 -----+π [36mRebasing onto [1m[36mmain[0m[36m...[0m+β [32mRebased onto [1m[32mmain[0m[0m+π [36mMerging 1 commit to [1m[36mmain[0m @ [2m[SHA][0m (no commit/squash needed)++* [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature-dirty-primary[m[33m)[m Add feature file++ feature.txt | 1 [32m+[m+ 1 file changed, 1 insertion(+)++β [32mMerged to [1m[32mmain[0m[0m (1 commit, 1 file, [32m+1[0m)+β [32mReturned to primary at [1m[32m[REPO][0m[0m+π [36mSwitching to [1m[36mmain[0m[36m...[0m++----- 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.+Abortingdiff --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.snapnew file mode 100644index 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 -----+β [32mCreated new worktree for [1m[32mfeature-from-main[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].feature-from-main[0m[0m++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.snapnew file mode 100644index 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 -----+β [32mSwitched to worktree for [1m[32mexisting-branch[0m[32m at [1m[32m[WORKTREE_EXISTING_BRANCH][0m[0m++To enable automatic cd, run: wt config shell++----- stderr -----