-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinit_test.go
More file actions
292 lines (246 loc) · 9.53 KB
/
init_test.go
File metadata and controls
292 lines (246 loc) · 9.53 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
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/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)
}