Skip to content

Commit

Permalink
Create and use MockRepoClient in unit tests (#922)
Browse files Browse the repository at this point in the history
Co-authored-by: Azeem Shaikh <azeems@google.com>
  • Loading branch information
azeemshaikh38 and azeemsgoogle committed Aug 26, 2021
1 parent 50fd921 commit 37696ac
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 32 deletions.
14 changes: 7 additions & 7 deletions checks/branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/google/go-github/v38/github"

"github.com/ossf/scorecard/v2/checker"
"github.com/ossf/scorecard/v2/clients"
sce "github.com/ossf/scorecard/v2/errors"
)

Expand All @@ -40,8 +41,6 @@ func init() {

// TODO: Use RepoClient interface instead of this.
type repositories interface {
Get(context.Context, string, string) (*github.Repository,
*github.Response, error)
ListBranches(ctx context.Context, owner string, repo string,
opts *github.BranchListOptions) ([]*github.Branch, *github.Response, error)
ListReleases(ctx context.Context, owner string, repo string, opts *github.ListOptions) (
Expand Down Expand Up @@ -81,11 +80,12 @@ func getBranchMapFrom(branches []*github.Branch) branchMap {
// BranchProtection runs Branch-Protection check.
func BranchProtection(c *checker.CheckRequest) checker.CheckResult {
// Checks branch protection on both release and development branch.
return checkReleaseAndDevBranchProtection(c.Ctx, c.Client.Repositories, c.Dlogger, c.Owner, c.Repo)
return checkReleaseAndDevBranchProtection(c.Ctx, c.RepoClient, c.Client.Repositories, c.Dlogger, c.Owner, c.Repo)
}

func checkReleaseAndDevBranchProtection(ctx context.Context, r repositories, dl checker.DetailLogger, ownerStr,
repoStr string) checker.CheckResult {
func checkReleaseAndDevBranchProtection(ctx context.Context,
repoClient clients.RepoClient, r repositories, dl checker.DetailLogger,
ownerStr, repoStr string) checker.CheckResult {
// Get all branches. This will include information on whether they are protected.
branches, _, err := r.ListBranches(ctx, ownerStr, repoStr, &github.BranchListOptions{})
if err != nil {
Expand Down Expand Up @@ -126,11 +126,11 @@ func checkReleaseAndDevBranchProtection(ctx context.Context, r repositories, dl
}

// Add default branch.
repo, _, err := r.Get(ctx, ownerStr, repoStr)
defaultBranch, err := repoClient.GetDefaultBranch()
if err != nil {
return checker.CreateRuntimeErrorResult(CheckBranchProtection, err)
}
checkBranches[repo.GetDefaultBranch()] = true
checkBranches[defaultBranch.Name] = true

protected := true
unknown := false
Expand Down
50 changes: 25 additions & 25 deletions checks/branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,20 @@ import (
"net/http"
"testing"

"github.com/golang/mock/gomock"
"github.com/google/go-github/v38/github"

"github.com/ossf/scorecard/v2/checker"
"github.com/ossf/scorecard/v2/clients"
sce "github.com/ossf/scorecard/v2/errors"
scut "github.com/ossf/scorecard/v2/utests"
)

type mockRepos struct {
branches []*string
protections map[string]*github.Protection
defaultBranch *string
releases []*string
nonadmin bool
}

func (m mockRepos) Get(ctx context.Context, o, r string) (
*github.Repository, *github.Response, error) {
return &github.Repository{
DefaultBranch: m.defaultBranch,
}, nil, nil
branches []*string
protections map[string]*github.Protection
releases []*string
nonadmin bool
}

func (m mockRepos) ListReleases(ctx context.Context, owner string,
Expand Down Expand Up @@ -88,7 +82,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
name string
expected scut.TestReturn
branches []*string
defaultBranch *string
defaultBranch string
releases []*string
protections map[string]*github.Protection
nonadmin bool
Expand All @@ -102,7 +96,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
NumberOfInfo: 2,
NumberOfDebug: 0,
},
defaultBranch: &main,
defaultBranch: main,
branches: []*string{&rel1, &main},
releases: nil,
protections: map[string]*github.Protection{
Expand Down Expand Up @@ -150,7 +144,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
NumberOfInfo: 9,
NumberOfDebug: 0,
},
defaultBranch: &main,
defaultBranch: main,
branches: []*string{&rel1, &main},
releases: []*string{&rel1},
protections: map[string]*github.Protection{
Expand Down Expand Up @@ -231,7 +225,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
NumberOfInfo: 14,
NumberOfDebug: 0,
},
defaultBranch: &main,
defaultBranch: main,
branches: []*string{&rel1, &main},
releases: []*string{&rel1},
protections: map[string]*github.Protection{
Expand Down Expand Up @@ -312,7 +306,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
NumberOfInfo: 2,
NumberOfDebug: 0,
},
defaultBranch: &main,
defaultBranch: main,
branches: []*string{&rel1, &main},
releases: []*string{&sha},
protections: map[string]*github.Protection{
Expand Down Expand Up @@ -360,7 +354,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
NumberOfInfo: 0,
NumberOfDebug: 0,
},
defaultBranch: &main,
defaultBranch: main,
branches: []*string{&main},
releases: []*string{nil},
protections: map[string]*github.Protection{
Expand Down Expand Up @@ -409,7 +403,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
NumberOfDebug: 0,
},
nonadmin: true,
defaultBranch: &main,
defaultBranch: main,
branches: []*string{&rel1, &main},
releases: []*string{&rel1},
protections: map[string]*github.Protection{
Expand All @@ -434,16 +428,22 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
m := mockRepos{
defaultBranch: tt.defaultBranch,
branches: tt.branches,
releases: tt.releases,
protections: tt.protections,
nonadmin: tt.nonadmin,
branches: tt.branches,
releases: tt.releases,
protections: tt.protections,
nonadmin: tt.nonadmin,
}

ctrl := gomock.NewController(t)
mockRepoClient := clients.NewMockRepoClient(ctrl)
mockRepoClient.EXPECT().GetDefaultBranch().
Return(clients.BranchRef{Name: tt.defaultBranch}, nil).
AnyTimes()
dl := scut.TestDetailLogger{}
r := checkReleaseAndDevBranchProtection(context.Background(), m,
r := checkReleaseAndDevBranchProtection(context.Background(), mockRepoClient, m,
&dl, "testowner", "testrepo")
scut.ValidateTestReturn(t, tt.name, &tt.expected, &r, &dl)
ctrl.Finish()
})
}
}
Expand Down
224 changes: 224 additions & 0 deletions clients/mock_repo_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 37696ac

Please sign in to comment.