diff --git a/gitdiff/parser_test.go b/gitdiff/parser_test.go index 94226e7..938e65b 100644 --- a/gitdiff/parser_test.go +++ b/gitdiff/parser_test.go @@ -2,6 +2,7 @@ package gitdiff import ( "bufio" + "encoding/json" "io" "os" "reflect" @@ -223,6 +224,98 @@ a wild fragment appears? } } +func TestParse(t *testing.T) { + tests := map[string]struct { + InputFile string + Output []*File + Err bool + }{ + "singleFile": { + InputFile: "testdata/single_file.patch", + Output: []*File{ + { + OldName: "dir/file.txt", + NewName: "dir/file.txt", + OldMode: os.FileMode(0100644), + OldOIDPrefix: "ebe9fa54", + NewOIDPrefix: "fe103e1d", + Fragments: []*Fragment{ + { + OldPosition: 3, + OldLines: 6, + NewPosition: 3, + NewLines: 8, + Comment: "fragment 1", + Lines: []FragmentLine{ + {OpContext, "context line\n"}, + {OpDelete, "old line 1\n"}, + {OpDelete, "old line 2\n"}, + {OpContext, "context line\n"}, + {OpAdd, "new line 1\n"}, + {OpAdd, "new line 2\n"}, + {OpAdd, "new line 3\n"}, + {OpContext, "context line\n"}, + {OpDelete, "old line 3\n"}, + {OpAdd, "new line 4\n"}, + {OpAdd, "new line 5\n"}, + }, + LinesAdded: 5, + LinesDeleted: 3, + LeadingContext: 1, + }, + { + OldPosition: 31, + OldLines: 2, + NewPosition: 33, + NewLines: 2, + Comment: "fragment 2", + Lines: []FragmentLine{ + {OpContext, "context line\n"}, + {OpDelete, "old line 4\n"}, + {OpAdd, "new line 6\n"}, + }, + LinesAdded: 1, + LinesDeleted: 1, + LeadingContext: 1, + }, + }, + }, + }, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + f, err := os.Open(test.InputFile) + if err != nil { + t.Fatalf("unexpected error opening input file: %v", err) + } + + files, err := Parse(f) + if test.Err { + if err == nil || err == io.EOF { + t.Fatalf("expected error parsing patch, but got %v", err) + } + return + } + if err != nil { + t.Fatalf("unexpected error parsing patch: %v", err) + } + + if len(test.Output) != len(files) { + t.Fatalf("incorrect number of parsed files: expected %d, actual %d", len(test.Output), len(files)) + } + for i := range test.Output { + if !reflect.DeepEqual(test.Output[i], files[i]) { + exp, _ := json.MarshalIndent(test.Output[i], "", " ") + act, _ := json.MarshalIndent(files[i], "", " ") + t.Errorf("incorrect file at position %d\nexpected: %s\nactual: %s", i, exp, act) + } + } + }) + } +} + func newTestParser(input string, init bool) *parser { p := &parser{r: bufio.NewReader(strings.NewReader(input))} if init { diff --git a/gitdiff/testdata/single_file.patch b/gitdiff/testdata/single_file.patch new file mode 100644 index 0000000..98b1628 --- /dev/null +++ b/gitdiff/testdata/single_file.patch @@ -0,0 +1,28 @@ +commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe +Author: Morton Haypenny +Date: Tue Apr 2 22:55:40 2019 -0700 + + A single file with multiple fragments. + + The content is arbitrary. + +diff --git a/dir/file.txt b/dir/file.txt +index ebe9fa54..fe103e1d 100644 +--- a/dir/file.txt ++++ b/dir/file.txt +@@ -3,6 +3,8 @@ fragment 1 + context line +-old line 1 +-old line 2 + context line ++new line 1 ++new line 2 ++new line 3 + context line +-old line 3 ++new line 4 ++new line 5 +@@ -31,2 +33,2 @@ fragment 2 + context line +-old line 4 ++new line 6