From 301c562d84315e95f50b5972881be51c1f34ec63 Mon Sep 17 00:00:00 2001 From: AdamKorcz Date: Wed, 22 Nov 2023 13:29:52 +0000 Subject: [PATCH] add test coverage Signed-off-by: AdamKorcz --- cmd/serve.go | 4 ++-- pkg/scorecard_test.go | 54 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index bd15eeee2bea..0aecc321681a 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -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) diff --git a/pkg/scorecard_test.go b/pkg/scorecard_test.go index bf4956a47f26..81d3204bc630 100644 --- a/pkg/scorecard_test.go +++ b/pkg/scorecard_test.go @@ -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" ) @@ -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, }, @@ -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) @@ -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