Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: AdamKorcz <[email protected]>
  • Loading branch information
AdamKorcz committed Nov 22, 2023
1 parent 7408b45 commit 301c562
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func serveCmd(o *options.Options) *cobra.Command {
ciiClient := clients.DefaultCIIBestPracticesClient()
checksToRun := checks.GetAll()
repoResult, err := pkg.RunScorecard(
ctx, repo, clients.HeadSHA /*commitSHA*/, o.CommitDepth, checksToRun,
repoClient, ossFuzzRepoClient, ciiClient, vulnsClient)
ctx, repo, clients.HeadSHA /*commitSHA*/, o.CommitDepth, checksToRun, repoClient,
ossFuzzRepoClient, ciiClient, vulnsClient)
if err != nil {
logger.Error(err, "running enabled scorecard checks on repo")
rw.WriteHeader(http.StatusInternalServerError)
Expand Down
54 changes: 51 additions & 3 deletions pkg/scorecard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ package pkg

import (
"context"
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/clients/localdir"
mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
"github.com/ossf/scorecard/v4/finding"
"github.com/ossf/scorecard/v4/finding/probe"
"github.com/ossf/scorecard/v4/log"
)

Expand Down Expand Up @@ -204,15 +208,48 @@ func TestExperimentalRunProbes(t *testing.T) {
name: "empty commits repos should return repo details but no checks",
args: args{
uri: "github.com/ossf/scorecard",
commitSHA: "",
commitSHA: "1a17bb812fb2ac23e9d09e86e122f8b67563aed7",
},
want: ScorecardResult{
Repo: RepoInfo{
Name: "github.com/ossf/scorecard",
Name: "github.com/ossf/scorecard",
CommitSHA: "1a17bb812fb2ac23e9d09e86e122f8b67563aed7",
},
RawResults: checker.RawResults{
Metadata: checker.MetadataData{
Metadata: map[string]string{
"repository.defaultBranch": "main",
"repository.host": "github.com",
"repository.name": "ossf/scorecard",
"repository.sha1": "1a17bb812fb2ac23e9d09e86e122f8b67563aed7",
"repository.uri": "github.com/ossf/scorecard",
},
},
},
Scorecard: ScorecardInfo{
CommitSHA: "unknown",
},
Findings: []finding.Finding{
{
Probe: "fuzzedWithOSSFuzz",
Message: "no OSSFuzz integration found",
Remediation: &probe.Remediation{
Text: fmt.Sprintf("%s%s%s\n%s%s",
"Follow the steps in ",
"https://github.com/google/oss-fuzz ",
"to integrate fuzzing for your project.",
"Over time, try to add fuzzing for more ",
"functionalities of your project."),
Markdown: fmt.Sprintf("%s%s%s\n%s%s",
"Follow the steps in [https://github.com",
"/google/oss-fuzz](https://github.com/google/oss-fuzz) ",
"to integrate fuzzing for your project.",
"Over time, try to add fuzzing for more ",
"functionalities of your project."),
Effort: 3,
},
},
},
},
wantErr: false,
},
Expand All @@ -226,6 +263,7 @@ func TestExperimentalRunProbes(t *testing.T) {
repo := mockrepo.NewMockRepo(ctrl)

repo.EXPECT().URI().Return(tt.args.uri).AnyTimes()
repo.EXPECT().Host().Return("github.com").AnyTimes()

mockRepoClient.EXPECT().InitRepo(repo, tt.args.commitSHA, 0).Return(nil)

Expand All @@ -243,8 +281,18 @@ func TestExperimentalRunProbes(t *testing.T) {
},
}, nil
})
mockRepoClient.EXPECT().GetDefaultBranchName().Return("main", nil).AnyTimes()
defer ctrl.Finish()
got, err := ExperimentalRunProbes(context.Background(), repo, tt.args.commitSHA, 0, nil, []string{"fuzzedWithOSSFuzz"}, mockRepoClient, nil, nil, nil)
got, err := ExperimentalRunProbes(context.Background(),
repo,
tt.args.commitSHA,
0,
nil,
[]string{"fuzzedWithOSSFuzz"},
mockRepoClient,
nil,
nil,
nil)
if (err != nil) != tt.wantErr {
t.Errorf("RunScorecard() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 301c562

Please sign in to comment.