Skip to content

Commit

Permalink
search for config file
Browse files Browse the repository at this point in the history
Signed-off-by: Raghav Kaul <raghavkaul+github@google.com>
  • Loading branch information
raghavkaul committed Jun 6, 2024
1 parent b83ac4e commit 0e87c08
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ func runScorecard(ctx context.Context,
go runEnabledChecks(ctx, repo, request, checksToRun, resultsCh)

if os.Getenv(options.EnvVarScorecardExperimental) == "1" {
r, err := findConfigFile(repoClient)
r := findConfigFile(repoClient)

logger := sclog.NewLogger(sclog.DefaultLevel)

if err == nil {
if r != nil {
defer r.Close()
checks := []string{}
for check := range checksToRun {
Expand All @@ -196,19 +196,19 @@ func runScorecard(ctx context.Context,
return ret, nil
}

func findConfigFile(rc clients.RepoClient) (io.ReadCloser, error) {
func findConfigFile(rc clients.RepoClient) io.ReadCloser {
// Look for a config file. Return first one regardless of validity
locs := []string{"scorecard.yml", ".scorecard.yml", ".github/scorecard.yml"}

for i := range locs {
cfr, err := rc.GetFileReader(locs[i])
if err == nil {
if err != nil {
continue
}
return cfr, nil
return cfr
}

return nil, fmt.Errorf("no scorecard.yml found")
return nil
}

func runEnabledProbes(request *checker.CheckRequest,
Expand Down
67 changes: 67 additions & 0 deletions pkg/scorecard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ package pkg

import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -341,3 +346,65 @@ func TestExperimentalRunProbes(t *testing.T) {
})
}
}

func Test_findConfigFile(t *testing.T) {
t.Parallel()

//nolint:govet
tests := []struct {
locs []string
desc string
wantFound bool
}{
{
desc: "scorecard.yml exists",
locs: []string{"scorecard.yml"},
wantFound: true,
},
{
desc: ".scorecard.yml exists",
locs: []string{".scorecard.yml"},
wantFound: true,
},
{
desc: ".github/scorecard.yml exists",
locs: []string{".github/scorecard.yml"},
wantFound: true,
},
{
desc: "multiple configs exist",
locs: []string{"scorecard.yml", ".github/scorecard.yml"},
wantFound: true,
},
{
desc: "no config exists so shouldn't find one",
locs: []string{},
wantFound: false,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)
mockRepoClient.EXPECT().GetFileReader(gomock.Any()).AnyTimes().DoAndReturn(func(filename string) (io.ReadCloser, error) {
if !slices.Contains(tt.locs, filename) {
return nil, fmt.Errorf("os.Open: %s", filename)
}
fullPath := filepath.Join("./testdata", filename)
f, err := os.Open(fullPath)
if err != nil {
return nil, fmt.Errorf("os.Open: %w", err)
}
return f, nil
})
r := findConfigFile(mockRepoClient)

if tt.wantFound != (r != nil) {
t.Errorf("wantFound: %+v got %+v", tt.wantFound, r)
}
})
}
}
5 changes: 5 additions & 0 deletions pkg/testdata/.github/scorecard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
annotations:
- checks:
- binary-artifacts
reasons:
- reason: test-data
5 changes: 5 additions & 0 deletions pkg/testdata/.scorecard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
annotations:
- checks:
- binary-artifacts
reasons:
- reason: test-data
5 changes: 5 additions & 0 deletions pkg/testdata/scorecard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
annotations:
- checks:
- binary-artifacts
reasons:
- reason: test-data

0 comments on commit 0e87c08

Please sign in to comment.