Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add tests for probe output format #3841

Merged
merged 7 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pkg/json_probe_results.go → pkg/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ type JSONScorecardProbeResult struct {
Findings []finding.Finding `json:"findings"`
}

func (r *ScorecardResult) AsPJSON(writer io.Writer) error {
// ProbeResultOption provides configuration options for the ScorecardResult probe output format.
type ProbeResultOption struct {
// Indent is used to control the JSON indentation. For example, if you want to pretty print.
Indent string
}

// AsPJSON writes results as JSON for flat findings without checks.
// It accepts an optional argument to configure the output.
func (r *ScorecardResult) AsPJSON(writer io.Writer, o *ProbeResultOption) error {
encoder := json.NewEncoder(writer)
out := JSONScorecardProbeResult{
Repo: jsonRepoV2{
Expand All @@ -46,6 +54,10 @@ func (r *ScorecardResult) AsPJSON(writer io.Writer) error {
Findings: r.Findings,
}

if o != nil {
encoder.SetIndent("", o.Indent)
}

if err := encoder.Encode(out); err != nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("encoder.Encode: %v", err))
}
Expand Down
88 changes: 88 additions & 0 deletions pkg/probe_test.go
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)
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/scorecard_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func FormatResults(
case options.FormatFJSON:
err = results.AsFJSON(opts.ShowDetails, log.ParseLevel(opts.LogLevel), doc, output)
case options.FormatPJSON:
err = results.AsPJSON(output)
var opts *ProbeResultOption
err = results.AsPJSON(output, opts)
case options.FormatRaw:
err = results.AsRawJSON(output)
default:
Expand Down
27 changes: 27 additions & 0 deletions pkg/testdata/probe1.json
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
}
]
}
Loading