-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_test.go
More file actions
394 lines (324 loc) · 10.4 KB
/
git_test.go
File metadata and controls
394 lines (324 loc) · 10.4 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
package main
import (
"maps"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
func requireNoError(t *testing.T, err error, msg ...any) {
t.Helper()
if err == nil {
return
}
if len(msg) == 1 {
t.Log(msg[0].(string))
} else if len(msg) > 1 {
t.Logf(msg[0].(string), msg[1:]...)
}
if ee, ok := err.(*exec.ExitError); ok {
t.Log("STDERR:", string(ee.Stderr))
}
t.Fatalf("expected no error, got: %s", err.Error())
}
type testRepository struct {
t *testing.T
root string
}
func (tr *testRepository) init() {
tr.root = tr.t.TempDir()
tr.git("init")
tr.git("config", "user.name", "A U Thor")
tr.git("config", "user.email", "author@home.arpa")
}
func (tr *testRepository) git(args ...string) []byte {
cmd := exec.Command("git", args...)
cmd.Dir = tr.root
out, err := cmd.Output()
requireNoError(tr.t, err)
return out
}
func (tr *testRepository) path(p ...string) string {
return filepath.Join(append([]string{tr.root}, p...)...)
}
func testRepo(t *testing.T) *testRepository {
t.Helper()
tr := &testRepository{t: t}
tr.init()
return tr
}
func TestCommitHashes(t *testing.T) {
testcases := []struct {
input string
ok bool
}{{
"deadbeef", true,
}, {
"HEAD", false,
}, {
"f8034fe40034a602c232b8cbe06ab79e518f71c1", true,
}, {
"fee", false,
}, {
"f", false,
}, {
"", false,
}, {
strings.Repeat("a", 40), true,
}, {
strings.Repeat("a", 41), false,
}}
for _, tc := range testcases {
t.Run(tc.input, func(t *testing.T) {
want := tc.ok
got := hashRegex.MatchString(tc.input)
if want != got {
t.Fatalf("commit hash check mismatch; got=%t, want=%t", got, want)
}
})
}
}
func TestCommitsSince(t *testing.T) {
tr := testRepo(t)
// Create a few commits
requireNoError(t, os.WriteFile(tr.path("file1"), []byte("content1"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "first commit")
hash1 := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
requireNoError(t, os.WriteFile(tr.path("file2"), []byte("content2"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "second commit")
hash2 := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
requireNoError(t, os.WriteFile(tr.path("file3"), []byte("content3"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "third commit")
hash3 := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
r := &Repository{path: tr.root}
t.Run("commits since first", func(t *testing.T) {
commits, err := r.CommitsSince(hash1)
requireNoError(t, err)
if len(commits) != 2 {
t.Fatalf("expected 2 commits, got %d: %v", len(commits), commits)
}
if commits[0] != hash2 || commits[1] != hash3 {
t.Errorf("expected [%s, %s], got %v", hash2, hash3, commits)
}
})
t.Run("commits since second", func(t *testing.T) {
commits, err := r.CommitsSince(hash2)
requireNoError(t, err)
if len(commits) != 1 {
t.Fatalf("expected 1 commit, got %d: %v", len(commits), commits)
}
if commits[0] != hash3 {
t.Errorf("expected [%s], got %v", hash3, commits)
}
})
t.Run("commits since HEAD (none)", func(t *testing.T) {
commits, err := r.CommitsSince(hash3)
requireNoError(t, err)
if len(commits) != 0 {
t.Errorf("expected no commits, got %v", commits)
}
})
t.Run("invalid base", func(t *testing.T) {
_, err := r.CommitsSince("nonexistent-ref-12345")
if err == nil {
t.Error("expected error for invalid reference")
}
})
t.Run("diverged history", func(t *testing.T) {
// Create a separate branch with different history
tr.git("checkout", "-b", "other-branch", hash1)
requireNoError(t, os.WriteFile(tr.path("other-file"), []byte("other"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "commit on other branch")
otherHash := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
// Go back to main branch
tr.git("checkout", "-")
// otherHash is not an ancestor of HEAD (hash3)
_, err := r.CommitsSince(otherHash)
if err == nil {
t.Error("expected error for diverged history")
}
if !strings.Contains(err.Error(), "not an ancestor") {
t.Errorf("expected 'not an ancestor' error, got: %v", err)
}
})
}
func TestCommitsBetween(t *testing.T) {
tr := testRepo(t)
// Create a few commits
requireNoError(t, os.WriteFile(tr.path("file1"), []byte("content1"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "first commit")
hash1 := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
requireNoError(t, os.WriteFile(tr.path("file2"), []byte("content2"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "second commit")
hash2 := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
requireNoError(t, os.WriteFile(tr.path("file3"), []byte("content3"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "third commit")
hash3 := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
r := &Repository{path: tr.root}
t.Run("commits between first and third", func(t *testing.T) {
commits, err := r.CommitsBetween(hash1, hash3)
requireNoError(t, err)
if len(commits) != 2 {
t.Fatalf("expected 2 commits, got %d: %v", len(commits), commits)
}
if commits[0] != hash2 || commits[1] != hash3 {
t.Errorf("expected [%s, %s], got %v", hash2, hash3, commits)
}
})
t.Run("commits between first and second", func(t *testing.T) {
commits, err := r.CommitsBetween(hash1, hash2)
requireNoError(t, err)
if len(commits) != 1 {
t.Fatalf("expected 1 commit, got %d: %v", len(commits), commits)
}
if commits[0] != hash2 {
t.Errorf("expected [%s], got %v", hash2, commits)
}
})
t.Run("commits between same commit (none)", func(t *testing.T) {
commits, err := r.CommitsBetween(hash3, hash3)
requireNoError(t, err)
if len(commits) != 0 {
t.Errorf("expected no commits, got %v", commits)
}
})
t.Run("diverged history", func(t *testing.T) {
// Create a separate branch with different history
tr.git("checkout", "-b", "other-branch", hash1)
requireNoError(t, os.WriteFile(tr.path("other-file"), []byte("other"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "commit on other branch")
otherHash := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
// Go back to main branch
tr.git("checkout", "-")
// otherHash is not an ancestor of hash3
_, err := r.CommitsBetween(otherHash, hash3)
if err == nil {
t.Error("expected error for diverged history")
}
if !strings.Contains(err.Error(), "not an ancestor") {
t.Errorf("expected 'not an ancestor' error, got: %v", err)
}
})
}
func TestStagedChanges(t *testing.T) {
tr := testRepo(t)
// Create initial commit
requireNoError(t, os.WriteFile(tr.path("existing.txt"), []byte("original"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "initial")
r := &Repository{path: tr.root}
t.Run("no staged changes", func(t *testing.T) {
changes, err := r.StagedChanges()
requireNoError(t, err)
if len(changes) != 0 {
t.Errorf("expected empty changes, got %d", len(changes))
}
})
t.Run("staged addition", func(t *testing.T) {
requireNoError(t, os.WriteFile(tr.path("new.txt"), []byte("new content"), 0o644))
tr.git("add", "new.txt")
changes, err := r.StagedChanges()
requireNoError(t, err)
if len(changes) != 1 {
t.Fatalf("expected 1 change, got %d", len(changes))
}
if string(changes["new.txt"].Content) != "new content" {
t.Errorf("unexpected content: %q", changes["new.txt"].Content)
}
if changes["new.txt"].Mode != "100644" {
t.Errorf("unexpected mode: %q", changes["new.txt"].Mode)
}
// Cleanup
tr.git("reset", "HEAD", "new.txt")
os.Remove(tr.path("new.txt"))
})
t.Run("staged executable", func(t *testing.T) {
requireNoError(t, os.WriteFile(tr.path("script.sh"), []byte("#!/bin/bash\necho hello"), 0o755))
tr.git("add", "script.sh")
changes, err := r.StagedChanges()
requireNoError(t, err)
if len(changes) != 1 {
t.Fatalf("expected 1 change, got %d", len(changes))
}
if changes["script.sh"].Mode != "100755" {
t.Errorf("expected executable mode 100755, got %q", changes["script.sh"].Mode)
}
// Cleanup
tr.git("reset", "HEAD", "script.sh")
os.Remove(tr.path("script.sh"))
})
t.Run("staged modification", func(t *testing.T) {
requireNoError(t, os.WriteFile(tr.path("existing.txt"), []byte("modified"), 0o644))
tr.git("add", "existing.txt")
changes, err := r.StagedChanges()
requireNoError(t, err)
if len(changes) != 1 {
t.Fatalf("expected 1 change, got %d", len(changes))
}
if string(changes["existing.txt"].Content) != "modified" {
t.Errorf("unexpected content: %q", changes["existing.txt"].Content)
}
// Cleanup - restore file to original state
tr.git("checkout", "HEAD", "--", "existing.txt")
})
t.Run("staged deletion", func(t *testing.T) {
tr.git("rm", "-f", "existing.txt")
changes, err := r.StagedChanges()
requireNoError(t, err)
if len(changes) != 1 {
t.Fatalf("expected 1 change, got %d", len(changes))
}
if changes["existing.txt"].Content != nil {
t.Errorf("expected nil for deletion, got %q", changes["existing.txt"].Content)
}
// Cleanup - restore file
tr.git("reset", "HEAD", "existing.txt")
tr.git("checkout", "existing.txt")
})
}
func TestChangedFiles(t *testing.T) {
// First, prep the test repository
tr := testRepo(t)
requireNoError(t, os.WriteFile(tr.path("file"), []byte("content"), 0o644))
requireNoError(t, os.WriteFile(tr.path("to-empty"), []byte("content"), 0o644))
requireNoError(t, os.WriteFile(tr.path("to-delete"), []byte("content"), 0o644))
tr.git("add", "-A")
tr.git("commit", "--message", "initial commit")
requireNoError(t, os.Truncate(tr.path("to-empty"), 0))
requireNoError(t, os.Remove(tr.path("to-delete")))
tr.git("add", "-A")
tr.git("commit", "--message", "second commit")
hash := strings.TrimSpace(string(tr.git("rev-parse", "HEAD")))
r := &Repository{path: tr.root}
changes, err := r.Changes(hash)
requireNoError(t, err)
if len(changes) != 1 {
t.Fatalf("expected 1 change, got %d", len(changes))
}
change := changes[0]
if len(change.entries) != 2 {
t.Fatalf("expected 2 entries, got %d", len(change.entries))
}
keys := slices.Sorted(maps.Keys(change.entries))
if keys[0] != "to-delete" || keys[1] != "to-empty" {
t.Fatalf("expected changed files to be 'to-delete' and 'to-empty', got %q", keys)
}
if change.entries["to-empty"].Content == nil {
t.Log("expected to-empty to have empty content, not nil")
t.Fail()
}
if change.entries["to-delete"].Content != nil {
t.Logf("expected to-delete to have nil content, got %q", change.entries["to-delete"].Content)
t.Fail()
}
}