@@ -7,36 +7,51 @@ import (
77 "testing"
88)
99
10- func setupTestRepo (t * testing.T ) (* Client , string ) {
10+ func setupTestRepo (t * testing.T ) (* Client , string , string ) {
1111 tmpDir := t .TempDir ()
1212
13- // Create a bare repo
14- bareDir := filepath .Join (tmpDir , ".bare" )
15- os .MkdirAll (bareDir , 0755 )
16-
17- client := NewClient (bareDir )
18- client .ExecGit ("init" , "--bare" )
13+ // Step 1: Create a temporary normal repo to get initial content
14+ setupDir := filepath .Join (tmpDir , "setup" )
15+ os .MkdirAll (setupDir , 0755 )
1916
20- // Set up a main worktree with an initial commit
21- mainDir := filepath .Join (tmpDir , "main" )
22- os .MkdirAll (mainDir , 0755 )
23-
24- mainClient := NewClient (mainDir )
25- mainClient .ExecGit ("init" )
26- mainClient .ExecGit ("config" , "user.name" , "Test User" )
27- mainClient .
ExecGit (
"config" ,
"user.email" ,
"[email protected] " )
17+ setupClient := NewClient (setupDir )
18+ setupClient .ExecGit ("init" )
19+ setupClient .ExecGit ("config" , "user.name" , "Test User" )
20+ setupClient .
ExecGit (
"config" ,
"user.email" ,
"[email protected] " )
2821
2922 // Create initial commit
30- testFile := filepath .Join (mainDir , "README.md" )
23+ testFile := filepath .Join (setupDir , "README.md" )
3124 os .WriteFile (testFile , []byte ("# Test Repo\n " ), 0644 )
32- mainClient .ExecGit ("add" , "README.md" )
33- mainClient .ExecGit ("commit" , "-m" , "Initial commit" )
25+ setupClient .ExecGit ("add" , "README.md" )
26+ setupClient .ExecGit ("commit" , "-m" , "Initial commit" )
27+
28+ // Detect default branch
29+ output , _ , _ := setupClient .ExecGit ("branch" , "--show-current" )
30+ defaultBranch := strings .TrimSpace (output )
31+ if defaultBranch == "" {
32+ output , _ , _ = setupClient .ExecGit ("rev-parse" , "--abbrev-ref" , "HEAD" )
33+ defaultBranch = strings .TrimSpace (output )
34+ }
3435
35- return NewClient (tmpDir ), tmpDir
36+ // Step 2: Create bare repo from the setup repo
37+ bareDir := filepath .Join (tmpDir , ".bare" )
38+ setupClient .ExecGit ("clone" , "--bare" , setupDir , bareDir )
39+
40+ // Step 3: Create first worktree from bare repo
41+ mainDir := filepath .Join (tmpDir , defaultBranch )
42+ bareClient := NewClient (bareDir )
43+ bareClient .ExecGit ("worktree" , "add" , mainDir , defaultBranch )
44+
45+ // Return client pointing to the project root (tmpDir) which contains .bare/
46+ return NewClient (tmpDir ), tmpDir , defaultBranch
3647}
3748
3849func TestWorktreeAdd (t * testing.T ) {
39- client , tmpDir := setupTestRepo (t )
50+ _ , tmpDir , defaultBranch := setupTestRepo (t )
51+
52+ // Use bare repo client for worktree commands
53+ bareDir := filepath .Join (tmpDir , ".bare" )
54+ bareClient := NewClient (bareDir )
4055
4156 tests := []struct {
4257 name string
@@ -49,14 +64,17 @@ func TestWorktreeAdd(t *testing.T) {
4964 name : "add new worktree" ,
5065 path : filepath .Join (tmpDir , "feature" ),
5166 branch : "feature/test" ,
52- track : true ,
67+ track : false , // Match production usage: branch pre-created, track=false
5368 wantErr : false ,
5469 },
5570 }
5671
5772 for _ , tt := range tests {
5873 t .Run (tt .name , func (t * testing.T ) {
59- err := client .WorktreeAdd (tt .path , tt .branch , tt .track )
74+ // Pre-create the branch (matches production workflow in branch.go)
75+ bareClient .ExecGit ("branch" , tt .branch , defaultBranch )
76+
77+ err := bareClient .WorktreeAdd (tt .path , tt .branch , tt .track )
6078 if (err != nil ) != tt .wantErr {
6179 t .Errorf ("WorktreeAdd() error = %v, wantErr %v" , err , tt .wantErr )
6280 }
@@ -65,13 +83,18 @@ func TestWorktreeAdd(t *testing.T) {
6583}
6684
6785func TestWorktreeList (t * testing.T ) {
68- client , tmpDir := setupTestRepo (t )
86+ _ , tmpDir , defaultBranch := setupTestRepo (t )
87+
88+ // Use bare repo client
89+ bareDir := filepath .Join (tmpDir , ".bare" )
90+ bareClient := NewClient (bareDir )
6991
70- // Add a worktree first
92+ // Add a worktree first (match production workflow: create branch, then worktree)
7193 featurePath := filepath .Join (tmpDir , "feature" )
72- client .WorktreeAdd (featurePath , "feature/test" , true )
94+ bareClient .ExecGit ("branch" , "feature/test" , defaultBranch )
95+ bareClient .WorktreeAdd (featurePath , "feature/test" , false )
7396
74- worktrees , err := client .WorktreeList ()
97+ worktrees , err := bareClient .WorktreeList ()
7598 if err != nil {
7699 t .Fatalf ("WorktreeList() error = %v" , err )
77100 }
@@ -94,20 +117,25 @@ func TestWorktreeList(t *testing.T) {
94117}
95118
96119func TestWorktreeRemove (t * testing.T ) {
97- client , tmpDir := setupTestRepo (t )
120+ _ , tmpDir , defaultBranch := setupTestRepo (t )
98121
99- // Add a worktree first
122+ // Use bare repo client
123+ bareDir := filepath .Join (tmpDir , ".bare" )
124+ bareClient := NewClient (bareDir )
125+
126+ // Add a worktree first (match production workflow: create branch, then worktree)
100127 featurePath := filepath .Join (tmpDir , "feature" )
101- client .WorktreeAdd (featurePath , "feature/test" , true )
128+ bareClient .ExecGit ("branch" , "feature/test" , defaultBranch )
129+ bareClient .WorktreeAdd (featurePath , "feature/test" , false )
102130
103131 // Now remove it
104- err := client .WorktreeRemove (featurePath )
132+ err := bareClient .WorktreeRemove (featurePath )
105133 if err != nil {
106134 t .Errorf ("WorktreeRemove() error = %v" , err )
107135 }
108136
109137 // Verify it's removed
110- worktrees , _ := client .WorktreeList ()
138+ worktrees , _ := bareClient .WorktreeList ()
111139 for _ , wt := range worktrees {
112140 if strings .Contains (wt , "feature" ) {
113141 t .Error ("WorktreeRemove() did not remove the worktree" )
@@ -116,10 +144,14 @@ func TestWorktreeRemove(t *testing.T) {
116144}
117145
118146func TestWorktreePrune (t * testing.T ) {
119- client , _ := setupTestRepo (t )
147+ _ , tmpDir , _ := setupTestRepo (t )
148+
149+ // Use bare repo client
150+ bareDir := filepath .Join (tmpDir , ".bare" )
151+ bareClient := NewClient (bareDir )
120152
121153 // Prune should not error even if nothing to prune
122- err := client .WorktreePrune ()
154+ err := bareClient .WorktreePrune ()
123155 if err != nil {
124156 t .Errorf ("WorktreePrune() error = %v" , err )
125157 }
0 commit comments