1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
|
package gitdiff
import (
"io"
"os"
"reflect"
"testing"
)
func TestParseGitFileHeader(t *testing.T) {
tests := map[string]struct {
Input string
Output *File
Err bool
}{
"fileContentChange": {
Input: `diff --git a/dir/file.txt b/dir/file.txt
index 1c23fcc..40a1b33 100644
--- a/dir/file.txt
+++ b/dir/file.txt
@@ -2,3 +4,5 @@
`,
Output: &File{
OldName: "dir/file.txt",
NewName: "dir/file.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "1c23fcc",
NewOIDPrefix: "40a1b33",
},
},
"newFile": {
Input: `diff --git a/dir/file.txt b/dir/file.txt
new file mode 100644
index 0000000..f5711e4
--- /dev/null
+++ b/dir/file.txt
`,
Output: &File{
NewName: "dir/file.txt",
NewMode: os.FileMode(0100644),
OldOIDPrefix: "0000000",
NewOIDPrefix: "f5711e4",
IsNew: true,
},
},
"newEmptyFile": {
Input: `diff --git a/empty.txt b/empty.txt
new file mode 100644
index 0000000..e69de29
`,
Output: &File{
NewName: "empty.txt",
NewMode: os.FileMode(0100644),
OldOIDPrefix: "0000000",
NewOIDPrefix: "e69de29",
IsNew: true,
},
},
"deleteFile": {
Input: `diff --git a/dir/file.txt b/dir/file.txt
deleted file mode 100644
index 44cc321..0000000
--- a/dir/file.txt
+++ /dev/null
`,
Output: &File{
OldName: "dir/file.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "44cc321",
NewOIDPrefix: "0000000",
IsDelete: true,
},
},
"changeMode": {
Input: `diff --git a/file.sh b/file.sh
old mode 100644
new mode 100755
`,
Output: &File{
OldName: "file.sh",
NewName: "file.sh",
OldMode: os.FileMode(0100644),
NewMode: os.FileMode(0100755),
},
},
"rename": {
Input: `diff --git a/foo.txt b/bar.txt
similarity index 100%
rename from foo.txt
rename to bar.txt
`,
Output: &File{
OldName: "foo.txt",
NewName: "bar.txt",
Score: 100,
IsRename: true,
},
},
"copy": {
Input: `diff --git a/file.txt b/copy.txt
similarity index 100%
copy from file.txt
copy to copy.txt
`,
Output: &File{
OldName: "file.txt",
NewName: "copy.txt",
Score: 100,
IsCopy: true,
},
},
"missingDefaultFilename": {
Input: `diff --git a/foo.sh b/bar.sh
old mode 100644
new mode 100755
`,
Err: true,
},
"missingNewFilename": {
Input: `diff --git a/file.txt b/file.txt
index 1c23fcc..40a1b33 100644
--- a/file.txt
`,
Err: true,
},
"missingOldFilename": {
Input: `diff --git a/file.txt b/file.txt
index 1c23fcc..40a1b33 100644
+++ b/file.txt
`,
Err: true,
},
"invalidHeaderLine": {
Input: `diff --git a/file.txt b/file.txt
index deadbeef
--- a/file.txt
+++ b/file.txt
`,
Err: true,
},
"notGitHeader": {
Input: `--- file.txt
+++ file.txt
@@ -0,0 +1 @@
`,
Output: nil,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
p := newTestParser(test.Input, true)
f, err := p.ParseGitFileHeader()
if test.Err {
if err == nil || err == io.EOF {
t.Fatalf("expected error parsing git file header, got %v", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error parsing git file header: %v", err)
}
if !reflect.DeepEqual(test.Output, f) {
t.Errorf("incorrect file\nexpected: %+v\n actual: %+v", test.Output, f)
}
})
}
}
func TestParseTraditionalFileHeader(t *testing.T) {
tests := map[string]struct {
Input string
Output *File
Err bool
}{
"fileContentChange": {
Input: `--- dir/file_old.txt 2019-03-21 23:00:00.0 -0700
+++ dir/file_new.txt 2019-03-21 23:30:00.0 -0700
@@ -0,0 +1 @@
`,
Output: &File{
OldName: "dir/file_new.txt",
NewName: "dir/file_new.txt",
},
},
"newFile": {
Input: `--- /dev/null 1969-12-31 17:00:00.0 -0700
+++ dir/file.txt 2019-03-21 23:30:00.0 -0700
@@ -0,0 +1 @@
`,
Output: &File{
NewName: "dir/file.txt",
IsNew: true,
},
},
"newFileTimestamp": {
Input: `--- dir/file.txt 1969-12-31 17:00:00.0 -0700
+++ dir/file.txt 2019-03-21 23:30:00.0 -0700
@@ -0,0 +1 @@
`,
Output: &File{
NewName: "dir/file.txt",
IsNew: true,
},
},
"deleteFile": {
Input: `--- dir/file.txt 2019-03-21 23:30:00.0 -0700
+++ /dev/null 1969-12-31 17:00:00.0 -0700
@@ -0,0 +1 @@
`,
Output: &File{
OldName: "dir/file.txt",
IsDelete: true,
},
},
"deleteFileTimestamp": {
Input: `--- dir/file.txt 2019-03-21 23:30:00.0 -0700
+++ dir/file.txt 1969-12-31 17:00:00.0 -0700
@@ -0,0 +1 @@
`,
Output: &File{
OldName: "dir/file.txt",
IsDelete: true,
},
},
"useShortestPrefixName": {
Input: `--- dir/file.txt 2019-03-21 23:00:00.0 -0700
+++ dir/file.txt~ 2019-03-21 23:30:00.0 -0700
@@ -0,0 +1 @@
`,
Output: &File{
OldName: "dir/file.txt",
NewName: "dir/file.txt",
},
},
"notTraditionalHeader": {
Input: `diff --git a/dir/file.txt b/dir/file.txt
--- a/dir/file.txt
+++ b/dir/file.txt
`,
Output: nil,
},
"noUnifiedFragment": {
Input: `--- dir/file_old.txt 2019-03-21 23:00:00.0 -0700
+++ dir/file_new.txt 2019-03-21 23:30:00.0 -0700
context line
+added line
`,
Output: nil,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
p := newTestParser(test.Input, true)
f, err := p.ParseTraditionalFileHeader()
if test.Err {
if err == nil || err == io.EOF {
t.Fatalf("expected error parsing traditional file header, got %v", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error parsing traditional file header: %v", err)
}
if !reflect.DeepEqual(test.Output, f) {
t.Errorf("incorrect file\nexpected: %+v\n actual: %+v", test.Output, f)
}
})
}
}
func TestCleanName(t *testing.T) {
tests := map[string]struct {
Input string
Drop int
Output string
}{
"alreadyClean": {
Input: "a/b/c.txt", Output: "a/b/c.txt",
},
"doubleSlashes": {
Input: "a//b/c.txt", Output: "a/b/c.txt",
},
"tripleSlashes": {
Input: "a///b/c.txt", Output: "a/b/c.txt",
},
"dropPrefix": {
Input: "a/b/c.txt", Drop: 2, Output: "c.txt",
},
"removeDoublesBeforeDrop": {
Input: "a//b/c.txt", Drop: 1, Output: "b/c.txt",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
output := cleanName(test.Input, test.Drop)
if output != test.Output {
t.Fatalf("incorrect output: expected %q, actual %q", test.Output, output)
}
})
}
}
func TestParseName(t *testing.T) {
tests := map[string]struct {
Input string
Term byte
Drop int
Output string
N int
Err bool
}{
"singleUnquoted": {
Input: "dir/file.txt", Output: "dir/file.txt", N: 12,
},
"singleQuoted": {
Input: `"dir/file.txt"`, Output: "dir/file.txt", N: 14,
},
"quotedWithEscape": {
Input: `"dir/\"quotes\".txt"`, Output: `dir/"quotes".txt`, N: 20,
},
"quotedWithSpaces": {
Input: `"dir/space file.txt"`, Output: "dir/space file.txt", N: 20,
},
"tabTerminator": {
Input: "dir/space file.txt\tfile2.txt", Term: '\t', Output: "dir/space file.txt", N: 18,
},
"dropPrefix": {
Input: "a/dir/file.txt", Drop: 1, Output: "dir/file.txt", N: 14,
},
"unquotedWithSpaces": {
Input: "dir/with spaces.txt", Output: "dir/with spaces.txt", N: 19,
},
"unquotedWithTrailingSpaces": {
Input: "dir/with spaces.space ", Output: "dir/with spaces.space ", N: 23,
},
"devNull": {
Input: "/dev/null", Term: '\t', Drop: 1, Output: "/dev/null", N: 9,
},
"newlineSeparates": {
Input: "dir/file.txt\n", Output: "dir/file.txt", N: 12,
},
"emptyString": {
Input: "", Err: true,
},
"emptyQuotedString": {
Input: `""`, Err: true,
},
"unterminatedQuotes": {
Input: `"dir/file.txt`, Err: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
output, n, err := parseName(test.Input, test.Term, test.Drop)
if test.Err {
if err == nil || err == io.EOF {
t.Fatalf("expected error parsing name, but got %v", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error parsing name: %v", err)
}
if output != test.Output {
t.Errorf("incorrect output: expected %q, actual: %q", test.Output, output)
}
if n != test.N {
t.Errorf("incorrect next position: expected %d, actual %d", test.N, n)
}
})
}
}
func TestParseGitHeaderData(t *testing.T) {
tests := map[string]struct {
InputFile *File
Line string
DefaultName string
OutputFile *File
End bool
Err bool
}{
"fragementEndsParsing": {
Line: "@@ -12,3 +12,2 @@\n",
End: true,
},
"unknownEndsParsing": {
Line: "GIT binary file\n",
End: true,
},
"oldFileName": {
Line: "--- a/dir/file.txt\n",
OutputFile: &File{
OldName: "dir/file.txt",
},
},
"oldFileNameDevNull": {
InputFile: &File{
IsNew: true,
},
Line: "--- /dev/null\n",
OutputFile: &File{
IsNew: true,
},
},
"oldFileNameInconsistent": {
InputFile: &File{
OldName: "dir/foo.txt",
},
Line: "--- a/dir/bar.txt\n",
Err: true,
},
"oldFileNameExistingCreateMismatch": {
InputFile: &File{
OldName: "dir/foo.txt",
IsNew: true,
},
Line: "--- /dev/null\n",
Err: true,
},
"oldFileNameParsedCreateMismatch": {
InputFile: &File{
IsNew: true,
},
Line: "--- a/dir/file.txt\n",
Err: true,
},
"oldFileNameMissing": {
Line: "--- \n",
Err: true,
},
"newFileName": {
Line: "+++ b/dir/file.txt\n",
OutputFile: &File{
NewName: "dir/file.txt",
},
},
"newFileNameDevNull": {
InputFile: &File{
IsDelete: true,
},
Line: "+++ /dev/null\n",
OutputFile: &File{
IsDelete: true,
},
},
"newFileNameInconsistent": {
InputFile: &File{
NewName: "dir/foo.txt",
},
Line: "+++ b/dir/bar.txt\n",
Err: true,
},
"newFileNameExistingDeleteMismatch": {
InputFile: &File{
NewName: "dir/foo.txt",
IsDelete: true,
},
Line: "+++ /dev/null\n",
Err: true,
},
"newFileNameParsedDeleteMismatch": {
InputFile: &File{
IsDelete: true,
},
Line: "+++ b/dir/file.txt\n",
Err: true,
},
"newFileNameMissing": {
Line: "+++ \n",
Err: true,
},
"oldMode": {
Line: "old mode 100644\n",
OutputFile: &File{
OldMode: os.FileMode(0100644),
},
},
"invalidOldMode": {
Line: "old mode rw\n",
Err: true,
},
"newMode": {
Line: "new mode 100755\n",
OutputFile: &File{
NewMode: os.FileMode(0100755),
},
},
"invalidNewMode": {
Line: "new mode rwx\n",
Err: true,
},
"deletedFileMode": {
Line: "deleted file mode 100644\n",
DefaultName: "dir/file.txt",
OutputFile: &File{
OldName: "dir/file.txt",
OldMode: os.FileMode(0100644),
IsDelete: true,
},
},
"newFileMode": {
Line: "new file mode 100755\n",
DefaultName: "dir/file.txt",
OutputFile: &File{
NewName: "dir/file.txt",
NewMode: os.FileMode(0100755),
IsNew: true,
},
},
"copyFrom": {
Line: "copy from dir/file.txt\n",
OutputFile: &File{
OldName: "dir/file.txt",
IsCopy: true,
},
},
"copyTo": {
Line: "copy to dir/file.txt\n",
OutputFile: &File{
NewName: "dir/file.txt",
IsCopy: true,
},
},
"renameFrom": {
Line: "rename from dir/file.txt\n",
OutputFile: &File{
OldName: "dir/file.txt",
IsRename: true,
},
},
"renameTo": {
Line: "rename to dir/file.txt\n",
OutputFile: &File{
NewName: "dir/file.txt",
IsRename: true,
},
},
"similarityIndex": {
Line: "similarity index 88%\n",
OutputFile: &File{
Score: 88,
},
},
"similarityIndexTooBig": {
Line: "similarity index 9001%\n",
OutputFile: &File{
Score: 0,
},
},
"similarityIndexInvalid": {
Line: "similarity index 12ab%\n",
Err: true,
},
"indexFullSHA1AndMode": {
Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14..04fab916d8f938173cbb8b93469855f0e838f098 100644\n",
OutputFile: &File{
OldOIDPrefix: "79c6d7f7b7e76c75b3d238f12fb1323f2333ba14",
NewOIDPrefix: "04fab916d8f938173cbb8b93469855f0e838f098",
OldMode: os.FileMode(0100644),
},
},
"indexFullSHA1NoMode": {
Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14..04fab916d8f938173cbb8b93469855f0e838f098\n",
OutputFile: &File{
OldOIDPrefix: "79c6d7f7b7e76c75b3d238f12fb1323f2333ba14",
NewOIDPrefix: "04fab916d8f938173cbb8b93469855f0e838f098",
},
},
"indexAbbrevSHA1AndMode": {
Line: "index 79c6d7..04fab9 100644\n",
OutputFile: &File{
OldOIDPrefix: "79c6d7",
NewOIDPrefix: "04fab9",
OldMode: os.FileMode(0100644),
},
},
"indexInvalid": {
Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14\n",
Err: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var f File
if test.InputFile != nil {
f = *test.InputFile
}
end, err := parseGitHeaderData(&f, test.Line, test.DefaultName)
if test.Err {
if err == nil || err == io.EOF {
t.Fatalf("expected error parsing header data, but got %v", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error parsing header data: %v", err)
}
if test.OutputFile != nil && !reflect.DeepEqual(test.OutputFile, &f) {
t.Errorf("incorrect output:\nexpected: %+v\nactual: %+v", test.OutputFile, &f)
}
if end != test.End {
t.Errorf("incorrect end state, expected %t, actual %t", test.End, end)
}
})
}
}
func TestParseGitHeaderName(t *testing.T) {
tests := map[string]struct {
Input string
Output string
Err bool
}{
"twoMatchingNames": {
Input: "a/dir/file.txt b/dir/file.txt",
Output: "dir/file.txt",
},
"twoDifferentNames": {
Input: "a/dir/foo.txt b/dir/bar.txt",
Output: "",
},
"matchingNamesWithSpaces": {
Input: "a/dir/file with spaces.txt b/dir/file with spaces.txt",
Output: "dir/file with spaces.txt",
},
"matchingNamesWithTrailingSpaces": {
Input: "a/dir/spaces b/dir/spaces ",
Output: "dir/spaces ",
},
"matchingNamesQuoted": {
Input: `"a/dir/\"quotes\".txt" "b/dir/\"quotes\".txt"`,
Output: `dir/"quotes".txt`,
},
"matchingNamesFirstQuoted": {
Input: `"a/dir/file.txt" b/dir/file.txt`,
Output: "dir/file.txt",
},
"matchingNamesSecondQuoted": {
Input: `a/dir/file.txt "b/dir/file.txt"`,
Output: "dir/file.txt",
},
"noSecondName": {
Input: "a/dir/foo.txt",
Output: "",
},
"noSecondNameQuoted": {
Input: `"a/dir/foo.txt"`,
Output: "",
},
"invalidName": {
Input: `"a/dir/file.txt b/dir/file.txt`,
Err: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
output, err := parseGitHeaderName(test.Input)
if test.Err {
if err == nil {
t.Fatalf("expected error parsing header name, but got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error parsing header name: %v", err)
}
if output != test.Output {
t.Errorf("incorrect output: expected %q, actual %q", test.Output, output)
}
})
}
}
func TestHasEpochTimestamp(t *testing.T) {
tests := map[string]struct {
Input string
Output bool
}{
"utcTimestamp": {
Input: "+++ file.txt\t1970-01-01 00:00:00 +0000\n",
Output: true,
},
"utcZoneWithColon": {
Input: "+++ file.txt\t1970-01-01 00:00:00 +00:00\n",
Output: true,
},
"utcZoneWithMilliseconds": {
Input: "+++ file.txt\t1970-01-01 00:00:00.000000 +00:00\n",
Output: true,
},
"westTimestamp": {
Input: "+++ file.txt\t1969-12-31 16:00:00 -0800\n",
Output: true,
},
"eastTimestamp": {
Input: "+++ file.txt\t1970-01-01 04:00:00 +0400\n",
Output: true,
},
"noTab": {
Input: "+++ file.txt 1970-01-01 00:00:00 +0000\n",
Output: false,
},
"invalidFormat": {
Input: "+++ file.txt\t1970-01-01T00:00:00Z\n",
Output: false,
},
"notEpoch": {
Input: "+++ file.txt\t2019-03-21 12:34:56.789 -0700\n",
Output: false,
},
"notTimestamp": {
Input: "+++ file.txt\trandom text\n",
Output: false,
},
"notTimestampShort": {
Input: "+++ file.txt\t0\n",
Output: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
output := hasEpochTimestamp(test.Input)
if output != test.Output {
t.Errorf("incorrect output: expected %t, actual %t", test.Output, output)
}
})
}
}
|