Skip to content

Commit

Permalink
🌱 Unit tests for checker result and request (#2844)
Browse files Browse the repository at this point in the history
Included tests for checker result and request

Signed-off-by: naveensrinivasan <[email protected]>
  • Loading branch information
naveensrinivasan authored Apr 10, 2023
1 parent 2f0e8d9 commit 964bbd9
Show file tree
Hide file tree
Showing 4 changed files with 606 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ issues:
# Default: 3
max-same-issues: 0
new-from-rev: ""
exclude-rules:
- path: '(.+)_test\.go'
linters:
- funlen
- goconst
- gocyclo
skip-files:
- cron/data/request.pb.go # autogenerated
linters:
Expand Down
122 changes: 122 additions & 0 deletions checker/check_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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 (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestListUnsupported(t *testing.T) {
t.Parallel()
type args struct {
required []RequestType
supported []RequestType
}
tests := []struct {
name string
args args
want []RequestType
}{
{
name: "empty",
args: args{
required: []RequestType{},
supported: []RequestType{},
},
want: []RequestType{FileBased},
},
{
name: "empty required",
args: args{
required: []RequestType{},
supported: []RequestType{FileBased},
},
want: []RequestType{},
},
{
name: "supported",
args: args{
required: []RequestType{FileBased},
supported: []RequestType{FileBased},
},
want: []RequestType{FileBased},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := ListUnsupported(tt.args.required, tt.args.supported); cmp.Equal(got, tt.want) {
t.Errorf("ListUnsupported() = %v, want %v", got, cmp.Diff(got, tt.want))
}
})
}
}

func Test_contains(t *testing.T) {
t.Parallel()
type args struct {
in []RequestType
exists RequestType
}
tests := []struct {
name string
args args
want bool
}{
{
name: "empty",
args: args{
in: []RequestType{},
exists: FileBased,
},
want: false,
},
{
name: "empty exists",
args: args{
in: []RequestType{FileBased},
exists: FileBased,
},
want: true,
},
{
name: "empty exists",
args: args{
in: []RequestType{FileBased},
exists: CommitBased,
},
want: false,
},
{
name: "empty exists",
args: args{
in: []RequestType{FileBased, CommitBased},
exists: CommitBased,
},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := contains(tt.args.in, tt.args.exists); got != tt.want {
t.Errorf("contains() = %v, want %v", got, tt.want)
}
})
}
}
22 changes: 8 additions & 14 deletions checker/check_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
const (
// DetailInfo is info-level log.
DetailInfo DetailType = iota
// DetailWarn is warn log.
// DetailWarn is warned log.
DetailWarn
// DetailDebug is debug log.
DetailDebug
Expand Down Expand Up @@ -75,7 +75,7 @@ type CheckDetail struct {
//
//nolint:govet
type LogMessage struct {
// Structured resuts.
// Structured results.
Finding *finding.Finding

// Non-structured results.
Expand Down Expand Up @@ -127,13 +127,11 @@ func NormalizeReason(reason string, score int) string {
}

// CreateResultWithScore is used when
// the check runs without runtime errors and we want to assign a
// the check runs without runtime errors, and we want to assign a
// specific score.
func CreateResultWithScore(name, reason string, score int) CheckResult {
return CheckResult{
Name: name,
// Old structure.
// New structure.
Name: name,
Version: 2,
Error: nil,
Score: score,
Expand All @@ -144,8 +142,8 @@ func CreateResultWithScore(name, reason string, score int) CheckResult {
// CreateProportionalScoreResult is used when
// the check runs without runtime errors and we assign a
// proportional score. This may be used if a check contains
// multiple tests and we want to assign a score proportional
// the the number of tests that succeeded.
// multiple tests, and we want to assign a score proportional
// the number of tests that succeeded.
func CreateProportionalScoreResult(name, reason string, b, t int) CheckResult {
score := CreateProportionalScore(b, t)
return CheckResult{
Expand Down Expand Up @@ -178,9 +176,7 @@ func CreateMinScoreResult(name, reason string) CheckResult {
// have enough evidence to set a score.
func CreateInconclusiveResult(name, reason string) CheckResult {
return CheckResult{
Name: name,
// Old structure.
// New structure.
Name: name,
Version: 2,
Score: InconclusiveResultScore,
Reason: reason,
Expand All @@ -190,9 +186,7 @@ func CreateInconclusiveResult(name, reason string) CheckResult {
// CreateRuntimeErrorResult is used when the check fails to run because of a runtime error.
func CreateRuntimeErrorResult(name string, e error) CheckResult {
return CheckResult{
Name: name,
// Old structure.
// New structure.
Name: name,
Version: 2,
Error: e,
Score: InconclusiveResultScore,
Expand Down
Loading

0 comments on commit 964bbd9

Please sign in to comment.