@@ -3,6 +3,8 @@ package gitdiff
3
3
import (
4
4
"bufio"
5
5
"io"
6
+ "os"
7
+ "reflect"
6
8
"strings"
7
9
"testing"
8
10
)
@@ -136,6 +138,91 @@ context line
136
138
}
137
139
}
138
140
141
+ func TestParseNextFileHeader (t * testing.T ) {
142
+ tests := map [string ]struct {
143
+ Input string
144
+ Output * File
145
+ Err bool
146
+ }{
147
+ "gitHeader" : {
148
+ Input : `commit 1acbae563cd6ef5750a82ee64e116c6eb065cb94
149
+ Author: Morton Haypenny <mhaypenny@example.com>
150
+ Date: Tue Apr 2 22:30:00 2019 -0700
151
+
152
+ This is a sample commit message.
153
+
154
+ diff --git a/file.txt b/file.txt
155
+ index cc34da1..1acbae5 100644
156
+ --- a/file.txt
157
+ +++ b/file.txt
158
+ @@ -1,3 +1,4 @@
159
+ ` ,
160
+ Output : & File {
161
+ OldName : "file.txt" ,
162
+ NewName : "file.txt" ,
163
+ OldMode : os .FileMode (0100644 ),
164
+ OldOIDPrefix : "cc34da1" ,
165
+ NewOIDPrefix : "1acbae5" ,
166
+ },
167
+ },
168
+ "traditionalHeader" : {
169
+ Input : `
170
+ --- file.txt 2019-04-01 22:58:14.833597918 -0700
171
+ +++ file.txt 2019-04-01 22:58:14.833597918 -0700
172
+ @@ -1,3 +1,4 @@
173
+ ` ,
174
+ Output : & File {
175
+ OldName : "file.txt" ,
176
+ NewName : "file.txt" ,
177
+ },
178
+ },
179
+ "noHeaders" : {
180
+ Input : `
181
+ this is a line
182
+ this is another line
183
+ --- could this be a header?
184
+ nope, it's just some dashes
185
+ ` ,
186
+ Output : nil ,
187
+ },
188
+ "detatchedFragmentLike" : {
189
+ Input : `
190
+ a wild fragment appears?
191
+ @@ -1,3 +1,4 ~1,5 @@
192
+ ` ,
193
+ Output : nil ,
194
+ },
195
+ "detatchedFragment" : {
196
+ Input : `
197
+ a wild fragment appears?
198
+ @@ -1,3 +1,4 @@
199
+ ` ,
200
+ Err : true ,
201
+ },
202
+ }
203
+
204
+ for name , test := range tests {
205
+ t .Run (name , func (t * testing.T ) {
206
+ p := newTestParser (test .Input , true )
207
+
208
+ f , err := p .ParseNextFileHeader ()
209
+ if test .Err {
210
+ if err == nil || err == io .EOF {
211
+ t .Fatalf ("expected error parsing next file header, but got %v" , err )
212
+ }
213
+ return
214
+ }
215
+ if err != nil {
216
+ t .Fatalf ("unexpected error parsing next file header: %v" , err )
217
+ }
218
+
219
+ if ! reflect .DeepEqual (test .Output , f ) {
220
+ t .Errorf ("incorrect file\n expected: %+v\n actual: %+v" , test .Output , f )
221
+ }
222
+ })
223
+ }
224
+ }
225
+
139
226
func newTestParser (input string , init bool ) * parser {
140
227
p := & parser {r : bufio .NewReader (strings .NewReader (input ))}
141
228
if init {
0 commit comments