mirror of
https://github.com/ossf/scorecard.git
synced 2024-10-05 13:17:08 +03:00
a788a3830d
* replaced localRepoClient with mockRepoClient Signed-off-by: seelder <seelder@ncsu.edu> * Update binary_artifact_test.go to use scut Updated the test to use scut. Updated the test data to use scut, including adding NumberOfInfo and NumberOfWarn for each test case Signed-off-by: seelder <seelder@ncsu.edu> * Update license_test to use gomock First attempt at updating license_test to use gomock instead of localDir. Note: localDir currently has a TODO for implementing ListLicenses. It returns an UnsupportedFeatures error, which is then handled in checks/raw/license. This first attempt replicates that existing behavior. Signed-off-by: seelder <seelder@ncsu.edu> * Update license_test documentation Clarified why the mock simply throws an error Signed-off-by: seelder <seelder@ncsu.edu> * Fixed linting error in license_test Signed-off-by: seelder <seelder@ncsu.edu> --------- Signed-off-by: seelder <seelder@ncsu.edu>
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
// Copyright 2021 OpenSSF Scorecard Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package checks
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/ossf/scorecard/v5/checker"
|
|
mockrepo "github.com/ossf/scorecard/v5/clients/mockclients"
|
|
scut "github.com/ossf/scorecard/v5/utests"
|
|
)
|
|
|
|
func TestBinaryArtifacts(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
name string
|
|
inputFolder string
|
|
err error
|
|
expected scut.TestReturn
|
|
}{
|
|
{
|
|
name: "Jar file",
|
|
inputFolder: "testdata/binaryartifacts/jars",
|
|
err: nil,
|
|
expected: scut.TestReturn{
|
|
Score: 8,
|
|
NumberOfInfo: 0,
|
|
NumberOfWarn: 2,
|
|
},
|
|
},
|
|
{
|
|
name: "non binary file",
|
|
inputFolder: "testdata/licensedir/withlicense",
|
|
err: nil,
|
|
expected: scut.TestReturn{
|
|
Score: checker.MaxResultScore,
|
|
NumberOfInfo: 0,
|
|
NumberOfWarn: 0,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt // Re-initializing variable so it is not changed while executing the closure below
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
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()
|
|
|
|
ctx := context.Background()
|
|
|
|
dl := scut.TestDetailLogger{}
|
|
|
|
req := checker.CheckRequest{
|
|
Ctx: ctx,
|
|
RepoClient: mockRepoClient,
|
|
Dlogger: &dl,
|
|
}
|
|
|
|
result := BinaryArtifacts(&req)
|
|
|
|
scut.ValidateTestReturn(t, tt.name, &tt.expected, &result, &dl)
|
|
|
|
ctrl.Finish()
|
|
})
|
|
}
|
|
}
|