Skip to content

Commit 47d923e

Browse files
committed
Add simple full file parsing test
For now, this uses JSON to print objects on error, which is hard to debug. It should probably use something like google/go-cmp instead, because these objects are now too large for direct comparison.
1 parent a1149ed commit 47d923e

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

gitdiff/parser_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitdiff
22

33
import (
44
"bufio"
5+
"encoding/json"
56
"io"
67
"os"
78
"reflect"
@@ -223,6 +224,98 @@ a wild fragment appears?
223224
}
224225
}
225226

227+
func TestParse(t *testing.T) {
228+
tests := map[string]struct {
229+
InputFile string
230+
Output []*File
231+
Err bool
232+
}{
233+
"singleFile": {
234+
InputFile: "testdata/single_file.patch",
235+
Output: []*File{
236+
{
237+
OldName: "dir/file.txt",
238+
NewName: "dir/file.txt",
239+
OldMode: os.FileMode(0100644),
240+
OldOIDPrefix: "ebe9fa54",
241+
NewOIDPrefix: "fe103e1d",
242+
Fragments: []*Fragment{
243+
{
244+
OldPosition: 3,
245+
OldLines: 6,
246+
NewPosition: 3,
247+
NewLines: 8,
248+
Comment: "fragment 1",
249+
Lines: []FragmentLine{
250+
{OpContext, "context line\n"},
251+
{OpDelete, "old line 1\n"},
252+
{OpDelete, "old line 2\n"},
253+
{OpContext, "context line\n"},
254+
{OpAdd, "new line 1\n"},
255+
{OpAdd, "new line 2\n"},
256+
{OpAdd, "new line 3\n"},
257+
{OpContext, "context line\n"},
258+
{OpDelete, "old line 3\n"},
259+
{OpAdd, "new line 4\n"},
260+
{OpAdd, "new line 5\n"},
261+
},
262+
LinesAdded: 5,
263+
LinesDeleted: 3,
264+
LeadingContext: 1,
265+
},
266+
{
267+
OldPosition: 31,
268+
OldLines: 2,
269+
NewPosition: 33,
270+
NewLines: 2,
271+
Comment: "fragment 2",
272+
Lines: []FragmentLine{
273+
{OpContext, "context line\n"},
274+
{OpDelete, "old line 4\n"},
275+
{OpAdd, "new line 6\n"},
276+
},
277+
LinesAdded: 1,
278+
LinesDeleted: 1,
279+
LeadingContext: 1,
280+
},
281+
},
282+
},
283+
},
284+
},
285+
}
286+
287+
for name, test := range tests {
288+
t.Run(name, func(t *testing.T) {
289+
f, err := os.Open(test.InputFile)
290+
if err != nil {
291+
t.Fatalf("unexpected error opening input file: %v", err)
292+
}
293+
294+
files, err := Parse(f)
295+
if test.Err {
296+
if err == nil || err == io.EOF {
297+
t.Fatalf("expected error parsing patch, but got %v", err)
298+
}
299+
return
300+
}
301+
if err != nil {
302+
t.Fatalf("unexpected error parsing patch: %v", err)
303+
}
304+
305+
if len(test.Output) != len(files) {
306+
t.Fatalf("incorrect number of parsed files: expected %d, actual %d", len(test.Output), len(files))
307+
}
308+
for i := range test.Output {
309+
if !reflect.DeepEqual(test.Output[i], files[i]) {
310+
exp, _ := json.MarshalIndent(test.Output[i], "", " ")
311+
act, _ := json.MarshalIndent(files[i], "", " ")
312+
t.Errorf("incorrect file at position %d\nexpected: %s\nactual: %s", i, exp, act)
313+
}
314+
}
315+
})
316+
}
317+
}
318+
226319
func newTestParser(input string, init bool) *parser {
227320
p := &parser{r: bufio.NewReader(strings.NewReader(input))}
228321
if init {

gitdiff/testdata/single_file.patch

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
2+
Author: Morton Haypenny <mhaypenny@example.com>
3+
Date: Tue Apr 2 22:55:40 2019 -0700
4+
5+
A single file with multiple fragments.
6+
7+
The content is arbitrary.
8+
9+
diff --git a/dir/file.txt b/dir/file.txt
10+
index ebe9fa54..fe103e1d 100644
11+
--- a/dir/file.txt
12+
+++ b/dir/file.txt
13+
@@ -3,6 +3,8 @@ fragment 1
14+
context line
15+
-old line 1
16+
-old line 2
17+
context line
18+
+new line 1
19+
+new line 2
20+
+new line 3
21+
context line
22+
-old line 3
23+
+new line 4
24+
+new line 5
25+
@@ -31,2 +33,2 @@ fragment 2
26+
context line
27+
-old line 4
28+
+new line 6

0 commit comments

Comments
 (0)