github.com/abhinav/git-pr@v0.6.1-0.20171029234004-54218d68c11b/cmd/git-pr/land_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 6 "github.com/abhinav/git-pr/cli/clitest" 7 "github.com/abhinav/git-pr/editor" 8 "github.com/abhinav/git-pr/editor/editortest" 9 "github.com/abhinav/git-pr/gateway/gatewaytest" 10 "github.com/abhinav/git-pr/ptr" 11 "github.com/abhinav/git-pr/repo" 12 "github.com/abhinav/git-pr/service" 13 "github.com/abhinav/git-pr/service/servicetest" 14 15 "github.com/golang/mock/gomock" 16 "github.com/google/go-github/github" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func TestLandCmd(t *testing.T) { 21 type prMap map[string][]*github.PullRequest 22 23 tests := []struct { 24 Desc string 25 26 Head string 27 CurrentBranch string 28 29 // Map of branch name to pull requests with that head. 30 PullRequestsByHead prMap 31 32 ExpectLandRequest *service.LandRequest 33 ReturnLandResponse *service.LandResponse 34 35 // If non-empty, an error with a message matching this will be 36 // expected 37 WantError string 38 }{ 39 { 40 Desc: "no PRs", 41 CurrentBranch: "feature1", 42 PullRequestsByHead: prMap{"feature1": nil}, 43 WantError: `Could not find PRs with head "feature1"`, 44 }, 45 { 46 Desc: "too many PRs", 47 CurrentBranch: "feature2", 48 PullRequestsByHead: prMap{ 49 "feature2": { 50 {HTMLURL: ptr.String("foo")}, 51 {HTMLURL: ptr.String("bar")}, 52 {HTMLURL: ptr.String("baz")}, 53 }, 54 }, 55 WantError: `Too many PRs found with head "feature2"`, 56 }, 57 { 58 Desc: "no arguments", 59 CurrentBranch: "feature3", 60 PullRequestsByHead: prMap{ 61 "feature3": {{HTMLURL: ptr.String("feature3")}}, 62 }, 63 ExpectLandRequest: &service.LandRequest{ 64 LocalBranch: "feature3", 65 PullRequest: &github.PullRequest{ 66 HTMLURL: ptr.String("feature3"), 67 }, 68 }, 69 ReturnLandResponse: &service.LandResponse{}, 70 }, 71 { 72 Desc: "explicit branch", 73 Head: "feature4", 74 CurrentBranch: "master", 75 PullRequestsByHead: prMap{ 76 "feature4": {{HTMLURL: ptr.String("feature4")}}, 77 }, 78 ExpectLandRequest: &service.LandRequest{ 79 PullRequest: &github.PullRequest{ 80 HTMLURL: ptr.String("feature4"), 81 }, 82 }, 83 ReturnLandResponse: &service.LandResponse{}, 84 }, 85 } 86 87 for _, tt := range tests { 88 t.Run(tt.Desc, func(t *testing.T) { 89 mockCtrl := gomock.NewController(t) 90 defer mockCtrl.Finish() 91 92 git := gatewaytest.NewMockGit(mockCtrl) 93 github := gatewaytest.NewMockGitHub(mockCtrl) 94 svc := servicetest.NewMockPR(mockCtrl) 95 ed := editortest.NewMockEditor(mockCtrl) 96 97 cb := &fakeConfigBuilder{ 98 ConfigBuilder: clitest.ConfigBuilder{ 99 Git: git, 100 GitHub: github, 101 Repo: &repo.Repo{Owner: "foo", Name: "bar"}, 102 }, 103 Service: svc, 104 } 105 cmd := landCmd{ 106 getConfig: cb.Build, 107 getEditor: func(string) (editor.Editor, error) { return ed, nil }, 108 } 109 cmd.Args.Branch = tt.Head 110 if cmd.Editor == "" { 111 cmd.Editor = "vi" 112 } 113 114 // Always return the current branch if requested. 115 git.EXPECT().CurrentBranch().Return(tt.CurrentBranch, nil).AnyTimes() 116 117 for head, prs := range tt.PullRequestsByHead { 118 github.EXPECT().ListPullRequestsByHead(gomock.Any(), "", head).Return(prs, nil) 119 } 120 121 if tt.ExpectLandRequest != nil { 122 if tt.ExpectLandRequest.Editor == nil { 123 tt.ExpectLandRequest.Editor = ed 124 } 125 svc.EXPECT().Land(gomock.Any(), tt.ExpectLandRequest).Return(tt.ReturnLandResponse, nil) 126 } 127 128 err := cmd.Execute(nil) 129 if tt.WantError != "" { 130 assert.Error(t, err, "expected failure") 131 assert.Contains(t, err.Error(), tt.WantError) 132 } else { 133 assert.NoError(t, err, "command rebase failed") 134 } 135 }) 136 } 137 }