Skip to content

Commit

Permalink
🌱 Unit tests dependency_update_tool
Browse files Browse the repository at this point in the history
Unit tests dependency_update_tool
#986

Signed-off-by: naveen <[email protected]>
  • Loading branch information
naveensrinivasan committed Jan 25, 2022
1 parent b6cba86 commit d4d81a0
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
180 changes: 180 additions & 0 deletions checks/evaluation/dependency_update_tool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright 2022 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 evaluation

import (
"testing"

"github.com/ossf/scorecard/v4/checker"
sce "github.com/ossf/scorecard/v4/errors"
scut "github.com/ossf/scorecard/v4/utests"
)

func TestDependencyUpdateTool(t *testing.T) {
t.Parallel()
//nolint
type args struct {
name string
dl checker.DetailLogger
r *checker.DependencyUpdateToolData
}
//nolint
tests := []struct {
name string
args args
want checker.CheckResult
err bool
expected scut.TestReturn
}{
{
name: "DependencyUpdateTool",
args: args{
name: "DependencyUpdateTool",
dl: &scut.TestDetailLogger{},
r: &checker.DependencyUpdateToolData{
Tools: []checker.Tool{
{
Name: "DependencyUpdateTool",
},
},
},
},
want: checker.CheckResult{
Score: -1,
},
err: false,
expected: scut.TestReturn{
Error: sce.ErrScorecardInternal,
Score: -1,
},
},
{
name: "empty tool list",
args: args{
name: "DependencyUpdateTool",
dl: &scut.TestDetailLogger{},
r: &checker.DependencyUpdateToolData{
Tools: []checker.Tool{},
},
},
want: checker.CheckResult{
Score: 0,
Error2: nil,
},
err: false,
expected: scut.TestReturn{
Score: 0,
NumberOfWarn: 2,
},
},
{
name: "Valid tool",
args: args{
name: "DependencyUpdateTool",
dl: &scut.TestDetailLogger{},
r: &checker.DependencyUpdateToolData{
Tools: []checker.Tool{
{
Name: "DependencyUpdateTool",
ConfigFiles: []checker.File{
{
Path: "/etc/dependency-update-tool.conf",
Snippet: `
[dependency-update-tool]
enabled = true
`,
Offset: 0,
Type: 0,
},
},
},
},
},
},
want: checker.CheckResult{
Score: 10,
Error2: nil,
},
expected: scut.TestReturn{
Error: nil,
Score: 10,
NumberOfInfo: 1,
},
err: false,
},
{
name: "more than one tool in the list",
args: args{
name: "DependencyUpdateTool",
dl: &scut.TestDetailLogger{},
r: &checker.DependencyUpdateToolData{
Tools: []checker.Tool{
{
Name: "DependencyUpdateTool",
},
{
Name: "DependencyUpdateTool",
},
},
},
},
want: checker.CheckResult{
Score: -1,
Error2: nil,
},
expected: scut.TestReturn{
Error: sce.ErrScorecardInternal,
Score: -1,
},
err: false,
},
{
name: "Nil r",
args: args{
name: "nil r",
dl: &scut.TestDetailLogger{},
},
want: checker.CheckResult{
Score: -1,
Error2: nil,
},
expected: scut.TestReturn{
Error: sce.ErrScorecardInternal,
Score: -1,
},
err: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

dl := scut.TestDetailLogger{}
got := DependencyUpdateTool(tt.args.name, &dl, tt.args.r)
if tt.want.Score != got.Score {
t.Errorf("DependencyUpdateTool() got Score = %v, want %v for %v", got.Score, tt.want.Score, tt.name)
}
if tt.err && got.Error2 == nil {
t.Errorf("DependencyUpdateTool() error = %v, want %v for %v", got.Error2, tt.want.Error2, tt.name)
return
}

if !scut.ValidateTestReturn(t, tt.name, &tt.expected, &got, &dl) {
t.Fatalf(tt.name)
}
})
}
}
1 change: 1 addition & 0 deletions utests/utlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func ValidateTestReturn(t *testing.T, name string, expected *TestReturn,
}
if !cmp.Equal(*expected, *actualTestReturn, cmp.Comparer(errCmp)) {
log.Println(name+":", cmp.Diff(*expected, *actualTestReturn))
t.Logf(name+":", cmp.Diff(*expected, *actualTestReturn))
return false
}
return true
Expand Down

0 comments on commit d4d81a0

Please sign in to comment.