-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathsnapshot_test.go
More file actions
427 lines (369 loc) · 12.2 KB
/
snapshot_test.go
File metadata and controls
427 lines (369 loc) · 12.2 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
package preflight
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/google/uuid"
)
// initTestRepo creates a real git repository in a temp directory with an
// initial commit and a bare "origin" remote so that Snapshot can push.
// It returns the worktree path and a cleanup-aware test helper.
func initTestRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
worktree := filepath.Join(dir, "work")
bare := filepath.Join(dir, "origin.git")
// Create the bare remote.
runGit(t, "", "init", "--bare", bare)
// Create the working repo.
runGit(t, "", "init", worktree)
runGit(t, worktree, "config", "user.email", "test@test.com")
runGit(t, worktree, "config", "user.name", "Test")
// Create an initial commit so HEAD exists.
initial := filepath.Join(worktree, "README.md")
if err := os.WriteFile(initial, []byte("# test\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit(t, worktree, "add", ".")
runGit(t, worktree, "commit", "-m", "initial commit")
// Add the bare repo as origin.
runGit(t, worktree, "remote", "add", "origin", bare)
return worktree
}
func runGit(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
if dir != "" {
cmd.Dir = dir
}
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
}
return strings.TrimSpace(string(out))
}
func TestSnapshot_CommittedChanges(t *testing.T) {
worktree := initTestRepo(t)
// Add a tracked file change (but don't commit it).
if err := os.WriteFile(filepath.Join(worktree, "README.md"), []byte("# updated\n"), 0o644); err != nil {
t.Fatal(err)
}
preflightID := uuid.MustParse("00000000-0000-0000-0000-000000000001")
result, err := Snapshot(worktree, preflightID)
if err != nil {
t.Fatalf("Snapshot() error: %v", err)
}
if len(result.Commit) != 40 {
t.Errorf("expected 40-char SHA, got %q (len %d)", result.Commit, len(result.Commit))
}
// The commit should exist in the repo.
runGit(t, worktree, "cat-file", "-t", result.Commit)
// The snapshot tree should contain the updated content.
content := runGit(t, worktree, "show", result.Commit+":README.md")
if content != "# updated" {
t.Errorf("snapshot content = %q, want %q", content, "# updated")
}
// The remote branch should have been pushed.
remoteCommit := runGit(t, worktree, "ls-remote", "origin", result.Ref)
if !strings.Contains(remoteCommit, result.Commit) {
t.Errorf("remote branch does not contain commit %s, got %q", result.Commit, remoteCommit)
}
}
func TestSnapshot_UntrackedFiles(t *testing.T) {
worktree := initTestRepo(t)
// Add a brand new untracked file.
if err := os.WriteFile(filepath.Join(worktree, "new-file.txt"), []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
preflightID := uuid.MustParse("00000000-0000-0000-0000-000000000002")
result, err := Snapshot(worktree, preflightID)
if err != nil {
t.Fatalf("Snapshot() error: %v", err)
}
// The snapshot should include the untracked file.
content := runGit(t, worktree, "show", result.Commit+":new-file.txt")
if content != "hello" {
t.Errorf("untracked file content = %q, want %q", content, "hello")
}
}
func TestSnapshot_DoesNotModifyRealIndex(t *testing.T) {
worktree := initTestRepo(t)
// Create an untracked file.
if err := os.WriteFile(filepath.Join(worktree, "untracked.txt"), []byte("data\n"), 0o644); err != nil {
t.Fatal(err)
}
// Record the index state before snapshot.
statusBefore := runGit(t, worktree, "status", "--porcelain")
_, err := Snapshot(worktree, uuid.MustParse("00000000-0000-0000-0000-000000000003"))
if err != nil {
t.Fatalf("Snapshot() error: %v", err)
}
// The real index should be unchanged.
statusAfter := runGit(t, worktree, "status", "--porcelain")
if statusBefore != statusAfter {
t.Errorf("git status changed after Snapshot:\nbefore: %q\nafter: %q", statusBefore, statusAfter)
}
}
func TestSnapshot_UniquePreflightIDs(t *testing.T) {
worktree := initTestRepo(t)
// First snapshot.
result1, err := Snapshot(worktree, uuid.MustParse("00000000-0000-0000-0000-000000000004"))
if err != nil {
t.Fatalf("first Snapshot() error: %v", err)
}
// Modify a file and snapshot with a different preflight ID.
if err := os.WriteFile(filepath.Join(worktree, "README.md"), []byte("# v2\n"), 0o644); err != nil {
t.Fatal(err)
}
result2, err := Snapshot(worktree, uuid.MustParse("00000000-0000-0000-0000-000000000005"))
if err != nil {
t.Fatalf("second Snapshot() error: %v", err)
}
if result1.Commit == result2.Commit {
t.Error("expected different commits for different snapshots")
}
// Both remote branches should exist with their respective commits.
remote1 := runGit(t, worktree, "ls-remote", "origin", result1.Ref)
if !strings.Contains(remote1, result1.Commit) {
t.Errorf("run-1 branch should point to %s, got %q", result1.Commit, remote1)
}
remote2 := runGit(t, worktree, "ls-remote", "origin", result2.Ref)
if !strings.Contains(remote2, result2.Commit) {
t.Errorf("run-2 branch should point to %s, got %q", result2.Commit, remote2)
}
}
func TestSnapshotResult_ShortCommit(t *testing.T) {
tests := []struct {
name string
commit string
want string
}{
{
name: "full SHA is truncated to 10 chars",
commit: "abc123def456789000aabbccddeeff0011223344",
want: "abc123def4",
},
{
name: "exactly 10 chars",
commit: "abc123def4",
want: "abc123def4",
},
{
name: "short commit returned as-is",
commit: "abc",
want: "abc",
},
{
name: "empty commit",
commit: "",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := SnapshotResult{Commit: tt.commit}
if got := r.ShortCommit(); got != tt.want {
t.Errorf("ShortCommit() = %q, want %q", got, tt.want)
}
})
}
}
// setupDiffEnv creates a temp git index seeded from HEAD and returns the env
// slice for use with diffFiles. The caller can stage changes into this index
// using git commands with the returned env.
func setupDiffEnv(t *testing.T, worktree string) []string {
t.Helper()
tmp, err := os.CreateTemp("", "git-index-test-*")
if err != nil {
t.Fatal(err)
}
tmpIndex := tmp.Name()
tmp.Close()
t.Cleanup(func() { os.Remove(tmpIndex) })
env := append(os.Environ(), "GIT_INDEX_FILE="+tmpIndex)
cmd := exec.Command("git", "read-tree", "HEAD")
cmd.Dir = worktree
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git read-tree HEAD: %v\n%s", err, out)
}
return env
}
func TestDiffFiles(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T, worktree string, env []string)
want []FileChange
}{
{
name: "modified file",
setup: func(t *testing.T, worktree string, env []string) {
t.Helper()
os.WriteFile(filepath.Join(worktree, "README.md"), []byte("# changed\n"), 0o644)
cmd := exec.Command("git", "add", "README.md")
cmd.Dir = worktree
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git add: %v\n%s", err, out)
}
},
want: []FileChange{{Status: "M", Path: "README.md"}},
},
{
name: "added file",
setup: func(t *testing.T, worktree string, env []string) {
t.Helper()
os.WriteFile(filepath.Join(worktree, "new.txt"), []byte("new\n"), 0o644)
cmd := exec.Command("git", "add", "new.txt")
cmd.Dir = worktree
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git add: %v\n%s", err, out)
}
},
want: []FileChange{{Status: "A", Path: "new.txt"}},
},
{
name: "deleted file",
setup: func(t *testing.T, worktree string, env []string) {
t.Helper()
os.Remove(filepath.Join(worktree, "README.md"))
cmd := exec.Command("git", "add", "README.md")
cmd.Dir = worktree
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git add: %v\n%s", err, out)
}
},
want: []FileChange{{Status: "D", Path: "README.md"}},
},
{
name: "renamed file",
setup: func(t *testing.T, worktree string, env []string) {
t.Helper()
os.Rename(filepath.Join(worktree, "README.md"), filepath.Join(worktree, "DOCS.md"))
cmd := exec.Command("git", "add", "-A")
cmd.Dir = worktree
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git add: %v\n%s", err, out)
}
},
want: []FileChange{{Status: "R", Path: "DOCS.md"}},
},
{
name: "file with spaces in name",
setup: func(t *testing.T, worktree string, env []string) {
t.Helper()
os.WriteFile(filepath.Join(worktree, "my file.txt"), []byte("data\n"), 0o644)
cmd := exec.Command("git", "add", "my file.txt")
cmd.Dir = worktree
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git add: %v\n%s", err, out)
}
},
want: []FileChange{{Status: "A", Path: "my file.txt"}},
},
{
name: "no changes",
setup: func(t *testing.T, worktree string, env []string) {
t.Helper()
},
want: nil,
},
{
name: "multiple changes",
setup: func(t *testing.T, worktree string, env []string) {
t.Helper()
os.WriteFile(filepath.Join(worktree, "README.md"), []byte("# v2\n"), 0o644)
os.WriteFile(filepath.Join(worktree, "a.txt"), []byte("a\n"), 0o644)
os.WriteFile(filepath.Join(worktree, "b.txt"), []byte("b\n"), 0o644)
cmd := exec.Command("git", "add", "-A")
cmd.Dir = worktree
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git add: %v\n%s", err, out)
}
},
want: []FileChange{
{Status: "M", Path: "README.md"},
{Status: "A", Path: "a.txt"},
{Status: "A", Path: "b.txt"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
worktree := initTestRepo(t)
env := setupDiffEnv(t, worktree)
tt.setup(t, worktree, env)
got, err := diffFiles(worktree, env, false)
if err != nil {
t.Fatalf("diffFiles() error: %v", err)
}
if len(got) != len(tt.want) {
t.Fatalf("diffFiles() returned %d files, want %d\ngot: %+v", len(got), len(tt.want), got)
}
for i := range tt.want {
if got[i].Status != tt.want[i].Status {
t.Errorf("file[%d].Status = %q, want %q", i, got[i].Status, tt.want[i].Status)
}
if got[i].Path != tt.want[i].Path {
t.Errorf("file[%d].Path = %q, want %q", i, got[i].Path, tt.want[i].Path)
}
}
})
}
}
func TestSnapshot_NoRemote(t *testing.T) {
worktree := initTestRepo(t)
// Remove the origin remote.
runGit(t, worktree, "remote", "remove", "origin")
// Add a change so we exercise the commit path too.
if err := os.WriteFile(filepath.Join(worktree, "README.md"), []byte("# changed\n"), 0o644); err != nil {
t.Fatal(err)
}
preflightID := uuid.MustParse("00000000-0000-0000-0000-000000000010")
result, err := Snapshot(worktree, preflightID)
if err != nil {
t.Fatalf("Snapshot() error: %v", err)
}
if !result.PushSkipped {
t.Error("expected PushSkipped to be true when no remote exists")
}
// The commit should still be valid.
if len(result.Commit) != 40 {
t.Errorf("expected 40-char SHA, got %q", result.Commit)
}
}
func TestSnapshot_CleanWorktree(t *testing.T) {
worktree := initTestRepo(t)
preflightID := uuid.MustParse("00000000-0000-0000-0000-000000000006")
result, err := Snapshot(worktree, preflightID)
if err != nil {
t.Fatalf("Snapshot() error: %v", err)
}
head := runGit(t, worktree, "rev-parse", "HEAD")
// Even with a clean worktree a new commit should always be created so
// that commit statuses are attributed to the preflight run rather than
// the shared HEAD commit.
if result.Commit == head {
t.Errorf("expected a new commit distinct from HEAD %s, but got the same SHA", head)
}
if len(result.Files) != 0 {
t.Errorf("expected no changed files, got %d", len(result.Files))
}
// The new commit should be reachable and its parent should be HEAD.
parent := runGit(t, worktree, "rev-parse", result.Commit+"^")
if parent != head {
t.Errorf("expected parent of snapshot commit to be HEAD %s, got %s", head, parent)
}
// The remote branch should exist and point to the new commit.
remoteRef := runGit(t, worktree, "ls-remote", "origin", result.Ref)
if !strings.Contains(remoteRef, result.Commit) {
t.Errorf("remote branch should point to snapshot commit %s, got %q", result.Commit, remoteRef)
}
}