-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_integration_test.go
More file actions
638 lines (541 loc) · 19.7 KB
/
cli_integration_test.go
File metadata and controls
638 lines (541 loc) · 19.7 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
package cli_test
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
)
var testBinary string
const testIncludeFile = ".test.worktreeinclude"
func TestMain(m *testing.M) {
_, file, _, _ := runtime.Caller(0)
repoRoot := filepath.Clean(filepath.Join(filepath.Dir(file), "..", ".."))
binDir, err := os.MkdirTemp("", "git-worktreeinclude-bin-")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to create temp bin dir: %v\n", err)
os.Exit(1)
}
defer func() {
_ = os.RemoveAll(binDir)
}()
testBinary = filepath.Join(binDir, "git-worktreeinclude")
build := exec.Command("go", "build", "-o", testBinary, "./cmd/git-worktreeinclude")
build.Dir = repoRoot
build.Stdout = os.Stdout
build.Stderr = os.Stderr
if err := build.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to build test binary: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
type fixture struct {
root string
wt string
}
type jsonResult struct {
DryRun bool `json:"dry_run"`
From string `json:"from"`
To string `json:"to"`
IncludeFile string `json:"include_file"`
Summary struct {
Matched int `json:"matched"`
Copied int `json:"copied"`
CopyPlanned int `json:"copy_planned"`
SkippedSame int `json:"skipped_same"`
SkippedMissingSrc int `json:"skipped_missing_src"`
Conflicts int `json:"conflicts"`
Errors int `json:"errors"`
} `json:"summary"`
Actions []struct {
Op string `json:"op"`
Path string `json:"path"`
Status string `json:"status"`
} `json:"actions"`
}
func TestApplyAC1AC2AC6AC7(t *testing.T) {
fx := setupFixture(t)
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--json")
if code != 0 {
t.Fatalf("apply --json exit code = %d, stderr=%s", code, stderr)
}
if strings.TrimSpace(stderr) != "" {
t.Fatalf("expected empty stderr, got %q", stderr)
}
res := decodeSingleJSON(t, stdout)
if realPath(t, res.From) != realPath(t, fx.root) {
t.Fatalf("expected source %q, got %q", fx.root, res.From)
}
envPath := filepath.Join(fx.wt, ".env")
envBytes, err := os.ReadFile(envPath)
if err != nil {
t.Fatalf("expected copied .env: %v", err)
}
if string(envBytes) != "SOURCE_ENV\n" {
t.Fatalf("unexpected .env content: %q", string(envBytes))
}
for _, a := range res.Actions {
if a.Path == "README.md" {
t.Fatalf("tracked file should not be copied")
}
}
if res.Summary.Errors != 0 {
t.Fatalf("expected no errors, got %d", res.Summary.Errors)
}
}
func TestApplyAC3ConflictExit3(t *testing.T) {
fx := setupFixture(t)
writeFile(t, filepath.Join(fx.wt, ".env.local"), "TARGET_LOCAL\n")
_, _, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile)
if code != 3 {
t.Fatalf("expected exit code 3, got %d", code)
}
got, err := os.ReadFile(filepath.Join(fx.wt, ".env.local"))
if err != nil {
t.Fatalf("read .env.local: %v", err)
}
if string(got) != "TARGET_LOCAL\n" {
t.Fatalf("target file should remain unchanged; got %q", string(got))
}
}
func TestApplyAC4ForceOverwrite(t *testing.T) {
fx := setupFixture(t)
writeFile(t, filepath.Join(fx.wt, ".env.local"), "TARGET_LOCAL\n")
_, _, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--force")
if code != 0 {
t.Fatalf("expected exit code 0, got %d", code)
}
got, err := os.ReadFile(filepath.Join(fx.wt, ".env.local"))
if err != nil {
t.Fatalf("read .env.local: %v", err)
}
if string(got) != "SOURCE_LOCAL\n" {
t.Fatalf("force should overwrite; got %q", string(got))
}
}
func TestApplyAC5DryRun(t *testing.T) {
fx := setupFixture(t)
if err := os.Remove(filepath.Join(fx.wt, ".env")); err != nil && !errors.Is(err, os.ErrNotExist) {
t.Fatalf("remove .env: %v", err)
}
_, _, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--dry-run")
if code != 0 {
t.Fatalf("expected exit code 0, got %d", code)
}
if _, err := os.Stat(filepath.Join(fx.wt, ".env")); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("dry-run should not create .env")
}
}
func TestApplyAC8MissingIncludeIsNoop(t *testing.T) {
fx := setupFixture(t)
stdout, _, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", ".missing-worktreeinclude", "--json")
if code != 0 {
t.Fatalf("expected exit code 0, got %d", code)
}
res := decodeSingleJSON(t, stdout)
if res.Summary.Matched != 0 || res.Summary.Copied != 0 || res.Summary.CopyPlanned != 0 || len(res.Actions) != 0 {
t.Fatalf("expected noop summary, got %+v", res.Summary)
}
}
func TestApplyUsesSourceIncludeWhenTargetIncludeMissing(t *testing.T) {
fx := setupFixture(t)
if err := os.Remove(filepath.Join(fx.wt, testIncludeFile)); err != nil {
t.Fatalf("remove target include: %v", err)
}
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--json")
if code != 0 {
t.Fatalf("expected exit code 0, got %d stderr=%s", code, stderr)
}
res := decodeSingleJSON(t, stdout)
if res.Summary.Copied != 2 {
t.Fatalf("expected source include to be used, got summary=%+v", res.Summary)
}
}
func TestApplyNoopWhenSourceIncludeMissingEvenIfTargetHasInclude(t *testing.T) {
fx := setupFixture(t)
if err := os.Remove(filepath.Join(fx.root, testIncludeFile)); err != nil {
t.Fatalf("remove source include: %v", err)
}
writeFile(t, filepath.Join(fx.wt, testIncludeFile), ".env\n")
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--json")
if code != 0 {
t.Fatalf("expected exit code 0, got %d stderr=%s", code, stderr)
}
res := decodeSingleJSON(t, stdout)
if res.Summary.Matched != 0 || res.Summary.Copied != 0 || res.Summary.CopyPlanned != 0 || len(res.Actions) != 0 {
t.Fatalf("expected source-missing include no-op, got summary=%+v", res.Summary)
}
humanStdout, _, humanCode := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile)
if humanCode != 0 {
t.Fatalf("expected human apply exit code 0, got %d", humanCode)
}
if !strings.Contains(humanStdout, "Hint: include file was not found in source worktree") {
t.Fatalf("apply output missing compatibility hint: %s", humanStdout)
}
dryRunOut, _, dryRunCode := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--dry-run", "--verbose")
if dryRunCode != 0 {
t.Fatalf("apply --dry-run --verbose exit code = %d", dryRunCode)
}
if !strings.Contains(dryRunOut, "not found in source; found at target path") {
t.Fatalf("apply --dry-run --verbose output missing source/target compatibility hint: %s", dryRunOut)
}
}
func TestApplyReadsIncludeFileIgnoredByGlobalExcludes(t *testing.T) {
fx := setupFixture(t)
globalIgnore := filepath.Join(t.TempDir(), "global_ignore")
writeFile(t, globalIgnore, ".global.worktreeinclude\n")
runGit(t, fx.root, "config", "core.excludesFile", globalIgnore)
writeFile(t, filepath.Join(fx.root, ".global.worktreeinclude"), ".env\n")
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", ".global.worktreeinclude", "--json")
if code != 0 {
t.Fatalf("expected exit code 0, got %d stderr=%s", code, stderr)
}
res := decodeSingleJSON(t, stdout)
if res.Summary.Copied == 0 {
t.Fatalf("expected ignored include file to be read, got summary=%+v", res.Summary)
}
}
func TestApplyRejectsIncludeOutsideRepo(t *testing.T) {
fx := setupFixture(t)
outside := filepath.Join(filepath.Dir(fx.root), "outside.include")
writeFile(t, outside, ".env\n")
_, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", outside)
if code != 4 {
t.Fatalf("expected exit code 4, got %d stderr=%s", code, stderr)
}
if !strings.Contains(stderr, "include path must be inside source repository root") {
t.Fatalf("unexpected stderr: %s", stderr)
}
}
func TestApplyRejectsIncludeSymlinkEscape(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlink behavior and permissions vary on Windows")
}
fx := setupFixture(t)
outsideDir := filepath.Join(filepath.Dir(fx.root), "outside")
if err := os.MkdirAll(outsideDir, 0o755); err != nil {
t.Fatalf("mkdir outside dir: %v", err)
}
outsideInclude := filepath.Join(outsideDir, "outside.include")
writeFile(t, outsideInclude, ".env\n")
linkPath := filepath.Join(fx.root, ".include-link")
if err := os.Symlink(outsideInclude, linkPath); err != nil {
t.Fatalf("create include symlink: %v", err)
}
_, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", ".include-link")
if code != 4 {
t.Fatalf("expected exit code 4, got %d stderr=%s", code, stderr)
}
if !strings.Contains(stderr, "include path must be inside source repository root") {
t.Fatalf("unexpected stderr: %s", stderr)
}
}
func TestApplyJSONConflictOutputContract(t *testing.T) {
fx := setupFixture(t)
writeFile(t, filepath.Join(fx.wt, ".env.local"), "TARGET_LOCAL\n")
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--json")
if code != 3 {
t.Fatalf("expected conflict exit code 3, got %d stderr=%s", code, stderr)
}
if strings.TrimSpace(stderr) != "" {
t.Fatalf("expected empty stderr for --json conflict output, got %q", stderr)
}
res := decodeSingleJSON(t, stdout)
if res.Summary.Conflicts == 0 {
t.Fatalf("expected conflicts > 0 in JSON summary")
}
}
func TestApplyWithLongIncludeLine(t *testing.T) {
fx := setupFixture(t)
longInclude := filepath.Join(fx.root, ".long.worktreeinclude")
longPattern := strings.Repeat("a", 70*1024)
writeFile(t, longInclude, longPattern+"\n.env\n")
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", longInclude, "--json")
if code != 0 {
t.Fatalf("expected exit code 0 with long include line, got %d stderr=%s", code, stderr)
}
res := decodeSingleJSON(t, stdout)
if res.Summary.Copied == 0 {
t.Fatalf("expected at least one copied file, got summary=%+v", res.Summary)
}
}
func TestApplyDryRunVerboseOutput(t *testing.T) {
fx := setupFixture(t)
stdout, _, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--dry-run", "--verbose")
if code != 0 {
t.Fatalf("apply --dry-run --verbose exit code = %d", code)
}
if !strings.Contains(stdout, "APPLY from:") {
t.Fatalf("apply --dry-run output missing source root: %s", stdout)
}
if !strings.Contains(stdout, "APPLY to:") {
t.Fatalf("apply --dry-run output missing target root: %s", stdout)
}
if !strings.Contains(stdout, "SUMMARY matched=") {
t.Fatalf("apply --dry-run output missing summary: %s", stdout)
}
if !strings.Contains(stdout, "copy_planned=") {
t.Fatalf("apply --dry-run output should use copy_planned= not copied=: %s", stdout)
}
if strings.Contains(stdout, "copied=") {
t.Fatalf("apply --dry-run output should not use copied=: %s", stdout)
}
if !strings.Contains(stdout, "INCLUDE file:") {
t.Fatalf("apply --dry-run output missing include status: %s", stdout)
}
}
func TestApplyDryRunJSON(t *testing.T) {
fx := setupFixture(t)
if err := os.Remove(filepath.Join(fx.wt, ".env")); err != nil && !errors.Is(err, os.ErrNotExist) {
t.Fatalf("remove .env: %v", err)
}
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--from", "auto", "--include", testIncludeFile, "--dry-run", "--json")
if code != 0 {
t.Fatalf("apply --dry-run --json exit code = %d, stderr=%s", code, stderr)
}
res := decodeSingleJSON(t, stdout)
if !res.DryRun {
t.Fatalf("expected dry_run=true in JSON output")
}
if res.Summary.CopyPlanned == 0 {
t.Fatalf("expected copy_planned > 0 in dry-run JSON summary, got %+v", res.Summary)
}
if res.Summary.Copied != 0 {
t.Fatalf("expected copied=0 in dry-run JSON summary, got %+v", res.Summary)
}
for _, a := range res.Actions {
if a.Op == "copy" && a.Status != "planned" {
t.Fatalf("expected all copy actions to have status=planned in dry-run, got %+v", a)
}
}
if _, err := os.Stat(filepath.Join(fx.wt, ".env")); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("dry-run should not create .env")
}
}
func TestGitExtensionInvocation(t *testing.T) {
fx := setupFixture(t)
binDir := filepath.Dir(testBinary)
env := []string{fmt.Sprintf("PATH=%s%c%s", binDir, os.PathListSeparator, os.Getenv("PATH"))}
stdout, stderr, code := runCmd(t, fx.wt, env, "git", "-C", fx.wt, "worktreeinclude", "apply", "--from", "auto", "--include", testIncludeFile, "--json")
if code != 0 {
t.Fatalf("git worktreeinclude apply failed: code=%d stderr=%s", code, stderr)
}
_ = decodeSingleJSON(t, stdout)
}
func TestRootCommandHelpAndNoImplicitApply(t *testing.T) {
fx := setupFixture(t)
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary)
if code != 0 {
t.Fatalf("root help exit code = %d stderr=%s", code, stderr)
}
if !strings.Contains(stdout, "NAME:") {
t.Fatalf("root help output missing NAME: %s", stdout)
}
_, stderr, code = runCmd(t, fx.wt, nil, testBinary, "--from", "auto")
if code != 2 {
t.Fatalf("expected exit code 2 for root --from auto, got %d", code)
}
if !strings.Contains(stderr, "Incorrect Usage") {
t.Fatalf("expected usage error for root --from auto, got: %s", stderr)
}
}
func TestRootVersionFlags(t *testing.T) {
fx := setupFixture(t)
prefix := "git-worktreeinclude version "
for _, args := range [][]string{{"--version"}, {"-v"}} {
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, args...)
if code != 0 {
t.Fatalf("version command failed for %v: code=%d stderr=%s", args, code, stderr)
}
if strings.TrimSpace(stderr) != "" {
t.Fatalf("expected empty stderr for %v, got %q", args, stderr)
}
line := strings.TrimSpace(stdout)
if !strings.HasPrefix(line, prefix) {
t.Fatalf("unexpected version output for %v: %q", args, line)
}
if strings.TrimSpace(strings.TrimPrefix(line, prefix)) == "" {
t.Fatalf("version value is empty for %v: %q", args, line)
}
}
}
func TestUnknownSubcommandAtRoot(t *testing.T) {
fx := setupFixture(t)
_, stderr, code := runCmd(t, fx.wt, nil, testBinary, "nope")
if code != 3 {
t.Fatalf("expected exit code 3 for unknown subcommand, got %d", code)
}
if !strings.Contains(stderr, "No help topic for 'nope'") {
t.Fatalf("unexpected stderr for unknown subcommand: %s", stderr)
}
}
func TestUsageErrorWritesHelpToStderr(t *testing.T) {
fx := setupFixture(t)
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--unknown")
if code != 2 {
t.Fatalf("expected exit code 2 for usage error, got %d", code)
}
if strings.TrimSpace(stdout) != "" {
t.Fatalf("expected no stdout for usage error, got: %q", stdout)
}
if !strings.Contains(stderr, "Incorrect Usage") || !strings.Contains(stderr, "NAME:") {
t.Fatalf("stderr should contain usage header and help: %s", stderr)
}
}
func TestApplyUsageValidationErrorsGoToStderr(t *testing.T) {
fx := setupFixture(t)
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "extra")
if code != 2 {
t.Fatalf("expected exit code 2 for apply positional usage error, got %d", code)
}
if strings.TrimSpace(stdout) != "" {
t.Fatalf("expected no stdout for apply usage error, got: %q", stdout)
}
if !strings.Contains(stderr, "apply does not accept positional arguments") {
t.Fatalf("stderr should contain apply usage detail: %s", stderr)
}
if !strings.Contains(stderr, "USAGE:") {
t.Fatalf("stderr should include apply help: %s", stderr)
}
}
func TestApplyQuietVerboseUsageValidationErrorsGoToStderr(t *testing.T) {
fx := setupFixture(t)
stdout, stderr, code := runCmd(t, fx.wt, nil, testBinary, "apply", "--dry-run", "--quiet", "--verbose")
if code != 2 {
t.Fatalf("expected exit code 2 for apply usage error, got %d", code)
}
if strings.TrimSpace(stdout) != "" {
t.Fatalf("expected no stdout for apply usage error, got: %q", stdout)
}
if !strings.Contains(stderr, "--quiet and --verbose cannot be used together") {
t.Fatalf("stderr should contain apply usage detail: %s", stderr)
}
if !strings.Contains(stderr, "USAGE:") {
t.Fatalf("stderr should include apply help: %s", stderr)
}
}
func TestMergeEnvOverridesExistingKey(t *testing.T) {
base := []string{"PATH=/usr/bin", "HOME=/tmp/home"}
overrides := []string{"PATH=/custom/bin:/usr/bin"}
merged := mergeEnv(base, overrides)
pathValue := ""
for _, kv := range merged {
if strings.HasPrefix(kv, "PATH=") {
pathValue = strings.TrimPrefix(kv, "PATH=")
break
}
}
if pathValue != "/custom/bin:/usr/bin" {
t.Fatalf("expected PATH override to win, got %q", pathValue)
}
}
func setupFixture(t *testing.T) fixture {
t.Helper()
base := t.TempDir()
repo := filepath.Join(base, "repo")
if err := os.MkdirAll(repo, 0o755); err != nil {
t.Fatalf("mkdir repo: %v", err)
}
runGit(t, repo, "init", "-q")
runGit(t, repo, "config", "user.name", "Test User")
runGit(t, repo, "config", "user.email", "test@example.com")
runGit(t, repo, "branch", "-M", "main")
writeFile(t, filepath.Join(repo, "README.md"), "tracked\n")
writeFile(t, filepath.Join(repo, ".gitignore"), ".env\n.env.local\n")
writeFile(t, filepath.Join(repo, testIncludeFile), ".env\n.env.local\nREADME.md\n")
runGit(t, repo, "add", "README.md", ".gitignore", testIncludeFile)
runGit(t, repo, "commit", "-q", "-m", "init")
writeFile(t, filepath.Join(repo, ".env"), "SOURCE_ENV\n")
writeFile(t, filepath.Join(repo, ".env.local"), "SOURCE_LOCAL\n")
wt := filepath.Join(base, "wt")
runGit(t, repo, "worktree", "add", "-q", wt, "-b", "feature")
return fixture{root: repo, wt: wt}
}
func runGit(t *testing.T, dir string, args ...string) string {
t.Helper()
stdout, stderr, code := runCmd(t, dir, nil, "git", append([]string{"-C", dir}, args...)...)
if code != 0 {
t.Fatalf("git %s failed: code=%d stderr=%s", strings.Join(args, " "), code, stderr)
}
return stdout
}
func writeFile(t *testing.T, path, content string) {
t.Helper()
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
func runCmd(t *testing.T, dir string, env []string, name string, args ...string) (stdout string, stderr string, exitCode int) {
t.Helper()
cmd := exec.Command(name, args...)
cmd.Dir = dir
cmd.Env = mergeEnv(os.Environ(), env)
var out bytes.Buffer
var errBuf bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errBuf
err := cmd.Run()
if err == nil {
return out.String(), errBuf.String(), 0
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return out.String(), errBuf.String(), exitErr.ExitCode()
}
t.Fatalf("failed to run command %s %v: %v", name, args, err)
return "", "", 1
}
func mergeEnv(base []string, overrides []string) []string {
merged := make(map[string]string, len(base)+len(overrides))
for _, kv := range base {
parts := strings.SplitN(kv, "=", 2)
if len(parts) != 2 {
continue
}
merged[parts[0]] = parts[1]
}
for _, kv := range overrides {
parts := strings.SplitN(kv, "=", 2)
if len(parts) != 2 {
continue
}
merged[parts[0]] = parts[1]
}
keys := make([]string, 0, len(merged))
for k := range merged {
keys = append(keys, k)
}
sort.Strings(keys)
out := make([]string, 0, len(keys))
for _, k := range keys {
out = append(out, k+"="+merged[k])
}
return out
}
func decodeSingleJSON(t *testing.T, raw string) jsonResult {
t.Helper()
dec := json.NewDecoder(strings.NewReader(raw))
var res jsonResult
if err := dec.Decode(&res); err != nil {
t.Fatalf("invalid JSON output: %v; raw=%q", err, raw)
}
if err := dec.Decode(&struct{}{}); err != io.EOF {
t.Fatalf("expected single JSON object, trailing data exists: %q", raw)
}
return res
}
func realPath(t *testing.T, p string) string {
t.Helper()
if rp, err := filepath.EvalSymlinks(p); err == nil {
return filepath.Clean(rp)
}
return filepath.Clean(p)
}