Skip to content

Commit

Permalink
update mocks to support multiple calls to mocks functions and validat…
Browse files Browse the repository at this point in the history
…ing each call
  • Loading branch information
bhamail committed Sep 18, 2024
1 parent c92be89 commit ae7f180
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 30 deletions.
77 changes: 53 additions & 24 deletions github/github_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,29 @@ import (

// RepositoriesMock mocks RepositoriesService
type RepositoriesMock struct {
t *testing.T
assertParameters bool
expectedCtx context.Context
expectedOwner, expectedRepo, expectedRef, expectedUser string
t *testing.T
/* callCount is the number of times the createRepoStatus function has been called by production code. */
callCount int
/* assertParameters is a slice of booleans that determine whether to assert the parameters passed to the function for
each call to the CreateStatus function. If the value at the index of the callCount is true, the parameters will be
asserted.
*/
assertParameters []bool
expectedCtx []context.Context
expectedOwner, expectedRepo, expectedRef, expectedUser []string
// expectedOpts *github.ListOptions
expectedCreateStatusRepoStatus *github.RepoStatus
createStatusRepoStatus *github.RepoStatus
createStatusResponse *github.Response
createStatusError error
expectedCreateStatusRepoStatus []*github.RepoStatus
createStatusRepoStatus []*github.RepoStatus
createStatusResponse []*github.Response
createStatusError []error
isCollaboratorResult bool
isCollaboratorResp *github.Response
isCollaboratorErr error
}

var _ RepositoriesService = (*RepositoriesMock)(nil)

func setupMockRepositoriesService(t *testing.T, assertParameters bool) (mock *RepositoriesMock) {
func setupMockRepositoriesService(t *testing.T, assertParameters []bool) (mock *RepositoriesMock) {
mock = &RepositoriesMock{
t: t,
assertParameters: assertParameters,
Expand All @@ -61,15 +67,38 @@ func (r *RepositoriesMock) ListStatuses(ctx context.Context, owner, repo, ref st
panic("implement me")
}

func (r *RepositoriesMock) CreateStatus(ctx context.Context, owner, repo, ref string, status *github.RepoStatus) (*github.RepoStatus, *github.Response, error) {
if r.assertParameters {
assert.Equal(r.t, r.expectedCtx, ctx)
assert.Equal(r.t, r.expectedOwner, owner)
assert.Equal(r.t, r.expectedRepo, repo)
assert.Equal(r.t, r.expectedRef, ref)
assert.Equal(r.t, r.expectedCreateStatusRepoStatus, status)
func (r *RepositoriesMock) CreateStatus(ctx context.Context, owner, repo, ref string, status *github.RepoStatus) (retRepoStatus *github.RepoStatus, createStatusResponse *github.Response, createStatusError error) {
defer func() { r.callCount++ }()
if r.assertParameters != nil && r.assertParameters[r.callCount] {
if r.expectedCtx != nil {
assert.Equal(r.t, r.expectedCtx[r.callCount], ctx)
}
if r.expectedOwner != nil {
assert.Equal(r.t, r.expectedOwner[r.callCount], owner)
}
if r.expectedRepo != nil {
assert.Equal(r.t, r.expectedRepo[r.callCount], repo)
}
if r.expectedRef != nil {
assert.Equal(r.t, r.expectedRef[r.callCount], ref)
}
if r.expectedCreateStatusRepoStatus != nil {
assert.Equal(r.t, r.expectedCreateStatusRepoStatus[r.callCount], status)
}
}

if r.createStatusRepoStatus != nil {
retRepoStatus = r.createStatusRepoStatus[r.callCount]
}

if r.createStatusRepoStatus != nil {
createStatusResponse = r.createStatusResponse[r.callCount]
}
return r.createStatusRepoStatus, r.createStatusResponse, r.createStatusError

if r.createStatusError != nil {
createStatusError = r.createStatusError[r.callCount]
}
return
}

// Get returns a repository.
Expand All @@ -86,11 +115,11 @@ func (r *RepositoriesMock) Get(context.Context, string, string) (*github.Reposit
}

func (r *RepositoriesMock) IsCollaborator(ctx context.Context, owner, repo, user string) (bool, *github.Response, error) {
if r.assertParameters {
assert.Equal(r.t, r.expectedCtx, ctx)
assert.Equal(r.t, r.expectedOwner, owner)
assert.Equal(r.t, r.expectedRepo, repo)
assert.Equal(r.t, r.expectedUser, user)
if r.assertParameters != nil && r.assertParameters[r.callCount] {
assert.Equal(r.t, r.expectedCtx[r.callCount], ctx)
assert.Equal(r.t, r.expectedOwner[r.callCount], owner)
assert.Equal(r.t, r.expectedRepo[r.callCount], repo)
assert.Equal(r.t, r.expectedUser[r.callCount], user)
}
return r.isCollaboratorResult, r.isCollaboratorResp, r.isCollaboratorErr
}
Expand Down Expand Up @@ -204,7 +233,7 @@ func (a *AppsMock) GetInstallation(ctx context.Context, id int64) (*github.Insta
return a.mockInstallation, a.mockInstallationResp, a.mockInstallationError
}

var appSlug = "myAppSlug"
var MockAppSlug = "myAppSlug"

func SetupMockGHJWT() (resetImpl func()) {
origGHJWT := GHJWTImpl
Expand All @@ -214,7 +243,7 @@ func SetupMockGHJWT() (resetImpl func()) {
GHJWTImpl = &GHJWTMock{
AppsMock: AppsMock{
mockInstallation: &github.Installation{
AppSlug: &appSlug,
AppSlug: &MockAppSlug,
},
},
}
Expand Down
31 changes: 25 additions & 6 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package github

import (
"context"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -351,7 +352,7 @@ func TestWithFullEnvironment(t *testing.T) {
t.Run("TestHandlePullRequestListCommitsError", func(t *testing.T) {
forcedError := fmt.Errorf("forced ListCommits error")
GHImpl = &GHInterfaceMock{
RepositoriesMock: *setupMockRepositoriesService(t, false),
RepositoriesMock: *setupMockRepositoriesService(t, []bool{false}),
PullRequestsMock: PullRequestsMock{
mockListCommitsError: forcedError,
},
Expand All @@ -369,7 +370,7 @@ func TestWithFullEnvironment(t *testing.T) {
GHJWTImpl = &GHJWTMock{
AppsMock: AppsMock{
mockInstallation: &github.Installation{
AppSlug: &appSlug,
AppSlug: &MockAppSlug,
},
mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}},
mockApp: &github.App{ExternalURL: &mockExternalUrl},
Expand All @@ -389,7 +390,25 @@ func TestWithFullEnvironment(t *testing.T) {

t.Run("TestHandlePullRequestListCommitsUnsignedCommit", func(t *testing.T) {
authors := []string{"john", "doe"}
GHImpl = getGHMock(getMockRepositoryCommits(authors, false), nil, nil)

repositoriesMock := *setupMockRepositoriesService(t, []bool{true, true})
repositoriesMock.expectedCtx = []context.Context{context.Background(), context.Background()}

status1 := &github.RepoStatus{
State: github.String("pending"),
Description: github.String("Paul Botsco, the CLA verifier is running"),
Context: &MockAppSlug,
}
status2 := &github.RepoStatus{
State: github.String("failure"),
Description: github.String("One or more commits haven't met our Quality requirements."),
Context: &MockAppSlug,
}
repositoriesMock.expectedCreateStatusRepoStatus = []*github.RepoStatus{
status1, status2,
}

GHImpl = getGHMock(getMockRepositoryCommits(authors, false), nil, &repositoriesMock)
mockDB, logger := setupMockDB(t, false)
err := HandlePullRequest(logger, mockDB, webhook.PullRequestPayload{}, 0, "")
assert.NoError(t, err)
Expand All @@ -412,7 +431,7 @@ func TestHandlePullRequestGetAppError(t *testing.T) {
GHJWTImpl = &GHJWTMock{
AppsMock: AppsMock{
mockInstallation: &github.Installation{
AppSlug: &appSlug,
AppSlug: &MockAppSlug,
},
//mockAppErr: forcedError,
mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusNotFound}},
Expand Down Expand Up @@ -496,7 +515,7 @@ func TestHandlePullRequestListCommitsNoAuthor(t *testing.T) {
GHJWTImpl = &GHJWTMock{
AppsMock: AppsMock{
mockInstallation: &github.Installation{
AppSlug: &appSlug,
AppSlug: &MockAppSlug,
},
mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}},
mockApp: &github.App{ExternalURL: &mockExternalUrl},
Expand Down Expand Up @@ -630,7 +649,7 @@ func TestReviewPriorPRsEvaluatePRError(t *testing.T) {
GHImpl = &GHInterfaceMock{
RepositoriesMock: RepositoriesMock{
t: t,
createStatusError: forcedError,
createStatusError: []error{forcedError},
},
}

Expand Down

0 comments on commit ae7f180

Please sign in to comment.