-
Notifications
You must be signed in to change notification settings - Fork 128
/
git_test.go
108 lines (91 loc) · 1.92 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
// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"bytes"
"flag"
"fmt"
stdlog "log"
"os"
"testing"
goversion "github.com/mcuadros/go-version"
"github.com/stretchr/testify/assert"
"golang.org/x/sync/errgroup"
)
const repoPath = "testdata/testrepo.git"
var testrepo *Repository
func TestMain(m *testing.M) {
flag.Parse()
if testing.Verbose() {
SetOutput(os.Stdout)
}
// Set up the test repository
if !isExist(repoPath) {
if err := Clone("https://github.com/gogs/git-module-testrepo.git", repoPath, CloneOptions{
Bare: true,
}); err != nil {
stdlog.Fatal(err)
}
}
var err error
testrepo, err = Open(repoPath)
if err != nil {
stdlog.Fatal(err)
}
os.Exit(m.Run())
}
func TestSetPrefix(t *testing.T) {
old := logPrefix
new := "[custom] "
SetPrefix(new)
defer SetPrefix(old)
assert.Equal(t, new, logPrefix)
}
func Test_log(t *testing.T) {
old := logOutput
defer SetOutput(old)
tests := []struct {
format string
args []interface{}
expOutput string
}{
{
format: "",
expOutput: "[git-module] \n",
},
{
format: "something",
expOutput: "[git-module] something\n",
},
{
format: "val: %v",
args: []interface{}{123},
expOutput: "[git-module] val: 123\n",
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
log(test.format, test.args...)
assert.Equal(t, test.expOutput, buf.String())
})
}
}
func TestBinVersion(t *testing.T) {
g := errgroup.Group{}
for i := 0; i < 30; i++ {
g.Go(func() error {
version, err := BinVersion()
assert.Nil(t, err)
if !goversion.Compare(version, "1.8.3", ">=") {
return fmt.Errorf("version: expected >= 1.8.3 but got %q", version)
}
return nil
})
}
if err := g.Wait(); err != nil {
t.Fatal(err)
}
}