-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine_integration_test.go
More file actions
359 lines (313 loc) · 10.1 KB
/
engine_integration_test.go
File metadata and controls
359 lines (313 loc) · 10.1 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
package engine
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/satococoa/git-worktreeinclude/internal/exitcode"
)
type engineFixture struct {
root string
wt string
}
const testIncludeFile = ".test.worktreeinclude"
func TestEngineApplyCopiesIgnoredFiles(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
})
if err != nil {
t.Fatalf("Apply returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply exit code = %d, want %d", code, exitcode.OK)
}
if res.Summary.Copied != 2 {
t.Fatalf("expected 2 copied files, got %+v", res.Summary)
}
gotEnv, err := os.ReadFile(filepath.Join(fx.wt, ".env"))
if err != nil {
t.Fatalf("read copied .env: %v", err)
}
if string(gotEnv) != "SOURCE_ENV\n" {
t.Fatalf("unexpected .env content: %q", gotEnv)
}
for _, a := range res.Actions {
if a.Path == "README.md" {
t.Fatalf("tracked file must not be copied")
}
}
}
func TestEngineApplyConflictAndForce(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
writeFile(t, filepath.Join(fx.wt, ".env.local"), "TARGET_LOCAL\n")
_, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
})
if err != nil {
t.Fatalf("Apply returned error: %v", err)
}
if code != exitcode.Conflict {
t.Fatalf("Apply exit code = %d, want %d", code, exitcode.Conflict)
}
gotConflict, err := os.ReadFile(filepath.Join(fx.wt, ".env.local"))
if err != nil {
t.Fatalf("read conflict target .env.local: %v", err)
}
if string(gotConflict) != "TARGET_LOCAL\n" {
t.Fatalf("target should remain unchanged on conflict, got %q", gotConflict)
}
_, code, err = e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
Force: true,
})
if err != nil {
t.Fatalf("Apply --force returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply --force exit code = %d, want %d", code, exitcode.OK)
}
gotForced, err := os.ReadFile(filepath.Join(fx.wt, ".env.local"))
if err != nil {
t.Fatalf("read forced .env.local: %v", err)
}
if string(gotForced) != "SOURCE_LOCAL\n" {
t.Fatalf("target should be overwritten with --force, got %q", gotForced)
}
}
func TestEngineApplyIncludeValidationAndNoop(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: ".missing-worktreeinclude",
})
if err != nil {
t.Fatalf("Apply with missing include returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply with missing include exit code = %d, want %d", code, exitcode.OK)
}
if res.Summary.Matched != 0 || res.Summary.Copied != 0 || len(res.Actions) != 0 {
t.Fatalf("expected missing include no-op, got %+v", res.Summary)
}
outside := filepath.Join(filepath.Dir(fx.root), "outside.include")
writeFile(t, outside, ".env\n")
_, code, err = e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: outside,
})
if err == nil {
t.Fatalf("expected error for include outside repository")
}
if code != exitcode.Env {
t.Fatalf("Apply include outside exit code = %d, want %d", code, exitcode.Env)
}
if !strings.Contains(err.Error(), "include path must be inside source repository root") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestEngineApplyUsesSourceIncludeWhenTargetIncludeMissing(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
if err := os.Remove(filepath.Join(fx.wt, testIncludeFile)); err != nil {
t.Fatalf("remove target include: %v", err)
}
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
})
if err != nil {
t.Fatalf("Apply returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply exit code = %d, want %d", code, exitcode.OK)
}
if res.Summary.Copied != 2 {
t.Fatalf("expected 2 copied files, got %+v", res.Summary)
}
}
func TestEngineApplyNoopWhenSourceIncludeMissingEvenIfTargetHasInclude(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
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")
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
})
if err != nil {
t.Fatalf("Apply returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply exit code = %d, want %d", code, exitcode.OK)
}
if res.Summary.Matched != 0 || res.Summary.Copied != 0 || len(res.Actions) != 0 {
t.Fatalf("expected source-missing include no-op, got %+v", res.Summary)
}
if res.IncludeFound {
t.Fatalf("expected include to be missing")
}
if res.IncludeMissingHint != IncludeMissingHintSourceMissingTargetExists {
t.Fatalf("unexpected include hint: %q", res.IncludeMissingHint)
}
}
func TestEngineApplyReadsIncludeFileIgnoredByGlobalExcludes(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
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")
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: ".global.worktreeinclude",
})
if err != nil {
t.Fatalf("Apply returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply exit code = %d, want %d", code, exitcode.OK)
}
if res.Summary.Copied == 0 {
t.Fatalf("expected ignored include file to be read, got %+v", res.Summary)
}
}
func TestEngineApplyHintsWhenTargetIncludeIsSymlink(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlink behavior and permissions vary on Windows")
}
fx := setupEngineFixture(t)
e := NewEngine()
if err := os.Remove(filepath.Join(fx.root, testIncludeFile)); err != nil {
t.Fatalf("remove source include: %v", err)
}
if err := os.Remove(filepath.Join(fx.wt, testIncludeFile)); err != nil {
t.Fatalf("remove target include: %v", err)
}
brokenTarget := filepath.Join(filepath.Dir(fx.wt), "missing.include")
if err := os.Symlink(brokenTarget, filepath.Join(fx.wt, testIncludeFile)); err != nil {
t.Fatalf("create target symlink include: %v", err)
}
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
DryRun: true,
})
if err != nil {
t.Fatalf("Apply returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply exit code = %d, want %d", code, exitcode.OK)
}
if res.IncludeMissingHint != IncludeMissingHintSourceMissingTargetExists {
t.Fatalf("expected target-only include hint, got %q", res.IncludeMissingHint)
}
}
func TestEngineApplyDryRunIncludesMetadata(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
DryRun: true,
})
if err != nil {
t.Fatalf("Apply returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply exit code = %d, want %d", code, exitcode.OK)
}
if !res.IncludeFound {
t.Fatalf("expected include file to be found")
}
if res.PatternCount != 3 {
t.Fatalf("unexpected pattern count: got %d want 3", res.PatternCount)
}
}
func TestEngineApplyDryRunCopyPlanned(t *testing.T) {
fx := setupEngineFixture(t)
e := NewEngine()
res, code, err := e.Apply(context.Background(), fx.wt, ApplyOptions{
From: "auto",
Include: testIncludeFile,
DryRun: true,
})
if err != nil {
t.Fatalf("Apply dry-run returned error: %v", err)
}
if code != exitcode.OK {
t.Fatalf("Apply dry-run exit code = %d, want %d", code, exitcode.OK)
}
if !res.DryRun {
t.Fatalf("expected DryRun=true in result")
}
if res.Summary.CopyPlanned == 0 {
t.Fatalf("expected CopyPlanned > 0 in dry-run summary, got %+v", res.Summary)
}
if res.Summary.Copied != 0 {
t.Fatalf("expected Copied=0 in dry-run summary, got %+v", res.Summary)
}
for _, a := range res.Actions {
if a.Op == "copy" && a.Status != "planned" {
t.Fatalf("expected copy actions to have status=planned in dry-run, got %+v", a)
}
}
}
func TestErrorCodeFromCLIError(t *testing.T) {
err := &CLIError{Code: exitcode.Env, Msg: "x"}
if got := errorCode(err); got != exitcode.Env {
t.Fatalf("errorCode(CLIError) = %d, want %d", got, exitcode.Env)
}
if got := errorCode(errors.New("plain")); got != exitcode.Internal {
t.Fatalf("errorCode(plain) = %d, want %d", got, exitcode.Internal)
}
}
func setupEngineFixture(t *testing.T) engineFixture {
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 engineFixture{root: repo, wt: wt}
}
func runGit(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
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)
}
}