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

🌱 Unit Tests for checker/client #2851

Merged
merged 2 commits into from
Apr 13, 2023
Merged
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
126 changes: 126 additions & 0 deletions checker/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2023 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 checker

import (
"context"
"testing"

"github.com/ossf/scorecard/v4/log"
)

// nolint:paralleltest
// because we are using t.Setenv.
func TestGetClients(t *testing.T) { //nolint:gocognit
type args struct { //nolint:govet
ctx context.Context
repoURI string
localURI string
logger *log.Logger
}
tests := []struct { //nolint:govet
name string
args args
shouldOSSFuzzBeNil bool
shouldRepoClientBeNil bool
shouldVulnClientBeNil bool
shouldRepoBeNil bool
shouldCIIBeNil bool
wantErr bool
experimental bool
}{
{
name: "localURI is not empty",
args: args{
ctx: context.Background(),
repoURI: "",
localURI: "foo",
},
shouldOSSFuzzBeNil: false,
shouldRepoClientBeNil: false,
shouldVulnClientBeNil: false,
shouldRepoBeNil: true,
wantErr: true,
},
{
name: "repoURI is not empty",
args: args{
ctx: context.Background(),
repoURI: "foo",
localURI: "",
},
shouldOSSFuzzBeNil: false,
shouldRepoClientBeNil: false,
shouldVulnClientBeNil: false,
shouldRepoBeNil: true,
wantErr: true,
},
{
name: "repoURI is gitlab which is not supported",
args: args{
ctx: context.Background(),
repoURI: "https://gitlab.com/ossf/scorecard",
localURI: "",
},
shouldOSSFuzzBeNil: false,
shouldRepoClientBeNil: false,
shouldVulnClientBeNil: false,
shouldRepoBeNil: true,
wantErr: true,
},
{
name: "repoURI is gitlab and experimental is true",
args: args{
ctx: context.Background(),
repoURI: "https://gitlab.com/ossf/scorecard",
localURI: "",
},
shouldOSSFuzzBeNil: false,
shouldRepoBeNil: false,
shouldRepoClientBeNil: false,
shouldVulnClientBeNil: false,
shouldCIIBeNil: false,
wantErr: false,
experimental: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if tt.experimental {
t.Setenv("SCORECARD_EXPERIMENTAL", "true")
}
got, repoClient, ossFuzzClient, ciiClient, vulnsClient, err := GetClients(tt.args.ctx, tt.args.repoURI, tt.args.localURI, tt.args.logger) //nolint:lll
if (err != nil) != tt.wantErr {
t.Fatalf("GetClients() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.shouldRepoBeNil != (got == nil) {
t.Errorf("GetClients() got = %v", got)
}
if repoClient != nil && tt.shouldRepoClientBeNil {
t.Errorf("GetClients() repoClient = %v ", repoClient)
}
if ossFuzzClient != nil && tt.shouldOSSFuzzBeNil {
t.Errorf("GetClients() ossFuzzClient = %v ", ossFuzzClient)
}
if ciiClient != nil && tt.shouldCIIBeNil {
t.Errorf("GetClients() ciiClient = %v", ciiClient)
}
if vulnsClient != nil && tt.shouldVulnClientBeNil {
t.Errorf("GetClients() vulnsClient = %v", vulnsClient)
}
})
}
}