|
| 1 | +// Copyright 2023 OpenSSF 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 format |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + |
| 24 | + "github.com/ossf/scorecard/v4/checker" |
| 25 | + "github.com/ossf/scorecard/v4/pkg" |
| 26 | +) |
| 27 | + |
| 28 | +func TestJSON(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + a, b pkg.ScorecardResult |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "repo commit SHA standardized", |
| 36 | + a: pkg.ScorecardResult{ |
| 37 | + Repo: pkg.RepoInfo{ |
| 38 | + Name: "github.com/foo/bar", |
| 39 | + CommitSHA: "commit a", |
| 40 | + }, |
| 41 | + }, |
| 42 | + b: pkg.ScorecardResult{ |
| 43 | + Repo: pkg.RepoInfo{ |
| 44 | + Name: "github.com/foo/bar", |
| 45 | + CommitSHA: "commit b", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "dates standardized", |
| 51 | + a: pkg.ScorecardResult{ |
| 52 | + Date: time.Now(), |
| 53 | + }, |
| 54 | + b: pkg.ScorecardResult{ |
| 55 | + Date: time.Now().AddDate(0, 0, -1), |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "scorecard info standardized", |
| 60 | + a: pkg.ScorecardResult{ |
| 61 | + Scorecard: pkg.ScorecardInfo{ |
| 62 | + Version: "version a", |
| 63 | + CommitSHA: "scorecard commit x", |
| 64 | + }, |
| 65 | + }, |
| 66 | + b: pkg.ScorecardResult{ |
| 67 | + Scorecard: pkg.ScorecardInfo{ |
| 68 | + Version: "version b", |
| 69 | + CommitSHA: "scorecard commit y", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "check order standardized", |
| 75 | + a: pkg.ScorecardResult{ |
| 76 | + Checks: []checker.CheckResult{ |
| 77 | + { |
| 78 | + Name: "Token-Permissions", |
| 79 | + Score: 10, |
| 80 | + }, |
| 81 | + { |
| 82 | + Name: "License", |
| 83 | + Score: 10, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + b: pkg.ScorecardResult{ |
| 88 | + Checks: []checker.CheckResult{ |
| 89 | + { |
| 90 | + Name: "License", |
| 91 | + Score: 10, |
| 92 | + }, |
| 93 | + { |
| 94 | + Name: "Token-Permissions", |
| 95 | + Score: 10, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "detail order standardized", |
| 102 | + a: pkg.ScorecardResult{ |
| 103 | + Checks: []checker.CheckResult{ |
| 104 | + { |
| 105 | + Name: "Token-Permissions", |
| 106 | + Score: 10, |
| 107 | + Details: []checker.CheckDetail{ |
| 108 | + { |
| 109 | + Msg: checker.LogMessage{ |
| 110 | + Text: "foo", |
| 111 | + }, |
| 112 | + Type: checker.DetailInfo, |
| 113 | + }, |
| 114 | + { |
| 115 | + Msg: checker.LogMessage{ |
| 116 | + Text: "bar", |
| 117 | + }, |
| 118 | + Type: checker.DetailWarn, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + b: pkg.ScorecardResult{ |
| 125 | + Checks: []checker.CheckResult{ |
| 126 | + { |
| 127 | + Name: "Token-Permissions", |
| 128 | + Score: 10, |
| 129 | + Details: []checker.CheckDetail{ |
| 130 | + { |
| 131 | + Msg: checker.LogMessage{ |
| 132 | + Text: "bar", |
| 133 | + }, |
| 134 | + Type: checker.DetailWarn, |
| 135 | + }, |
| 136 | + { |
| 137 | + Msg: checker.LogMessage{ |
| 138 | + Text: "foo", |
| 139 | + }, |
| 140 | + Type: checker.DetailInfo, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + } |
| 148 | + for _, tt := range tests { |
| 149 | + tt := tt |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + t.Parallel() |
| 152 | + var bufA, bufB bytes.Buffer |
| 153 | + err := JSON(&tt.a, &bufA) |
| 154 | + if err != nil { |
| 155 | + t.Errorf("unexpected error: %v", err) |
| 156 | + } |
| 157 | + err = JSON(&tt.b, &bufB) |
| 158 | + if err != nil { |
| 159 | + t.Errorf("unexpected error: %v", err) |
| 160 | + } |
| 161 | + if bufA.String() != bufB.String() { |
| 162 | + t.Errorf("outputs not identical: %s", cmp.Diff(bufA.String(), bufB.String())) |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func Test_normalize_nil_safe(t *testing.T) { |
| 169 | + var x, y *pkg.ScorecardResult |
| 170 | + normalize(x) |
| 171 | + normalize(y) |
| 172 | + if !cmp.Equal(x, y) { |
| 173 | + t.Errorf("normalized results differ: %v", cmp.Diff(x, y)) |
| 174 | + } |
| 175 | +} |
0 commit comments