1package git23import (4 "strings"5 "testing"67 "github.com/stretchr/testify/assert"8)910func TestDiffSection_NumLines(t *testing.T) {11 section := &DiffSection{12 Lines: []*DiffLine{13 {14 Type: DiffLineAdd,15 Content: "a line",16 LeftLine: 1,17 RightLine: 10,18 },19 },20 }2122 assert.Equal(t, 1, section.NumLines())23}2425func TestDiffSection_Line(t *testing.T) {26 lineDelete := &DiffLine{27 Type: DiffLineDelete,28 Content: `- <groupId>com.ambientideas</groupId>`,29 LeftLine: 4,30 RightLine: 0,31 }32 lineAdd := &DiffLine{33 Type: DiffLineAdd,34 Content: `+ <groupId>com.github</groupId>`,35 LeftLine: 0,36 RightLine: 4,37 }38 section := &DiffSection{39 Lines: []*DiffLine{40 {41 Type: DiffLineSection,42 Content: "@@ -1,7 +1,7 @@",43 }, {44 Type: DiffLinePlain,45 Content: ` <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"`,46 LeftLine: 1,47 RightLine: 1,48 }, {49 Type: DiffLinePlain,50 Content: ` xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">`,51 LeftLine: 2,52 RightLine: 2,53 }, {54 Type: DiffLinePlain,55 Content: ` <modelVersion>4.0.0</modelVersion>`,56 LeftLine: 3,57 RightLine: 3,58 },59 lineDelete,60 lineAdd,61 {62 Type: DiffLinePlain,63 Content: ` <artifactId>egitdemo</artifactId>`,64 LeftLine: 5,65 RightLine: 5,66 }, {67 Type: DiffLinePlain,68 Content: ` <packaging>jar</packaging>`,69 LeftLine: 6,70 RightLine: 6,71 }, {72 Type: DiffLinePlain,73 Content: ` <version>1.0-SNAPSHOT</version>`,74 LeftLine: 7,75 RightLine: 7,76 },77 },78 }7980 assert.Equal(t, lineDelete, section.Line(lineDelete.Type, 4))81 assert.Equal(t, lineAdd, section.Line(lineAdd.Type, 4))82}8384func TestDiffFile(t *testing.T) {85 file := &DiffFile{86 Name: ".gitmodules",87 Type: DiffFileAdd,88 Index: "6abde17",89 Sections: []*DiffSection{90 {91 Lines: []*DiffLine{92 {93 Type: DiffLineSection,94 Content: "@@ -0,0 +1,3 @@",95 }, {96 Type: DiffLineAdd,97 Content: `+[submodule "gogs/docs-api"]`,98 LeftLine: 0,99 RightLine: 1,100 }, {101 Type: DiffLineAdd,102 Content: `+ path = gogs/docs-api`,103 LeftLine: 0,104 RightLine: 2,105 },106 },107 },108 },109 numAdditions: 2,110 numDeletions: 0,111 oldName: "",112 isBinary: false,113 isSubmodule: false,114 isIncomplete: true,115 }116117 assert.Equal(t, 1, file.NumSections())118 assert.Equal(t, 2, file.NumAdditions())119 assert.Equal(t, 0, file.NumDeletions())120 assert.True(t, file.IsCreated())121 assert.False(t, file.IsDeleted())122 assert.False(t, file.IsRenamed())123 assert.Empty(t, file.OldName())124 assert.False(t, file.IsBinary())125 assert.False(t, file.IsSubmodule())126 assert.True(t, file.IsIncomplete())127}128129func TestDiff(t *testing.T) {130 diff := &Diff{131 Files: []*DiffFile{132 {133 Name: "run.sh",134 Type: DiffFileRename,135 Index: "",136 Sections: nil,137 numAdditions: 0,138 numDeletions: 0,139 oldName: "runme.sh",140 isBinary: false,141 isSubmodule: false,142 isIncomplete: false,143 mode: 100644,144 oldMode: 100644,145 },146 },147 totalAdditions: 10,148 totalDeletions: 20,149 isIncomplete: false,150 }151152 assert.Equal(t, 1, diff.NumFiles())153 assert.Equal(t, 10, diff.TotalAdditions())154 assert.Equal(t, 20, diff.TotalDeletions())155 assert.False(t, diff.IsIncomplete())156}157158func TestStreamParseDiff(t *testing.T) {159 tests := []struct {160 input string161 maxFileLines int162 maxLineChars int163 maxFiles int164 expDiff *Diff165 }{166 {167 input: `diff --git a/.gitmodules b/.gitmodules168new file mode 100644169index 0000000..6abde17170--- /dev/null171+++ b/.gitmodules172@@ -0,0 +1,3 @@173+[submodule "gogs/docs-api"]174+ path = gogs/docs-api175+ url = https://github.com/gogs/docs-api.git176diff --git a/gogs/docs-api b/gogs/docs-api177new file mode 160000178index 0000000..6b08f76179--- /dev/null180+++ b/gogs/docs-api181@@ -0,0 +1 @@182+Subproject commit 6b08f76a5313fa3d26859515b30aa17a5faa2807`,183 expDiff: &Diff{184 Files: []*DiffFile{185 {186 Name: ".gitmodules",187 Type: DiffFileAdd,188 Index: "6abde17",189 OldIndex: "0000000",190 Sections: []*DiffSection{191 {192 Lines: []*DiffLine{193 {194 Type: DiffLineSection,195 Content: "@@ -0,0 +1,3 @@",196 }, {197 Type: DiffLineAdd,198 Content: `+[submodule "gogs/docs-api"]`,199 LeftLine: 0,200 RightLine: 1,201 }, {202 Type: DiffLineAdd,203 Content: `+ path = gogs/docs-api`,204 LeftLine: 0,205 RightLine: 2,206 }, {207 Type: DiffLineAdd,208 Content: `+ url = https://github.com/gogs/docs-api.git`,209 LeftLine: 0,210 RightLine: 3,211 },212 },213 numAdditions: 3,214 numDeletions: 0,215 },216 },217 numAdditions: 3,218 numDeletions: 0,219 oldName: ".gitmodules",220 isBinary: false,221 isSubmodule: false,222 isIncomplete: false,223 mode: 0100644,224 oldMode: 0100644,225 },226 {227 Name: "gogs/docs-api",228 Type: DiffFileAdd,229 Index: "6b08f76",230 OldIndex: "0000000",231 Sections: []*DiffSection{232 {233 Lines: []*DiffLine{234 {235 Type: DiffLineSection,236 Content: "@@ -0,0 +1 @@",237 }, {238 Type: DiffLineAdd,239 Content: `+Subproject commit 6b08f76a5313fa3d26859515b30aa17a5faa2807`,240 LeftLine: 0,241 RightLine: 1,242 },243 },244 numAdditions: 1,245 numDeletions: 0,246 },247 },248 numAdditions: 1,249 numDeletions: 0,250 oldName: "gogs/docs-api",251 isBinary: false,252 isSubmodule: true,253 isIncomplete: false,254 mode: 0160000,255 oldMode: 0160000,256 },257 },258 totalAdditions: 4,259 totalDeletions: 0,260 isIncomplete: false,261 },262 },263 {264 input: `diff --git a/pom.xml b/pom.xml265index ee791be..9997571 100644266--- a/pom.xml267+++ b/pom.xml268@@ -1,7 +1,7 @@269 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"270 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">271 <modelVersion>4.0.0</modelVersion>272- <groupId>com.ambientideas</groupId>273+ <groupId>com.github</groupId>274 <artifactId>egitdemo</artifactId>275 <packaging>jar</packaging>276 <version>1.0-SNAPSHOT</version>`,277 expDiff: &Diff{278 Files: []*DiffFile{279 {280 Name: "pom.xml",281 Type: DiffFileChange,282 Index: "9997571",283 OldIndex: "ee791be",284 Sections: []*DiffSection{285 {286 Lines: []*DiffLine{287 {288 Type: DiffLineSection,289 Content: "@@ -1,7 +1,7 @@",290 }, {291 Type: DiffLinePlain,292 Content: ` <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"`,293 LeftLine: 1,294 RightLine: 1,295 }, {296 Type: DiffLinePlain,297 Content: ` xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">`,298 LeftLine: 2,299 RightLine: 2,300 }, {301 Type: DiffLinePlain,302 Content: ` <modelVersion>4.0.0</modelVersion>`,303 LeftLine: 3,304 RightLine: 3,305 }, {306 Type: DiffLineDelete,307 Content: `- <groupId>com.ambientideas</groupId>`,308 LeftLine: 4,309 RightLine: 0,310 }, {311 Type: DiffLineAdd,312 Content: `+ <groupId>com.github</groupId>`,313 LeftLine: 0,314 RightLine: 4,315 }, {316 Type: DiffLinePlain,317 Content: ` <artifactId>egitdemo</artifactId>`,318 LeftLine: 5,319 RightLine: 5,320 }, {321 Type: DiffLinePlain,322 Content: ` <packaging>jar</packaging>`,323 LeftLine: 6,324 RightLine: 6,325 }, {326 Type: DiffLinePlain,327 Content: ` <version>1.0-SNAPSHOT</version>`,328 LeftLine: 7,329 RightLine: 7,330 },331 },332 numAdditions: 1,333 numDeletions: 1,334 },335 },336 numAdditions: 1,337 numDeletions: 1,338 oldName: "pom.xml",339 isBinary: false,340 isSubmodule: false,341 isIncomplete: false,342 oldMode: 0100644,343 mode: 0100644,344 },345 },346 totalAdditions: 1,347 totalDeletions: 1,348 isIncomplete: false,349 },350 },351 {352 input: `diff --git a/img/sourcegraph.png b/img/sourcegraph.png353new file mode 100644354index 0000000..2ce9188355Binary files /dev/null and b/img/sourcegraph.png differ`,356 expDiff: &Diff{357 Files: []*DiffFile{358 {359 Name: "img/sourcegraph.png",360 Type: DiffFileAdd,361 Index: "2ce9188",362 OldIndex: "0000000",363 Sections: nil,364 numAdditions: 0,365 numDeletions: 0,366 oldName: "img/sourcegraph.png",367 isBinary: true,368 isSubmodule: false,369 isIncomplete: false,370 mode: 0100644,371 oldMode: 0100644,372 },373 },374 totalAdditions: 0,375 totalDeletions: 0,376 isIncomplete: false,377 },378 },379 {380 input: `diff --git a/fix.txt b/fix.txt381deleted file mode 100644382index e69de29..0000000`,383 expDiff: &Diff{384 Files: []*DiffFile{385 {386 Name: "fix.txt",387 Type: DiffFileDelete,388 Index: "0000000",389 OldIndex: "e69de29",390 Sections: nil,391 numAdditions: 0,392 numDeletions: 0,393 oldName: "fix.txt",394 isBinary: false,395 isSubmodule: false,396 isIncomplete: false,397 mode: 0100644,398 oldMode: 0100644,399 },400 },401 totalAdditions: 0,402 totalDeletions: 0,403 isIncomplete: false,404 },405 },406 {407 input: `diff --git a/runme.sh b/run.sh408similarity index 100%409rename from runme.sh410rename to run.sh`,411 expDiff: &Diff{412 Files: []*DiffFile{413 {414 Name: "run.sh",415 Type: DiffFileRename,416 Index: "",417 Sections: nil,418 numAdditions: 0,419 numDeletions: 0,420 oldName: "runme.sh",421 isBinary: false,422 isSubmodule: false,423 isIncomplete: false,424 },425 },426 totalAdditions: 0,427 totalDeletions: 0,428 isIncomplete: false,429 },430 },431 {432 input: `433diff --git a/dir/file.txt b/dir/file.txt434index b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0..ab80bda5dd90d8b42be25ac2c7a071b722171f09 100644435--- a/dir/file.txt436+++ b/dir/file.txt437@@ -1 +1,3 @@438-hello439\ No newline at end of file440+hello441+442+fdsfdsfds443\ No newline at end of file`,444 expDiff: &Diff{445 Files: []*DiffFile{446 {447 Name: "dir/file.txt",448 Type: DiffFileChange,449 Index: "ab80bda5dd90d8b42be25ac2c7a071b722171f09",450 OldIndex: "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0",451 Sections: []*DiffSection{452 {453 Lines: []*DiffLine{454 {455 Type: DiffLineSection,456 Content: "@@ -1 +1,3 @@",457 }, {458 Type: DiffLineDelete,459 Content: `-hello`,460 LeftLine: 1,461 RightLine: 0,462 }, {463 Type: DiffLineAdd,464 Content: `+hello`,465 LeftLine: 0,466 RightLine: 1,467 }, {468 Type: DiffLineAdd,469 Content: `+`,470 LeftLine: 0,471 RightLine: 2,472 }, {473 Type: DiffLineAdd,474 Content: `+fdsfdsfds`,475 LeftLine: 0,476 RightLine: 3,477 },478 },479 numAdditions: 3,480 numDeletions: 1,481 },482 },483 numAdditions: 3,484 numDeletions: 1,485 oldName: "dir/file.txt",486 isBinary: false,487 isSubmodule: false,488 isIncomplete: false,489 mode: 0100644,490 oldMode: 0100644,491 },492 },493 totalAdditions: 3,494 totalDeletions: 1,495 isIncomplete: false,496 },497 },498 {499 input: `diff --git a/src/app/tabs/teacher/teacher.module.ts b/src/app/tabs/friends/friends.module.ts500similarity index 69%501rename from src/app/tabs/teacher/teacher.module.ts502rename to src/app/tabs/friends/friends.module.ts503index ce53c7e..56a156b 100644504--- a/src/app/tabs/teacher/teacher.module.ts505+++ b/src/app/tabs/friends/friends.module.ts506@@ -2,9 +2,9 @@ import { IonicModule } from '@ionic/angular'507 import { RouterModule } from '@angular/router'508 import { NgModule } from '@angular/core'509 import { CommonModule } from '@angular/common'510-import { FormsModule } from '@angular/forms'511-import { TeacherPage } from './teacher.page'512 import { ComponentsModule } from '@components/components.module'513+import { FormsModule } from '@angular/forms'514+import { FriendsPage } from './friends.page'`,515 expDiff: &Diff{516 Files: []*DiffFile{517 {518 Name: "src/app/tabs/friends/friends.module.ts",519 Type: DiffFileRename,520 Index: "56a156b",521 OldIndex: "ce53c7e",522 Sections: []*DiffSection{523 {524 Lines: []*DiffLine{525 {526 Type: DiffLineSection,527 Content: "@@ -2,9 +2,9 @@ import { IonicModule } from '@ionic/angular'",528 }, {529 Type: DiffLinePlain,530 Content: ` import { RouterModule } from '@angular/router'`,531 LeftLine: 2,532 RightLine: 2,533 }, {534 Type: DiffLinePlain,535 Content: ` import { NgModule } from '@angular/core'`,536 LeftLine: 3,537 RightLine: 3,538 }, {539 Type: DiffLinePlain,540 Content: ` import { CommonModule } from '@angular/common'`,541 LeftLine: 4,542 RightLine: 4,543 }, {544 Type: DiffLineDelete,545 Content: `-import { FormsModule } from '@angular/forms'`,546 LeftLine: 5,547 RightLine: 0,548 }, {549 Type: DiffLineDelete,550 Content: `-import { TeacherPage } from './teacher.page'`,551 LeftLine: 6,552 RightLine: 0,553 }, {554 Type: DiffLinePlain,555 Content: ` import { ComponentsModule } from '@components/components.module'`,556 LeftLine: 7,557 RightLine: 5,558 }, {559 Type: DiffLineAdd,560 Content: `+import { FormsModule } from '@angular/forms'`,561 LeftLine: 0,562 RightLine: 6,563 }, {564 Type: DiffLineAdd,565 Content: `+import { FriendsPage } from './friends.page'`,566 LeftLine: 0,567 RightLine: 7,568 },569 },570 numAdditions: 2,571 numDeletions: 2,572 },573 },574 numAdditions: 2,575 numDeletions: 2,576 oldName: "src/app/tabs/teacher/teacher.module.ts",577 mode: 0100644,578 oldMode: 0100644,579 },580 },581 totalAdditions: 2,582 totalDeletions: 2,583 },584 },585 {586 input: `diff --git a/.travis.yml b/.travis.yml587index 335db7ea..51d7543e 100644588--- a/.travis.yml589+++ b/.travis.yml590@@ -1,9 +1,6 @@591 sudo: false592 language: go593 go:594- - 1.4.x595- - 1.5.x596- - 1.6.x597 - 1.7.x598 - 1.8.x599 - 1.9.x600@@ -12,6 +9,7 @@ go:601 - 1.12.x602 - 1.13.x603604+install: go get -v ./...605 script:606 - go get golang.org/x/tools/cmd/cover607 - go get github.com/smartystreets/goconvey`,608 maxFileLines: 2,609 expDiff: &Diff{610 Files: []*DiffFile{611 {612 Name: ".travis.yml",613 Type: DiffFileChange,614 Index: "51d7543e",615 OldIndex: "335db7ea",616 Sections: []*DiffSection{617 {618 Lines: []*DiffLine{619 {620 Type: DiffLineSection,621 Content: "@@ -1,9 +1,6 @@",622 }, {623 Type: DiffLinePlain,624 Content: ` sudo: false`,625 LeftLine: 1,626 RightLine: 1,627 }, {628 Type: DiffLinePlain,629 Content: ` language: go`,630 LeftLine: 2,631 RightLine: 2,632 }, {633 Type: DiffLinePlain,634 Content: ` go:`,635 LeftLine: 3,636 RightLine: 3,637 }, {638 Type: DiffLineDelete,639 Content: `- - 1.4.x`,640 LeftLine: 4,641 RightLine: 0,642 }, {643 Type: DiffLineDelete,644 Content: `- - 1.5.x`,645 LeftLine: 5,646 RightLine: 0,647 }, {648 Type: DiffLineDelete,649 Content: `- - 1.6.x`,650 LeftLine: 6,651 RightLine: 0,652 }, {653 Type: DiffLinePlain,654 Content: ` - 1.7.x`,655 LeftLine: 7,656 RightLine: 4,657 }, {658 Type: DiffLinePlain,659 Content: ` - 1.8.x`,660 LeftLine: 8,661 RightLine: 5,662 }, {663 Type: DiffLinePlain,664 Content: ` - 1.9.x`,665 LeftLine: 9,666 RightLine: 6,667 },668 },669 numAdditions: 0,670 numDeletions: 3,671 },672 },673 numAdditions: 0,674 numDeletions: 3,675 oldName: ".travis.yml",676 isBinary: false,677 isSubmodule: false,678 isIncomplete: true,679 mode: 0100644,680 oldMode: 0100644,681 },682 },683 totalAdditions: 0,684 totalDeletions: 3,685 isIncomplete: true,686 },687 },688 {689 input: `diff --git a/.gitmodules b/.gitmodules690new file mode 100644691index 0000000..6abde17692--- /dev/null693+++ b/.gitmodules694@@ -0,0 +1,3 @@695+[submodule "gogs/docs-api"]696+ path = gogs/docs-api697+ url = https://github.com/gogs/docs-api.git`,698 maxLineChars: 30,699 expDiff: &Diff{700 Files: []*DiffFile{701 {702 Name: ".gitmodules",703 Type: DiffFileAdd,704 Index: "6abde17",705 OldIndex: "0000000",706 Sections: []*DiffSection{707 {708 Lines: []*DiffLine{709 {710 Type: DiffLineSection,711 Content: "@@ -0,0 +1,3 @@",712 }, {713 Type: DiffLineAdd,714 Content: `+[submodule "gogs/docs-api"]`,715 LeftLine: 0,716 RightLine: 1,717 }, {718 Type: DiffLineAdd,719 Content: `+ path = gogs/docs-api`,720 LeftLine: 0,721 RightLine: 2,722 },723 },724 numAdditions: 2,725 numDeletions: 0,726 },727 },728 numAdditions: 2,729 numDeletions: 0,730 oldName: ".gitmodules",731 isBinary: false,732 isSubmodule: false,733 isIncomplete: true,734 mode: 0100644,735 oldMode: 0100644,736 },737 },738 totalAdditions: 2,739 totalDeletions: 0,740 isIncomplete: true,741 },742 },743 {744 input: `diff --git a/.gitmodules b/.gitmodules745new file mode 100644746index 0000000..6abde17747--- /dev/null748+++ b/.gitmodules749@@ -0,0 +1,3 @@750+[submodule "gogs/docs-api"]751+ path = gogs/docs-api752+ url = https://github.com/gogs/docs-api.git753diff --git a/gogs/docs-api b/gogs/docs-api754new file mode 160000755index 0000000..6b08f76756--- /dev/null757+++ b/gogs/docs-api758@@ -0,0 +1 @@759+Subproject commit 6b08f76a5313fa3d26859515b30aa17a5faa2807`,760 maxFileLines: 2,761 maxLineChars: 30,762 maxFiles: 1,763 expDiff: &Diff{764 Files: []*DiffFile{765 {766 Name: ".gitmodules",767 Type: DiffFileAdd,768 Index: "6abde17",769 OldIndex: "0000000",770 Sections: []*DiffSection{771 {772 Lines: []*DiffLine{773 {774 Type: DiffLineSection,775 Content: "@@ -0,0 +1,3 @@",776 }, {777 Type: DiffLineAdd,778 Content: `+[submodule "gogs/docs-api"]`,779 LeftLine: 0,780 RightLine: 1,781 }, {782 Type: DiffLineAdd,783 Content: `+ path = gogs/docs-api`,784 LeftLine: 0,785 RightLine: 2,786 },787 },788 numAdditions: 2,789 numDeletions: 0,790 },791 },792 numAdditions: 2,793 numDeletions: 0,794 oldName: ".gitmodules",795 isBinary: false,796 isSubmodule: false,797 isIncomplete: true,798 mode: 0100644,799 oldMode: 0100644,800 },801 },802 totalAdditions: 2,803 totalDeletions: 0,804 isIncomplete: true,805 },806 },807 {808 input: `diff --git a/go.mod b/go.mod809old mode 100644810new mode 100755`,811 maxFileLines: 0,812 maxLineChars: 0,813 maxFiles: 1,814 expDiff: &Diff{815 Files: []*DiffFile{816 {817 Name: "go.mod",818 oldName: "go.mod",819 Type: DiffFileChange,820 mode: 0100755,821 oldMode: 0100644,822 },823 },824 totalAdditions: 0,825 totalDeletions: 0,826 isIncomplete: false,827 },828 },829 }830 for _, test := range tests {831 t.Run("", func(t *testing.T) {832 done := make(chan SteamParseDiffResult)833 go StreamParseDiff(strings.NewReader(test.input), done, test.maxFiles, test.maxFileLines, test.maxLineChars)834 result := <-done835 if result.Err != nil {836 t.Fatal(result.Err)837 }838839 assert.Equal(t, test.expDiff, result.Diff)840 })841 }842}