1package git23import (4 "context"5 "testing"67 "github.com/stretchr/testify/assert"8)910func TestCommit(t *testing.T) {11 ctx := context.Background()12 c, err := testrepo.CatFileCommit(ctx, testrepoMarks[22].String())13 if err != nil {14 t.Fatal(err)15 }16 t.Run("ID", func(t *testing.T) {17 assert.Equal(t, testrepoMarks[22].String(), c.ID.String())18 })1920 t.Run("Summary", func(t *testing.T) {21 assert.Equal(t, "Merge pull request #35 from githubtraining/travis-yml-docker", c.Summary())22 })23}2425func TestCommit_Parent(t *testing.T) {26 ctx := context.Background()27 c, err := testrepo.CatFileCommit(ctx, testrepoMarks[22].String())28 if err != nil {29 t.Fatal(err)30 }3132 t.Run("ParentsCount", func(t *testing.T) {33 assert.Equal(t, 2, c.ParentsCount())34 })3536 t.Run("Parent", func(t *testing.T) {37 t.Run("no such parent", func(t *testing.T) {38 _, err := c.Parent(ctx, c.ParentsCount()+1)39 assert.Equal(t, ErrParentNotExist, err)40 })4142 tests := []struct {43 n int44 expParentID string45 }{46 {47 n: 0,48 expParentID: testrepoMarks[20].String(),49 },50 {51 n: 1,52 expParentID: testrepoMarks[21].String(),53 },54 }55 for _, test := range tests {56 t.Run("", func(t *testing.T) {57 p, err := c.Parent(ctx, test.n)58 if err != nil {59 t.Fatal(err)60 }61 assert.Equal(t, test.expParentID, p.ID.String())62 })63 }64 })65}6667func TestCommit_CommitByPath(t *testing.T) {68 ctx := context.Background()69 tests := []struct {70 id string71 opt CommitByRevisionOptions72 expCommitID string73 }{74 {75 id: testrepoMarks[12].String(),76 opt: CommitByRevisionOptions{77 Path: "", // No path gets back to the commit itself78 },79 expCommitID: testrepoMarks[12].String(),80 },81 {82 id: testrepoMarks[12].String(),83 opt: CommitByRevisionOptions{84 Path: "resources/labels.properties",85 },86 expCommitID: testrepoMarks[1].String(),87 },88 }89 for _, test := range tests {90 t.Run("", func(t *testing.T) {91 c, err := testrepo.CatFileCommit(ctx, test.id)92 if err != nil {93 t.Fatal(err)94 }9596 cc, err := c.CommitByPath(ctx, test.opt)97 if err != nil {98 t.Fatal(err)99 }100101 assert.Equal(t, test.expCommitID, cc.ID.String())102 })103 }104}105106// commitsToIDs returns a list of IDs for given commits.107func commitsToIDs(commits []*Commit) []string {108 ids := make([]string, len(commits))109 for i := range commits {110 ids[i] = commits[i].ID.String()111 }112 return ids113}114115func TestCommit_CommitsByPage(t *testing.T) {116 ctx := context.Background()117 // There are at most 5 commits can be used for pagination before this commit.118 c, err := testrepo.CatFileCommit(ctx, testrepoMarks[5].String())119 if err != nil {120 t.Fatal(err)121 }122123 tests := []struct {124 page int125 size int126 opt CommitsByPageOptions127 expCommitIDs []string128 }{129 {130 page: 0,131 size: 2,132 expCommitIDs: []string{133 testrepoMarks[5].String(),134 testrepoMarks[4].String(),135 },136 },137 {138 page: 1,139 size: 2,140 expCommitIDs: []string{141 testrepoMarks[5].String(),142 testrepoMarks[4].String(),143 },144 },145 {146 page: 2,147 size: 2,148 expCommitIDs: []string{149 testrepoMarks[3].String(),150 testrepoMarks[2].String(),151 },152 },153 {154 page: 3,155 size: 2,156 expCommitIDs: []string{157 testrepoMarks[1].String(),158 },159 },160 {161 page: 4,162 size: 2,163 expCommitIDs: []string{},164 },165166 {167 page: 2,168 size: 2,169 opt: CommitsByPageOptions{170 Path: "src",171 },172 expCommitIDs: []string{173 testrepoMarks[1].String(),174 },175 },176 }177 for _, test := range tests {178 t.Run("", func(t *testing.T) {179 commits, err := c.CommitsByPage(ctx, test.page, test.size, test.opt)180 if err != nil {181 t.Fatal(err)182 }183184 assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))185 })186 }187}188189func TestCommit_SearchCommits(t *testing.T) {190 ctx := context.Background()191 tests := []struct {192 id string193 pattern string194 opt SearchCommitsOptions195 expCommitIDs []string196 }{197 {198 id: testrepoMarks[12].String(),199 pattern: "",200 expCommitIDs: []string{201 testrepoMarks[12].String(),202 testrepoMarks[11].String(),203 testrepoMarks[10].String(),204 testrepoMarks[9].String(),205 testrepoMarks[8].String(),206 testrepoMarks[7].String(),207 testrepoMarks[6].String(),208 testrepoMarks[5].String(),209 testrepoMarks[4].String(),210 testrepoMarks[3].String(),211 testrepoMarks[2].String(),212 testrepoMarks[1].String(),213 },214 },215 {216 id: testrepoMarks[12].String(),217 pattern: "",218 opt: SearchCommitsOptions{219 MaxCount: 3,220 },221 expCommitIDs: []string{222 testrepoMarks[12].String(),223 testrepoMarks[11].String(),224 testrepoMarks[10].String(),225 },226 },227228 {229 id: testrepoMarks[12].String(),230 pattern: "feature",231 expCommitIDs: []string{232 testrepoMarks[12].String(),233 testrepoMarks[10].String(),234 },235 },236 {237 id: testrepoMarks[12].String(),238 pattern: "feature",239 opt: SearchCommitsOptions{240 MaxCount: 1,241 },242 expCommitIDs: []string{243 testrepoMarks[12].String(),244 },245 },246247 {248 id: testrepoMarks[12].String(),249 pattern: "add.*",250 opt: SearchCommitsOptions{251 Path: "src",252 },253 expCommitIDs: []string{254 testrepoMarks[10].String(),255 testrepoMarks[9].String(),256 testrepoMarks[6].String(),257 testrepoMarks[2].String(),258 testrepoMarks[1].String(),259 },260 },261 {262 id: testrepoMarks[12].String(),263 pattern: "add.*",264 opt: SearchCommitsOptions{265 MaxCount: 2,266 Path: "src",267 },268 expCommitIDs: []string{269 testrepoMarks[10].String(),270 testrepoMarks[9].String(),271 },272 },273 }274 for _, test := range tests {275 t.Run("", func(t *testing.T) {276 c, err := testrepo.CatFileCommit(ctx, test.id)277 if err != nil {278 t.Fatal(err)279 }280281 commits, err := c.SearchCommits(ctx, test.pattern, test.opt)282 if err != nil {283 t.Fatal(err)284 }285286 assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))287 })288 }289}290291func TestCommit_ShowNameStatus(t *testing.T) {292 ctx := context.Background()293 tests := []struct {294 id string295 opt ShowNameStatusOptions296 expStatus *NameStatus297 }{298 {299 id: testrepoMarks[1].String(),300 expStatus: &NameStatus{301 Added: []string{302 "README.txt",303 "resources/labels.properties",304 "src/Main.groovy",305 },306 },307 },308 {309 id: testrepoMarks[2].String(),310 expStatus: &NameStatus{311 Modified: []string{312 "src/Main.groovy",313 },314 },315 },316 {317 id: testrepoMarks[3].String(),318 expStatus: &NameStatus{319 Added: []string{320 "src/Square.groovy",321 },322 Modified: []string{323 "src/Main.groovy",324 },325 },326 },327 {328 id: testrepoMarks[27].String(),329 expStatus: &NameStatus{330 Removed: []string{331 "fix.txt",332 },333 },334 },335 }336 for _, test := range tests {337 t.Run("", func(t *testing.T) {338 c, err := testrepo.CatFileCommit(ctx, test.id)339 if err != nil {340 t.Fatal(err)341 }342343 status, err := c.ShowNameStatus(ctx, test.opt)344 if err != nil {345 t.Fatal(err)346 }347348 assert.Equal(t, test.expStatus, status)349 })350 }351}352353func TestCommit_CommitsCount(t *testing.T) {354 ctx := context.Background()355 tests := []struct {356 id string357 opt RevListCountOptions358 expCount int64359 }{360 {361 id: testrepoMarks[1].String(),362 expCount: 1,363 },364 {365 id: testrepoMarks[5].String(),366 expCount: 5,367 },368 {369 id: testrepoMarks[27].String(),370 expCount: 27,371 },372373 {374 id: testrepoMarks[21].String(),375 opt: RevListCountOptions{376 Path: "README.txt",377 },378 expCount: 3,379 },380 {381 id: testrepoMarks[21].String(),382 opt: RevListCountOptions{383 Path: "resources",384 },385 expCount: 1,386 },387 }388 for _, test := range tests {389 t.Run("", func(t *testing.T) {390 c, err := testrepo.CatFileCommit(ctx, test.id)391 if err != nil {392 t.Fatal(err)393 }394395 count, err := c.CommitsCount(ctx, test.opt)396 if err != nil {397 t.Fatal(err)398 }399400 assert.Equal(t, test.expCount, count)401 })402 }403}404405func TestCommit_FilesChangedAfter(t *testing.T) {406 ctx := context.Background()407 tests := []struct {408 id string409 after string410 opt DiffNameOnlyOptions411 expFiles []string412 }{413 {414 id: testrepoMarks[27].String(),415 after: testrepoMarks[26].String(),416 expFiles: []string{"fix.txt"},417 },418 {419 id: testrepoMarks[27].String(),420 after: testrepoMarks[24].String(),421 expFiles: []string{"fix.txt", "pom.xml", "src/test/java/com/github/AppTest.java"},422 },423424 {425 id: testrepoMarks[27].String(),426 after: testrepoMarks[24].String(),427 opt: DiffNameOnlyOptions{428 Path: "src",429 },430 expFiles: []string{"src/test/java/com/github/AppTest.java"},431 },432 {433 id: testrepoMarks[27].String(),434 after: testrepoMarks[24].String(),435 opt: DiffNameOnlyOptions{436 Path: "resources",437 },438 expFiles: []string{},439 },440 }441 for _, test := range tests {442 t.Run("", func(t *testing.T) {443 c, err := testrepo.CatFileCommit(ctx, test.id)444 if err != nil {445 t.Fatal(err)446 }447448 files, err := c.FilesChangedAfter(ctx, test.after, test.opt)449 if err != nil {450 t.Fatal(err)451 }452453 assert.Equal(t, test.expFiles, files)454 })455 }456}457458func TestCommit_CommitsAfter(t *testing.T) {459 ctx := context.Background()460 tests := []struct {461 id string462 after string463 opt RevListOptions464 expCommitIDs []string465 }{466 {467 id: testrepoMarks[27].String(),468 after: testrepoMarks[24].String(),469 expCommitIDs: []string{470 testrepoMarks[27].String(),471 testrepoMarks[26].String(),472 testrepoMarks[25].String(),473 },474 },475 {476 id: testrepoMarks[27].String(),477 after: testrepoMarks[24].String(),478 opt: RevListOptions{479 Path: "src",480 },481 expCommitIDs: []string{482 testrepoMarks[25].String(),483 },484 },485 }486 for _, test := range tests {487 t.Run("", func(t *testing.T) {488 c, err := testrepo.CatFileCommit(ctx, test.id)489 if err != nil {490 t.Fatal(err)491 }492493 commits, err := c.CommitsAfter(ctx, test.after, test.opt)494 if err != nil {495 t.Fatal(err)496 }497498 assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))499 })500 }501}502503func TestCommit_Ancestors(t *testing.T) {504 ctx := context.Background()505 tests := []struct {506 id string507 opt LogOptions508 expCommitIDs []string509 }{510 {511 id: testrepoMarks[12].String(),512 opt: LogOptions{513 MaxCount: 3,514 },515 expCommitIDs: []string{516 testrepoMarks[11].String(),517 testrepoMarks[10].String(),518 testrepoMarks[9].String(),519 },520 },521 {522 id: testrepoMarks[1].String(),523 expCommitIDs: []string{},524 },525 }526 for _, test := range tests {527 t.Run("", func(t *testing.T) {528 c, err := testrepo.CatFileCommit(ctx, test.id)529 if err != nil {530 t.Fatal(err)531 }532533 commits, err := c.Ancestors(ctx, test.opt)534 if err != nil {535 t.Fatal(err)536 }537538 assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))539 })540 }541}542543func TestCommit_IsImageFile(t *testing.T) {544 ctx := context.Background()545546 t.Run("not a blob", func(t *testing.T) {547 c, err := testrepo.CatFileCommit(ctx, testrepoMarks[29].String())548 if err != nil {549 t.Fatal(err)550 }551552 isImage, err := c.IsImageFile(ctx, "gogs/docs-api")553 if err != nil {554 t.Fatal(err)555 }556 assert.False(t, isImage)557 })558559 tests := []struct {560 id string561 name string562 expVal bool563 }{564 {565 id: testrepoMarks[28].String(),566 name: "README.txt",567 expVal: false,568 },569 {570 id: testrepoMarks[28].String(),571 name: "img/sourcegraph.png",572 expVal: true,573 },574 }575 for _, test := range tests {576 t.Run("", func(t *testing.T) {577 c, err := testrepo.CatFileCommit(ctx, test.id)578 if err != nil {579 t.Fatal(err)580 }581582 isImage, err := c.IsImageFile(ctx, test.name)583 if err != nil {584 t.Fatal(err)585 }586587 assert.Equal(t, test.expVal, isImage)588 })589 }590}591592func TestCommit_IsImageFileByIndex(t *testing.T) {593 ctx := context.Background()594595 t.Run("not a blob", func(t *testing.T) {596 c, err := testrepo.CatFileCommit(ctx, testrepoMarks[29].String())597 if err != nil {598 t.Fatal(err)599 }600601 isImage, err := c.IsImageFileByIndex(ctx, testrepoMarks[40].String()) // "gogs"602 if err != nil {603 t.Fatal(err)604 }605 assert.False(t, isImage)606 })607608 tests := []struct {609 id string610 index string611 expVal bool612 }{613 {614 id: testrepoMarks[28].String(),615 index: testrepoMarks[39].String(), // "README.txt"616 expVal: false,617 },618 {619 id: testrepoMarks[28].String(),620 index: testrepoMarks[49].String(), // "img/sourcegraph.png"621 expVal: true,622 },623 }624 for _, test := range tests {625 t.Run("", func(t *testing.T) {626 c, err := testrepo.CatFileCommit(ctx, test.id)627 if err != nil {628 t.Fatal(err)629 }630631 isImage, err := c.IsImageFileByIndex(ctx, test.index)632 if err != nil {633 t.Fatal(err)634 }635636 assert.Equal(t, test.expVal, isImage)637 })638 }639}