github.com/abhinav/git-fu@v0.6.1-0.20171029234004-54218d68c11b/git/unique_test.go (about) 1 package git 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/abhinav/git-pr/gateway/gatewaytest" 8 9 "github.com/golang/mock/gomock" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestCheckoutUniqueBranch(t *testing.T) { 15 tests := []struct { 16 Desc string 17 Prefix string 18 ExistingBranches []string 19 20 Want string 21 WantErr []string 22 }{ 23 { 24 Desc: "no conflicts", 25 Prefix: "foo/bar", 26 Want: "foo/bar", 27 }, 28 { 29 Desc: "one conflict", 30 Prefix: "foo-bar", 31 ExistingBranches: []string{"foo-bar"}, 32 Want: "foo-bar/2", 33 }, 34 { 35 Desc: "two conflicts", 36 Prefix: "foo", 37 ExistingBranches: []string{"foo", "foo/2"}, 38 Want: "foo/3", 39 }, 40 { 41 Desc: "too many attempts", 42 Prefix: "bar", 43 ExistingBranches: []string{ 44 "bar", "bar/2", "bar/3", "bar/4", "bar/5", "bar/6", "bar/7", 45 "bar/8", "bar/9", "bar/10", 46 }, 47 WantErr: []string{ 48 `could not find a unique branch name with prefix "bar"`, 49 `"someref" may not be a valid git ref`, 50 }, 51 }, 52 } 53 54 for _, tt := range tests { 55 t.Run(tt.Desc, func(t *testing.T) { 56 mockCtrl := gomock.NewController(t) 57 defer mockCtrl.Finish() 58 59 git := gatewaytest.NewMockGit(mockCtrl) 60 for _, br := range tt.ExistingBranches { 61 git.EXPECT().CreateBranchAndCheckout(br, "someref"). 62 Return(fmt.Errorf("branch %q already exists", br)) 63 } 64 65 if tt.Want != "" { 66 git.EXPECT().CreateBranchAndCheckout(tt.Want, "someref"). 67 Return(nil) 68 } 69 70 got, err := CheckoutUniqueBranch(git, tt.Prefix, "someref") 71 if len(tt.WantErr) > 0 { 72 require.Error(t, err, "expected failure") 73 for _, msg := range tt.WantErr { 74 assert.Contains(t, err.Error(), msg) 75 } 76 return 77 } 78 79 require.NoError(t, err, "expected success") 80 assert.Equal(t, tt.Want, got) 81 }) 82 } 83 }