Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Update Binary-Artifacts and License checks #4079

Merged
merged 6 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions checks/binary_artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ package checks

import (
"context"
"errors"
"io"
"os"
"testing"

"github.com/golang/mock/gomock"

"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/clients"
"github.com/ossf/scorecard/v5/clients/localdir"
"github.com/ossf/scorecard/v5/log"
mockrepo "github.com/ossf/scorecard/v5/clients/mockclients"
scut "github.com/ossf/scorecard/v5/utests"
)

Expand All @@ -34,22 +33,26 @@ func TestBinaryArtifacts(t *testing.T) {
name string
inputFolder string
err error
expected checker.CheckResult
expected scut.TestReturn
}{
{
name: "Jar file",
inputFolder: "testdata/binaryartifacts/jars",
err: nil,
expected: checker.CheckResult{
Score: 8,
expected: scut.TestReturn{
Score: 8,
NumberOfInfo: 0,
NumberOfWarn: 2,
},
},
{
name: "non binary file",
inputFolder: "testdata/licensedir/withlicense",
err: nil,
expected: checker.CheckResult{
Score: 10,
expected: scut.TestReturn{
Score: checker.MaxResultScore,
NumberOfInfo: 0,
NumberOfWarn: 0,
},
},
}
Expand All @@ -58,35 +61,39 @@ func TestBinaryArtifacts(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

logger := log.NewLogger(log.DebugLevel)

// TODO: Use gMock instead of Localdir here.
ctrl := gomock.NewController(t)
repo, err := localdir.MakeLocalDirRepo(tt.inputFolder)

if !errors.Is(err, tt.err) {
t.Errorf("MakeLocalDirRepo: %v, expected %v", err, tt.err)
}
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)

ctx := context.Background()
mockRepoClient.EXPECT().ListFiles(gomock.Any()).DoAndReturn(func(predicate func(string) (bool, error)) ([]string, error) {
var files []string
dirFiles, err := os.ReadDir(tt.inputFolder)
if err == nil {
for _, file := range dirFiles {
files = append(files, file.Name())
}
print(files)
}
return files, err
}).AnyTimes()

client := localdir.CreateLocalDirClient(ctx, logger)
if err := client.InitRepo(repo, clients.HeadSHA, 0); err != nil {
t.Errorf("InitRepo: %v", err)
}
mockRepoClient.EXPECT().GetFileReader(gomock.Any()).DoAndReturn(func(file string) (io.ReadCloser, error) {
return os.Open("./" + tt.inputFolder + "/" + file)
}).AnyTimes()

ctx := context.Background()

dl := scut.TestDetailLogger{}

req := checker.CheckRequest{
Ctx: ctx,
RepoClient: client,
RepoClient: mockRepoClient,
Dlogger: &dl,
}

result := BinaryArtifacts(&req)
if result.Score != tt.expected.Score {
t.Errorf("BinaryArtifacts: %v, expected %v for tests %v", result.Score, tt.expected.Score, tt.name)
}

scut.ValidateTestReturn(t, tt.name, &tt.expected, &result, &dl)

ctrl.Finish()
})
Expand Down
46 changes: 28 additions & 18 deletions checks/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ package checks

import (
"context"
"errors"
"fmt"
"io"
"os"
"testing"

"github.com/golang/mock/gomock"

"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/clients"
"github.com/ossf/scorecard/v5/clients/localdir"
"github.com/ossf/scorecard/v5/log"
clients "github.com/ossf/scorecard/v5/clients"
mockrepo "github.com/ossf/scorecard/v5/clients/mockclients"
scut "github.com/ossf/scorecard/v5/utests"
)

Expand Down Expand Up @@ -66,28 +67,37 @@ func TestLicenseFileSubdirectory(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

logger := log.NewLogger(log.DebugLevel)

// TODO: Use gMock instead of Localdir here.
ctrl := gomock.NewController(t)
repo, err := localdir.MakeLocalDirRepo(tt.inputFolder)

if !errors.Is(err, tt.err) {
t.Errorf("MakeLocalDirRepo: %v, expected %v", err, tt.err)
}
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)

mockRepoClient.EXPECT().ListFiles(gomock.Any()).DoAndReturn(func(predicate func(string) (bool, error)) ([]string, error) {
var files []string
dirFiles, err := os.ReadDir(tt.inputFolder)
if err == nil {
for _, file := range dirFiles {
files = append(files, file.Name())
}
print(files)
}
return files, err
}).AnyTimes()

mockRepoClient.EXPECT().GetFileReader(gomock.Any()).DoAndReturn(func(file string) (io.ReadCloser, error) {
return os.Open("./" + tt.inputFolder + "/" + file)
}).AnyTimes()

// Currently the check itself handles this error gracefully,
// searching through the directory to find the license file(s)
// if that functionality is ever changed, this mock needs to be updated accordingly
mockRepoClient.EXPECT().ListLicenses().Return(nil, fmt.Errorf("ListLicenses: %w", clients.ErrUnsupportedFeature)).AnyTimes()

ctx := context.Background()

client := localdir.CreateLocalDirClient(ctx, logger)
if err := client.InitRepo(repo, clients.HeadSHA, 0); err != nil {
t.Errorf("InitRepo: %v", err)
}

dl := scut.TestDetailLogger{}

req := checker.CheckRequest{
Ctx: ctx,
RepoClient: client,
RepoClient: mockRepoClient,
Dlogger: &dl,
}

Expand Down
Loading