{{ message }}
Repository navigation
Expand file tree
/
Copy pathhelpers_test.go
More file actions
349 lines (288 loc) · 8.98 KB
/
Copy pathhelpers_test.go
File metadata and controls
349 lines (288 loc) · 8.98 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
// e2e/helpers_test.go
package e2e_test
import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestEnv provides a test environment with an isolated git repository.
type TestEnv struct {
t *testing.T
WorkDir string
RemoteDir string
BinaryPath string
}
// Result captures command execution output.
type Result struct {
Stdout string
Stderr string
ExitCode int
}
func (r *Result) Success() bool {
return r.ExitCode == 0
}
func (r *Result) Failed() bool {
return r.ExitCode != 0
}
func (r *Result) ContainsStdout(s string) bool {
return strings.Contains(r.Stdout, s)
}
func (r *Result) ContainsStderr(s string) bool {
return strings.Contains(r.Stderr, s)
}
// NewTestEnv creates a new test environment with an initialized git repo.
func NewTestEnv(t *testing.T) *TestEnv {
t.Helper()
workDir := t.TempDir()
env := &TestEnv{
t: t,
WorkDir: workDir,
BinaryPath: binaryPath,
}
env.Git("init")
env.Git("config", "user.email", "test@example.com")
env.Git("config", "user.name", "Test User")
// Prevent git from opening an editor (for commits, rebases, etc.)
// This is set in git config so it applies to all git subprocesses,
// including those spawned by gh-stack commands.
env.Git("config", "core.editor", "cat")
env.WriteFile("README.md", "# Test Repository\n")
env.Git("add", ".")
env.Git("commit", "-m", "initial commit")
return env
}
// NewTestEnvWithRemote creates test env with a simulated remote.
func NewTestEnvWithRemote(t *testing.T) *TestEnv {
t.Helper()
env := NewTestEnv(t)
remoteDir := t.TempDir()
env.RemoteDir = remoteDir
cmd := exec.Command("git", "clone", "--bare", env.WorkDir, remoteDir)
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to create bare remote: %v", err)
}
env.Git("remote", "add", "origin", remoteDir)
env.Git("push", "-u", "origin", "main")
return env
}
// Run executes gh-stack with args, returns Result (doesn't fail on non-zero).
func (e *TestEnv) Run(args ...string) *Result {
e.t.Helper()
var stdout, stderr bytes.Buffer
cmd := exec.Command(e.BinaryPath, args...)
cmd.Dir = e.WorkDir
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Ensure git subprocesses spawned by gh-stack don't open an editor
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
err := cmd.Run()
result := &Result{
Stdout: stdout.String(),
Stderr: stderr.String(),
ExitCode: 0,
}
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
result.ExitCode = exitErr.ExitCode()
} else {
e.t.Fatalf("failed to run command: %v", err)
}
}
return result
}
// MustRun executes gh-stack and fails the test on non-zero exit.
func (e *TestEnv) MustRun(args ...string) *Result {
e.t.Helper()
result := e.Run(args...)
if result.Failed() {
e.t.Fatalf("gh-stack %s failed (exit %d):\nstdout: %s\nstderr: %s",
strings.Join(args, " "), result.ExitCode, result.Stdout, result.Stderr)
}
return result
}
// Git executes a git command in the work directory.
func (e *TestEnv) Git(args ...string) string {
e.t.Helper()
var stdout, stderr bytes.Buffer
cmd := exec.Command("git", args...)
cmd.Dir = e.WorkDir
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Prevent git from opening an editor (for commits, rebases, etc.)
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
if err := cmd.Run(); err != nil {
e.t.Fatalf("git %s failed: %v\nstderr: %s", strings.Join(args, " "), err, stderr.String())
}
return strings.TrimSpace(stdout.String())
}
// GitRemote executes git in the remote (bare) repo.
func (e *TestEnv) GitRemote(args ...string) string {
e.t.Helper()
if e.RemoteDir == "" {
e.t.Fatal("GitRemote called but no remote configured")
}
var stdout, stderr bytes.Buffer
cmd := exec.Command("git", args...)
cmd.Dir = e.RemoteDir
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
if err := cmd.Run(); err != nil {
e.t.Fatalf("git (remote) %s failed: %v\nstderr: %s", strings.Join(args, " "), err, stderr.String())
}
return strings.TrimSpace(stdout.String())
}
// WriteFile writes content to a file in the work directory.
func (e *TestEnv) WriteFile(name, content string) {
e.t.Helper()
path := filepath.Join(e.WorkDir, name)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
e.t.Fatalf("failed to create directory %s: %v", dir, err)
}
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
e.t.Fatalf("failed to write file %s: %v", name, err)
}
}
// CreateCommit creates a commit with a unique file.
func (e *TestEnv) CreateCommit(msg string) string {
e.t.Helper()
filename := strings.ReplaceAll(msg, " ", "-") + ".txt"
e.WriteFile(filename, msg+"\n")
e.Git("add", filename)
e.Git("commit", "-m", msg)
return e.Git("rev-parse", "HEAD")
}
// CreateCommitOnFile modifies a specific file and commits.
func (e *TestEnv) CreateCommitOnFile(filename, content, msg string) string {
e.t.Helper()
e.WriteFile(filename, content)
e.Git("add", filename)
e.Git("commit", "-m", msg)
return e.Git("rev-parse", "HEAD")
}
// CurrentBranch returns the current branch name.
func (e *TestEnv) CurrentBranch() string {
e.t.Helper()
return e.Git("rev-parse", "--abbrev-ref", "HEAD")
}
// BranchTip returns the SHA of a branch's tip.
func (e *TestEnv) BranchTip(branch string) string {
e.t.Helper()
return e.Git("rev-parse", branch)
}
// AssertBranch fails if current branch doesn't match.
func (e *TestEnv) AssertBranch(expected string) {
e.t.Helper()
actual := e.CurrentBranch()
if actual != expected {
e.t.Errorf("expected branch %q, got %q", expected, actual)
}
}
// AssertBranchExists fails if branch doesn't exist.
func (e *TestEnv) AssertBranchExists(branch string) {
e.t.Helper()
cmd := exec.Command("git", "rev-parse", "--verify", "refs/heads/"+branch)
cmd.Dir = e.WorkDir
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
if err := cmd.Run(); err != nil {
e.t.Errorf("expected branch %q to exist", branch)
}
}
// AssertClean fails if working tree is dirty.
func (e *TestEnv) AssertClean() {
e.t.Helper()
status := e.Git("status", "--porcelain")
if status != "" {
e.t.Errorf("expected clean working tree, got:\n%s", status)
}
}
// AssertRebaseInProgress fails if no rebase is active.
func (e *TestEnv) AssertRebaseInProgress() {
e.t.Helper()
rebaseMerge := filepath.Join(e.WorkDir, ".git", "rebase-merge")
rebaseApply := filepath.Join(e.WorkDir, ".git", "rebase-apply")
_, err1 := os.Stat(rebaseMerge)
_, err2 := os.Stat(rebaseApply)
if err1 != nil && err2 != nil {
e.t.Error("expected rebase in progress")
}
}
// AssertNoRebaseInProgress fails if rebase is active.
func (e *TestEnv) AssertNoRebaseInProgress() {
e.t.Helper()
rebaseMerge := filepath.Join(e.WorkDir, ".git", "rebase-merge")
rebaseApply := filepath.Join(e.WorkDir, ".git", "rebase-apply")
_, err1 := os.Stat(rebaseMerge)
_, err2 := os.Stat(rebaseApply)
if err1 == nil || err2 == nil {
e.t.Error("expected no rebase in progress")
}
}
// AssertAncestor fails if ancestor is not an ancestor of descendant.
func (e *TestEnv) AssertAncestor(ancestor, descendant string) {
e.t.Helper()
cmd := exec.Command("git", "merge-base", "--is-ancestor", ancestor, descendant)
cmd.Dir = e.WorkDir
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
if err := cmd.Run(); err != nil {
e.t.Errorf("expected %q to be ancestor of %q", ancestor, descendant)
}
}
// CreateWorktree creates a linked worktree for the given branch at the given path.
func (e *TestEnv) CreateWorktree(branch, wtPath string) {
e.t.Helper()
cmd := exec.Command("git", "worktree", "add", wtPath, branch)
cmd.Dir = e.WorkDir
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
e.t.Fatalf("git worktree add %s %s failed: %v\nstderr: %s", wtPath, branch, err, stderr.String())
}
}
// GitInWorktree executes a git command in a worktree directory.
func (e *TestEnv) GitInWorktree(wtPath string, args ...string) string {
e.t.Helper()
var stdout, stderr bytes.Buffer
cmd := exec.Command("git", args...)
cmd.Dir = wtPath
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
if err := cmd.Run(); err != nil {
e.t.Fatalf("git (worktree %s) %s failed: %v\nstderr: %s", wtPath, strings.Join(args, " "), err, stderr.String())
}
return strings.TrimSpace(stdout.String())
}
// RunInDir executes gh-stack in a specific directory (e.g. a worktree).
func (e *TestEnv) RunInDir(dir string, args ...string) *Result {
e.t.Helper()
var stdout, stderr bytes.Buffer
cmd := exec.Command(e.BinaryPath, args...)
cmd.Dir = dir
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = append(os.Environ(), "GIT_EDITOR=cat")
err := cmd.Run()
result := &Result{
Stdout: stdout.String(),
Stderr: stderr.String(),
ExitCode: 0,
}
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
result.ExitCode = exitErr.ExitCode()
} else {
e.t.Fatalf("failed to run command in %s: %v", dir, err)
}
}
return result
}
You can’t perform that action at this time.
