github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/gits/provider_test.go (about) 1 // +build unit 2 3 package gits_test 4 5 import ( 6 "errors" 7 "fmt" 8 "io/ioutil" 9 "os" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/olli-ai/jx/v2/pkg/auth" 15 "github.com/olli-ai/jx/v2/pkg/gits" 16 mocks "github.com/olli-ai/jx/v2/pkg/gits/mocks" 17 utiltests "github.com/olli-ai/jx/v2/pkg/tests" 18 "github.com/olli-ai/jx/v2/pkg/util" 19 "github.com/stretchr/testify/assert" 20 ) 21 22 type FakeOrgLister struct { 23 orgNames []string 24 fail bool 25 } 26 27 func (l FakeOrgLister) ListOrganisations() ([]gits.GitOrganisation, error) { 28 if l.fail { 29 return nil, errors.New("fail") 30 } 31 32 orgs := make([]gits.GitOrganisation, len(l.orgNames)) 33 for _, v := range l.orgNames { 34 orgs = append(orgs, gits.GitOrganisation{Login: v}) 35 } 36 return orgs, nil 37 } 38 39 func Test_getOrganizations(t *testing.T) { 40 t.Parallel() 41 tests := []struct { 42 testDescription string 43 orgLister gits.OrganisationLister 44 userName string 45 want []string 46 }{ 47 {"Should return user name when ListOrganisations() fails", FakeOrgLister{fail: true}, "testuser", []string{"testuser"}}, 48 {"Should return user name when organization list is empty", FakeOrgLister{orgNames: []string{}}, "testuser", []string{"testuser"}}, 49 {"Should include user name when only 1 organization exists", FakeOrgLister{orgNames: []string{"testorg"}}, "testuser", []string{"testorg", "testuser"}}, 50 {"Should include user name together with all organizations when multiple exists", FakeOrgLister{orgNames: []string{"testorg", "anotherorg"}}, "testuser", []string{"anotherorg", "testorg", "testuser"}}, 51 } 52 for _, tt := range tests { 53 t.Run(tt.testDescription, func(t *testing.T) { 54 result := gits.GetOrganizations(tt.orgLister, tt.userName) 55 assert.Equal(t, tt.want, result) 56 }) 57 } 58 } 59 60 func createAuthConfigSvc(authConfig *auth.AuthConfig, fileName string) *auth.ConfigService { 61 authConfigSvc, _ := auth.NewFileAuthConfigService(fileName, "") 62 authConfigSvc.SetConfig(authConfig) 63 return &authConfigSvc 64 } 65 66 func createAuthConfig(currentServer *auth.AuthServer, piplineServer, pipelineUser string, servers ...*auth.AuthServer) *auth.AuthConfig { 67 servers = append(servers, currentServer) 68 return &auth.AuthConfig{ 69 Servers: servers, 70 CurrentServer: currentServer.URL, 71 PipeLineServer: piplineServer, 72 PipeLineUsername: pipelineUser, 73 } 74 } 75 76 func createAuthServer(url string, name string, kind string, currentUser *auth.UserAuth, users ...*auth.UserAuth) *auth.AuthServer { 77 if currentUser != nil { 78 users = append(users, currentUser) 79 } 80 currentUsername := "" 81 if currentUser != nil { 82 currentUsername = currentUser.Username 83 } 84 return &auth.AuthServer{ 85 URL: url, 86 Name: name, 87 Kind: kind, 88 Users: users, 89 CurrentUser: currentUsername, 90 } 91 } 92 93 func createGitProvider(t assert.TestingT, kind string, server *auth.AuthServer, user *auth.UserAuth, git gits.Gitter) gits.GitProvider { 94 switch kind { 95 case gits.KindGitHub: 96 gitHubProvider, err := gits.NewGitHubProvider(server, user, git) 97 assert.NoError(t, err, "should create GitHub provider without error") 98 return gitHubProvider 99 case gits.KindGitlab: 100 gitlabProvider, err := gits.NewGitlabProvider(server, user, git) 101 assert.NoError(t, err, "should create Gitlab provider without error") 102 return gitlabProvider 103 case gits.KindGitea: 104 giteaProvider, err := gits.NewGiteaProvider(server, user, git) 105 assert.NoError(t, err, "should create Gitea provider without error") 106 return giteaProvider 107 case gits.KindBitBucketServer: 108 bitbucketServerProvider, err := gits.NewBitbucketServerProvider(server, user, git) 109 assert.NoError(t, err, "should create Bitbucket server provider without error") 110 return bitbucketServerProvider 111 case gits.KindBitBucketCloud: 112 bitbucketCloudProvider, err := gits.NewBitbucketCloudProvider(server, user, git) 113 assert.NoError(t, err, "should create Bitbucket cloud provider without error") 114 return bitbucketCloudProvider 115 default: 116 return nil 117 } 118 } 119 120 func setUserAuthInEnv(kind string, username string, apiToken string) error { 121 prefix := strings.ToUpper(kind) 122 err := os.Setenv(prefix+"_USERNAME", username) 123 if err != nil { 124 return err 125 } 126 return os.Setenv(prefix+"_API_TOKEN", apiToken) 127 } 128 129 func unsetUserAuthInEnv(kind string) error { 130 prefix := strings.ToUpper(kind) 131 err := os.Unsetenv(prefix + "_USERNAME") 132 if err != nil { 133 return err 134 } 135 return os.Unsetenv(prefix + "_API_TOKEN") 136 } 137 138 func getAndCleanEnviron(kind string) (map[string]string, error) { 139 prefix := strings.ToUpper(kind) 140 keys := []string{ 141 prefix + "_USERNAME", 142 prefix + "_API_TOKEN", 143 "GIT_USERNAME", 144 "GIT_API_TOKEN", 145 } 146 return util.GetAndCleanEnviron(keys) 147 } 148 149 func restoreEnviron(t assert.TestingT, environ map[string]string) { 150 err := util.RestoreEnviron(environ) 151 assert.NoError(t, err, "should restore the env variable") 152 } 153 154 func TestCreateGitProviderFromURL(t *testing.T) { 155 // This test is setting some environment variable which is causing other tests creating the git 156 // provider to fail when executed in parallel. 157 // t.Parallel() 158 utiltests.SkipForWindows(t, "go-expect does not work on Windows") 159 160 git := mocks.NewMockGitter() 161 162 tests := []struct { 163 description string 164 setup func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) 165 cleanup func(c *utiltests.ConsoleWrapper, donech chan struct{}) 166 Name string 167 providerKind string 168 hostURL string 169 ghOwner string 170 git gits.Gitter 171 numUsers int 172 currUser int 173 pipelineUser int 174 username string 175 apiToken string 176 batchMode bool 177 inCluster bool 178 wantError bool 179 }{ 180 {"create GitHub provider for one user", 181 nil, 182 nil, 183 "GitHub", 184 gits.KindGitHub, 185 "https://github.com", "", 186 git, 187 1, 188 0, 189 0, 190 "test", 191 "test", 192 false, 193 false, 194 false, 195 }, 196 {"create GitHub provider for multiple users", 197 nil, 198 nil, 199 "GitHub", 200 gits.KindGitHub, 201 "https://github.com", "", 202 git, 203 2, 204 1, 205 1, 206 "test", 207 "test", 208 false, 209 false, 210 false, 211 }, 212 {"create GitHub provider for pipline user when in cluster ", 213 nil, 214 nil, 215 "GitHub", 216 gits.KindGitHub, 217 "https://github.com", "", 218 git, 219 2, 220 1, 221 0, 222 "test", 223 "test", 224 false, 225 true, 226 false, 227 }, 228 {"create GitHub provider for user from environment", 229 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 230 err := setUserAuthInEnv(gits.KindGitHub, "test", "test") 231 assert.NoError(t, err, "should configure the user auth in environment") 232 console := utiltests.NewTerminal(t, nil) 233 donech := make(chan struct{}) 234 go func() { 235 defer close(donech) 236 }() 237 return console, donech 238 }, 239 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 240 err := unsetUserAuthInEnv(gits.KindGitHub) 241 assert.NoError(t, err, "should reset the user auth in environment") 242 c.Close() 243 <-donech 244 c.Cleanup() 245 246 }, 247 "GitHub", 248 gits.KindGitHub, 249 "https://github.com", "", 250 git, 251 0, 252 0, 253 0, 254 "test", 255 "test", 256 false, 257 false, 258 false, 259 }, 260 {"create GitHub provider in batch mode ", 261 nil, 262 nil, 263 "GitHub", 264 gits.KindGitHub, 265 "https://github.com", "", 266 git, 267 0, 268 0, 269 0, 270 "", 271 "", 272 true, 273 false, 274 true, 275 }, 276 {"create GitHub provider in interactive mode", 277 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 278 c := utiltests.NewTerminal(t, nil) 279 assert.NotNil(t, c, "console should not be nil") 280 assert.NotNil(t, c.Stdio, "term should not be nil") 281 donech := make(chan struct{}) 282 go func() { 283 defer close(donech) 284 c.ExpectString("github.com username:") 285 c.SendLine("test") 286 c.ExpectString("API Token:") 287 c.SendLine("test") 288 c.ExpectEOF() 289 }() 290 return c, donech 291 }, 292 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 293 c.Close() 294 <-donech 295 c.Cleanup() 296 }, 297 "GitHub", 298 gits.KindGitHub, 299 "https://github.com", "", 300 git, 301 0, 302 0, 303 0, 304 "test", 305 "test", 306 false, 307 false, 308 false, 309 }, 310 {"create Gitlab provider for one user", 311 nil, 312 nil, 313 "Gitlab", 314 gits.KindGitlab, 315 "https://github.com", "", 316 git, 317 1, 318 0, 319 0, 320 "test", 321 "test", 322 false, 323 false, 324 false, 325 }, 326 {"create Gitlab provider for multiple users", 327 nil, 328 nil, 329 "Gitlab", 330 gits.KindGitHub, 331 "https://gitlab.com", "", 332 git, 333 2, 334 1, 335 1, 336 "test", 337 "test", 338 false, 339 false, 340 false, 341 }, 342 {"create Gitlab provider for user from environment", 343 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 344 err := setUserAuthInEnv(gits.KindGitlab, "test", "test") 345 assert.NoError(t, err, "should configure the user auth in environment") 346 c := utiltests.NewTerminal(t, nil) 347 donech := make(chan struct{}) 348 go func() { 349 defer close(donech) 350 }() 351 return c, donech 352 }, 353 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 354 err := unsetUserAuthInEnv(gits.KindGitlab) 355 assert.NoError(t, err, "should reset the user auth in environment") 356 c.Close() 357 <-donech 358 c.Cleanup() 359 }, 360 "Gitlab", 361 gits.KindGitlab, 362 "https://gitlab.com", "", 363 git, 364 0, 365 0, 366 0, 367 "test", 368 "test", 369 false, 370 false, 371 false, 372 }, 373 {"create Gitlab provider in batch mode ", 374 nil, 375 nil, 376 "Gitlab", 377 gits.KindGitlab, 378 "https://gitlab.com", "", 379 git, 380 0, 381 0, 382 0, 383 "", 384 "", 385 true, 386 false, 387 true, 388 }, 389 {"create Gitlab provider in interactive mode", 390 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 391 c := utiltests.NewTerminal(t, nil) 392 assert.NotNil(t, c, "console should not be nil") 393 assert.NotNil(t, c.Stdio, "term should not be nil") 394 donech := make(chan struct{}) 395 go func() { 396 defer close(donech) 397 c.ExpectString("gitlab.com username:") 398 c.SendLine("test") 399 c.ExpectString("API Token:") 400 c.SendLine("test") 401 c.ExpectEOF() 402 }() 403 return c, donech 404 }, 405 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 406 c.Close() 407 <-donech 408 c.Cleanup() 409 }, 410 "Gitlab", 411 gits.KindGitlab, 412 "https://gitlab.com", "", 413 git, 414 0, 415 0, 416 0, 417 "test", 418 "test", 419 false, 420 false, 421 false, 422 }, 423 {"create Gitea provider for one user", 424 nil, 425 nil, 426 "Gitea", 427 gits.KindGitea, 428 "https://gitea.com", "", 429 git, 430 1, 431 0, 432 0, 433 "test", 434 "test", 435 false, 436 false, 437 false, 438 }, 439 {"create Gitea provider for multiple users", 440 nil, 441 nil, 442 "Gitea", 443 gits.KindGitea, 444 "https://gitea.com", "", 445 git, 446 2, 447 1, 448 1, 449 "test", 450 "test", 451 false, 452 false, 453 false, 454 }, 455 {"create Gitea provider for user from environment", 456 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 457 err := setUserAuthInEnv(gits.KindGitea, "test", "test") 458 assert.NoError(t, err, "should configure the user auth in environment") 459 c := utiltests.NewTerminal(t, nil) 460 donech := make(chan struct{}) 461 go func() { 462 defer close(donech) 463 }() 464 return c, donech 465 }, 466 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 467 err := unsetUserAuthInEnv(gits.KindGitea) 468 assert.NoError(t, err, "should reset the user auth in environment") 469 c.Close() 470 <-donech 471 c.Cleanup() 472 }, 473 "Gitea", 474 gits.KindGitea, 475 "https://gitea.com", "", 476 git, 477 0, 478 0, 479 0, 480 "test", 481 "test", 482 false, 483 false, 484 false, 485 }, 486 {"create Gitea provider in batch mode ", 487 nil, 488 nil, 489 "Gitea", 490 gits.KindGitea, 491 "https://gitea.com", "", 492 git, 493 0, 494 0, 495 0, 496 "", 497 "", 498 true, 499 false, 500 true, 501 }, 502 {"create Gitea provider in interactive mode", 503 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 504 c := utiltests.NewTerminal(t, nil) 505 assert.NotNil(t, c, "console should not be nil") 506 assert.NotNil(t, c.Stdio, "term should not be nil") 507 donech := make(chan struct{}) 508 go func() { 509 defer close(donech) 510 c.ExpectString("gitea.com username:") 511 c.SendLine("test") 512 c.ExpectString("API Token:") 513 c.SendLine("test") 514 c.ExpectEOF() 515 }() 516 return c, donech 517 }, 518 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 519 c.Close() 520 <-donech 521 c.Cleanup() 522 }, 523 "Gitea", 524 gits.KindGitea, 525 "https://gitea.com", "", 526 git, 527 0, 528 0, 529 0, 530 "test", 531 "test", 532 false, 533 false, 534 false, 535 }, 536 {"create BitbucketServer provider for one user", 537 nil, 538 nil, 539 "BitbucketServer", 540 gits.KindBitBucketServer, 541 "https://bitbucket-server.com", "", 542 git, 543 1, 544 0, 545 0, 546 "test", 547 "test", 548 false, 549 false, 550 false, 551 }, 552 {"create BitbucketServer provider for multiple users", 553 nil, 554 nil, 555 "BitbucketServer", 556 gits.KindBitBucketServer, 557 "https://bitbucket-server.com", "", 558 git, 559 2, 560 1, 561 1, 562 "test", 563 "test", 564 false, 565 false, 566 false, 567 }, 568 {"create BitbucketServer provider for user from environment", 569 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 570 err := setUserAuthInEnv(gits.KindBitBucketServer, "test", "test") 571 assert.NoError(t, err, "should configure the user auth in environment") 572 c := utiltests.NewTerminal(t, nil) 573 donech := make(chan struct{}) 574 go func() { 575 defer close(donech) 576 }() 577 return c, donech 578 }, 579 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 580 err := unsetUserAuthInEnv(gits.KindBitBucketServer) 581 assert.NoError(t, err, "should reset the user auth in environment") 582 c.Close() 583 <-donech 584 c.Cleanup() 585 }, 586 "BitbucketServer", 587 gits.KindBitBucketServer, 588 "https://bitbucket-server.com", "", 589 git, 590 0, 591 0, 592 0, 593 "test", 594 "test", 595 false, 596 false, 597 false, 598 }, 599 {"create BitbucketServer provider in batch mode ", 600 nil, 601 nil, 602 "BitbucketServer", 603 gits.KindBitBucketServer, 604 "https://bitbucket-server.com", "", 605 git, 606 0, 607 0, 608 0, 609 "", 610 "", 611 true, 612 false, 613 true, 614 }, 615 {"create BitbucketServer provider in interactive mode", 616 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 617 c := utiltests.NewTerminal(t, nil) 618 assert.NotNil(t, c, "console should not be nil") 619 assert.NotNil(t, c.Stdio, "term should not be nil") 620 donech := make(chan struct{}) 621 go func() { 622 defer close(donech) 623 c.ExpectString("bitbucket-server.com username:") 624 c.SendLine("test") 625 c.ExpectString("API Token:") 626 c.SendLine("test") 627 c.ExpectEOF() 628 }() 629 return c, donech 630 }, 631 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 632 c.Close() 633 c.Cleanup() 634 <-donech 635 }, 636 "BitbucketServer", 637 gits.KindBitBucketServer, 638 "https://bitbucket-server.com", "", 639 git, 640 0, 641 0, 642 0, 643 "test", 644 "test", 645 false, 646 false, 647 false, 648 }, 649 {"create BitbucketCloud provider for one user", 650 nil, 651 nil, 652 "BitbucketCloud", 653 gits.KindBitBucketCloud, 654 "https://bitbucket.org", "", 655 git, 656 1, 657 0, 658 0, 659 "test", 660 "test", 661 false, 662 false, 663 false, 664 }, 665 {"create BitbucketCloud provider for multiple users", 666 nil, 667 nil, 668 "BitbucketCloud", 669 gits.KindBitBucketCloud, 670 "https://bitbucket.org", "", 671 git, 672 2, 673 1, 674 1, 675 "test", 676 "test", 677 false, 678 false, 679 false, 680 }, 681 {"create BitbucketCloud provider for user from environment", 682 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 683 err := setUserAuthInEnv(gits.KindBitBucketCloud, "test", "test") 684 assert.NoError(t, err, "should configure the user auth in environment") 685 c := utiltests.NewTerminal(t, nil) 686 donech := make(chan struct{}) 687 go func() { 688 defer close(donech) 689 }() 690 return c, donech 691 }, 692 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 693 err := unsetUserAuthInEnv(gits.KindBitBucketCloud) 694 assert.NoError(t, err, "should reset the user auth in environment") 695 c.Close() 696 <-donech 697 c.Cleanup() 698 }, 699 "BitbucketCloud", 700 gits.KindBitBucketCloud, 701 "https://bitbucket.org", "", 702 git, 703 0, 704 0, 705 0, 706 "test", 707 "test", 708 false, 709 false, 710 false, 711 }, 712 {"create BitbucketCloud provider in batch mode ", 713 nil, 714 nil, 715 "BitbucketCloud", 716 gits.KindBitBucketCloud, 717 "https://bitbucket.org", "", 718 git, 719 0, 720 0, 721 0, 722 "", 723 "", 724 true, 725 false, 726 true, 727 }, 728 {"create BitbucketCloud provider in interactive mode", 729 func(t assert.TestingT) (*utiltests.ConsoleWrapper, chan struct{}) { 730 c := utiltests.NewTerminal(t, nil) 731 assert.NotNil(t, c, "console should not be nil") 732 assert.NotNil(t, c.Stdio, "term should not be nil") 733 donech := make(chan struct{}) 734 go func() { 735 defer close(donech) 736 c.ExpectString("bitbucket.org username:") 737 c.SendLine("test") 738 c.ExpectString("API Token:") 739 c.SendLine("test") 740 c.ExpectEOF() 741 }() 742 return c, donech 743 }, 744 func(c *utiltests.ConsoleWrapper, donech chan struct{}) { 745 c.Close() 746 <-donech 747 c.Cleanup() 748 }, 749 "BitbucketCloud", 750 gits.KindBitBucketCloud, 751 "https://bitbucket.org", "", 752 git, 753 0, 754 0, 755 0, 756 "test", 757 "test", 758 false, 759 false, 760 false, 761 }, 762 } 763 764 for _, tc := range tests { 765 t.Run(tc.description, func(t *testing.T) { 766 utiltests.Retry(t, 5, 10*time.Second, func(r *utiltests.R) { 767 768 environ, err := getAndCleanEnviron(tc.providerKind) 769 assert.NoError(r, err, "should clean the env variables") 770 defer restoreEnviron(r, environ) 771 772 var console *utiltests.ConsoleWrapper 773 var donech chan struct{} 774 if tc.setup != nil { 775 console, donech = tc.setup(r) 776 } 777 778 var users []*auth.UserAuth 779 var currUser *auth.UserAuth 780 var pipelineUser *auth.UserAuth 781 var server *auth.AuthServer 782 var authSvc *auth.ConfigService 783 configFile, err := ioutil.TempFile("", "test-config") 784 defer os.Remove(configFile.Name()) 785 if tc.numUsers > 0 { 786 for u := 1; u <= tc.numUsers; u++ { 787 user := &auth.UserAuth{ 788 Username: fmt.Sprintf("%s-%d", tc.username, u), 789 ApiToken: fmt.Sprintf("%s-%d", tc.apiToken, u), 790 } 791 users = append(users, user) 792 } 793 assert.True(r, len(users) > tc.currUser, "current user index should be smaller than number of users") 794 currUser = users[tc.currUser] 795 pipelineUser = users[tc.pipelineUser] 796 if len(users) > 1 { 797 users = append(users[:tc.currUser], users[tc.currUser+1:]...) 798 } else { 799 users = []*auth.UserAuth{} 800 } 801 server = createAuthServer(tc.hostURL, tc.Name, tc.providerKind, currUser, users...) 802 authSvc = createAuthConfigSvc(createAuthConfig(server, server.URL, pipelineUser.Username), configFile.Name()) 803 } else { 804 currUser = &auth.UserAuth{ 805 Username: tc.username, 806 ApiToken: tc.apiToken, 807 } 808 server = createAuthServer(tc.hostURL, tc.Name, tc.providerKind, currUser, users...) 809 s, err := auth.NewFileAuthConfigService(configFile.Name(), "") 810 authSvc = &s 811 assert.NoError(r, err) 812 } 813 814 var result gits.GitProvider 815 handles := util.IOFileHandles{} 816 if console != nil { 817 handles.In = console.In 818 handles.Out = console.Out 819 handles.Err = console.Err 820 } 821 result, err = gits.CreateProviderForURL(tc.inCluster, *authSvc, tc.providerKind, tc.hostURL, tc.ghOwner, tc.git, tc.batchMode, handles) 822 if tc.wantError { 823 assert.Error(r, err, "should fail to create provider") 824 assert.Nil(r, result, "created provider should be nil") 825 } else { 826 assert.NoError(r, err, "should create provider without error") 827 assert.NotNil(r, result, "created provider should not be nil") 828 if tc.inCluster { 829 want := createGitProvider(r, tc.providerKind, server, pipelineUser, tc.git) 830 assert.NotNil(r, want, "expected provider should not be nil") 831 assertProvider(r, want, result) 832 } else { 833 want := createGitProvider(r, tc.providerKind, server, currUser, tc.git) 834 assert.NotNil(r, want, "expected provider should not be nil") 835 assertProvider(r, want, result) 836 } 837 } 838 839 if tc.cleanup != nil { 840 tc.cleanup(console, donech) 841 } 842 }) 843 }) 844 } 845 } 846 847 func Test_ShortSha(t *testing.T) { 848 t.Parallel() 849 850 testCases := []struct { 851 SHA string 852 expectedShortSHA string 853 }{ 854 {"", ""}, 855 {"f73f71c", "f73f71c"}, 856 {"f73f71cd6", "f73f71cd6"}, 857 {"f73f71cd6856a3854f05c37f1833b3e3e96a5b8a", "f73f71cd6"}, 858 } 859 860 for _, testCase := range testCases { 861 t.Run(fmt.Sprintf("%s-%s", t.Name(), testCase.SHA), func(t *testing.T) { 862 commit := gits.GitCommit{ 863 SHA: testCase.SHA, 864 } 865 assert.Equal(t, testCase.expectedShortSHA, commit.ShortSha()) 866 }) 867 } 868 } 869 870 func Test_Subject(t *testing.T) { 871 t.Parallel() 872 873 testCases := []struct { 874 SHA string 875 Message string 876 expectedSubject string 877 }{ 878 {"1", "", ""}, 879 {"2", "subject only", "subject only"}, 880 {"3", "subject\nbody", "subject"}, 881 {"4", "subject\nbody line 1\nbody line 2", "subject"}, 882 } 883 884 for _, testCase := range testCases { 885 t.Run(fmt.Sprintf("%s-%s", t.Name(), testCase.SHA), func(t *testing.T) { 886 commit := gits.GitCommit{ 887 SHA: testCase.SHA, 888 Message: testCase.Message, 889 } 890 assert.Equal(t, testCase.expectedSubject, commit.Subject()) 891 }) 892 } 893 } 894 895 func assertProvider(t assert.TestingT, want gits.GitProvider, result gits.GitProvider) { 896 assert.Equal(t, want.Kind(), result.Kind()) 897 assert.Equal(t, want.ServerURL(), result.ServerURL()) 898 assert.Equal(t, want.UserAuth(), result.UserAuth()) 899 }