2021-12-03 20:28:27 +03:00
|
|
|
// Copyright 2020 Security 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"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
|
2022-01-12 22:49:01 +03:00
|
|
|
"github.com/ossf/scorecard/v4/checker"
|
2022-02-15 01:24:35 +03:00
|
|
|
"github.com/ossf/scorecard/v4/clients"
|
2022-01-12 22:49:01 +03:00
|
|
|
"github.com/ossf/scorecard/v4/clients/localdir"
|
2022-01-21 02:57:39 +03:00
|
|
|
"github.com/ossf/scorecard/v4/log"
|
2022-01-12 22:49:01 +03:00
|
|
|
scut "github.com/ossf/scorecard/v4/utests"
|
2021-12-03 20:28:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLicenseFileSubdirectory(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
inputFolder string
|
|
|
|
err error
|
|
|
|
expected scut.TestReturn
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "With LICENSE",
|
2022-02-01 00:41:42 +03:00
|
|
|
inputFolder: "testdata/licensedir/withlicense",
|
2021-12-03 20:28:27 +03:00
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfInfo: 1,
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Without LICENSE",
|
2022-02-01 00:41:42 +03:00
|
|
|
inputFolder: "testdata/licensedir/withoutlicense",
|
2021-12-03 20:28:27 +03:00
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MinResultScore,
|
|
|
|
},
|
|
|
|
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()
|
|
|
|
|
2022-01-25 20:17:46 +03:00
|
|
|
logger := log.NewLogger(log.DebugLevel)
|
2021-12-03 20:28:27 +03:00
|
|
|
|
2022-02-01 00:41:42 +03:00
|
|
|
// TODO: Use gMock instead of Localdir here.
|
2021-12-03 20:28:27 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client := localdir.CreateLocalDirClient(ctx, logger)
|
2022-02-15 01:24:35 +03:00
|
|
|
if err := client.InitRepo(repo, clients.HeadSHA); err != nil {
|
2021-12-03 20:28:27 +03:00
|
|
|
t.Errorf("InitRepo: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dl := scut.TestDetailLogger{}
|
|
|
|
|
|
|
|
req := checker.CheckRequest{
|
|
|
|
Ctx: ctx,
|
|
|
|
RepoClient: client,
|
|
|
|
Dlogger: &dl,
|
|
|
|
}
|
|
|
|
|
2022-04-14 04:20:05 +03:00
|
|
|
res := License(&req)
|
2021-12-03 20:28:27 +03:00
|
|
|
|
|
|
|
if !scut.ValidateTestReturn(t, tt.name, &tt.expected, &res, &dl) {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrl.Finish()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|