@@ -133,17 +133,18 @@ context line
133133 }
134134
135135 if test .EndLine != p .Line (0 ) {
136- t .Errorf ("incorrect position after parsing\n expected: %q\n actual : %q" , test .EndLine , p .Line (0 ))
136+ t .Errorf ("incorrect position after parsing\n expected: %q\n actual : %q" , test .EndLine , p .Line (0 ))
137137 }
138138 })
139139 }
140140}
141141
142142func TestParseNextFileHeader (t * testing.T ) {
143143 tests := map [string ]struct {
144- Input string
145- Output * File
146- Err bool
144+ Input string
145+ Output * File
146+ Preamble string
147+ Err bool
147148 }{
148149 "gitHeader" : {
149150 Input : `commit 1acbae563cd6ef5750a82ee64e116c6eb065cb94
@@ -165,6 +166,13 @@ index cc34da1..1acbae5 100644
165166 OldOIDPrefix : "cc34da1" ,
166167 NewOIDPrefix : "1acbae5" ,
167168 },
169+ Preamble : `commit 1acbae563cd6ef5750a82ee64e116c6eb065cb94
170+ Author: Morton Haypenny <mhaypenny@example.com>
171+ Date: Tue Apr 2 22:30:00 2019 -0700
172+
173+ This is a sample commit message.
174+
175+ ` ,
168176 },
169177 "traditionalHeader" : {
170178 Input : `
@@ -176,6 +184,7 @@ index cc34da1..1acbae5 100644
176184 OldName : "file.txt" ,
177185 NewName : "file.txt" ,
178186 },
187+ Preamble : "\n " ,
179188 },
180189 "noHeaders" : {
181190 Input : `
@@ -184,7 +193,8 @@ this is another line
184193--- could this be a header?
185194nope, it's just some dashes
186195` ,
187- Output : nil ,
196+ Output : nil ,
197+ Preamble : "" ,
188198 },
189199 "detatchedFragmentLike" : {
190200 Input : `
@@ -206,7 +216,7 @@ a wild fragment appears?
206216 t .Run (name , func (t * testing.T ) {
207217 p := newTestParser (test .Input , true )
208218
209- f , err := p .ParseNextFileHeader ()
219+ f , pre , err := p .ParseNextFileHeader ()
210220 if test .Err {
211221 if err == nil || err == io .EOF {
212222 t .Fatalf ("expected error parsing next file header, but got %v" , err )
@@ -217,8 +227,11 @@ a wild fragment appears?
217227 t .Fatalf ("unexpected error parsing next file header: %v" , err )
218228 }
219229
230+ if test .Preamble != pre {
231+ t .Errorf ("incorrect preamble\n expected: %q\n actual: %q" , test .Preamble , pre )
232+ }
220233 if ! reflect .DeepEqual (test .Output , f ) {
221- t .Errorf ("incorrect file\n expected: %+v\n actual : %+v" , test .Output , f )
234+ t .Errorf ("incorrect file\n expected: %+v\n actual : %+v" , test .Output , f )
222235 }
223236 })
224237 }
@@ -266,9 +279,20 @@ func TestParse(t *testing.T) {
266279 },
267280 }
268281
282+ expectedPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
283+ Author: Morton Haypenny <mhaypenny@example.com>
284+ Date: Tue Apr 2 22:55:40 2019 -0700
285+
286+ A file with multiple fragments.
287+
288+ The content is arbitrary.
289+
290+ `
291+
269292 tests := map [string ]struct {
270293 InputFile string
271294 Output []* File
295+ Preamble string
272296 Err bool
273297 }{
274298 "oneFile" : {
@@ -283,6 +307,7 @@ func TestParse(t *testing.T) {
283307 Fragments : expectedFragments ,
284308 },
285309 },
310+ Preamble : expectedPreamble ,
286311 },
287312 "twoFiles" : {
288313 InputFile : "testdata/two_files.patch" ,
@@ -304,6 +329,7 @@ func TestParse(t *testing.T) {
304329 Fragments : expectedFragments ,
305330 },
306331 },
332+ Preamble : expectedPreamble ,
307333 },
308334 }
309335
@@ -314,7 +340,7 @@ func TestParse(t *testing.T) {
314340 t .Fatalf ("unexpected error opening input file: %v" , err )
315341 }
316342
317- files , err := Parse (f )
343+ files , pre , err := Parse (f )
318344 if test .Err {
319345 if err == nil || err == io .EOF {
320346 t .Fatalf ("expected error parsing patch, but got %v" , err )
@@ -328,11 +354,14 @@ func TestParse(t *testing.T) {
328354 if len (test .Output ) != len (files ) {
329355 t .Fatalf ("incorrect number of parsed files: expected %d, actual %d" , len (test .Output ), len (files ))
330356 }
357+ if test .Preamble != pre {
358+ t .Errorf ("incorrect preamble\n expected: %q\n actual: %q" , test .Preamble , pre )
359+ }
331360 for i := range test .Output {
332361 if ! reflect .DeepEqual (test .Output [i ], files [i ]) {
333362 exp , _ := json .MarshalIndent (test .Output [i ], "" , " " )
334363 act , _ := json .MarshalIndent (files [i ], "" , " " )
335- t .Errorf ("incorrect file at position %d\n expected: %s\n actual : %s" , i , exp , act )
364+ t .Errorf ("incorrect file at position %d\n expected: %s\n actual : %s" , i , exp , act )
336365 }
337366 }
338367 })
0 commit comments