Skip to content

Commit f7b329e

Browse files
✨ Unit test for all_checks
Addresses #435 Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
1 parent 7710369 commit f7b329e

21 files changed

+163
-28
lines changed

checks/all_checks.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,20 @@
1515
// Package checks defines all Scorecard checks.
1616
package checks
1717

18-
import "github.com/ossf/scorecard/v4/checker"
18+
import (
19+
"github.com/ossf/scorecard/v4/checker"
20+
)
1921

2022
// AllChecks is the list of all security checks that will be run.
2123
var AllChecks = checker.CheckNameToFnMap{}
2224

23-
func registerCheck(name string, fn checker.CheckFn) {
25+
func registerCheck(name string, fn checker.CheckFn) error {
26+
if name == "" {
27+
return errInternalNameCannotBeEmpty
28+
}
29+
if fn == nil {
30+
return errInternalCheckFuncCannotBeNil
31+
}
2432
AllChecks[name] = fn
33+
return nil
2534
}

checks/all_checks_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2020 Security Scorecard Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package checks defines all Scorecard checks.
16+
package checks
17+
18+
import (
19+
"testing"
20+
21+
"github.com/ossf/scorecard/v4/checker"
22+
)
23+
24+
func Test_registerCheck(t *testing.T) {
25+
t.Parallel()
26+
//nolint
27+
type args struct {
28+
name string
29+
fn checker.CheckFn
30+
}
31+
//nolint
32+
tests := []struct {
33+
name string
34+
args args
35+
wanterr bool
36+
}{
37+
{
38+
name: "registerCheck",
39+
args: args{
40+
name: "test",
41+
fn: func(x *checker.CheckRequest) checker.CheckResult { return checker.CheckResult{} },
42+
},
43+
wanterr: false,
44+
},
45+
{
46+
name: "empty func",
47+
args: args{
48+
name: "test",
49+
},
50+
wanterr: true,
51+
},
52+
{
53+
name: "empty name",
54+
args: args{
55+
name: "",
56+
fn: func(x *checker.CheckRequest) checker.CheckResult { return checker.CheckResult{} },
57+
},
58+
wanterr: true,
59+
},
60+
}
61+
for _, tt := range tests {
62+
tt := tt
63+
t.Run(tt.name, func(t *testing.T) {
64+
t.Parallel()
65+
if err := registerCheck(tt.args.name, tt.args.fn); (err != nil) != tt.wanterr {
66+
t.Errorf("registerCheck() error = %v, wantErr %v", err, tt.wanterr)
67+
}
68+
})
69+
}
70+
}

checks/binary_artifact.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const CheckBinaryArtifacts string = "Binary-Artifacts"
2626

2727
//nolint
2828
func init() {
29-
registerCheck(CheckBinaryArtifacts, BinaryArtifacts)
29+
if err := registerCheck(CheckBinaryArtifacts, BinaryArtifacts); err != nil {
30+
// this should never happen
31+
panic(err)
32+
}
3033
}
3134

3235
// BinaryArtifacts will check the repository contains binary artifacts.

checks/branch_protection.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const (
2828

2929
//nolint:gochecknoinits
3030
func init() {
31-
registerCheck(CheckBranchProtection, BranchProtection)
31+
if err := registerCheck(CheckBranchProtection, BranchProtection); err != nil {
32+
// this should never happen
33+
panic(err)
34+
}
3235
}
3336

3437
// BranchProtection runs the Branch-Protection check.

checks/ci_tests.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ const (
3131

3232
//nolint:gochecknoinits
3333
func init() {
34-
registerCheck(CheckCITests, CITests)
34+
if err := registerCheck(CheckCITests, CITests); err != nil {
35+
// this should never happen
36+
panic(err)
37+
}
3538
}
3639

3740
// CITests runs CI-Tests check.

checks/cii_best_practices.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ const (
3232

3333
//nolint:gochecknoinits
3434
func init() {
35-
registerCheck(CheckCIIBestPractices, CIIBestPractices)
35+
if err := registerCheck(CheckCIIBestPractices, CIIBestPractices); err != nil {
36+
// this should never happen
37+
panic(err)
38+
}
3639
}
3740

3841
// CIIBestPractices runs CII-Best-Practices check.

checks/code_review.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ const CheckCodeReview = "Code-Review"
2727

2828
//nolint:gochecknoinits
2929
func init() {
30-
registerCheck(CheckCodeReview, DoesCodeReview)
30+
if err := registerCheck(CheckCodeReview, DoesCodeReview); err != nil {
31+
// this should never happen
32+
panic(err)
33+
}
3134
}
3235

3336
// DoesCodeReview attempts to determine whether a project requires review before code gets merged.

checks/contributors.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ const (
3131

3232
//nolint:gochecknoinits
3333
func init() {
34-
registerCheck(CheckContributors, Contributors)
34+
if err := registerCheck(CheckContributors, Contributors); err != nil {
35+
// this should never happen
36+
panic(err)
37+
}
3538
}
3639

3740
// Contributors run Contributors check.

checks/dangerous_workflow.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ func containsUntrustedContextPattern(variable string) bool {
5959

6060
//nolint:gochecknoinits
6161
func init() {
62-
registerCheck(CheckDangerousWorkflow, DangerousWorkflow)
62+
if err := registerCheck(CheckDangerousWorkflow, DangerousWorkflow); err != nil {
63+
// this should never happen
64+
panic(err)
65+
}
6366
}
6467

6568
// Holds stateful data to pass thru callbacks.

checks/dependency_update_tool.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const CheckDependencyUpdateTool = "Dependency-Update-Tool"
2626

2727
//nolint
2828
func init() {
29-
registerCheck(CheckDependencyUpdateTool, DependencyUpdateTool)
29+
if err := registerCheck(CheckDependencyUpdateTool, DependencyUpdateTool); err != nil {
30+
// this should never happen
31+
panic(err)
32+
}
3033
}
3134

3235
// DependencyUpdateTool checks if the repository uses a dependency update tool.

0 commit comments

Comments
 (0)