-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_test.go
More file actions
575 lines (482 loc) · 15.2 KB
/
git_test.go
File metadata and controls
575 lines (482 loc) · 15.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
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
package git
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// initTestRepo creates a temporary git repository with an initial commit.
// Returns the path to the repository.
func initTestRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
cmds := [][]string{
{"git", "init"},
{"git", "config", "user.email", "test@example.com"},
{"git", "config", "user.name", "Test User"},
}
for _, args := range cmds {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
require.NoError(t, err, "failed to run %v: %s", args, string(out))
}
// Create a file and commit it so HEAD exists.
require.NoError(t, os.WriteFile(filepath.Join(dir, "README.md"), []byte("# Test\n"), 0644))
cmds = [][]string{
{"git", "add", "README.md"},
{"git", "commit", "-m", "initial commit"},
}
for _, args := range cmds {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
require.NoError(t, err, "failed to run %v: %s", args, string(out))
}
return dir
}
// --- RepoStatus method tests ---
func TestRepoStatus_IsDirty(t *testing.T) {
tests := []struct {
name string
status RepoStatus
expected bool
}{
{
name: "clean repo",
status: RepoStatus{},
expected: false,
},
{
name: "modified files",
status: RepoStatus{Modified: 3},
expected: true,
},
{
name: "untracked files",
status: RepoStatus{Untracked: 1},
expected: true,
},
{
name: "staged files",
status: RepoStatus{Staged: 2},
expected: true,
},
{
name: "all types dirty",
status: RepoStatus{Modified: 1, Untracked: 2, Staged: 3},
expected: true,
},
{
name: "only ahead is not dirty",
status: RepoStatus{Ahead: 5},
expected: false,
},
{
name: "only behind is not dirty",
status: RepoStatus{Behind: 2},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.status.IsDirty())
})
}
}
func TestRepoStatus_HasUnpushed(t *testing.T) {
tests := []struct {
name string
status RepoStatus
expected bool
}{
{
name: "no commits ahead",
status: RepoStatus{Ahead: 0},
expected: false,
},
{
name: "commits ahead",
status: RepoStatus{Ahead: 3},
expected: true,
},
{
name: "behind but not ahead",
status: RepoStatus{Behind: 5},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.status.HasUnpushed())
})
}
}
func TestRepoStatus_HasUnpulled(t *testing.T) {
tests := []struct {
name string
status RepoStatus
expected bool
}{
{
name: "no commits behind",
status: RepoStatus{Behind: 0},
expected: false,
},
{
name: "commits behind",
status: RepoStatus{Behind: 2},
expected: true,
},
{
name: "ahead but not behind",
status: RepoStatus{Ahead: 3},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.status.HasUnpulled())
})
}
}
// --- GitError tests ---
func TestGitError_Error(t *testing.T) {
tests := []struct {
name string
err *GitError
expected string
}{
{
name: "stderr takes precedence",
err: &GitError{Args: []string{"status"}, Err: errors.New("exit 1"), Stderr: "fatal: not a git repository"},
expected: "git command \"git status\" failed: fatal: not a git repository",
},
{
name: "falls back to underlying error",
err: &GitError{Args: []string{"status"}, Err: errors.New("exit status 128"), Stderr: ""},
expected: "git command \"git status\" failed: exit status 128",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.err.Error())
})
}
}
func TestGitError_Unwrap(t *testing.T) {
inner := errors.New("underlying error")
gitErr := &GitError{Err: inner, Stderr: "stderr output"}
assert.Equal(t, inner, gitErr.Unwrap())
assert.True(t, errors.Is(gitErr, inner))
}
// --- IsNonFastForward tests ---
func TestIsNonFastForward(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "non-fast-forward message",
err: errors.New("! [rejected] main -> main (non-fast-forward)"),
expected: true,
},
{
name: "fetch first message",
err: errors.New("Updates were rejected because the remote contains work that you do not have locally. fetch first"),
expected: true,
},
{
name: "tip behind message",
err: errors.New("Updates were rejected because the tip of your current branch is behind"),
expected: true,
},
{
name: "unrelated error",
err: errors.New("connection refused"),
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, IsNonFastForward(tt.err))
})
}
}
// --- gitCommand tests with real git repos ---
func TestGitCommand_Good(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
out, err := gitCommand(context.Background(), dir, "rev-parse", "--abbrev-ref", "HEAD")
require.NoError(t, err)
// Default branch could be main or master depending on git config.
branch := out
assert.NotEmpty(t, branch)
}
func TestGitCommand_Bad_InvalidDir(t *testing.T) {
_, err := gitCommand(context.Background(), "/nonexistent/path", "status")
require.Error(t, err)
}
func TestGitCommand_Bad_NotARepo(t *testing.T) {
dir, _ := filepath.Abs(t.TempDir())
_, err := gitCommand(context.Background(), dir, "status")
require.Error(t, err)
// Should be a GitError with stderr.
var gitErr *GitError
if errors.As(err, &gitErr) {
assert.Contains(t, gitErr.Stderr, "not a git repository")
assert.Equal(t, []string{"status"}, gitErr.Args)
}
}
// --- getStatus integration tests ---
func TestGetStatus_Good_CleanRepo(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
status := getStatus(context.Background(), dir, "test-repo")
require.NoError(t, status.Error)
assert.Equal(t, "test-repo", status.Name)
assert.Equal(t, dir, status.Path)
assert.NotEmpty(t, status.Branch)
assert.False(t, status.IsDirty())
}
func TestGetStatus_Good_ModifiedFile(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// Modify the existing tracked file.
require.NoError(t, os.WriteFile(filepath.Join(dir, "README.md"), []byte("# Modified\n"), 0644))
status := getStatus(context.Background(), dir, "modified-repo")
require.NoError(t, status.Error)
assert.Equal(t, 1, status.Modified)
assert.True(t, status.IsDirty())
}
func TestGetStatus_Good_UntrackedFile(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// Create a new untracked file.
require.NoError(t, os.WriteFile(filepath.Join(dir, "newfile.txt"), []byte("hello"), 0644))
status := getStatus(context.Background(), dir, "untracked-repo")
require.NoError(t, status.Error)
assert.Equal(t, 1, status.Untracked)
assert.True(t, status.IsDirty())
}
func TestGetStatus_Good_StagedFile(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// Create and stage a new file.
require.NoError(t, os.WriteFile(filepath.Join(dir, "staged.txt"), []byte("staged"), 0644))
cmd := exec.Command("git", "add", "staged.txt")
cmd.Dir = dir
require.NoError(t, cmd.Run())
status := getStatus(context.Background(), dir, "staged-repo")
require.NoError(t, status.Error)
assert.Equal(t, 1, status.Staged)
assert.True(t, status.IsDirty())
}
func TestGetStatus_Good_MixedChanges(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// Create untracked file.
require.NoError(t, os.WriteFile(filepath.Join(dir, "untracked.txt"), []byte("new"), 0644))
// Modify tracked file.
require.NoError(t, os.WriteFile(filepath.Join(dir, "README.md"), []byte("# Changed\n"), 0644))
// Create and stage another file.
require.NoError(t, os.WriteFile(filepath.Join(dir, "staged.txt"), []byte("staged"), 0644))
cmd := exec.Command("git", "add", "staged.txt")
cmd.Dir = dir
require.NoError(t, cmd.Run())
status := getStatus(context.Background(), dir, "mixed-repo")
require.NoError(t, status.Error)
assert.Equal(t, 1, status.Modified, "expected 1 modified file")
assert.Equal(t, 1, status.Untracked, "expected 1 untracked file")
assert.Equal(t, 1, status.Staged, "expected 1 staged file")
assert.True(t, status.IsDirty())
}
func TestGetStatus_Good_DeletedTrackedFile(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// Delete the tracked file (unstaged deletion).
require.NoError(t, os.Remove(filepath.Join(dir, "README.md")))
status := getStatus(context.Background(), dir, "deleted-repo")
require.NoError(t, status.Error)
assert.Equal(t, 1, status.Modified, "deletion in working tree counts as modified")
assert.True(t, status.IsDirty())
}
func TestGetStatus_Good_StagedDeletion(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// Stage a deletion.
cmd := exec.Command("git", "rm", "README.md")
cmd.Dir = dir
require.NoError(t, cmd.Run())
status := getStatus(context.Background(), dir, "staged-delete-repo")
require.NoError(t, status.Error)
assert.Equal(t, 1, status.Staged, "staged deletion counts as staged")
assert.True(t, status.IsDirty())
}
func TestGetStatus_Bad_InvalidPath(t *testing.T) {
status := getStatus(context.Background(), "/nonexistent/path", "bad-repo")
assert.Error(t, status.Error)
assert.Equal(t, "bad-repo", status.Name)
}
func TestGetStatus_Bad_RelativePath(t *testing.T) {
status := getStatus(context.Background(), "relative/path", "rel-repo")
assert.Error(t, status.Error)
assert.Contains(t, status.Error.Error(), "path must be absolute")
assert.Equal(t, "rel-repo", status.Name)
}
// --- Status (parallel multi-repo) tests ---
func TestStatus_Good_MultipleRepos(t *testing.T) {
dir1, _ := filepath.Abs(initTestRepo(t))
dir2, _ := filepath.Abs(initTestRepo(t))
// Make dir2 dirty.
require.NoError(t, os.WriteFile(filepath.Join(dir2, "extra.txt"), []byte("extra"), 0644))
results := Status(context.Background(), StatusOptions{
Paths: []string{dir1, dir2},
Names: map[string]string{
dir1: "clean-repo",
dir2: "dirty-repo",
},
})
require.Len(t, results, 2)
assert.Equal(t, "clean-repo", results[0].Name)
assert.NoError(t, results[0].Error)
assert.False(t, results[0].IsDirty())
assert.Equal(t, "dirty-repo", results[1].Name)
assert.NoError(t, results[1].Error)
assert.True(t, results[1].IsDirty())
}
func TestStatus_Good_EmptyPaths(t *testing.T) {
results := Status(context.Background(), StatusOptions{
Paths: []string{},
})
assert.Empty(t, results)
}
func TestStatus_Good_NameFallback(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// No name mapping — path should be used as name.
results := Status(context.Background(), StatusOptions{
Paths: []string{dir},
Names: map[string]string{},
})
require.Len(t, results, 1)
assert.Equal(t, dir, results[0].Name, "name should fall back to path")
}
func TestStatus_Good_WithErrors(t *testing.T) {
validDir, _ := filepath.Abs(initTestRepo(t))
invalidDir, _ := filepath.Abs("/nonexistent/path")
results := Status(context.Background(), StatusOptions{
Paths: []string{validDir, invalidDir},
Names: map[string]string{
validDir: "good",
invalidDir: "bad",
},
})
require.Len(t, results, 2)
assert.NoError(t, results[0].Error)
assert.Error(t, results[1].Error)
}
// --- PushMultiple tests ---
func TestPushMultiple_Good_NoRemote(t *testing.T) {
// Push without a remote will fail but we can test the result structure.
dir, _ := filepath.Abs(initTestRepo(t))
results, err := PushMultiple(context.Background(), []string{dir}, map[string]string{
dir: "test-repo",
})
assert.Error(t, err)
require.Len(t, results, 1)
assert.Equal(t, "test-repo", results[0].Name)
assert.Equal(t, dir, results[0].Path)
// Push without remote should fail.
assert.False(t, results[0].Success)
assert.Error(t, results[0].Error)
}
func TestPushMultiple_Good_NameFallback(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
results, err := PushMultiple(context.Background(), []string{dir}, map[string]string{})
assert.Error(t, err)
require.Len(t, results, 1)
assert.Equal(t, dir, results[0].Name, "name should fall back to path")
}
// --- Pull tests ---
func TestPull_Bad_NoRemote(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
err := Pull(context.Background(), dir)
assert.Error(t, err, "pull without remote should fail")
}
// --- Push tests ---
func TestPush_Bad_NoRemote(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
err := Push(context.Background(), dir)
assert.Error(t, err, "push without remote should fail")
}
// --- Context cancellation test ---
func TestGetStatus_Good_ContextCancellation(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately.
status := getStatus(ctx, dir, "cancelled-repo")
// With a cancelled context, the git commands should fail.
assert.Error(t, status.Error)
}
// --- getAheadBehind with a tracking branch ---
func TestGetAheadBehind_Good_WithUpstream(t *testing.T) {
// Create a bare remote and a clone to test ahead/behind counts.
bareDir, _ := filepath.Abs(t.TempDir())
cloneDir, _ := filepath.Abs(t.TempDir())
// Initialise the bare repo.
cmd := exec.Command("git", "init", "--bare")
cmd.Dir = bareDir
require.NoError(t, cmd.Run())
// Clone it.
cmd = exec.Command("git", "clone", bareDir, cloneDir)
require.NoError(t, cmd.Run())
// Configure user in clone.
for _, args := range [][]string{
{"git", "config", "user.email", "test@example.com"},
{"git", "config", "user.name", "Test User"},
} {
cmd = exec.Command(args[0], args[1:]...)
cmd.Dir = cloneDir
require.NoError(t, cmd.Run())
}
// Create initial commit and push.
require.NoError(t, os.WriteFile(filepath.Join(cloneDir, "file.txt"), []byte("v1"), 0644))
for _, args := range [][]string{
{"git", "add", "."},
{"git", "commit", "-m", "initial"},
{"git", "push", "origin", "HEAD"},
} {
cmd = exec.Command(args[0], args[1:]...)
cmd.Dir = cloneDir
out, err := cmd.CombinedOutput()
require.NoError(t, err, "command %v failed: %s", args, string(out))
}
// Make a local commit without pushing (ahead by 1).
require.NoError(t, os.WriteFile(filepath.Join(cloneDir, "file.txt"), []byte("v2"), 0644))
for _, args := range [][]string{
{"git", "add", "."},
{"git", "commit", "-m", "local commit"},
} {
cmd = exec.Command(args[0], args[1:]...)
cmd.Dir = cloneDir
require.NoError(t, cmd.Run())
}
ahead, behind, err := getAheadBehind(context.Background(), cloneDir)
assert.NoError(t, err)
assert.Equal(t, 1, ahead, "should be 1 commit ahead")
assert.Equal(t, 0, behind, "should not be behind")
}
// --- Renamed file detection ---
func TestGetStatus_Good_RenamedFile(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
// Rename via git mv (stages the rename).
cmd := exec.Command("git", "mv", "README.md", "GUIDE.md")
cmd.Dir = dir
require.NoError(t, cmd.Run())
status := getStatus(context.Background(), dir, "renamed-repo")
require.NoError(t, status.Error)
assert.Equal(t, 1, status.Staged, "rename should count as staged")
assert.True(t, status.IsDirty())
}