-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌱 Add tests for probe output format (#3841)
* rename probe format file Signed-off-by: Spencer Schrock <[email protected]> * add option parameter to probe result output function the ability to pretty print will help write a test function Signed-off-by: Spencer Schrock <[email protected]> * add test func Signed-off-by: Spencer Schrock <[email protected]> * add docstring Signed-off-by: Spencer Schrock <[email protected]> * clarify test. add another finding Signed-off-by: Spencer Schrock <[email protected]> * force add the test file Signed-off-by: Spencer Schrock <[email protected]> --------- Signed-off-by: Spencer Schrock <[email protected]>
- Loading branch information
1 parent
5a0356b
commit a762812
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2024 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 pkg | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/ossf/scorecard/v4/finding" | ||
) | ||
|
||
func Test_AsPJSON(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
expected string | ||
result ScorecardResult | ||
}{ | ||
{ | ||
name: "multiple findings displayed", | ||
expected: "./testdata/probe1.json", | ||
result: ScorecardResult{ | ||
Repo: RepoInfo{ | ||
Name: "foo", | ||
CommitSHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
}, | ||
Scorecard: ScorecardInfo{ | ||
Version: "1.2.3", | ||
CommitSHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | ||
}, | ||
Date: time.Date(2024, time.February, 1, 13, 48, 0, 0, time.UTC), | ||
Findings: []finding.Finding{ | ||
{ | ||
Probe: "check for X", | ||
Outcome: finding.OutcomePositive, | ||
Message: "found X", | ||
Location: &finding.Location{ | ||
Path: "some/path/to/file", | ||
Type: finding.FileTypeText, | ||
}, | ||
}, | ||
{ | ||
Probe: "check for Y", | ||
Outcome: finding.OutcomeNegative, | ||
Message: "did not find Y", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
// pretty print results so the test files are easier to read | ||
opt := &ProbeResultOption{Indent: " "} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
expected, err := os.ReadFile(tt.expected) | ||
if err != nil { | ||
t.Fatalf("cannot read expected results file: %v", err) | ||
} | ||
|
||
var result bytes.Buffer | ||
if err = tt.result.AsPJSON(&result, opt); err != nil { | ||
t.Fatalf("AsPJSON: %v", err) | ||
} | ||
|
||
if diff := cmp.Diff(expected, result.Bytes()); diff != "" { | ||
t.Errorf("results differ: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"date": "2024-02-01", | ||
"repo": { | ||
"name": "foo", | ||
"commit": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
}, | ||
"scorecard": { | ||
"version": "1.2.3", | ||
"commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" | ||
}, | ||
"findings": [ | ||
{ | ||
"location": { | ||
"path": "some/path/to/file", | ||
"type": 3 | ||
}, | ||
"probe": "check for X", | ||
"message": "found X", | ||
"outcome": 12 | ||
}, | ||
{ | ||
"probe": "check for Y", | ||
"message": "did not find Y", | ||
"outcome": 0 | ||
} | ||
] | ||
} |