scorecard/checks/license_test.go
S. E. Elder a788a3830d
🌱 Update Binary-Artifacts and License checks (#4079)
* 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>
2024-05-03 14:50:50 -07:00

112 lines
3.0 KiB
Go

// Copyright 2020 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"
"fmt"
"io"
"os"
"testing"
"github.com/golang/mock/gomock"
"github.com/ossf/scorecard/v5/checker"
clients "github.com/ossf/scorecard/v5/clients"
mockrepo "github.com/ossf/scorecard/v5/clients/mockclients"
scut "github.com/ossf/scorecard/v5/utests"
)
func TestLicenseFileSubdirectory(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputFolder string
err error
expected scut.TestReturn
}{
{
name: "With LICENSE",
inputFolder: "testdata/licensedir/withlicense",
expected: scut.TestReturn{
Error: nil,
Score: 9, // Does not have approved format
NumberOfInfo: 1,
NumberOfWarn: 1,
},
err: nil,
},
{
name: "Without LICENSE",
inputFolder: "testdata/licensedir/withoutlicense",
expected: scut.TestReturn{
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 1,
NumberOfInfo: 0,
NumberOfDebug: 1,
},
err: nil,
},
}
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()
// 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()
dl := scut.TestDetailLogger{}
req := checker.CheckRequest{
Ctx: ctx,
RepoClient: mockRepoClient,
Dlogger: &dl,
}
res := License(&req)
scut.ValidateTestReturn(t, tt.name, &tt.expected, &res, &dl)
ctrl.Finish()
})
}
}