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

🐛 Fix nils #3750

Merged
merged 2 commits into from
Dec 28, 2023
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
5 changes: 5 additions & 0 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@

type jsonFloatScore float64

var errNoDoc = errors.New("doc is nil")

func (s jsonFloatScore) MarshalJSON() ([]byte, error) {
// Note: for integers, this will show as X.0.
return []byte(fmt.Sprintf("%.1f", s)), nil
Expand Down Expand Up @@ -147,6 +149,9 @@
if e != nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("GetCheck: %s: %v", checkResult.Name, e))
}
if doc == nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("GetCheck: %s: %v", checkResult.Name, errNoDoc))
}

Check warning on line 154 in pkg/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/json.go#L153-L154

Added lines #L153 - L154 were not covered by tests

tmpResult := jsonCheckResultV2{
Name: checkResult.Name,
Expand Down
7 changes: 6 additions & 1 deletion pkg/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ func computeCategory(checkName string, repos []string) (string, error) {
}
}

// createSARIFRuns takes a map of runs and returns a sorted slice of runs.
// It sorts the keys of the map, iterates over them, and appends the corresponding run to the result slice.
// If the run is nil, it is skipped.
func createSARIFRuns(runs map[string]*run) []run {
res := []run{}
// Sort keys.
Expand All @@ -561,7 +564,9 @@ func createSARIFRuns(runs map[string]*run) []run {

// Iterate over keys.
for _, k := range keys {
res = append(res, *runs[k])
if runs[k] != nil {
res = append(res, *runs[k])
}
}
return res
}
Expand Down
77 changes: 77 additions & 0 deletions pkg/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"os"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -852,3 +853,79 @@ func TestSARIFOutput(t *testing.T) {
})
}
}

func Test_createSARIFRuns(t *testing.T) {
t.Parallel()
type args struct {
runs map[string]*run
}
tests := []struct {
name string
args args
want []run
}{
{
name: "empty runs",
args: args{
runs: map[string]*run{},
},
want: []run{},
},
{
name: "nil runs are skipped",
args: args{
runs: map[string]*run{
"run1": nil,
"run2": {
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
want: []run{
{
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
{
name: "one run",
args: args{
runs: map[string]*run{
"run1": {
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
want: []run{
{
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := createSARIFRuns(tt.args.runs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("createSARIFRuns() = %v, want %v", got, tt.want)
}
})
}
}
Loading