-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcommit_test.go
230 lines (206 loc) · 5.97 KB
/
commit_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
package main
import (
"fmt"
"log"
"testing"
git "github.com/libgit2/git2go"
)
type testDatum struct {
sha string
repo string
additions int64
deletions int64
hunkcount int64
futureChanges int64
pastChanges int64
futureDifferentAuthors int64
pastDifferentAuthors int64
}
/*
func TestMain(m *testing.M) {
KnownCVEs = NewMitreCves()
if err := KnownCVEs.Read("data/cve.xml"); err != nil {
log.Fatal(err)
}
os.Exit(m.Run())
}
*/
func TestGetGitMetadata(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
var testData = []testDatum{
testDatum{"63ab52b7ecdbcaf097862da79efa72e678920725",
"FFmpeg/FFmpeg", 11, 13, 2, 485, 594, 82, 23},
testDatum{"0a59a18b4ed63a1d20ac62489e72fb81d4b3c363",
"FFmpeg/FFmpeg", 166, 134, 14, 1502, 622, 205, 55},
testDatum{"24dee483e9e925c2ab79dd582f70c9a55ab9ba4d",
"bagder/curl", 707, 1748, 64, 7471, 710, 609, 63},
}
RepoBasePath = "repos/"
for _, data := range testData {
r := &Repository{Name: data.repo}
if err := r.clone(); err != nil {
t.Errorf("cloning %s: %v", data.repo, err)
}
c := &Commit{
Repository: r,
Sha: data.sha,
}
if err := c.GetGitMetadata(); err != nil {
t.Fatalf("GetGitMetadata(): %v", err)
}
if c.Additions != data.additions {
t.Error(c, "should have", data.additions, "additions, has", c.Additions)
}
if c.Deletions != data.deletions {
t.Error(c, "should have", data.deletions, "deletions, has", c.Deletions)
}
if c.HunkCount != data.hunkcount {
t.Error(c, "should have", data.hunkcount, "hunks, has", c.HunkCount)
}
if c.FutureChanges != data.futureChanges {
t.Error(c, "should have", data.futureChanges, "future changes, has", c.FutureChanges)
}
if c.PastChanges != data.pastChanges {
t.Error(c, "should have", data.pastChanges, "past changes, has", c.PastChanges)
}
if c.FutureDifferentAuthors != data.futureDifferentAuthors {
t.Error(c, "should have", data.futureDifferentAuthors, "future different authors, has", c.FutureDifferentAuthors)
}
if c.PastDifferentAuthors != data.pastDifferentAuthors {
t.Error(c, "should have", data.pastDifferentAuthors, "past different authors, has", c.PastDifferentAuthors)
}
}
}
func TestFixCommit(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
var testData = []testDatum{
testDatum{"330d57fb98a916fa8e1363846540dd420e99499a", "torvalds/linux", 0, 0, 0, 0, 0, 0, 0},
}
RepoBasePath = "../repos/"
KnownCVEs = NewMitreCves()
if err := KnownCVEs.Read("data/cve.xml"); err != nil {
log.Fatal(err)
}
for _, data := range testData {
r := &Repository{Name: data.repo}
if err := r.clone(); err != nil {
t.Errorf("cloning %s: %v", data.repo, err)
}
c := &Commit{
Repository: r,
Sha: data.sha,
}
if err := c.GetGitMetadata(); err != nil {
t.Error("GetGitMetadata(): %v", err)
}
if err := c.fixCommit(); err != nil {
t.Error("fixCommit(): %v", err)
}
if c.Type != "fixing_commit" {
t.Errorf("Not marked as fixing commit: %s", c.Message)
}
}
}
func TestBlameCommit(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
var testData = []testDatum{
testDatum{"ae8bb84ecd46d7b6ef557a87725923ac8d09dce0", "libguestfs/libguestfs", 0, 0, 0, 0, 0, 0, 0},
testDatum{"330d57fb98a916fa8e1363846540dd420e99499a", "torvalds/linux", 0, 0, 0, 0, 0, 0, 0},
}
RepoBasePath = "../repos/"
for _, data := range testData {
r := &Repository{Name: data.repo}
if err := r.clone(); err != nil {
t.Errorf("cloning %s: %v", data.repo, err)
}
c := &Commit{
Repository: r,
Sha: data.sha,
}
blamedSha, err := c.getBlameCommitSha()
if err != nil {
t.Error("blameCommit(): %v", err)
}
if blamedSha == "" {
t.Errorf("No blame for commit: %#v", c)
}
}
}
func TestCommitGetFunctions(t *testing.T) {
var testData = map[string]([]string){
"7471039d7ed95c5a80338694a9a5c9a03a382232": []string{"modified"},
"aba1ee55b69a812e74413638bb4c5cc05145633a": []string{"added"},
"f2291572e5a3bf50103deb6335f6e1554a905f17": []string{"added", "deleted"},
}
RepoBasePath = "./testdata/"
r := &Repository{Name: "testrepo"}
for sha, funStates := range testData {
c := &Commit{Repository: r, Sha: sha}
if err := c.GetGitMetadata(); err != nil {
t.Error(err)
}
if len(c.Functions) != len(funStates) {
for _, f := range c.Functions {
t.Logf("%s", f.String())
}
t.Fatalf("%s: expected %d functions, got %d", c, len(funStates), len(c.Functions))
}
for i, state := range funStates {
f := c.Functions[i]
if f.State != state {
t.Fatalf("%s: expected %s, got %s", f.String(), state, f.State)
}
}
for _, f := range c.Functions {
t.Logf("%s", f.String())
}
}
}
func TestDiffRename(t *testing.T) {
RepoBasePath = "./repos"
emptyEachHunkCB := func(hunk git.DiffHunk) (git.DiffForEachLineCallback, error) {
return func(line git.DiffLine) error {
return nil
}, nil
}
r := &Repository{Name: "tieto/pidgin"}
c := &Commit{Repository: r, Sha: "fa502d1211999c1af956490c667f82846c5524af"}
d, _, err := c.diff()
checkErr(t, err)
//opts := git.DefaultDiffFindOptions()
//opts.Flags = git.DiffFindRenames
//d.FindSimilar(opts)
d.ForEach(func(delta git.DiffDelta, num float64) (git.DiffForEachHunkCallback, error) {
fmt.Println(delta.OldFile.Path)
return emptyEachHunkCB, nil
}, git.DiffDetailLines)
del, _ := d.NumDeltas()
t.Log("deltas", del)
}
func TestSetPatchKeywords(t *testing.T) {
c := &Commit{
Patch: `if then if if catch else foo ?!? ;then`,
}
c.SetPatchKeywords()
if v, ok := c.PatchKeywords.Map["if"]; !ok || v.String != "3" {
t.Error("expected 3 ifs")
}
if v, ok := c.PatchKeywords.Map["auto"]; !ok || v.String != "0" {
t.Error("expected 0 autos")
}
if v, ok := c.PatchKeywords.Map["then"]; !ok || v.String != "2" {
t.Error("expected 2 then")
}
t.Log(c.PatchKeywords)
}
func checkErr(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
}