this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add tests for parsing full binary fragments

+164 -7
+108
gitdiff/parser_binary_test.go
··· 193 193 } 194 194 } 195 195 196 + func TestParseBinaryFragments(t *testing.T) { 197 + tests := map[string]struct { 198 + Input string 199 + File File 200 + 201 + Binary bool 202 + Fragment *BinaryFragment 203 + ReverseFragment *BinaryFragment 204 + Err bool 205 + }{ 206 + "dataWithReverse": { 207 + Input: `GIT binary patch 208 + literal 40 209 + gcmZQzU|?i` + "`" + `U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx 210 + 211 + literal 0 212 + HcmV?d00001 213 + 214 + `, 215 + Binary: true, 216 + Fragment: &BinaryFragment{ 217 + Method: BinaryPatchLiteral, 218 + Size: 40, 219 + Data: fib(10, binary.BigEndian), 220 + }, 221 + ReverseFragment: &BinaryFragment{ 222 + Method: BinaryPatchLiteral, 223 + Size: 0, 224 + Data: []byte{}, 225 + }, 226 + }, 227 + "dataWithoutReverse": { 228 + Input: `GIT binary patch 229 + literal 40 230 + gcmZQzU|?i` + "`" + `U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx 231 + 232 + `, 233 + Binary: true, 234 + Fragment: &BinaryFragment{ 235 + Method: BinaryPatchLiteral, 236 + Size: 40, 237 + Data: fib(10, binary.BigEndian), 238 + }, 239 + }, 240 + "noData": { 241 + Input: "Binary files differ\n", 242 + Binary: true, 243 + }, 244 + "text": { 245 + Input: `@@ -1 +1 @@ 246 + -old line 247 + +new line 248 + `, 249 + Binary: false, 250 + }, 251 + "missingData": { 252 + Input: "GIT binary patch\n", 253 + Err: true, 254 + }, 255 + "invalidData": { 256 + Input: `GIT binary patch 257 + literal 20 258 + TcmZQzU|?i'U?w2V48*Je09XJG 259 + 260 + `, 261 + Err: true, 262 + }, 263 + "invalidReverseData": { 264 + Input: `GIT binary patch 265 + literal 20 266 + TcmZQzU|?i` + "`" + `U?w2V48*Je09XJG 267 + 268 + literal 0 269 + zcmV?d00001 270 + 271 + `, 272 + Err: true, 273 + }, 274 + } 275 + 276 + for name, test := range tests { 277 + t.Run(name, func(t *testing.T) { 278 + p := newTestParser(test.Input, true) 279 + 280 + file := test.File 281 + _, err := p.ParseBinaryFragments(&file) 282 + if test.Err { 283 + if err == nil || err == io.EOF { 284 + t.Fatalf("expected error parsing binary fragments, but got %v", err) 285 + } 286 + return 287 + } 288 + if err != nil { 289 + t.Fatalf("unexpected error parsing binary fragments: %v", err) 290 + } 291 + if test.Binary != file.IsBinary { 292 + t.Errorf("incorrect binary state: expected %t, actual %t", test.Binary, file.IsBinary) 293 + } 294 + if !reflect.DeepEqual(test.Fragment, file.BinaryFragment) { 295 + t.Errorf("incorrect binary fragment\nexpected: %+v\n actual: %+v", test.Fragment, file.BinaryFragment) 296 + } 297 + if !reflect.DeepEqual(test.ReverseFragment, file.ReverseBinaryFragment) { 298 + t.Errorf("incorrect reverse binary fragment\nexpected: %+v\n actual: %+v", test.ReverseFragment, file.ReverseBinaryFragment) 299 + } 300 + }) 301 + } 302 + } 303 + 196 304 func fib(n int, ord binary.ByteOrder) []byte { 197 305 buf := make([]byte, 4*n) 198 306 for i := 0; i < len(buf); i += 4 {
+40 -7
gitdiff/parser_test.go
··· 2 2 3 3 import ( 4 4 "bufio" 5 + "encoding/binary" 5 6 "encoding/json" 6 7 "io" 7 8 "os" ··· 280 281 } 281 282 282 283 func TestParse(t *testing.T) { 283 - expectedFragments := []*TextFragment{ 284 + textFragments := []*TextFragment{ 284 285 { 285 286 OldPosition: 3, 286 287 OldLines: 6, ··· 321 322 }, 322 323 } 323 324 324 - expectedPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe 325 + textPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe 325 326 Author: Morton Haypenny <mhaypenny@example.com> 326 327 Date: Tue Apr 2 22:55:40 2019 -0700 327 328 ··· 331 332 332 333 ` 333 334 335 + binaryPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe 336 + Author: Morton Haypenny <mhaypenny@example.com> 337 + Date: Tue Apr 2 22:55:40 2019 -0700 338 + 339 + A binary file with the first 10 fibonacci numbers. 340 + 341 + ` 334 342 tests := map[string]struct { 335 343 InputFile string 336 344 Output []*File ··· 346 354 OldMode: os.FileMode(0100644), 347 355 OldOIDPrefix: "ebe9fa54", 348 356 NewOIDPrefix: "fe103e1d", 349 - TextFragments: expectedFragments, 357 + TextFragments: textFragments, 350 358 }, 351 359 }, 352 - Preamble: expectedPreamble, 360 + Preamble: textPreamble, 353 361 }, 354 362 "twoFiles": { 355 363 InputFile: "testdata/two_files.patch", ··· 360 368 OldMode: os.FileMode(0100644), 361 369 OldOIDPrefix: "ebe9fa54", 362 370 NewOIDPrefix: "fe103e1d", 363 - TextFragments: expectedFragments, 371 + TextFragments: textFragments, 364 372 }, 365 373 { 366 374 OldName: "dir/file2.txt", ··· 368 376 OldMode: os.FileMode(0100644), 369 377 OldOIDPrefix: "417ebc70", 370 378 NewOIDPrefix: "67514b7f", 371 - TextFragments: expectedFragments, 379 + TextFragments: textFragments, 372 380 }, 373 381 }, 374 - Preamble: expectedPreamble, 382 + Preamble: textPreamble, 383 + }, 384 + "newBinaryFile": { 385 + InputFile: "testdata/new_binary_file.patch", 386 + Output: []*File{ 387 + { 388 + OldName: "", 389 + NewName: "dir/ten.bin", 390 + NewMode: os.FileMode(0100644), 391 + OldOIDPrefix: "0000000000000000000000000000000000000000", 392 + NewOIDPrefix: "77b068ba48c356156944ea714740d0d5ca07bfec", 393 + IsNew: true, 394 + IsBinary: true, 395 + BinaryFragment: &BinaryFragment{ 396 + Method: BinaryPatchLiteral, 397 + Size: 40, 398 + Data: fib(10, binary.BigEndian), 399 + }, 400 + ReverseBinaryFragment: &BinaryFragment{ 401 + Method: BinaryPatchLiteral, 402 + Size: 0, 403 + Data: []byte{}, 404 + }, 405 + }, 406 + }, 407 + Preamble: binaryPreamble, 375 408 }, 376 409 } 377 410
+16
gitdiff/testdata/new_binary_file.patch
··· 1 + commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe 2 + Author: Morton Haypenny <mhaypenny@example.com> 3 + Date: Tue Apr 2 22:55:40 2019 -0700 4 + 5 + A binary file with the first 10 fibonacci numbers. 6 + 7 + diff --git a/dir/ten.bin b/dir/ten.bin 8 + new file mode 100644 9 + index 0000000000000000000000000000000000000000..77b068ba48c356156944ea714740d0d5ca07bfec 10 + GIT binary patch 11 + literal 40 12 + gcmZQzU|?i`U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx 13 + 14 + literal 0 15 + HcmV?d00001 16 +