|
| 1 | +// Copyright 2024 Palantir Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// pull_test.go |
| 16 | + |
| 17 | +package pull |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-github/v66/github" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/mock" |
| 26 | +) |
| 27 | + |
| 28 | +type MockGitHubClient struct { |
| 29 | + mock.Mock |
| 30 | +} |
| 31 | + |
| 32 | +func (m *MockGitHubClient) ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opts *github.ListOptions) ([]*github.PullRequest, *github.Response, error) { |
| 33 | + args := m.Called(ctx, owner, repo, sha, opts) |
| 34 | + return args.Get(0).([]*github.PullRequest), args.Get(1).(*github.Response), args.Error(2) |
| 35 | +} |
| 36 | + |
| 37 | +func (m *MockGitHubClient) List(ctx context.Context, owner, repo string, opts *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) { |
| 38 | + args := m.Called(ctx, owner, repo, opts) |
| 39 | + return args.Get(0).([]*github.PullRequest), args.Get(1).(*github.Response), args.Error(2) |
| 40 | +} |
| 41 | + |
| 42 | +func TestGetOpenPullRequestsForSHA(t *testing.T) { |
| 43 | + mockClient := new(MockGitHubClient) |
| 44 | + ctx := context.Background() |
| 45 | + owner := "owner" |
| 46 | + repo := "repo" |
| 47 | + sha := "sha" |
| 48 | + |
| 49 | + pr := &github.PullRequest{ |
| 50 | + State: github.String("open"), |
| 51 | + Head: &github.PullRequestBranch{SHA: github.String(sha)}, |
| 52 | + } |
| 53 | + |
| 54 | + mockClient.On("ListPullRequestsWithCommit", ctx, owner, repo, sha, mock.Anything).Return([]*github.PullRequest{pr}, &github.Response{NextPage: 0}, nil) |
| 55 | + |
| 56 | + prs, err := getOpenPullRequestsForSHA(ctx, mockClient, owner, repo, sha) |
| 57 | + assert.NoError(t, err) |
| 58 | + assert.Len(t, prs, 1) |
| 59 | + assert.Equal(t, sha, prs[0].GetHead().GetSHA()) |
| 60 | + |
| 61 | + mockClient.AssertExpectations(t) |
| 62 | +} |
| 63 | + |
| 64 | +func TestListOpenPullRequestsForSHA(t *testing.T) { |
| 65 | + mockClient := new(MockGitHubClient) |
| 66 | + ctx := context.Background() |
| 67 | + owner := "owner" |
| 68 | + repo := "repo" |
| 69 | + sha := "sha" |
| 70 | + |
| 71 | + pr := &github.PullRequest{ |
| 72 | + State: github.String("open"), |
| 73 | + Head: &github.PullRequestBranch{SHA: github.String(sha)}, |
| 74 | + } |
| 75 | + |
| 76 | + mockClient.On("List", ctx, owner, repo, mock.Anything).Return([]*github.PullRequest{pr}, &github.Response{NextPage: 0}, nil) |
| 77 | + |
| 78 | + prs, err := ListAllOpenPullRequestsFilteredBySHA(ctx, mockClient, owner, repo, sha) |
| 79 | + assert.NoError(t, err) |
| 80 | + assert.Len(t, prs, 1) |
| 81 | + assert.Equal(t, sha, prs[0].GetHead().GetSHA()) |
| 82 | + |
| 83 | + mockClient.AssertExpectations(t) |
| 84 | +} |
| 85 | + |
| 86 | +func TestGetAllPossibleOpenPullRequestsForSHA_FirstMethodReturnsResults(t *testing.T) { |
| 87 | + mockClient := new(MockGitHubClient) |
| 88 | + ctx := context.Background() |
| 89 | + owner := "owner" |
| 90 | + repo := "repo" |
| 91 | + sha := "sha" |
| 92 | + |
| 93 | + pr := &github.PullRequest{ |
| 94 | + State: github.String("open"), |
| 95 | + Head: &github.PullRequestBranch{SHA: github.String(sha)}, |
| 96 | + } |
| 97 | + |
| 98 | + // Mock the first method to return a valid pull request. |
| 99 | + mockClient.On("ListPullRequestsWithCommit", ctx, owner, repo, sha, mock.Anything).Return([]*github.PullRequest{pr}, &github.Response{NextPage: 0}, nil).Once() |
| 100 | + // Mock the second method to not be called. |
| 101 | + mockClient.On("List", ctx, owner, repo, mock.Anything).Return(nil, nil, nil).Maybe() |
| 102 | + |
| 103 | + prs, err := GetAllPossibleOpenPullRequestsForSHA(ctx, mockClient, owner, repo, sha) |
| 104 | + assert.NoError(t, err) |
| 105 | + assert.Len(t, prs, 1) |
| 106 | + assert.Equal(t, sha, prs[0].GetHead().GetSHA()) |
| 107 | + |
| 108 | + mockClient.AssertExpectations(t) |
| 109 | +} |
| 110 | + |
| 111 | +func TestGetAllPossibleOpenPullRequestsForSHA_SecondMethodReturnsResults(t *testing.T) { |
| 112 | + mockClient := new(MockGitHubClient) |
| 113 | + ctx := context.Background() |
| 114 | + owner := "owner" |
| 115 | + repo := "repo" |
| 116 | + sha := "sha" |
| 117 | + |
| 118 | + pr := &github.PullRequest{ |
| 119 | + State: github.String("open"), |
| 120 | + Head: &github.PullRequestBranch{SHA: github.String(sha)}, |
| 121 | + } |
| 122 | + |
| 123 | + // Mock the first method to return no results. |
| 124 | + mockClient.On("ListPullRequestsWithCommit", ctx, owner, repo, sha, mock.Anything).Return([]*github.PullRequest{}, &github.Response{NextPage: 0}, nil).Once() |
| 125 | + // Mock the second method to return a valid pull request. |
| 126 | + mockClient.On("List", ctx, owner, repo, mock.Anything).Return([]*github.PullRequest{pr}, &github.Response{NextPage: 0}, nil).Once() |
| 127 | + |
| 128 | + prs, err := GetAllPossibleOpenPullRequestsForSHA(ctx, mockClient, owner, repo, sha) |
| 129 | + assert.NoError(t, err) |
| 130 | + assert.Len(t, prs, 1) |
| 131 | + assert.Equal(t, sha, prs[0].GetHead().GetSHA()) |
| 132 | + |
| 133 | + mockClient.AssertExpectations(t) |
| 134 | +} |
| 135 | + |
| 136 | +func TestGetAllPossibleOpenPullRequestsForSHA_NoResults(t *testing.T) { |
| 137 | + mockClient := new(MockGitHubClient) |
| 138 | + ctx := context.Background() |
| 139 | + owner := "owner" |
| 140 | + repo := "repo" |
| 141 | + sha := "sha" |
| 142 | + |
| 143 | + // Mock both methods to return no results. |
| 144 | + mockClient.On("ListPullRequestsWithCommit", ctx, owner, repo, sha, mock.Anything).Return([]*github.PullRequest{}, &github.Response{NextPage: 0}, nil).Once() |
| 145 | + mockClient.On("List", ctx, owner, repo, mock.Anything).Return([]*github.PullRequest{}, &github.Response{NextPage: 0}, nil).Once() |
| 146 | + |
| 147 | + prs, err := GetAllPossibleOpenPullRequestsForSHA(ctx, mockClient, owner, repo, sha) |
| 148 | + assert.NoError(t, err) |
| 149 | + assert.Len(t, prs, 0) |
| 150 | + |
| 151 | + mockClient.AssertExpectations(t) |
| 152 | +} |
| 153 | + |
| 154 | +func TestGetAllPossibleOpenPullRequestsForSHA_Errors(t *testing.T) { |
| 155 | + mockClient := new(MockGitHubClient) |
| 156 | + ctx := context.Background() |
| 157 | + owner := "owner" |
| 158 | + repo := "repo" |
| 159 | + sha := "sha" |
| 160 | + |
| 161 | + // Mock the first method to return an error. |
| 162 | + mockClient.On("ListPullRequestsWithCommit", ctx, owner, repo, sha, mock.Anything).Return([]*github.PullRequest{}, &github.Response{}, assert.AnError).Once() |
| 163 | + // Mock the second method to not be called. |
| 164 | + mockClient.On("List", ctx, owner, repo, mock.Anything).Return(nil, nil, nil).Maybe() |
| 165 | + |
| 166 | + prs, err := GetAllPossibleOpenPullRequestsForSHA(ctx, mockClient, owner, repo, sha) |
| 167 | + assert.Error(t, err) |
| 168 | + assert.Nil(t, prs) |
| 169 | + |
| 170 | + mockClient.AssertExpectations(t) |
| 171 | +} |
| 172 | + |
| 173 | +func TestListOpenPullRequestsForRef(t *testing.T) { |
| 174 | + mockClient := new(MockGitHubClient) |
| 175 | + ctx := context.Background() |
| 176 | + owner := "owner" |
| 177 | + repo := "repo" |
| 178 | + ref := "refs/heads/main" |
| 179 | + |
| 180 | + pr := &github.PullRequest{ |
| 181 | + State: github.String("open"), |
| 182 | + Base: &github.PullRequestBranch{Ref: github.String("main")}, |
| 183 | + } |
| 184 | + |
| 185 | + mockClient.On("List", ctx, owner, repo, mock.Anything).Return([]*github.PullRequest{pr}, &github.Response{NextPage: 0}, nil) |
| 186 | + |
| 187 | + prs, err := GetAllOpenPullRequestsForRef(ctx, mockClient, owner, repo, ref) |
| 188 | + assert.NoError(t, err) |
| 189 | + assert.Len(t, prs, 1) |
| 190 | + assert.Equal(t, "main", prs[0].GetBase().GetRef()) |
| 191 | + |
| 192 | + mockClient.AssertExpectations(t) |
| 193 | +} |
0 commit comments