Repository navigation
Expand file tree
/
Copy pathutils_test.go
More file actions
727 lines (638 loc) · 20 KB
/
Copy pathutils_test.go
File metadata and controls
727 lines (638 loc) · 20 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
package cmd
import (
"errors"
"fmt"
"io"
"strings"
"testing"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/cli/go-gh/v2/pkg/repository"
"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"
)
func TestIsInterruptError_DirectMatch(t *testing.T) {
if !isInterruptError(terminal.InterruptErr) {
t.Error("expected true for terminal.InterruptErr")
}
}
func TestIsInterruptError_Wrapped(t *testing.T) {
// This is how the prompter library wraps the interrupt error.
wrapped := fmt.Errorf("could not prompt: %w", terminal.InterruptErr)
if !isInterruptError(wrapped) {
t.Error("expected true for wrapped interrupt error")
}
}
func TestIsInterruptError_DoubleWrapped(t *testing.T) {
// Simulate additional wrapping by callers.
inner := fmt.Errorf("could not prompt: %w", terminal.InterruptErr)
outer := fmt.Errorf("stack selection: %w", inner)
if !isInterruptError(outer) {
t.Error("expected true for double-wrapped interrupt error")
}
}
func TestIsInterruptError_NonInterrupt(t *testing.T) {
if isInterruptError(errors.New("some other error")) {
t.Error("expected false for non-interrupt error")
}
}
func TestIsInterruptError_Nil(t *testing.T) {
if isInterruptError(nil) {
t.Error("expected false for nil error")
}
}
func TestPrintInterrupt_Output(t *testing.T) {
cfg, outR, errR := config.NewTestConfig()
printInterrupt(cfg)
output := collectOutput(cfg, outR, errR)
if !strings.Contains(output, "Received interrupt, aborting operation") {
t.Errorf("expected interrupt message, got: %s", output)
}
// Should NOT contain error marker (✗)
if strings.Contains(output, "\u2717") {
t.Errorf("interrupt message should not use error format, got: %s", output)
}
}
func TestErrInterrupt_IsDistinct(t *testing.T) {
if errors.Is(errInterrupt, terminal.InterruptErr) {
t.Error("errInterrupt sentinel should not match terminal.InterruptErr")
}
if !errors.Is(errInterrupt, errInterrupt) {
t.Error("errInterrupt should match itself")
}
}
func TestEnsureRerere_SkipsWhenAlreadyEnabled(t *testing.T) {
enableCalled := false
restore := git.SetOps(&git.MockOps{
IsRerereEnabledFn: func() (bool, error) { return true, nil },
EnableRerereFn: func() error {
enableCalled = true
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
_ = ensureRerere(cfg)
collectOutput(cfg, outR, errR)
if enableCalled {
t.Error("EnableRerere should not be called when already enabled")
}
}
func TestEnsureRerere_SkipsWhenDeclined(t *testing.T) {
enableCalled := false
restore := git.SetOps(&git.MockOps{
IsRerereEnabledFn: func() (bool, error) { return false, nil },
IsRerereDeclinedFn: func() (bool, error) { return true, nil },
EnableRerereFn: func() error {
enableCalled = true
return nil
},
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
_ = ensureRerere(cfg)
collectOutput(cfg, outR, errR)
if enableCalled {
t.Error("EnableRerere should not be called when user previously declined")
}
}
func TestEnsureRerere_SkipsWhenNonInteractive(t *testing.T) {
enableCalled := false
declinedSaved := false
restore := git.SetOps(&git.MockOps{
IsRerereEnabledFn: func() (bool, error) { return false, nil },
IsRerereDeclinedFn: func() (bool, error) { return false, nil },
EnableRerereFn: func() error {
enableCalled = true
return nil
},
SaveRerereDeclinedFn: func() error {
declinedSaved = true
return nil
},
})
defer restore()
// NewTestConfig is non-interactive (pipes, not a TTY).
cfg, outR, errR := config.NewTestConfig()
_ = ensureRerere(cfg)
collectOutput(cfg, outR, errR)
if enableCalled {
t.Error("EnableRerere should not be called in non-interactive mode")
}
if declinedSaved {
t.Error("SaveRerereDeclined should not be called in non-interactive mode")
}
}
func TestResolvePR_ByPRNumber(t *testing.T) {
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{
{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "feat-1", PullRequest: &stack.PullRequestRef{Number: 42, URL: "https://github.com/o/r/pull/42"}},
{Branch: "feat-2", PullRequest: &stack.PullRequestRef{Number: 43, URL: "https://github.com/o/r/pull/43"}},
},
},
},
}
cfg, _, _ := config.NewTestConfig()
s, br, err := resolvePR(cfg, sf, "42")
assert.NoError(t, err)
assert.Equal(t, "feat-1", br.Branch)
assert.Equal(t, 42, br.PullRequest.Number)
assert.Equal(t, "main", s.Trunk.Branch)
}
func TestResolvePR_ByPRURL(t *testing.T) {
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{
{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "feat-1", PullRequest: &stack.PullRequestRef{Number: 42, URL: "https://github.com/o/r/pull/42"}},
},
},
},
}
cfg, _, _ := config.NewTestConfig()
s, br, err := resolvePR(cfg, sf, "https://github.com/o/r/pull/42")
assert.NoError(t, err)
assert.Equal(t, "feat-1", br.Branch)
assert.Equal(t, "main", s.Trunk.Branch)
}
func TestResolvePR_ByBranchName(t *testing.T) {
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{
{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "feat-1", PullRequest: &stack.PullRequestRef{Number: 42}},
{Branch: "feat-2", PullRequest: &stack.PullRequestRef{Number: 43}},
},
},
},
}
cfg, _, _ := config.NewTestConfig()
s, br, err := resolvePR(cfg, sf, "feat-2")
assert.NoError(t, err)
assert.Equal(t, "feat-2", br.Branch)
assert.Equal(t, 43, br.PullRequest.Number)
assert.Equal(t, "main", s.Trunk.Branch)
}
func TestResolvePR_NotFound(t *testing.T) {
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{
{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "feat-1"}},
},
},
}
cfg, _, _ := config.NewTestConfig()
_, _, err := resolvePR(cfg, sf, "nonexistent")
assert.Error(t, err)
assert.Contains(t, err.Error(), "no locally tracked stack found")
}
func TestResolvePR_URLPrecedesNumber(t *testing.T) {
// A PR URL that contains number 99 should resolve via URL parsing,
// even if PR #99 doesn't exist — the URL parser extracts the number.
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{
{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "feat-1", PullRequest: &stack.PullRequestRef{Number: 99, URL: "https://github.com/o/r/pull/99"}},
},
},
},
}
cfg, _, _ := config.NewTestConfig()
_, br, err := resolvePR(cfg, sf, "https://github.com/o/r/pull/99")
assert.NoError(t, err)
assert.Equal(t, 99, br.PullRequest.Number)
}
func TestSyncStackPRs_NoTrackedPR_OnlyAdoptsOpenPRs(t *testing.T) {
// A branch with no tracked PR should only adopt OPEN PRs,
// not stale merged/closed PRs from a previous branch name usage.
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "reused-branch"}, // no PullRequest
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
// FindPRForBranch (OPEN only) returns nil — no open PR.
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return nil, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
// Branch should still have no PR tracked.
assert.Nil(t, s.Branches[0].PullRequest)
}
func TestSyncStackPRs_NoTrackedPR_AdoptsOpenPR(t *testing.T) {
// A branch with no tracked PR should adopt an OPEN PR it discovers.
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "feature"}, // no PullRequest
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 99,
ID: "PR_99",
URL: "https://github.com/o/r/pull/99",
State: "OPEN",
}, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 99, s.Branches[0].PullRequest.Number)
assert.False(t, s.Branches[0].PullRequest.Merged)
}
func TestSyncStackPRs_TrackedPR_DetectsMerge(t *testing.T) {
// A branch with a tracked PR should detect when that PR gets merged.
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{
Branch: "feature",
PullRequest: &stack.PullRequestRef{
Number: 42,
ID: "PR_42",
URL: "https://github.com/o/r/pull/42",
},
},
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 42,
ID: "PR_42",
URL: "https://github.com/o/r/pull/42",
State: "MERGED",
Merged: true,
}, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 42, s.Branches[0].PullRequest.Number)
assert.True(t, s.Branches[0].PullRequest.Merged)
}
func TestSyncStackPRs_MergedBranch_StaysMerged(t *testing.T) {
// A merged branch should stay merged — no API calls, no changes.
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{
Branch: "merged-branch",
PullRequest: &stack.PullRequestRef{
Number: 20,
ID: "PR_20",
URL: "https://github.com/o/r/pull/20",
Merged: true,
},
},
},
}
apiCalled := false
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
apiCalled = true
return nil, nil
},
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
apiCalled = true
return nil, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 20, s.Branches[0].PullRequest.Number)
assert.True(t, s.Branches[0].PullRequest.Merged)
assert.False(t, apiCalled, "no API calls should be made for merged branches")
}
func TestSyncStackPRs_ClosedPR_ReplacedByOpenPR(t *testing.T) {
// A tracked PR that was closed (not merged) should be replaced
// by a new OPEN PR if one exists.
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{
Branch: "feature",
PullRequest: &stack.PullRequestRef{
Number: 10,
ID: "PR_10",
URL: "https://github.com/o/r/pull/10",
},
},
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 10,
State: "CLOSED",
Merged: false,
}, nil
},
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 15,
ID: "PR_15",
URL: "https://github.com/o/r/pull/15",
State: "OPEN",
}, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 15, s.Branches[0].PullRequest.Number)
assert.False(t, s.Branches[0].PullRequest.Merged)
}
func TestSyncStackPRs_TrackedOpenPR_UpdatesQueued(t *testing.T) {
// A tracked OPEN PR that enters a merge queue should have Queued set.
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{
Branch: "feature",
PullRequest: &stack.PullRequestRef{
Number: 42,
ID: "PR_42",
URL: "https://github.com/o/r/pull/42",
},
},
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 42,
State: "OPEN",
MergeQueueEntry: &github.MergeQueueEntry{
ID: "MQ_1",
},
}, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
assert.True(t, s.Branches[0].Queued)
}
func TestSyncStackPRs_ClosedPR_NoReplacement_ClearsPR(t *testing.T) {
// A tracked PR that was closed with no replacement OPEN PR should
// have its PR ref cleared so it doesn't appear as an active PR.
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{
Branch: "feature",
PullRequest: &stack.PullRequestRef{
Number: 10,
ID: "PR_10",
URL: "https://github.com/o/r/pull/10",
},
Queued: true,
},
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 10,
State: "CLOSED",
Merged: false,
}, nil
},
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return nil, nil // no open replacement
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
assert.Nil(t, s.Branches[0].PullRequest)
assert.False(t, s.Branches[0].Queued)
}
func TestSyncStackPRs_RemoteStack_UsesStackAPI(t *testing.T) {
// When the stack has a remote ID, sync should use the stack API
// as source of truth, matching PRs to branches by head ref name.
s := &stack.Stack{
ID: "100",
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
return []github.RemoteStack{
{ID: 100, PullRequests: []int{10, 11}},
}, nil
},
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
switch number {
case 10:
return &github.PullRequest{Number: 10, ID: "PR_10", URL: "https://github.com/o/r/pull/10", HeadRefName: "b1", State: "OPEN"}, nil
case 11:
return &github.PullRequest{Number: 11, ID: "PR_11", URL: "https://github.com/o/r/pull/11", HeadRefName: "b2", State: "MERGED", Merged: true}, nil
}
return nil, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
// b1 should be tracked with open PR
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 10, s.Branches[0].PullRequest.Number)
assert.False(t, s.Branches[0].PullRequest.Merged)
// b2 should be tracked with merged PR (stack API keeps closed/merged PRs)
require.NotNil(t, s.Branches[1].PullRequest)
assert.Equal(t, 11, s.Branches[1].PullRequest.Number)
assert.True(t, s.Branches[1].PullRequest.Merged)
}
func TestSyncStackPRs_RemoteStack_ClosedPRStaysAssociated(t *testing.T) {
// When using the stack API, a closed (not merged) PR should remain
// associated — the stack API is the source of truth, not PR state.
s := &stack.Stack{
ID: "200",
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "feature", PullRequest: &stack.PullRequestRef{Number: 5}},
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
return []github.RemoteStack{
{ID: 200, PullRequests: []int{5}},
}, nil
},
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
return &github.PullRequest{Number: 5, ID: "PR_5", URL: "https://github.com/o/r/pull/5", HeadRefName: "feature", State: "CLOSED"}, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
// PR should still be associated (not cleared), because the stack API says it's part of the stack.
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 5, s.Branches[0].PullRequest.Number)
assert.False(t, s.Branches[0].PullRequest.Merged)
}
func TestSyncStackPRs_RemoteStack_FallsBackOnAPIError(t *testing.T) {
// If the stack API fails, fall back to local discovery.
s := &stack.Stack{
ID: "300",
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "feature"},
},
}
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
return nil, fmt.Errorf("API error")
},
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return &github.PullRequest{Number: 77, ID: "PR_77", URL: "https://github.com/o/r/pull/77", State: "OPEN"}, nil
},
}
_ = syncStackPRs(cfg, s)
collectOutput(cfg, outR, errR)
// Should have fallen back to local discovery and found the open PR.
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 77, s.Branches[0].PullRequest.Number)
}
func TestParsePRURL(t *testing.T) {
tests := []struct {
name string
input string
wantN int
wantOK bool
}{
{"standard URL", "https://github.com/owner/repo/pull/42", 42, true},
{"with trailing slash", "https://github.com/owner/repo/pull/42/", 42, true},
{"with files tab", "https://github.com/owner/repo/pull/42/files", 42, true},
{"GHES URL", "https://ghes.example.com/owner/repo/pull/99", 99, true},
{"GHES URL with trailing slash", "https://ghes.example.com/owner/repo/pull/7/", 7, true},
{"not a PR URL", "https://github.com/owner/repo/issues/42", 0, false},
{"plain number", "42", 0, false},
{"branch name", "feat-1", 0, false},
{"empty", "", 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n, ok := parsePRURL(tt.input)
assert.Equal(t, tt.wantOK, ok)
if ok {
assert.Equal(t, tt.wantN, n)
}
})
}
}
func TestStackNeedsRebase_AllCurrent(t *testing.T) {
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
},
}
mock := &git.MockOps{
IsAncestorFn: func(a, d string) (bool, error) {
return true, nil
},
}
restore := git.SetOps(mock)
defer restore()
assert.False(t, stackNeedsRebase(s), "stack should not need rebase when all branches are current")
}
func TestStackNeedsRebase_FirstBranchStale(t *testing.T) {
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
},
}
mock := &git.MockOps{
IsAncestorFn: func(a, d string) (bool, error) {
if a == "main" && d == "b1" {
return false, nil
}
return true, nil
},
}
restore := git.SetOps(mock)
defer restore()
assert.True(t, stackNeedsRebase(s), "stack should need rebase when first branch is stale")
}
func TestStackNeedsRebase_SkipsMergedBranches(t *testing.T) {
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Merged: true}},
{Branch: "b2"},
},
}
mock := &git.MockOps{
IsAncestorFn: func(a, d string) (bool, error) {
return true, nil
},
}
restore := git.SetOps(mock)
defer restore()
assert.False(t, stackNeedsRebase(s), "should skip merged branches and find stack up to date")
}
// setTestTokenForHost sets cfg.TokenForHostFn to return the given token for
// any host. Also sets RepoOverride so tests don't depend on real git context.
func setTestTokenForHost(cfg *config.Config, token string) {
cfg.TokenForHostFn = func(string) (string, string) { return token, "test" }
cfg.RepoOverride = &repository.Repository{Host: "github.com", Owner: "o", Name: "r"}
}
func TestWarnStacksUnavailableOrPAT_ShowsPATMessage(t *testing.T) {
cfg, _, errR := config.NewTestConfig()
setTestTokenForHost(cfg, "github_pat_fine_grained")
warnStacksUnavailableOrPAT(cfg)
cfg.Err.Close()
errOut, _ := io.ReadAll(errR)
output := string(errOut)
assert.Contains(t, output, "Personal access tokens are not supported by gh stack")
assert.NotContains(t, output, "Stacked PRs are not enabled")
}
func TestWarnStacksUnavailableOrPAT_ShowsNotEnabledForOAuth(t *testing.T) {
cfg, _, errR := config.NewTestConfig()
setTestTokenForHost(cfg, "gho_oauth_token")
warnStacksUnavailableOrPAT(cfg)
cfg.Err.Close()
errOut, _ := io.ReadAll(errR)
output := string(errOut)
assert.Contains(t, output, "Stacked PRs are not enabled for this repository")
assert.NotContains(t, output, "Personal access tokens")
}