github.com/abhinav/git-pr@v0.6.1-0.20171029234004-54218d68c11b/cmd/git-pr/rebase_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/gateway/gatewaytest"
     8  	"github.com/abhinav/git-pr/ptr"
     9  	"github.com/abhinav/git-pr/repo"
    10  	"github.com/abhinav/git-pr/service"
    11  	"github.com/abhinav/git-pr/service/servicetest"
    12  
    13  	"github.com/golang/mock/gomock"
    14  	"github.com/google/go-github/github"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestRebaseCmd(t *testing.T) {
    19  	type prMap map[string][]*github.PullRequest
    20  
    21  	tests := []struct {
    22  		// Test description
    23  		Desc string
    24  
    25  		Base     string
    26  		Head     string
    27  		OnlyMine bool
    28  
    29  		// Name of the current branch (if requested)
    30  		CurrentBranch string
    31  
    32  		// Name of the current GitHub user
    33  		GitHubUser string
    34  
    35  		// Map of branch name to pull requests with that head.
    36  		PullRequestsByHead prMap
    37  		PullRequestsByBase prMap
    38  
    39  		ExpectRebaseRequest  *service.RebaseRequest
    40  		ReturnRebaseResponse *service.RebaseResponse
    41  
    42  		// If non-empty, an error with a message matching this will be
    43  		// expected
    44  		WantError string
    45  	}{
    46  		{
    47  			Desc:               "no matching PRs",
    48  			CurrentBranch:      "feature1",
    49  			PullRequestsByHead: prMap{"feature1": nil},
    50  			WantError:          `Could not find PRs with head "feature1"`,
    51  		},
    52  		{
    53  			Desc:          "too many PRs",
    54  			CurrentBranch: "feature2",
    55  			PullRequestsByHead: prMap{
    56  				"feature2": {
    57  					{HTMLURL: ptr.String("foo")},
    58  					{HTMLURL: ptr.String("bar")},
    59  					{HTMLURL: ptr.String("baz")},
    60  				},
    61  			},
    62  			WantError: `Too many PRs found with head "feature2"`,
    63  		},
    64  		{
    65  			Desc:          "no arguments, rebase dependents",
    66  			CurrentBranch: "feature3",
    67  			PullRequestsByHead: prMap{
    68  				"feature3": {
    69  					{Head: &github.PullRequestBranch{Ref: ptr.String("feature3")}},
    70  				},
    71  			},
    72  			PullRequestsByBase: prMap{
    73  				"feature3": {
    74  					{HTMLURL: ptr.String("foo")},
    75  					{HTMLURL: ptr.String("bar")},
    76  					{HTMLURL: ptr.String("baz")},
    77  				},
    78  			},
    79  			ExpectRebaseRequest: &service.RebaseRequest{
    80  				PullRequests: []*github.PullRequest{
    81  					{HTMLURL: ptr.String("foo")},
    82  					{HTMLURL: ptr.String("bar")},
    83  					{HTMLURL: ptr.String("baz")},
    84  				},
    85  				Base: "feature3",
    86  			},
    87  			ReturnRebaseResponse: &service.RebaseResponse{},
    88  		},
    89  		{
    90  			Desc:          "explicit head branch",
    91  			CurrentBranch: "master",
    92  			Head:          "feature4",
    93  			PullRequestsByHead: prMap{
    94  				"feature4": {
    95  					{
    96  						Head: &github.PullRequestBranch{Ref: ptr.String("feature4")},
    97  					},
    98  				},
    99  			},
   100  			PullRequestsByBase: prMap{
   101  				"feature4": {
   102  					{HTMLURL: ptr.String("foo")},
   103  					{HTMLURL: ptr.String("bar")},
   104  					{HTMLURL: ptr.String("baz")},
   105  				},
   106  			},
   107  			ExpectRebaseRequest: &service.RebaseRequest{
   108  				PullRequests: []*github.PullRequest{
   109  					{HTMLURL: ptr.String("foo")},
   110  					{HTMLURL: ptr.String("bar")},
   111  					{HTMLURL: ptr.String("baz")},
   112  				},
   113  				Base: "feature4",
   114  			},
   115  			ReturnRebaseResponse: &service.RebaseResponse{},
   116  		},
   117  		{
   118  			Desc:          "explicit base branch",
   119  			CurrentBranch: "feature5",
   120  			Base:          "dev",
   121  			PullRequestsByHead: prMap{
   122  				"feature5": {
   123  					{
   124  						Head:    &github.PullRequestBranch{Ref: ptr.String("feature5")},
   125  						HTMLURL: ptr.String("feature5"),
   126  					},
   127  				},
   128  			},
   129  			ExpectRebaseRequest: &service.RebaseRequest{
   130  				PullRequests: []*github.PullRequest{
   131  					{
   132  						Head:    &github.PullRequestBranch{Ref: ptr.String("feature5")},
   133  						HTMLURL: ptr.String("feature5"),
   134  					},
   135  				},
   136  				Base: "dev",
   137  			},
   138  			ReturnRebaseResponse: &service.RebaseResponse{},
   139  		},
   140  		{
   141  			Desc:          "only mine",
   142  			CurrentBranch: "feature6",
   143  			OnlyMine:      true,
   144  			GitHubUser:    "foo",
   145  			PullRequestsByHead: prMap{
   146  				"feature6": {
   147  					{Head: &github.PullRequestBranch{Ref: ptr.String("feature6")}},
   148  				},
   149  			},
   150  			PullRequestsByBase: prMap{
   151  				"feature6": {
   152  					{
   153  						HTMLURL: ptr.String("x"),
   154  						User:    &github.User{Login: ptr.String("foo")},
   155  					},
   156  					{
   157  						HTMLURL: ptr.String("y"),
   158  						User:    &github.User{Login: ptr.String("bar")},
   159  					},
   160  					{
   161  						HTMLURL: ptr.String("z"),
   162  						User:    &github.User{Login: ptr.String("foo")},
   163  					},
   164  				},
   165  			},
   166  			ExpectRebaseRequest: &service.RebaseRequest{
   167  				Author: "foo",
   168  				PullRequests: []*github.PullRequest{
   169  					{
   170  						HTMLURL: ptr.String("x"),
   171  						User:    &github.User{Login: ptr.String("foo")},
   172  					},
   173  					{
   174  						HTMLURL: ptr.String("y"),
   175  						User:    &github.User{Login: ptr.String("bar")},
   176  					},
   177  					{
   178  						HTMLURL: ptr.String("z"),
   179  						User:    &github.User{Login: ptr.String("foo")},
   180  					},
   181  				},
   182  				Base: "feature6",
   183  			},
   184  			ReturnRebaseResponse: &service.RebaseResponse{},
   185  		},
   186  	}
   187  
   188  	for _, tt := range tests {
   189  		t.Run(tt.Desc, func(t *testing.T) {
   190  			mockCtrl := gomock.NewController(t)
   191  			defer mockCtrl.Finish()
   192  
   193  			git := gatewaytest.NewMockGit(mockCtrl)
   194  			github := gatewaytest.NewMockGitHub(mockCtrl)
   195  			svc := servicetest.NewMockPR(mockCtrl)
   196  
   197  			cb := &fakeConfigBuilder{
   198  				ConfigBuilder: clitest.ConfigBuilder{
   199  					Git:        git,
   200  					GitHub:     github,
   201  					Repo:       &repo.Repo{Owner: "foo", Name: "bar"},
   202  					GitHubUser: tt.GitHubUser,
   203  				},
   204  				Service: svc,
   205  			}
   206  			cmd := rebaseCmd{
   207  				getConfig: cb.Build,
   208  				Base:      tt.Base,
   209  				OnlyMine:  tt.OnlyMine,
   210  			}
   211  			cmd.Args.Branch = tt.Head
   212  
   213  			// Always return the current branch if requested.
   214  			git.EXPECT().CurrentBranch().Return(tt.CurrentBranch, nil).AnyTimes()
   215  
   216  			for head, prs := range tt.PullRequestsByHead {
   217  				github.EXPECT().ListPullRequestsByHead(gomock.Any(), "", head).Return(prs, nil)
   218  			}
   219  
   220  			for base, prs := range tt.PullRequestsByBase {
   221  				github.EXPECT().ListPullRequestsByBase(gomock.Any(), base).Return(prs, nil)
   222  			}
   223  
   224  			if tt.ExpectRebaseRequest != nil {
   225  				svc.EXPECT().Rebase(gomock.Any(), tt.ExpectRebaseRequest).Return(tt.ReturnRebaseResponse, nil)
   226  			}
   227  
   228  			err := cmd.Execute(nil)
   229  			if tt.WantError != "" {
   230  				assert.Error(t, err, "expected failure")
   231  				assert.Contains(t, err.Error(), tt.WantError)
   232  			} else {
   233  				assert.NoError(t, err, "command rebase failed")
   234  			}
   235  		})
   236  	}
   237  }