Skip to content

Commit 631c705

Browse files
committed
Add basic tests for full git file header parsing
1 parent aa3e0d3 commit 631c705

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

gitdiff/parser_test.go

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

33
import (
44
"bufio"
5+
"os"
56
"reflect"
67
"strings"
78
"testing"
@@ -128,3 +129,61 @@ func TestParseFragmentHeader(t *testing.T) {
128129
})
129130
}
130131
}
132+
133+
func TestParseGitFileHeader(t *testing.T) {
134+
tests := map[string]struct {
135+
Input string
136+
Output *File
137+
Err bool
138+
}{
139+
"simpleFileChange": {
140+
Input: `diff --git a/dir/file.txt b/dir/file.txt
141+
index 1c23fcc..40a1b33 100644
142+
--- a/dir/file.txt
143+
+++ b/dir/file.txt
144+
`,
145+
Output: &File{
146+
OldName: "dir/file.txt",
147+
NewName: "dir/file.txt",
148+
OldMode: os.FileMode(0100644),
149+
OldOIDPrefix: "1c23fcc",
150+
NewOIDPrefix: "40a1b33",
151+
},
152+
},
153+
"fileCreation": {
154+
Input: `diff --git a/dir/file.txt b/dir/file.txt
155+
new file mode 100644
156+
index 0000000..f5711e4
157+
--- /dev/null
158+
+++ b/dir/file.txt
159+
`,
160+
Output: &File{
161+
NewName: "dir/file.txt",
162+
NewMode: os.FileMode(0100644),
163+
OldOIDPrefix: "0000000",
164+
NewOIDPrefix: "f5711e4",
165+
IsNew: true,
166+
},
167+
},
168+
}
169+
170+
for name, test := range tests {
171+
t.Run(name, func(t *testing.T) {
172+
p := &parser{r: bufio.NewReader(strings.NewReader(test.Input))}
173+
header, _ := p.Line()
174+
175+
var f File
176+
err := p.ParseGitFileHeader(&f, header)
177+
if test.Err {
178+
if err == nil {
179+
t.Fatalf("expected error parsing git header, got nil")
180+
}
181+
return
182+
}
183+
184+
if test.Output != nil && !reflect.DeepEqual(f, *test.Output) {
185+
t.Errorf("incorrect file\nexpected: %+v\nactual: %+v", *test.Output, f)
186+
}
187+
})
188+
}
189+
}

0 commit comments

Comments
 (0)