-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinit_test.go
More file actions
800 lines (685 loc) · 27.3 KB
/
Copy pathinit_test.go
File metadata and controls
800 lines (685 loc) · 27.3 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
package cmd
import (
"fmt"
"io"
"os"
"testing"
"github.com/github/gh-stack/internal/config"
"github.com/github/gh-stack/internal/git"
"github.com/github/gh-stack/internal/github"
"github.com/github/gh-stack/internal/stack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// collectOutput closes the write ends of the test config pipes and returns
// the captured stderr content. Shared across cmd test files.
func collectOutput(cfg *config.Config, outR, errR *os.File) string {
cfg.Out.Close()
cfg.Err.Close()
stderr, _ := io.ReadAll(errR)
outR.Close()
errR.Close()
return string(stderr)
}
func TestInit_CreatesStackWithCorrectTrunk(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"myBranch"}})
output := collectOutput(cfg, outR, errR)
require.NotContains(t, output, "\u2717", "unexpected error in output")
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
require.Len(t, sf.Stacks, 1)
s := sf.Stacks[0]
assert.Equal(t, "main", s.Trunk.Branch)
names := s.BranchNames()
require.Len(t, names, 1)
assert.Equal(t, "myBranch", names[0])
}
func TestInit_CustomTrunk(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"myBranch"}, base: "develop"})
output := collectOutput(cfg, outR, errR)
require.NotContains(t, output, "\u2717", "unexpected error")
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
assert.Equal(t, "develop", sf.Stacks[0].Trunk.Branch)
}
func TestInit_AdoptExistingBranches(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return true },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{
branches: []string{"b1", "b2", "b3"},
adopt: true,
})
output := collectOutput(cfg, outR, errR)
require.NotContains(t, output, "\u2717", "unexpected error")
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
names := sf.Stacks[0].BranchNames()
assert.Equal(t, []string{"b1", "b2", "b3"}, names)
}
func TestInit_PrefixStoredInStack(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"myBranch"}, prefix: "feat"})
collectOutput(cfg, outR, errR)
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
assert.Equal(t, "feat", sf.Stacks[0].Prefix)
}
func TestInit_PrefixAppliedToExplicitBranches(t *testing.T) {
gitDir := t.TempDir()
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"b1", "b2"}, prefix: "feat"})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err, "runInit should succeed")
require.NotContains(t, output, "\u2717", "unexpected error")
assert.Equal(t, []string{"feat/b1", "feat/b2"}, created, "branches should be created with prefix")
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
names := sf.Stacks[0].BranchNames()
assert.Equal(t, []string{"feat/b1", "feat/b2"}, names, "stack should store prefixed branch names")
}
func TestInit_InvalidPrefixRejectedBeforeBranchCreation(t *testing.T) {
gitDir := t.TempDir()
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
ValidateRefNameFn: func(name string) error {
return fmt.Errorf("invalid ref name: %s", name)
},
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"mybranch"}, prefix: "bad..prefix"})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs, "should reject invalid prefix")
assert.Contains(t, output, "invalid prefix")
assert.Empty(t, created, "no branches should be created when prefix is invalid")
}
func TestInit_AdoptFlagShowsDeprecationWarning(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return true },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "--adopt flag is deprecated")
}
func TestInit_RerereAlreadyEnabled(t *testing.T) {
gitDir := t.TempDir()
enableRerereCalled := false
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
IsRerereEnabledFn: func() (bool, error) { return true, nil },
EnableRerereFn: func() error {
enableRerereCalled = true
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"b1"}})
collectOutput(cfg, outR, errR)
assert.False(t, enableRerereCalled, "EnableRerere should not be called when rerere is already enabled")
}
func TestInit_RefuseIfBranchAlreadyInStack(t *testing.T) {
gitDir := t.TempDir()
// Pre-create stack file with "feature-1" as a non-trunk branch
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "feature-1"}},
}},
}
require.NoError(t, stack.Save(gitDir, sf), "saving seed stack")
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "feature-1", nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"newBranch"}})
output := collectOutput(cfg, outR, errR)
assert.Contains(t, output, "already part of a stack")
}
func TestInit_AdoptNonexistentBranch_CreatesIt(t *testing.T) {
// --adopt with missing branch now creates it (no error, just a deprecation warning)
gitDir := t.TempDir()
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return false },
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"nonexistent"}, adopt: true})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "--adopt flag is deprecated")
assert.Equal(t, []string{"nonexistent"}, created)
}
func TestInit_MultipleBranches_CreatesAll(t *testing.T) {
gitDir := t.TempDir()
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"b1", "b2", "b3"}})
output := collectOutput(cfg, outR, errR)
require.NotContains(t, output, "\u2717", "unexpected error")
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
names := sf.Stacks[0].BranchNames()
assert.Equal(t, []string{"b1", "b2", "b3"}, names)
}
func TestInit_AdoptWithExistingOpenPR(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return true },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
if branch == "b1" {
return &github.PullRequest{
Number: 42,
ID: "PR_42",
URL: "https://github.com/owner/repo/pull/42",
State: "OPEN",
HeadRefName: "b1",
}, nil
}
return nil, nil
},
}
err := runInit(cfg, &initOptions{
branches: []string{"b1", "b2"},
adopt: true,
})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err, "adopt should succeed even when branch has an open PR")
require.NotContains(t, output, "\u2717", "unexpected error in output")
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
require.Len(t, sf.Stacks, 1)
// b1 should have the open PR recorded
b1 := sf.Stacks[0].Branches[0]
require.NotNil(t, b1.PullRequest, "open PR should be recorded")
assert.Equal(t, 42, b1.PullRequest.Number)
assert.Equal(t, "https://github.com/owner/repo/pull/42", b1.PullRequest.URL)
// b2 should have no PR
b2 := sf.Stacks[0].Branches[1]
assert.Nil(t, b2.PullRequest, "branch without PR should have nil PullRequest")
}
func TestInit_AdoptIgnoresClosedAndMergedPRs(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return true },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
// FindPRForBranch only returns OPEN PRs — closed/merged PRs won't be
// returned by the API, so the mock returns nil for all branches.
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return nil, nil
},
}
err := runInit(cfg, &initOptions{
branches: []string{"b1", "b2"},
adopt: true,
})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err, "adopt should succeed when branches have closed/merged PRs")
require.NotContains(t, output, "\u2717", "unexpected error in output")
sf, err := stack.Load(gitDir)
require.NoError(t, err, "loading stack")
require.Len(t, sf.Stacks, 1)
// Neither branch should have a PR recorded (closed/merged are filtered out)
for _, b := range sf.Stacks[0].Branches {
assert.Nil(t, b.PullRequest, "closed/merged PRs should not be recorded for branch %s", b.Branch)
}
}
// --- Tests for spec scenarios ---
func TestInit_ImplicitAdopt_AllExist(t *testing.T) {
// Scenario 8: all branches exist → implicit adopt, PR detection runs
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return true },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
if branch == "b1" {
return &github.PullRequest{Number: 10, ID: "PR_10", URL: "https://example.com/10"}, nil
}
return nil, nil
},
}
err := runInit(cfg, &initOptions{branches: []string{"b1", "b2", "b3"}})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
require.NotContains(t, output, "\u2717")
assert.Contains(t, output, "Adopted")
assert.Contains(t, output, "Found PRs for 1 of 3")
sf, _ := stack.Load(gitDir)
assert.Equal(t, []string{"b1", "b2", "b3"}, sf.Stacks[0].BranchNames())
assert.NotNil(t, sf.Stacks[0].Branches[0].PullRequest)
}
func TestInit_ImplicitAdopt_AllMissing(t *testing.T) {
// Scenario 7: all branches missing → create all
gitDir := t.TempDir()
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"b1", "b2", "b3"}})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
require.NotContains(t, output, "\u2717")
assert.Contains(t, output, "Created stack")
assert.NotContains(t, output, "Adopted")
assert.Equal(t, []string{"b1", "b2", "b3"}, created)
}
func TestInit_ImplicitAdopt_Mixed(t *testing.T) {
// Scenario 11: mixed → adopts existing, creates missing
gitDir := t.TempDir()
existing := map[string]bool{"existing1": true, "existing2": true}
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(name string) bool { return existing[name] },
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"existing1", "new1", "existing2"}})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
require.NotContains(t, output, "\u2717")
assert.Contains(t, output, "Adopted")
assert.Equal(t, []string{"new1"}, created)
sf, _ := stack.Load(gitDir)
assert.Equal(t, []string{"existing1", "new1", "existing2"}, sf.Stacks[0].BranchNames())
}
func TestInit_PrefixDetection_ArgsCommonPrefix(t *testing.T) {
// Explicit branch names with a common prefix should NOT auto-detect
// a prefix — the slash is part of the branch name, not a convention.
// Users who want a prefix should use --prefix.
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error { return nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"feat/a", "feat/b", "feat/c"}})
collectOutput(cfg, outR, errR)
require.NoError(t, err)
sf, _ := stack.Load(gitDir)
assert.Equal(t, "", sf.Stacks[0].Prefix)
}
func TestInit_PrefixDetection_ArgsMixedPrefix(t *testing.T) {
// Scenario 10: args mixed prefixes → no prefix
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error { return nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"feat/a", "bug/b"}})
collectOutput(cfg, outR, errR)
require.NoError(t, err)
sf, _ := stack.Load(gitDir)
assert.Equal(t, "", sf.Stacks[0].Prefix)
}
func TestInit_PrefixDetection_ArgsNoSlash(t *testing.T) {
// No slashes → no prefix
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error { return nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"auth", "api", "ui"}})
collectOutput(cfg, outR, errR)
require.NoError(t, err)
sf, _ := stack.Load(gitDir)
assert.Equal(t, "", sf.Stacks[0].Prefix)
}
func TestInit_PrefixDetection_NestedPrefix(t *testing.T) {
// Explicit branch names with nested slashes should NOT auto-detect
// a prefix — the user typed the full branch name deliberately.
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error { return nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"sameen/feat/a", "sameen/feat/b"}})
collectOutput(cfg, outR, errR)
require.NoError(t, err)
sf, _ := stack.Load(gitDir)
assert.Equal(t, "", sf.Stacks[0].Prefix)
}
func TestInit_ExplicitPrefixSkipsDetection(t *testing.T) {
// Scenario 14: --prefix with args → explicit wins
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error { return nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"b1", "b2"}, prefix: "foo"})
collectOutput(cfg, outR, errR)
require.NoError(t, err)
sf, _ := stack.Load(gitDir)
assert.Equal(t, "foo", sf.Stacks[0].Prefix)
assert.Equal(t, []string{"foo/b1", "foo/b2"}, sf.Stacks[0].BranchNames())
}
func TestInit_WhatsNext_Fresh(t *testing.T) {
// Scenario 17: fresh single-branch → fresh format
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error { return nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"my-feature"}})
output := collectOutput(cfg, outR, errR)
assert.Contains(t, output, "Created stack")
assert.Contains(t, output, "main ← my-feature")
assert.Contains(t, output, "top of stack")
assert.Contains(t, output, "What's next:")
assert.Contains(t, output, "gh stack add")
assert.Contains(t, output, "gh stack view")
assert.Contains(t, output, "gh stack submit")
assert.NotContains(t, output, "Adopted")
}
func TestInit_WhatsNext_AdoptedWithPRs(t *testing.T) {
// Scenario 18: adopted multi-branch, 2 of 3 PRs → adopt format with PR count
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return true },
})
defer restore()
prBranches := map[string]bool{"b1": true, "b3": true}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
if prBranches[branch] {
return &github.PullRequest{Number: 1, ID: "PR_1", URL: "https://example.com/1"}, nil
}
return nil, nil
},
}
runInit(cfg, &initOptions{branches: []string{"b1", "b2", "b3"}})
output := collectOutput(cfg, outR, errR)
assert.Contains(t, output, "Adopted 3 branches")
assert.Contains(t, output, "Found PRs for 2 of 3")
assert.Contains(t, output, "What's next:")
assert.Contains(t, output, "gh stack view")
assert.Contains(t, output, "gh stack switch")
assert.Contains(t, output, "gh stack submit")
}
func TestInit_WhatsNext_AdoptedNoPRs(t *testing.T) {
// Scenario 19: adopted, 0 PRs → no PR summary line
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(string) bool { return true },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return nil, nil
},
}
runInit(cfg, &initOptions{branches: []string{"b1", "b2"}})
output := collectOutput(cfg, outR, errR)
assert.Contains(t, output, "Adopted 2 branches")
assert.NotContains(t, output, "Found PRs")
}
func TestInit_WhatsNext_MixedWithPR(t *testing.T) {
// Scenario 20: mixed (1 adopted, 1 created), 1 PR → adopt format
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
BranchExistsFn: func(name string) bool { return name == "existing" },
CreateBranchFn: func(name, base string) error { return nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
if branch == "existing" {
return &github.PullRequest{Number: 5, ID: "PR_5", URL: "https://example.com/5"}, nil
}
return nil, nil
},
}
runInit(cfg, &initOptions{branches: []string{"existing", "new-branch"}})
output := collectOutput(cfg, outR, errR)
assert.Contains(t, output, "Adopted")
assert.Contains(t, output, "Found PRs for 1 of 2")
}
func TestInit_Interactive_OnTrunk(t *testing.T) {
// Scenario 1: on trunk → shows hint about multi-branch args
// Note: full interactive Input() prompt requires TTY; test via args path instead.
// Here we verify that the hint line appears and non-interactive errors correctly.
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
// Not interactive → should error with guidance
err := runInit(cfg, &initOptions{})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs)
assert.Contains(t, output, "interactive input required")
}
func TestInit_Interactive_OnFeatureBranch_UseCurrent(t *testing.T) {
// Scenario 2: on feature branch → select, choose "use current"
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "feat/auth", nil },
BranchExistsFn: func(name string) bool { return name == "feat/auth" },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
cfg.ForceInteractive = true
// Select option 0 = "Use current branch"
cfg.SelectFn = func(prompt, def string, options []string) (int, error) {
return 0, nil
}
err := runInit(cfg, &initOptions{})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "Initializing a stack from main")
// Branch already exists → should be treated as adopted
assert.Contains(t, output, "Adopted")
sf, _ := stack.Load(gitDir)
require.Len(t, sf.Stacks, 1)
assert.Equal(t, []string{"feat/auth"}, sf.Stacks[0].BranchNames())
// Prefix detection Y/n prompt fails gracefully without a TTY,
// so prefix is not set. The args-path prefix detection is tested separately.
}
func TestInit_TwoPassValidation_NoBranchCreatedOnError(t *testing.T) {
// Verify that if arg 3 fails validation, args 1 and 2 are NOT created
gitDir := t.TempDir()
// Pre-create a stack with "dup" branch
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "dup"}},
}},
}
require.NoError(t, stack.Save(gitDir, sf))
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"new1", "new2", "dup"}})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs)
assert.Contains(t, output, "already exists in a stack")
assert.Empty(t, created, "no branches should be created when later arg fails validation")
}
func TestInit_TwoPassValidation_InvalidRefName(t *testing.T) {
// Verify that an invalid ref name in the args list prevents any branch creation
gitDir := t.TempDir()
var created []string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
DefaultBranchFn: func() (string, error) { return "main", nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
ValidateRefNameFn: func(name string) error {
if name == "invalid..name" {
return fmt.Errorf("invalid ref name: %s", name)
}
return nil
},
CreateBranchFn: func(name, base string) error {
created = append(created, name)
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
err := runInit(cfg, &initOptions{branches: []string{"valid-branch", "invalid..name", "another-branch"}})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs)
assert.Contains(t, output, "invalid branch name")
assert.Empty(t, created, "no branches should be created when an arg has an invalid ref name")
}