-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathview_test.go
More file actions
553 lines (486 loc) · 15.2 KB
/
Copy pathview_test.go
File metadata and controls
553 lines (486 loc) · 15.2 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
package cmd
import (
"encoding/json"
"io"
"os"
"path/filepath"
"testing"
"time"
"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 TestTimeAgo(t *testing.T) {
tests := []struct {
name string
duration time.Duration
want string
}{
{"seconds", 30 * time.Second, "30 seconds ago"},
{"one second", 1 * time.Second, "1 second ago"},
{"minutes", 5 * time.Minute, "5 minutes ago"},
{"one minute", 1 * time.Minute, "1 minute ago"},
{"hours", 3 * time.Hour, "3 hours ago"},
{"one hour", 1 * time.Hour, "1 hour ago"},
{"days", 2 * 24 * time.Hour, "2 days ago"},
{"one day", 24 * time.Hour, "1 day ago"},
{"months", 60 * 24 * time.Hour, "2 months ago"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := timeAgo(time.Now().Add(-tt.duration))
assert.Equal(t, tt.want, result)
})
}
}
func TestViewJSON(t *testing.T) {
git.SetOps(&git.MockOps{
IsAncestorFn: func(ancestor, descendant string) (bool, error) {
return true, nil // all branches are linear
},
})
tests := []struct {
name string
stack *stack.Stack
currentBranch string
wantTrunk string
wantBranches int
wantCurrent string
}{
{
name: "basic stack with PRs",
stack: &stack.Stack{
Trunk: stack.BranchRef{Branch: "main", Head: "aaa"},
Branches: []stack.BranchRef{
{
Branch: "feat/01",
Head: "bbb",
Base: "aaa",
PullRequest: &stack.PullRequestRef{Number: 42, URL: "https://github.com/o/r/pull/42"},
},
{
Branch: "feat/02",
Head: "ccc",
Base: "bbb",
PullRequest: &stack.PullRequestRef{Number: 43, URL: "https://github.com/o/r/pull/43"},
},
},
},
currentBranch: "feat/02",
wantTrunk: "main",
wantBranches: 2,
wantCurrent: "feat/02",
},
{
name: "stack with merged branch",
stack: &stack.Stack{
Trunk: stack.BranchRef{Branch: "main", Head: "aaa"},
Branches: []stack.BranchRef{
{
Branch: "layer-1",
Head: "bbb",
Base: "aaa",
PullRequest: &stack.PullRequestRef{Number: 10, Merged: true},
},
{
Branch: "layer-2",
Head: "ccc",
Base: "bbb",
},
},
},
currentBranch: "layer-2",
wantTrunk: "main",
wantBranches: 2,
wantCurrent: "layer-2",
},
{
name: "empty stack",
stack: &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{},
},
currentBranch: "main",
wantTrunk: "main",
wantBranches: 0,
wantCurrent: "main",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, outR, _ := config.NewTestConfig()
defer outR.Close()
err := viewJSON(cfg, tt.stack, tt.currentBranch)
require.NoError(t, err)
cfg.Out.Close()
raw, err := io.ReadAll(outR)
require.NoError(t, err)
var got viewJSONOutput
err = json.Unmarshal(raw, &got)
require.NoError(t, err, "output should be valid JSON: %s", string(raw))
assert.Equal(t, tt.wantTrunk, got.Trunk)
assert.Equal(t, tt.wantCurrent, got.CurrentBranch)
assert.Len(t, got.Branches, tt.wantBranches)
})
}
}
func TestViewJSON_BranchFields(t *testing.T) {
git.SetOps(&git.MockOps{
IsAncestorFn: func(ancestor, descendant string) (bool, error) {
// feat/02 needs rebase
if descendant == "feat/02" {
return false, nil
}
return true, nil
},
})
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main", Head: "aaa111"},
Branches: []stack.BranchRef{
{
Branch: "feat/01",
Head: "bbb222",
Base: "aaa111",
PullRequest: &stack.PullRequestRef{Number: 42, URL: "https://github.com/o/r/pull/42", Merged: true},
},
{
Branch: "feat/02",
Head: "ccc333",
Base: "bbb222",
PullRequest: &stack.PullRequestRef{Number: 43, URL: "https://github.com/o/r/pull/43"},
},
},
}
cfg, outR, _ := config.NewTestConfig()
defer outR.Close()
err := viewJSON(cfg, s, "feat/02")
require.NoError(t, err)
cfg.Out.Close()
raw, err := io.ReadAll(outR)
require.NoError(t, err)
var got viewJSONOutput
require.NoError(t, json.Unmarshal(raw, &got))
// First branch: merged
b0 := got.Branches[0]
assert.Equal(t, "feat/01", b0.Name)
assert.Equal(t, "bbb222", b0.Head)
assert.Equal(t, "aaa111", b0.Base)
assert.False(t, b0.IsCurrent)
assert.True(t, b0.IsMerged)
assert.False(t, b0.NeedsRebase, "merged branches should not need rebase")
require.NotNil(t, b0.PR)
assert.Equal(t, 42, b0.PR.Number)
assert.Equal(t, "MERGED", b0.PR.State)
assert.Equal(t, "https://github.com/o/r/pull/42", b0.PR.URL)
// Second branch: current, needs rebase
b1 := got.Branches[1]
assert.Equal(t, "feat/02", b1.Name)
assert.True(t, b1.IsCurrent)
assert.False(t, b1.IsMerged)
assert.True(t, b1.NeedsRebase)
require.NotNil(t, b1.PR)
assert.Equal(t, 43, b1.PR.Number)
assert.Equal(t, "OPEN", b1.PR.State)
}
// TestViewShort_ActiveStack verifies that --short output contains all branch
// names and the trunk for an active stack.
func TestViewShort_ActiveStack(t *testing.T) {
s := stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
{Branch: "b3"},
},
}
tmpDir := t.TempDir()
writeStackFile(t, tmpDir, s)
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return tmpDir, nil },
CurrentBranchFn: func() (string, error) { return "b2", nil },
IsAncestorFn: func(string, string) (bool, error) { return true, nil },
RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil },
})
defer restore()
cfg, outR, _ := config.NewTestConfig()
cmd := ViewCmd(cfg)
cmd.SetArgs([]string{"--short"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
cfg.Out.Close()
raw, _ := io.ReadAll(outR)
output := string(raw)
assert.NoError(t, err)
assert.Contains(t, output, "b1")
assert.Contains(t, output, "b2")
assert.Contains(t, output, "b3")
assert.Contains(t, output, "main")
}
func TestShortPRSuffix_PlainURLFallback(t *testing.T) {
t.Setenv("GH_STACK_HYPERLINKS", "0")
cfg, outR, errR := config.NewTestConfig()
defer cfg.Out.Close()
defer cfg.Err.Close()
defer outR.Close()
defer errR.Close()
b := stack.BranchRef{
PullRequest: &stack.PullRequestRef{
Number: 42,
URL: "https://github.com/o/r/pull/42",
},
}
suffix := shortPRSuffix(cfg, b, "", "", "")
assert.Equal(t, " #42 (https://github.com/o/r/pull/42)", suffix)
assert.NotContains(t, suffix, "\x1b]8")
}
// TestViewShort_FullyMergedStack verifies that --short output shows merged
// branches correctly when all branches in the stack are merged.
func TestViewShort_FullyMergedStack(t *testing.T) {
s := stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 1, Merged: true}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 2, Merged: true}},
},
}
tmpDir := t.TempDir()
writeStackFile(t, tmpDir, s)
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return tmpDir, nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
IsAncestorFn: func(string, string) (bool, error) { return true, nil },
RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil },
})
defer restore()
cfg, outR, _ := config.NewTestConfig()
cmd := ViewCmd(cfg)
cmd.SetArgs([]string{"--short"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
cfg.Out.Close()
raw, _ := io.ReadAll(outR)
output := string(raw)
assert.NoError(t, err)
assert.Contains(t, output, "b1")
assert.Contains(t, output, "b2")
}
// TestViewShort_QueuedStack verifies that --short output shows queued
// branches with a "queued" separator and the ◎ icon.
func TestViewShort_QueuedStack(t *testing.T) {
s := stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 1}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 2}},
{Branch: "b3", PullRequest: &stack.PullRequestRef{Number: 3}},
},
}
tmpDir := t.TempDir()
writeStackFile(t, tmpDir, s)
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return tmpDir, nil },
CurrentBranchFn: func() (string, error) { return "b3", nil },
IsAncestorFn: func(string, string) (bool, error) { return true, nil },
RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil },
})
defer restore()
// Mock GitHub client to return b1 as queued (MergeQueueEntry set)
cfg, outR, _ := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
switch number {
case 1:
return &github.PullRequest{
Number: 1,
ID: "PR_1",
State: "OPEN",
MergeQueueEntry: &github.MergeQueueEntry{ID: "MQE_1"},
}, nil
case 2:
return &github.PullRequest{Number: 2, ID: "PR_2", State: "OPEN"}, nil
case 3:
return &github.PullRequest{Number: 3, ID: "PR_3", State: "OPEN"}, nil
}
return nil, nil
},
}
cmd := ViewCmd(cfg)
cmd.SetArgs([]string{"--short"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
cfg.Out.Close()
raw, _ := io.ReadAll(outR)
output := string(raw)
assert.NoError(t, err)
assert.Contains(t, output, "b1")
assert.Contains(t, output, "b2")
assert.Contains(t, output, "b3")
assert.Contains(t, output, "queued", "should show queued separator")
assert.Contains(t, output, "◎", "should show queued icon for b1")
}
// TestViewShort_MixedQueuedAndMerged verifies that --short output shows
// both "queued" and "merged" separators in the correct order.
func TestViewShort_MixedQueuedAndMerged(t *testing.T) {
s := stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 1, Merged: true}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 2}},
{Branch: "b3", PullRequest: &stack.PullRequestRef{Number: 3}},
},
}
tmpDir := t.TempDir()
writeStackFile(t, tmpDir, s)
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return tmpDir, nil },
CurrentBranchFn: func() (string, error) { return "b3", nil },
IsAncestorFn: func(string, string) (bool, error) { return true, nil },
RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil },
})
defer restore()
// b1 is merged (persisted), b2 is queued (from API)
cfg, outR, _ := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
switch number {
case 2:
return &github.PullRequest{
Number: 2,
ID: "PR_2",
State: "OPEN",
MergeQueueEntry: &github.MergeQueueEntry{ID: "MQE_2"},
}, nil
case 3:
return &github.PullRequest{Number: 3, ID: "PR_3", State: "OPEN"}, nil
}
return nil, nil
},
}
cmd := ViewCmd(cfg)
cmd.SetArgs([]string{"--short"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
cfg.Out.Close()
raw, _ := io.ReadAll(outR)
output := string(raw)
assert.NoError(t, err)
assert.Contains(t, output, "queued", "should show queued separator")
assert.Contains(t, output, "merged", "should show merged separator")
// "merged" section (b1) should appear below "queued" section (b2) in output
// Since we render top-to-bottom: b3 (active) -> queued separator -> b2 -> merged separator -> b1
queuedIdx := indexOf(output, "queued")
mergedIdx := indexOf(output, "merged")
assert.Less(t, queuedIdx, mergedIdx, "queued separator should appear before merged separator")
}
func indexOf(s, substr string) int {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return i
}
}
return -1
}
// writeStackFileMulti writes a stack file with multiple stacks.
func writeStackFileMulti(t *testing.T, dir string, stacks ...stack.Stack) {
t.Helper()
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: stacks,
}
data, err := json.MarshalIndent(sf, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(dir, "gh-stack"), data, 0644))
}
func TestRunViewJSON_NotInStack(t *testing.T) {
tmpDir := t.TempDir()
writeStackFile(t, tmpDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "feat/01"}},
})
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return tmpDir, nil },
CurrentBranchFn: func() (string, error) { return "unrelated-branch", nil },
})
defer restore()
cfg, _, errR := config.NewTestConfig()
cmd := ViewCmd(cfg)
cmd.SetArgs([]string{"--json"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
cfg.Err.Close()
errOut, _ := io.ReadAll(errR)
assert.ErrorIs(t, err, ErrNotInStack, "expected exit code 2")
assert.Contains(t, string(errOut), "not part of a stack")
}
func TestRunViewJSON_MultipleStacks(t *testing.T) {
tmpDir := t.TempDir()
// "main" is the trunk of both stacks → disambiguation.
writeStackFileMulti(t, tmpDir,
stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "feat/01"}},
},
stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "feat/02"}},
},
)
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return tmpDir, nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
})
defer restore()
cfg, _, errR := config.NewTestConfig()
cmd := ViewCmd(cfg)
cmd.SetArgs([]string{"--json"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
cfg.Err.Close()
errOut, _ := io.ReadAll(errR)
assert.ErrorIs(t, err, ErrDisambiguate, "expected exit code 6")
assert.Contains(t, string(errOut), "belongs to multiple stacks")
}
func TestRunViewJSON_SingleStack(t *testing.T) {
tmpDir := t.TempDir()
writeStackFile(t, tmpDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main", Head: "aaa"},
Branches: []stack.BranchRef{
{
Branch: "feat/01",
Head: "bbb",
Base: "aaa",
PullRequest: &stack.PullRequestRef{Number: 10, URL: "https://github.com/o/r/pull/10"},
},
},
})
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return tmpDir, nil },
CurrentBranchFn: func() (string, error) { return "feat/01", nil },
IsAncestorFn: func(string, string) (bool, error) { return true, nil },
})
defer restore()
cfg, outR, _ := config.NewTestConfig()
cmd := ViewCmd(cfg)
cmd.SetArgs([]string{"--json"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
cfg.Out.Close()
raw, _ := io.ReadAll(outR)
require.NoError(t, err)
var got viewJSONOutput
require.NoError(t, json.Unmarshal(raw, &got), "output should be valid JSON: %s", string(raw))
assert.Equal(t, "main", got.Trunk)
assert.Len(t, got.Branches, 1)
assert.Equal(t, "feat/01", got.Branches[0].Name)
assert.True(t, got.Branches[0].IsCurrent)
}