-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjourney_remote_test.go
More file actions
461 lines (376 loc) · 15 KB
/
Copy pathjourney_remote_test.go
File metadata and controls
461 lines (376 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package e2e
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// setupRemoteRepo creates a bare "origin" repository with remote branches
func setupRemoteRepo(t *testing.T, tempDir string) string {
t.Helper()
// Create bare origin repository
originPath := filepath.Join(tempDir, "origin.git")
cmd := exec.Command("git", "init", "--bare", originPath)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to create bare origin: %v", err)
}
// Set default branch to main
cmd = exec.Command("git", "symbolic-ref", "HEAD", "refs/heads/main")
cmd.Dir = originPath
_ = cmd.Run()
// Create a working repo and push some branches
workPath := filepath.Join(tempDir, "work")
cmd = exec.Command("git", "clone", originPath, workPath)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to clone origin: %v", err)
}
// Configure git user
cmd = exec.Command("git", "config", "user.email", "test@example.com")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "config", "user.name", "Test User")
cmd.Dir = workPath
_ = cmd.Run()
// Create initial commit on main
readmePath := filepath.Join(workPath, "README.md")
_ = os.WriteFile(readmePath, []byte("# Test"), 0644)
cmd = exec.Command("git", "add", ".")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "commit", "-m", "initial")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "push", "origin", "main")
cmd.Dir = workPath
_ = cmd.Run()
// Create feature/remote branch
cmd = exec.Command("git", "checkout", "-b", "feature/remote")
cmd.Dir = workPath
_ = cmd.Run()
featurePath := filepath.Join(workPath, "feature.txt")
_ = os.WriteFile(featurePath, []byte("feature"), 0644)
cmd = exec.Command("git", "add", ".")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "commit", "-m", "feature")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "push", "origin", "feature/remote")
cmd.Dir = workPath
_ = cmd.Run()
return originPath
}
// TestAddRemoteBranch tests adding a remote branch as worktree
func TestAddRemoteBranch(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "remote-add")
originPath := setupRemoteRepo(t, tempDir)
// Clone with baretree
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
// Configure fetch refspec (needed for bare clone)
bareDir := filepath.Join(projectDir, ".git")
cmd := exec.Command("git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
cmd.Dir = bareDir
_ = cmd.Run()
cmd = exec.Command("git", "fetch", "origin")
cmd.Dir = bareDir
_ = cmd.Run()
// Delete local branch to ensure we test remote tracking
// (bare clone may have created local branches)
cmd = exec.Command("git", "branch", "-D", "feature/remote")
cmd.Dir = bareDir
_ = cmd.Run() // Ignore error if branch doesn't exist
t.Run("add remote branch auto-detect", func(t *testing.T) {
stdout := runBtSuccess(t, projectDir, "add", "feature/remote")
assertOutputContains(t, stdout, "Tracking remote branch")
assertOutputContains(t, stdout, "origin/feature/remote")
assertOutputContains(t, stdout, "Worktree created")
// Verify worktree exists
assertFileExists(t, filepath.Join(projectDir, "feature", "remote"))
// Verify tracking is set up
cmd := exec.Command("git", "branch", "-vv")
cmd.Dir = bareDir
output, _ := cmd.Output()
assertOutputContains(t, string(output), "origin/feature/remote")
})
}
// TestAddRemoteBranchExplicit tests adding a remote branch with explicit remote/branch format
func TestAddRemoteBranchExplicit(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "remote-explicit")
originPath := setupRemoteRepo(t, tempDir)
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
// Configure fetch refspec
bareDir := filepath.Join(projectDir, ".git")
cmd := exec.Command("git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
cmd.Dir = bareDir
_ = cmd.Run()
cmd = exec.Command("git", "fetch", "origin")
cmd.Dir = bareDir
_ = cmd.Run()
t.Run("add with explicit origin/branch format", func(t *testing.T) {
stdout := runBtSuccess(t, projectDir, "add", "origin/feature/remote")
assertOutputContains(t, stdout, "Worktree created")
assertFileExists(t, filepath.Join(projectDir, "feature", "remote"))
})
}
// TestAddAutoFetch tests that auto-fetch is the default when remotes are configured
func TestAddAutoFetch(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "remote-fetch")
originPath := setupRemoteRepo(t, tempDir)
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
// Configure fetch refspec
bareDir := filepath.Join(projectDir, ".git")
cmd := exec.Command("git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
cmd.Dir = bareDir
_ = cmd.Run()
// Create a new branch on origin after clone
workPath := filepath.Join(tempDir, "work")
cmd = exec.Command("git", "checkout", "-b", "feature/new-after-clone")
cmd.Dir = workPath
_ = cmd.Run()
newFilePath := filepath.Join(workPath, "new.txt")
_ = os.WriteFile(newFilePath, []byte("new"), 0644)
cmd = exec.Command("git", "add", ".")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "commit", "-m", "new feature")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "push", "origin", "feature/new-after-clone")
cmd.Dir = workPath
_ = cmd.Run()
t.Run("auto-fetch gets new remote branches by default", func(t *testing.T) {
stdout := runBtSuccess(t, projectDir, "add", "feature/new-after-clone")
assertOutputContains(t, stdout, "Fetching from remotes")
assertOutputContains(t, stdout, "Tracking remote branch")
assertOutputContains(t, stdout, "Worktree created")
assertFileExists(t, filepath.Join(projectDir, "feature", "new-after-clone"))
})
}
// TestAddNoFetch tests the --no-fetch option skips auto-fetch
func TestAddNoFetch(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "no-fetch")
originPath := setupRemoteRepo(t, tempDir)
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
// Configure fetch refspec
bareDir := filepath.Join(projectDir, ".git")
cmd := exec.Command("git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
cmd.Dir = bareDir
_ = cmd.Run()
// Create a new branch on origin after clone (not yet fetched)
workPath := filepath.Join(tempDir, "work")
cmd = exec.Command("git", "checkout", "-b", "feature/unfetched")
cmd.Dir = workPath
_ = cmd.Run()
newFilePath := filepath.Join(workPath, "unfetched.txt")
_ = os.WriteFile(newFilePath, []byte("unfetched"), 0644)
cmd = exec.Command("git", "add", ".")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "commit", "-m", "unfetched feature")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "push", "origin", "feature/unfetched")
cmd.Dir = workPath
_ = cmd.Run()
t.Run("no-fetch skips auto-fetch so branch is not found", func(t *testing.T) {
_, stderr := runBtExpectError(t, projectDir, "add", "--no-fetch", "feature/unfetched")
assertOutputNotContains(t, stderr, "Fetching from remotes")
assertOutputContains(t, stderr, "not found")
})
}
// TestAddUpstreamBehindWarning tests the upstream behind warning in non-TTY mode
func TestAddUpstreamBehindWarning(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "upstream-behind")
originPath := setupRemoteRepo(t, tempDir)
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
bareDir := filepath.Join(projectDir, ".git")
// Configure fetch refspec and fetch
cmd := exec.Command("git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
cmd.Dir = bareDir
_ = cmd.Run()
cmd = exec.Command("git", "fetch", "origin")
cmd.Dir = bareDir
_ = cmd.Run()
// Set upstream for main branch
cmd = exec.Command("git", "config", "branch.main.remote", "origin")
cmd.Dir = bareDir
_ = cmd.Run()
cmd = exec.Command("git", "config", "branch.main.merge", "refs/heads/main")
cmd.Dir = bareDir
_ = cmd.Run()
// Push a new commit to origin after clone
workPath := filepath.Join(tempDir, "work")
cmd = exec.Command("git", "checkout", "main")
cmd.Dir = workPath
_ = cmd.Run()
newFilePath := filepath.Join(workPath, "extra.txt")
_ = os.WriteFile(newFilePath, []byte("extra"), 0644)
cmd = exec.Command("git", "add", ".")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "commit", "-m", "extra commit")
cmd.Dir = workPath
_ = cmd.Run()
cmd = exec.Command("git", "push", "origin", "main")
cmd.Dir = workPath
_ = cmd.Run()
t.Run("aborts when default branch is behind upstream without force", func(t *testing.T) {
// auto-fetch will update remote refs, making local main behind origin/main
stdout, stderr := runBtExpectError(t, projectDir, "add", "-b", "feat/behind-test")
// Should show warning about being behind
combined := stdout + stderr
assertOutputContains(t, combined, "Warning: 'main' is")
assertOutputContains(t, combined, "behind its upstream")
})
t.Run("force skips behind check", func(t *testing.T) {
// --force should skip the behind check and create the worktree
stdout := runBtSuccess(t, projectDir, "add", "-b", "feat/behind-force", "--force")
// Should not show warning
assertOutputNotContains(t, stdout, "Warning:")
assertOutputContains(t, stdout, "Worktree created")
})
}
// TestAddBranchNotFound tests error when branch doesn't exist
func TestAddBranchNotFound(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "branch-not-found")
runBtSuccess(t, tempDir, "repo", "init", "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
t.Run("add non-existent branch shows helpful error", func(t *testing.T) {
_, stderr := runBtExpectError(t, projectDir, "add", "nonexistent-branch")
assertOutputContains(t, stderr, "not found")
assertOutputContains(t, stderr, "bt add -b nonexistent-branch")
})
}
// TestAddLocalBranchPriority tests that local branches take priority
func TestAddLocalBranchPriority(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "local-priority")
originPath := setupRemoteRepo(t, tempDir)
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
// Configure fetch refspec and fetch
bareDir := filepath.Join(projectDir, ".git")
cmd := exec.Command("git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
cmd.Dir = bareDir
_ = cmd.Run()
cmd = exec.Command("git", "fetch", "origin")
cmd.Dir = bareDir
_ = cmd.Run()
// Create a local branch with different content
cmd = exec.Command("git", "branch", "feature/remote")
cmd.Dir = bareDir
_ = cmd.Run()
t.Run("local branch takes priority over remote", func(t *testing.T) {
stdout := runBtSuccess(t, projectDir, "add", "feature/remote")
// Should NOT say "Tracking remote branch" since local exists
assertOutputNotContains(t, stdout, "Tracking remote branch")
assertOutputContains(t, stdout, "Worktree created")
})
}
// TestAddNewBranchWithRemoteBase tests that --base with a remote-only branch
// correctly resolves the branch and creates the intended new branch name
func TestAddNewBranchWithRemoteBase(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "remote-base")
originPath := setupRemoteRepo(t, tempDir)
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
// Configure fetch refspec and fetch
bareDir := filepath.Join(projectDir, ".git")
cmd := exec.Command("git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
cmd.Dir = bareDir
_ = cmd.Run()
cmd = exec.Command("git", "fetch", "origin")
cmd.Dir = bareDir
_ = cmd.Run()
// Delete local feature/remote branch to ensure it's remote-only
cmd = exec.Command("git", "branch", "-D", "feature/remote")
cmd.Dir = bareDir
_ = cmd.Run()
t.Run("new branch based on remote-only branch", func(t *testing.T) {
stdout := runBtSuccess(t, projectDir, "add", "-b", "feat/new", "--base", "feature/remote")
// Should show the resolved remote ref as base
assertOutputContains(t, stdout, "Based on 'origin/feature/remote (remote)'")
assertOutputContains(t, stdout, "Worktree created")
// Verify worktree was created with correct branch name
assertFileExists(t, filepath.Join(projectDir, "feat", "new"))
// Verify the branch name is feat/new, not feature/remote (DWIM bug fix)
cmd := exec.Command("git", "branch", "--list", "feat/new")
cmd.Dir = bareDir
output, err := cmd.Output()
if err != nil || !strings.Contains(string(output), "feat/new") {
t.Errorf("expected branch 'feat/new' to exist, got: %s", string(output))
}
})
}
// TestAddNewBranchWithLocalBase tests --base with a local branch
func TestAddNewBranchWithLocalBase(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "local-base")
originPath := setupRemoteRepo(t, tempDir)
runBtSuccess(t, tempDir, "repo", "clone", originPath, "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
t.Run("new branch based on local main", func(t *testing.T) {
stdout := runBtSuccess(t, projectDir, "add", "-b", "feat/from-main", "--base", "main")
assertOutputContains(t, stdout, "Based on 'main (local)'")
assertOutputContains(t, stdout, "Worktree created")
assertFileExists(t, filepath.Join(projectDir, "feat", "from-main"))
})
}
// TestAddNewBranchWithNonexistentBase tests --base with a branch that doesn't exist
func TestAddNewBranchWithNonexistentBase(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "nonexistent-base")
runBtSuccess(t, tempDir, "repo", "init", "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
t.Run("error when base branch does not exist", func(t *testing.T) {
_, stderr := runBtExpectError(t, projectDir, "add", "-b", "feat/new", "--base", "nonexistent")
assertOutputContains(t, stderr, "base branch 'nonexistent' not found")
})
}
// TestAddNewBranchShowsBaseInfo tests that creating a new branch shows base information
func TestAddNewBranchShowsBaseInfo(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
tempDir := createTempDir(t, "base-info")
runBtSuccess(t, tempDir, "repo", "init", "test-repo")
projectDir := filepath.Join(tempDir, "test-repo")
t.Run("new branch without --base shows HEAD", func(t *testing.T) {
stdout := runBtSuccess(t, projectDir, "add", "-b", "feat/no-base")
assertOutputContains(t, stdout, "Based on HEAD (main)")
assertOutputContains(t, stdout, "Worktree created")
})
}