forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff_test.go
238 lines (187 loc) · 5.09 KB
/
diff_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
package git
import (
"errors"
"strings"
"testing"
)
func TestFindSimilar(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
originalTree, newTree := createTestTrees(t, repo)
diffOpt, _ := DefaultDiffOptions()
diff, err := repo.DiffTreeToTree(originalTree, newTree, &diffOpt)
checkFatal(t, err)
if diff == nil {
t.Fatal("no diff returned")
}
findOpts, err := DefaultDiffFindOptions()
checkFatal(t, err)
findOpts.Flags = DiffFindBreakRewrites
err = diff.FindSimilar(&findOpts)
checkFatal(t, err)
numDiffs := 0
numAdded := 0
numDeleted := 0
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
numDiffs++
switch file.Status {
case DeltaAdded:
numAdded++
case DeltaDeleted:
numDeleted++
}
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
return func(line DiffLine) error {
return nil
}, nil
}, nil
}, DiffDetailLines)
if numDiffs != 2 {
t.Fatal("Incorrect number of files in diff")
}
if numAdded != 1 {
t.Fatal("Incorrect number of new files in diff")
}
if numDeleted != 1 {
t.Fatal("Incorrect number of deleted files in diff")
}
}
func TestDiffTreeToTree(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
originalTree, newTree := createTestTrees(t, repo)
callbackInvoked := false
opts := DiffOptions{
NotifyCallback: func(diffSoFar *Diff, delta DiffDelta, matchedPathSpec string) error {
callbackInvoked = true
return nil
},
OldPrefix: "x1/",
NewPrefix: "y1/",
}
diff, err := repo.DiffTreeToTree(originalTree, newTree, &opts)
checkFatal(t, err)
if !callbackInvoked {
t.Fatal("callback not invoked")
}
if diff == nil {
t.Fatal("no diff returned")
}
files := make([]string, 0)
hunks := make([]DiffHunk, 0)
lines := make([]DiffLine, 0)
patches := make([]string, 0)
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
patch, err := diff.Patch(len(patches))
if err != nil {
return nil, err
}
defer patch.Free()
patchStr, err := patch.String()
if err != nil {
return nil, err
}
patches = append(patches, patchStr)
files = append(files, file.OldFile.Path)
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
hunks = append(hunks, hunk)
return func(line DiffLine) error {
lines = append(lines, line)
return nil
}, nil
}, nil
}, DiffDetailLines)
checkFatal(t, err)
if len(files) != 1 {
t.Fatal("Incorrect number of files in diff")
}
if files[0] != "README" {
t.Fatal("File in diff was expected to be README")
}
if len(hunks) != 1 {
t.Fatal("Incorrect number of hunks in diff")
}
if hunks[0].OldStart != 1 || hunks[0].NewStart != 1 {
t.Fatal("Incorrect hunk")
}
if len(lines) != 2 {
t.Fatal("Incorrect number of lines in diff")
}
if lines[0].Content != "foo\n" {
t.Fatal("Incorrect lines in diff")
}
if lines[1].Content != "file changed\n" {
t.Fatal("Incorrect lines in diff")
}
if want1, want2 := "x1/README", "y1/README"; !strings.Contains(patches[0], want1) || !strings.Contains(patches[0], want2) {
t.Errorf("Diff patch doesn't contain %q or %q\n\n%s", want1, want2, patches[0])
}
stats, err := diff.Stats()
checkFatal(t, err)
if stats.Insertions() != 1 {
t.Fatal("Incorrect number of insertions in diff")
}
if stats.Deletions() != 1 {
t.Fatal("Incorrect number of deletions in diff")
}
if stats.FilesChanged() != 1 {
t.Fatal("Incorrect number of changed files in diff")
}
errTest := errors.New("test error")
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
return nil, errTest
}, DiffDetailLines)
if err != errTest {
t.Fatal("Expected custom error to be returned")
}
}
func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTree *Tree) {
var err error
_, originalTreeId := seedTestRepo(t, repo)
originalTree, err = repo.LookupTree(originalTreeId)
checkFatal(t, err)
_, newTreeId := updateReadme(t, repo, "file changed\n")
newTree, err = repo.LookupTree(newTreeId)
checkFatal(t, err)
return originalTree, newTree
}
func TestDiffBlobs(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
id1, err := odb.Write([]byte("hello\nhello\n"), ObjectBlob)
checkFatal(t, err)
id2, err := odb.Write([]byte("hallo\nhallo\n"), ObjectBlob)
checkFatal(t, err)
blob1, err := repo.LookupBlob(id1)
checkFatal(t, err)
blob2, err := repo.LookupBlob(id2)
checkFatal(t, err)
var files, hunks, lines int
err = DiffBlobs(blob1, "hi", blob2, "hi", nil,
func(delta DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
files++
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
hunks++
return func(line DiffLine) error {
lines++
return nil
}, nil
}, nil
},
DiffDetailLines)
if files != 1 {
t.Fatal("Bad number of files iterated")
}
if hunks != 1 {
t.Fatal("Bad number of hunks iterated")
}
// two removals, two additions
if lines != 4 {
t.Fatalf("Bad number of lines iterated")
}
}