-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_test.go
816 lines (692 loc) · 25 KB
/
git_test.go
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
package git
import (
"bytes"
"github.com/eighty4/maestro/testutil"
"github.com/eighty4/maestro/util"
"github.com/stretchr/testify/assert"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestMain(m *testing.M) {
util.InitLoggingWithLevel("ERROR")
m.Run()
}
func gitIntegrationTest(t *testing.T) {
//if os.Getenv("MAESTRO_TEST_GIT") != "true" {
// t.Skip("skipping git integration tests")
//}
}
func skipOnCi(t *testing.T) {
if os.Getenv("MAESTRO_CI") == "true" {
t.Skip("skipping on ci test run")
}
}
func testCloneChannel(t *testing.T, c <-chan *CloneUpdate, expStatus CloneStatus, expMessage string) {
u, ok := <-c
assert.True(t, ok)
assert.Equal(t, Cloning, u.Status)
assert.Equal(t, "", u.Error)
u, ok = <-c
assert.True(t, ok)
assert.Equal(t, expStatus, u.Status)
assert.Equal(t, expMessage, u.Error)
u, ok = <-c
assert.False(t, ok)
assert.Nil(t, u)
}
func TestClone_IntoExistingDir(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
c := Clone(dir, "https://github.com/eighty4/sse")
testCloneChannel(t, c, Cloned, "")
}
func TestClone_IntoNewDir(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
c := Clone(filepath.Join(dir, "sse"), "https://github.com/eighty4/sse")
testCloneChannel(t, c, Cloned, "")
}
const cloneAuthRequired = `
Cloning into 'asdf'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled`
func TestMakeCloneErrorUpdate_ForAuthRequired(t *testing.T) {
u := makeCloneErrorUpdate(cloneAuthRequired[1:])
assert.Equal(t, AuthRequired, u.Status)
assert.Equal(t, "authentication required", u.Error)
}
func TestClone_Fails_WithAuthFailed(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
c := Clone(dir, "https://github.com/eighty4/asdf")
testCloneChannel(t, c, AuthRequired, "authentication required")
}
const cloneBadRedirect = `
fatal: unable to update url base from redirection:
asked for: https://yahoo.com/info/refs?service=git-upload-pack
redirect: https://www.yahoo.com/`
func TestMakeCloneErrorUpdate_ForBadRedirect(t *testing.T) {
u := makeCloneErrorUpdate(cloneBadRedirect[1:])
assert.Equal(t, BadRedirect, u.Status)
assert.Equal(t, "following http redirect did not connect to a git repository", u.Error)
}
func TestClone_Fails_WithBadRedirect(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
c := Clone(dir, "https://yahoo.com")
testCloneChannel(t, c, BadRedirect, "following http redirect did not connect to a git repository")
}
const cloneNotFound = `
remote: Not Found
fatal: repository 'https://github.com/asdgsadgasdgasgasdg/' not found`
func TestMakeCloneErrorUpdate_ForRepoNotFound(t *testing.T) {
u := makeCloneErrorUpdate(cloneNotFound[1:])
assert.Equal(t, CloneRepoNotFound, u.Status)
assert.Equal(t, "repository not found", u.Error)
}
func TestClone_Fails_WithNotFound(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
c := Clone(dir, "https://github.com/asdgsadgasdgasgasdg")
testCloneChannel(t, c, CloneRepoNotFound, "repository not found")
}
func testPullChannel(t *testing.T, p <-chan *PullUpdate, expStatus PullStatus, expMessage string, expRepoState *RepoState, expStashList []*StashedChangeset) {
u, ok := <-p
assert.True(t, ok)
assert.Equal(t, Pulling, u.Status)
assert.Equal(t, "", u.Error)
u, ok = <-p
assert.True(t, ok)
assert.Equal(t, expStatus, u.Status)
assert.Equal(t, expMessage, u.Error)
if expRepoState == nil {
assert.Nil(t, u.RepoState)
} else {
assert.Equal(t, expRepoState.LocalCommits, u.RepoState.LocalCommits)
}
if expStashList == nil {
assert.Nil(t, u.StashList)
} else {
assert.Equal(t, len(expStashList), len(u.StashList))
for i, stashed := range u.StashList {
assert.Equal(t, expStashList[i].Description, stashed.Description)
assert.Equal(t, expStashList[i].Name, stashed.Name)
assert.Equal(t, expStashList[i].OnBranch, stashed.OnBranch)
assert.Equal(t, expStashList[i].OnCommitHash, stashed.OnCommitHash)
}
}
u, ok = <-p
assert.False(t, ok)
assert.Nil(t, u)
}
func assertNotRebasing(t *testing.T, dir string) {
gitStatusCmd := exec.Command("git", "status")
gitStatusCmd.Dir = dir
var stdout bytes.Buffer
gitStatusCmd.Stdout = &stdout
if err := gitStatusCmd.Run(); err != nil {
t.Fatal(err)
} else {
assert.False(t, strings.Contains(stdout.String(), "You are currently rebasing branch"))
}
}
func TestPull(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.ResetHard(t, dir, 1)
testPullChannel(t, Pull(dir), Pulled, "", &RepoState{LocalCommits: 0}, nil)
}
func TestPull_WithLocalCommits(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.CommitNewFile(t, dir, "file1")
testPullChannel(t, Pull(dir), Pulled, "", &RepoState{LocalCommits: 1}, nil)
}
func TestPull_WithUnstagedChanges(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.OpenFileForWriting(t, dir, "README.md", func(f *os.File) {
if _, err := f.WriteString("unstaged change"); err != nil {
t.Fatal(err)
}
})
expStatus := Pulled
expMessage := ""
if runtime.GOOS == "windows" {
expStatus = UnstagedChanges
expMessage = "unstaged changes"
}
testPullChannel(t, Pull(dir), expStatus, expMessage, &RepoState{LocalCommits: 0}, nil)
}
func TestPull_WithStashedChanges(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.MkFile(t, dir, "stashed_file")
testutil.GitAdd(t, dir, "stashed_file")
testutil.GitStash(t, dir)
testPullChannel(t, Pull(dir), Pulled, "", &RepoState{LocalCommits: 0}, []*StashedChangeset{{
Description: "adding pkg.go.dev badge",
Name: "stash@{0}",
OnBranch: "main",
OnCommitHash: "7f04bd8",
}})
}
func TestPull_Fails_DirNotRepo(t *testing.T) {
gitIntegrationTest(t)
_ = `
fatal: not a git repository (or any of the parent directories): .git`
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testPullChannel(t, Pull(dir), NotRepository, "not a repository", nil, nil)
}
const pullDetachedHead = `
You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>`
func TestMakePullErrorUpdate_ForDetachedHead(t *testing.T) {
u := makePullErrorUpdate(pullDetachedHead[1:])
assert.Equal(t, DetachedHead, u.Status)
assert.Equal(t, "detached from a branch", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithDetachedHead(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.Checkout(t, dir, "5692a1bb7f5796ec3c0237c8cb0a87212b36b91e")
testPullChannel(t, Pull(dir), DetachedHead, "detached from a branch", &RepoState{
LocalCommits: 0,
StagedChanges: 0,
UnstagedChanges: 0,
UntrackedFiles: 0,
}, nil)
}
const pullNotPossibleToFastForward = `
fatal: Not possible to fast-forward, aborting.
`
func TestMakePullErrorUpdate_ForMergeConflict(t *testing.T) {
u := makePullErrorUpdate(pullNotPossibleToFastForward[1:])
assert.Equal(t, MergeConflict, u.Status)
assert.Equal(t, "unable to pull without a merge or interactive rebase", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithMergeConflict(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.ResetHard(t, dir, 1)
testutil.OpenFileForOverwriting(t, dir, "README.md", func(f *os.File) {
if _, err := f.WriteString("merge conflict"); err != nil {
t.Fatal(err)
}
})
testutil.AddAndCommit(t, dir, "README.md")
testPullChannel(t, Pull(dir), MergeConflict, "unable to pull without a merge or interactive rebase", &RepoState{
LocalCommits: 0,
StagedChanges: 0,
UnstagedChanges: 0,
UntrackedFiles: 0,
}, nil)
assertNotRebasing(t, dir)
}
const pullMergeConflict = `
error: Your local changes to the following files would be overwritten by merge:
README.md
Please commit your changes or stash them before you merge.
Aborting`
func TestMakePullErrorUpdate_ForOverwritesLocalChanges(t *testing.T) {
u := makePullErrorUpdate(pullMergeConflict[1:])
assert.Equal(t, OverwritesLocalChanges, u.Status)
assert.Equal(t, "local changes would be overwritten", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithOverwritesLocalChanges(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.ResetHard(t, dir, 1)
testutil.OpenFileForOverwriting(t, dir, "README.md", func(f *os.File) {
if _, err := f.WriteString("merge conflict"); err != nil {
t.Fatal(err)
}
})
// todo should OverwritesLocalChanges report as UnstagedChanges on all platforms?
expStatus := OverwritesLocalChanges
expMessage := "local changes would be overwritten"
if runtime.GOOS == "windows" {
expStatus = UnstagedChanges
expMessage = "unstaged changes"
}
testPullChannel(t, Pull(dir), expStatus, expMessage, &RepoState{LocalCommits: 0, StagedChanges: 0, UnstagedChanges: 1, UntrackedFiles: 0}, nil)
assertNotRebasing(t, dir)
}
const pullConnectionFailure = `
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.`
func TestMakePullErrorUpdate_ForConnectionFailure(t *testing.T) {
u := makePullErrorUpdate(pullConnectionFailure[1:])
assert.Equal(t, ConnectionFailure, u.Status)
assert.Equal(t, "connection failure with remote repository", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithConnectionFailure_WithoutFailureReason(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.SetGitRemoteOriginUrl(t, dir, "[email protected]:eighty4/sse")
ogGscValue := os.Getenv("GIT_SSH_COMMAND")
if err := os.Setenv("GIT_SSH_COMMAND", "false"); err != nil {
t.Fatal(err.Error())
}
testPullChannel(t, Pull(dir), ConnectionFailure, "connection failure with remote repository", &RepoState{LocalCommits: 0, StagedChanges: 0, UnstagedChanges: 1, UntrackedFiles: 0}, nil)
_ = os.Setenv("GIT_SSH_COMMAND", ogGscValue)
}
const pullConnectionFailureWithReason = `
exit 1: line 0: exit: too many arguments
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.`
func TestMakePullErrorUpdate_ForConnectionFailureWithReason(t *testing.T) {
u := makePullErrorUpdate(pullConnectionFailureWithReason[1:])
assert.Equal(t, ConnectionFailure, u.Status)
assert.Equal(t, "\"exit 1: line 0: exit: too many arguments\"", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithConnectionFailure_WithFailureReason(t *testing.T) {
gitIntegrationTest(t)
if runtime.GOOS != "darwin" {
t.Skip("output does not match on linux")
}
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.SetGitRemoteOriginUrl(t, dir, "[email protected]:aaaaaaaaaaaaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaa")
ogGscValue := os.Getenv("GIT_SSH_COMMAND")
if err := os.Setenv("GIT_SSH_COMMAND", "sleep foo"); err != nil {
t.Fatal(err.Error())
}
testPullChannel(t, Pull(dir), ConnectionFailure, `"usage: sleep seconds"`, &RepoState{LocalCommits: 0, StagedChanges: 0, UnstagedChanges: 1, UntrackedFiles: 0}, nil)
_ = os.Setenv("GIT_SSH_COMMAND", ogGscValue)
}
const pullGitHubNotFound = `
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.`
func TestMakePullErrorUpdate_ForGitHubNotFound(t *testing.T) {
u := makePullErrorUpdate(pullGitHubNotFound[1:])
assert.Equal(t, PullRepoNotFound, u.Status)
assert.Equal(t, "repository not found", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithGitHubRepoNotFoundConnectionError_MappedToRepositoryNotFound(t *testing.T) {
t.Skip("`git pull` fails with `unable to fork` when running all tests in git_test.go, passes individually")
skipOnCi(t)
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.SetGitRemoteOriginUrl(t, dir, "[email protected]:eighty4/aaaaaaaaaaaaaaaaaaaaaaaaaaa")
testPullChannel(t, Pull(dir), PullRepoNotFound, "repository not found", nil, nil)
}
const pullCouldNotResolveHost = `
fatal: unable to access 'https://github.com/eighty4/sse/': Could not resolve host: github.com`
func TestMakePullErrorUpdate_ForCouldNotResolveHost(t *testing.T) {
u := makePullErrorUpdate(pullCouldNotResolveHost[1:])
assert.Equal(t, CouldNotResolveHost, u.Status)
assert.Equal(t, "could not resolve host", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithCouldNotResolveHost(t *testing.T) {
gitIntegrationTest(t)
t.Skip("not really testable")
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://gibhub.com/eighty4/sse")
testPullChannel(t, Pull(dir), CouldNotResolveHost, "could not resolve host", nil, nil)
}
const pullRemoteBranchNotFound = `
Your configuration specifies to merge with the ref 'refs/heads/macrame'
from the remote, but no such ref was fetched.`
func TestMakePullErrorUpdate_ForRemoteBranchNotFound(t *testing.T) {
u := makePullErrorUpdate(pullRemoteBranchNotFound[1:])
assert.Equal(t, RemoteBranchNotFound, u.Status)
assert.Equal(t, "tracking branch not found on remote", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithRemoteBranchNotFound(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
gitCommitCmd := exec.Command("git", "config", "branch.main.merge", "macrame")
gitCommitCmd.Dir = dir
var gitCommitCmdStderr bytes.Buffer
gitCommitCmd.Stderr = &gitCommitCmdStderr
if err := gitCommitCmd.Run(); err != nil {
t.Fatal(gitCommitCmdStderr.String())
}
testPullChannel(t, Pull(dir), RemoteBranchNotFound, "tracking branch not found on remote", &RepoState{LocalCommits: 0, StagedChanges: 0, UnstagedChanges: 1, UntrackedFiles: 0}, nil)
}
const pullUnsetUpstream = `
Initialized empty Git repository in /private/var/folders/r6/0hg_xym96qbgc8m049hxjrxr0000gn/T/maestro-test2377015730/.git/
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=<remote>/<branch> main`
func TestMakePullErrorUpdate_ForUnsetUpstream(t *testing.T) {
u := makePullErrorUpdate(pullUnsetUpstream[1:])
assert.Equal(t, UnsetUpstream, u.Status)
assert.Equal(t, "not tracking an upstream remote", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithUnsetUpstream(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.InitRepo(t, dir)
testPullChannel(t, Pull(dir), UnsetUpstream, "not tracking an upstream remote", &RepoState{LocalCommits: 0, StagedChanges: 0, UnstagedChanges: 1, UntrackedFiles: 0}, nil)
}
const pullRepositoryNotFound = `
fatal: repository 'https://eighty4.io/' not found`
func TestMakePullErrorUpdate_ForRepositoryNotFound(t *testing.T) {
u := makePullErrorUpdate(pullRepositoryNotFound[1:])
assert.Equal(t, PullRepoNotFound, u.Status)
assert.Equal(t, "repository not found", u.Error)
assert.Nil(t, u.RepoState)
}
func TestPull_Fails_WithRepositoryNotFound(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.SetGitRemoteOriginUrl(t, dir, "https://bing.com")
testPullChannel(t, Pull(dir), PullRepoNotFound, "repository not found", &RepoState{LocalCommits: 0, StagedChanges: 0, UnstagedChanges: 1, UntrackedFiles: 0}, nil)
}
func TestRevParseShowTopLevel_NotRepo(t *testing.T) {
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
result, err := RevParseShowTopLevel(dir)
assert.NotNil(t, err)
assert.Empty(t, result)
}
func TestRevParseShowTopLevel_TopLevelDir(t *testing.T) {
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.InitRepo(t, dir)
result, err := RevParseShowTopLevel(dir)
assert.Nil(t, err)
assert.Equal(t, dir, result)
}
func TestRevParseShowTopLevel_Subdir(t *testing.T) {
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.InitRepo(t, dir)
subdir := filepath.Join(dir, "foo")
testutil.MkDir(t, subdir)
result, err := RevParseShowTopLevel(subdir)
assert.Nil(t, err)
assert.Equal(t, dir, result)
}
func TestStashList(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.MkFile(t, dir, "stashed_file")
testutil.GitAdd(t, dir, "stashed_file")
testutil.GitStash(t, dir)
stashes, err := StashList(dir)
assert.Nil(t, err)
assert.Equal(t, 1, len(stashes))
assert.Equal(t, "stash@{0}", stashes[0].Name)
assert.Equal(t, "main", stashes[0].OnBranch)
assert.Equal(t, "adding pkg.go.dev badge", stashes[0].Description)
assert.Equal(t, "7f04bd8", stashes[0].OnCommitHash)
gitRevParseCmd := exec.Command("git", "rev-parse", "--short", "HEAD")
gitRevParseCmd.Dir = dir
var stdoutBuf bytes.Buffer
gitRevParseCmd.Stdout = &stdoutBuf
if err := gitRevParseCmd.Run(); err != nil {
t.Fatal(err)
}
assert.Equal(t, strings.TrimSpace(stdoutBuf.String()), stashes[0].OnCommitHash)
}
func TestStashList_EmptyStash(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
stashes, err := StashList(dir)
assert.Nil(t, err)
assert.Nil(t, stashes)
}
func TestStashList_Errors_NotRepository(t *testing.T) {
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
stashes, err := StashList(dir)
assert.Nil(t, stashes)
assert.NotNil(t, err)
}
func TestStatus_NoLocalCommits(t *testing.T) {
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.InitRepo(t, dir)
s, err := Status(dir)
assert.Nil(t, err)
assert.Equal(t, false, s.BranchesDiverged)
assert.Equal(t, 0, s.LocalCommits)
}
func TestStatus_WithLocalCommits(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.CommitNewFile(t, dir, "file1")
testutil.CommitNewFile(t, dir, "file2")
s, err := Status(dir)
assert.Nil(t, err)
assert.Equal(t, false, s.BranchesDiverged)
assert.Equal(t, 2, s.LocalCommits)
assert.Equal(t, 0, s.StagedChanges)
assert.Equal(t, 0, s.UnstagedChanges)
assert.Equal(t, 0, s.UntrackedFiles)
}
func TestStatus_WithDivergedBranches(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.MkFile(t, dir, "newFile")
testutil.AddAndAmendCommit(t, dir, "newFile")
s, err := Status(dir)
assert.Nil(t, err)
assert.Equal(t, true, s.BranchesDiverged)
assert.Equal(t, 0, s.LocalCommits)
assert.Equal(t, 0, s.StagedChanges)
assert.Equal(t, 0, s.UnstagedChanges)
assert.Equal(t, 0, s.UntrackedFiles)
}
func TestStatus_WithStagedChanges(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.OpenFileForOverwriting(t, dir, "LICENSE", func(f *os.File) {
_, _ = f.WriteString("license")
})
testutil.OpenFileForOverwriting(t, dir, "README.md", func(f *os.File) {
_, _ = f.WriteString("readme")
})
testutil.GitAdd(t, dir, "LICENSE")
testutil.GitAdd(t, dir, "README.md")
s, err := Status(dir)
assert.Nil(t, err)
assert.Equal(t, false, s.BranchesDiverged)
assert.Equal(t, 0, s.LocalCommits)
assert.Equal(t, 2, s.StagedChanges)
assert.Equal(t, 0, s.UnstagedChanges)
assert.Equal(t, 0, s.UntrackedFiles)
}
func TestStatus_WithUnstagedChanges(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.OpenFileForOverwriting(t, dir, "LICENSE", func(f *os.File) {
_, _ = f.WriteString("license")
})
testutil.OpenFileForOverwriting(t, dir, "README.md", func(f *os.File) {
_, _ = f.WriteString("readme")
})
s, err := Status(dir)
assert.Nil(t, err)
assert.Equal(t, false, s.BranchesDiverged)
assert.Equal(t, 0, s.LocalCommits)
assert.Equal(t, 0, s.StagedChanges)
assert.Equal(t, 2, s.UnstagedChanges)
assert.Equal(t, 0, s.UntrackedFiles)
}
func TestStatus_WithUntrackedFiles(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.MkFile(t, dir, "new_file")
testutil.MkFile(t, dir, "another_new_file")
s, err := Status(dir)
assert.Nil(t, err)
assert.Equal(t, false, s.BranchesDiverged)
assert.Equal(t, 0, s.LocalCommits)
assert.Equal(t, 0, s.StagedChanges)
assert.Equal(t, 0, s.UnstagedChanges)
assert.Equal(t, 2, s.UntrackedFiles)
}
func TestStatus_WithAllStateTypes(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
testutil.CommitNewFile(t, dir, "file1")
testutil.OpenFileForOverwriting(t, dir, "LICENSE", func(f *os.File) {
_, _ = f.WriteString("license")
})
testutil.OpenFileForOverwriting(t, dir, "README.md", func(f *os.File) {
_, _ = f.WriteString("readme")
})
testutil.GitAdd(t, dir, "README.md")
testutil.MkFile(t, dir, "new_file")
s, err := Status(dir)
assert.Nil(t, err)
assert.Equal(t, false, s.BranchesDiverged)
assert.Equal(t, 1, s.LocalCommits)
assert.Equal(t, 1, s.StagedChanges)
assert.Equal(t, 1, s.UnstagedChanges)
assert.Equal(t, 1, s.UntrackedFiles)
}
func TestParsePullCommitRange(t *testing.T) {
const gitPullStdout = `Updating 700148d..7f04bd8
Fast-forward
README.md | 3 +++
sse.go | 1 +
2 files changed, 4 insertions(+)
`
from, to, err := parsePulledCommitRange(gitPullStdout)
assert.Nil(t, err)
assert.Equal(t, "700148d", from)
assert.Equal(t, "7f04bd8", to)
}
func TestParsePullCommitRange_BadInput(t *testing.T) {
_, _, err := parsePulledCommitRange("2\n")
assert.Equal(t, "failed to find commit hashes in git pull stdout", err.Error())
}
func TestParsePullCommitRange_GoodInput(t *testing.T) {
input := "Updating 0115386a..3456ecd9\nFast-forward\n"
from, to, err := parsePulledCommitRange(input)
assert.Nil(t, err)
assert.Equal(t, "0115386a", from)
assert.Equal(t, "3456ecd9", to)
}
func TestParseRevListCommitCount(t *testing.T) {
commitCount, err := parseRevListCommitCount("2\n")
assert.Nil(t, err)
assert.Equal(t, 2, commitCount)
}
func TestParseRevListCommitCount_BadInput(t *testing.T) {
_, err := parseRevListCommitCount("two\n")
assert.Equal(t, "strconv.Atoi: parsing \"two\": invalid syntax", err.Error())
}
func TestParseRevParseAbsolutePath_TrimsInput(t *testing.T) {
assert.Equal(t, "woo", parseRevParseAbsolutePath(" woo \n"))
}
func TestParseRevParseAbsolutePath_RewritesGitsUnixPathsAsWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip()
}
assert.Equal(t, "\\im\\a\\path", parseRevParseAbsolutePath("/im/a/path\n"))
}
func TestGetPulledCommitCount(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
stdout := `Updating ea0888b..7f04bd8
Fast-forward
README.md | 5 ++++-
sse.go | 1 +
2 files changed, 5 insertions(+), 1 deletion(-)`
count, err := getPulledCommitCount(dir, stdout)
assert.Nil(t, err)
assert.Equal(t, count, 3)
}
func TestGetPulledCommitCount_WhenAlreadyUpToDate(t *testing.T) {
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
count, err := getPulledCommitCount(dir, "Already up to date.")
assert.Nil(t, err)
assert.Equal(t, count, 0)
}
func TestParsePulledCommitCount(t *testing.T) {
stdout := `Updating ea0888b..7f04bd8
Fast-forward
README.md | 5 ++++-
sse.go | 1 +
2 files changed, 5 insertions(+), 1 deletion(-)`
from, to, err := parsePulledCommitRange(stdout)
assert.Nil(t, err)
assert.Equal(t, from, "ea0888b")
assert.Equal(t, to, "7f04bd8")
}
func TestRevListCommitCount(t *testing.T) {
gitIntegrationTest(t)
dir := testutil.MkTmpDir(t)
defer testutil.RmDir(t, dir)
testutil.CloneRepo(t, dir, "https://github.com/eighty4/sse")
count, err := RevListCommitCount(dir, "9a6992f988bee6e47540e53e434ad07911db3a30", "700148df1ec546c06ce1a54bc472fee3085a2842")
assert.Nil(t, err)
assert.Equal(t, count, 2)
}