-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinit_test.go
More file actions
381 lines (322 loc) · 12.5 KB
/
init_test.go
File metadata and controls
381 lines (322 loc) · 12.5 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
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_AdoptRejectsPrefix(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()
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, prefix: "feat"})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs)
assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered")
}
func TestInit_AdoptRejectsNumbered(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()
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, numbered: true})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs)
assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered")
}
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(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 false },
})
defer restore()
cfg, outR, errR := config.NewTestConfig()
runInit(cfg, &initOptions{branches: []string{"nonexistent"}, adopt: true})
output := collectOutput(cfg, outR, errR)
assert.Contains(t, output, "does not exist")
}
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)
}
}