@@ -133,17 +133,18 @@ context line
133
133
}
134
134
135
135
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 ))
137
137
}
138
138
})
139
139
}
140
140
}
141
141
142
142
func TestParseNextFileHeader (t * testing.T ) {
143
143
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
147
148
}{
148
149
"gitHeader" : {
149
150
Input : `commit 1acbae563cd6ef5750a82ee64e116c6eb065cb94
@@ -165,6 +166,13 @@ index cc34da1..1acbae5 100644
165
166
OldOIDPrefix : "cc34da1" ,
166
167
NewOIDPrefix : "1acbae5" ,
167
168
},
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
+ ` ,
168
176
},
169
177
"traditionalHeader" : {
170
178
Input : `
@@ -176,6 +184,7 @@ index cc34da1..1acbae5 100644
176
184
OldName : "file.txt" ,
177
185
NewName : "file.txt" ,
178
186
},
187
+ Preamble : "\n " ,
179
188
},
180
189
"noHeaders" : {
181
190
Input : `
@@ -184,7 +193,8 @@ this is another line
184
193
--- could this be a header?
185
194
nope, it's just some dashes
186
195
` ,
187
- Output : nil ,
196
+ Output : nil ,
197
+ Preamble : "" ,
188
198
},
189
199
"detatchedFragmentLike" : {
190
200
Input : `
@@ -206,7 +216,7 @@ a wild fragment appears?
206
216
t .Run (name , func (t * testing.T ) {
207
217
p := newTestParser (test .Input , true )
208
218
209
- f , err := p .ParseNextFileHeader ()
219
+ f , pre , err := p .ParseNextFileHeader ()
210
220
if test .Err {
211
221
if err == nil || err == io .EOF {
212
222
t .Fatalf ("expected error parsing next file header, but got %v" , err )
@@ -217,8 +227,11 @@ a wild fragment appears?
217
227
t .Fatalf ("unexpected error parsing next file header: %v" , err )
218
228
}
219
229
230
+ if test .Preamble != pre {
231
+ t .Errorf ("incorrect preamble\n expected: %q\n actual: %q" , test .Preamble , pre )
232
+ }
220
233
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 )
222
235
}
223
236
})
224
237
}
@@ -266,9 +279,20 @@ func TestParse(t *testing.T) {
266
279
},
267
280
}
268
281
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
+
269
292
tests := map [string ]struct {
270
293
InputFile string
271
294
Output []* File
295
+ Preamble string
272
296
Err bool
273
297
}{
274
298
"oneFile" : {
@@ -283,6 +307,7 @@ func TestParse(t *testing.T) {
283
307
Fragments : expectedFragments ,
284
308
},
285
309
},
310
+ Preamble : expectedPreamble ,
286
311
},
287
312
"twoFiles" : {
288
313
InputFile : "testdata/two_files.patch" ,
@@ -304,6 +329,7 @@ func TestParse(t *testing.T) {
304
329
Fragments : expectedFragments ,
305
330
},
306
331
},
332
+ Preamble : expectedPreamble ,
307
333
},
308
334
}
309
335
@@ -314,7 +340,7 @@ func TestParse(t *testing.T) {
314
340
t .Fatalf ("unexpected error opening input file: %v" , err )
315
341
}
316
342
317
- files , err := Parse (f )
343
+ files , pre , err := Parse (f )
318
344
if test .Err {
319
345
if err == nil || err == io .EOF {
320
346
t .Fatalf ("expected error parsing patch, but got %v" , err )
@@ -328,11 +354,14 @@ func TestParse(t *testing.T) {
328
354
if len (test .Output ) != len (files ) {
329
355
t .Fatalf ("incorrect number of parsed files: expected %d, actual %d" , len (test .Output ), len (files ))
330
356
}
357
+ if test .Preamble != pre {
358
+ t .Errorf ("incorrect preamble\n expected: %q\n actual: %q" , test .Preamble , pre )
359
+ }
331
360
for i := range test .Output {
332
361
if ! reflect .DeepEqual (test .Output [i ], files [i ]) {
333
362
exp , _ := json .MarshalIndent (test .Output [i ], "" , " " )
334
363
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 )
336
365
}
337
366
}
338
367
})
0 commit comments