From d4d81a01df17365ffe6a11a69f074d42d4643d7a Mon Sep 17 00:00:00 2001 From: naveen <172697+naveensrinivasan@users.noreply.github.com> Date: Sun, 23 Jan 2022 18:44:36 +0000 Subject: [PATCH] :seedling: Unit tests dependency_update_tool Unit tests dependency_update_tool https://github.com/ossf/scorecard/issues/986 Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com> --- .../evaluation/dependency_update_tool_test.go | 180 ++++++++++++++++++ utests/utlib.go | 1 + 2 files changed, 181 insertions(+) create mode 100644 checks/evaluation/dependency_update_tool_test.go diff --git a/checks/evaluation/dependency_update_tool_test.go b/checks/evaluation/dependency_update_tool_test.go new file mode 100644 index 00000000000..e39d337bd71 --- /dev/null +++ b/checks/evaluation/dependency_update_tool_test.go @@ -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) + } + }) + } +} diff --git a/utests/utlib.go b/utests/utlib.go index 51f093728f5..8d39874e563 100644 --- a/utests/utlib.go +++ b/utests/utlib.go @@ -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