Skip to content

Commit

Permalink
Fix flaky Test_FromDraconEnrichedIssuesRun (Fixes #105)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavlos Tzianos <[email protected]>
  • Loading branch information
Pavlos Tzianos committed Mar 11, 2024
1 parent 04b02fc commit 338e01b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 6 additions & 2 deletions pkg/sarif/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ type DraconIssueCollection struct {
Issues []*v1.Issue
}

// FromDraconEnrichedIssuesRun transforms a set of LaunchToolResponse to ONE sarif document with one run per launch tool response, by default it skips duplicates unless reportDuplicates is set to true.
// FromDraconEnrichedIssuesRun transforms a set of LaunchToolResponse to ONE sarif document with
// one run per launch tool response, by default it skips duplicates unless reportDuplicates is set
// to true.
func FromDraconEnrichedIssuesRun(responses []*v1.EnrichedLaunchToolResponse, reportDuplicates bool) (*sarif.Report, error) {
// if you are not ignoring duplicates use resultProvenance in each message to mark duplicates
// annotations become attachments in each findings with the description the json of the label
// annotations become attachments in each findings with the description the json of the label
sarifReport, err := sarif.New(sarif.Version210)
if err != nil {
return &sarif.Report{}, err
}

for _, enrichedResponse := range responses {
tool := sarif.NewSimpleTool(enrichedResponse.GetOriginalResults().GetToolName())
run := sarif.NewRun(*tool)
Expand All @@ -38,6 +41,7 @@ func FromDraconEnrichedIssuesRun(responses []*v1.EnrichedLaunchToolResponse, rep
var sarifResults []*sarif.Result

for _, issue := range enrichedResponse.Issues {
// TODO(#119): improve this to avoid O(n^2)
rule, err := run.GetRuleById(issue.RawIssue.Type)
if err != nil {
rule = run.AddRule(issue.RawIssue.Type)
Expand Down
16 changes: 14 additions & 2 deletions pkg/sarif/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package sarif

import (
"os"
"slices"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -232,8 +234,18 @@ func Test_FromDraconEnrichedIssuesRun(t *testing.T) {

report, err := FromDraconEnrichedIssuesRun(responses, false)
require.NoError(t, err)
require.NotNil(t, report)
require.EqualValues(t, report, expected)
require.Len(t, report.Runs, 1)
require.Len(t, report.Runs[0].Results, 1)
require.Len(t, report.Runs[0].Results[0].Attachments, len(expected.Runs[0].Results[0].Attachments))

slices.SortFunc(expected.Runs[0].Results[0].Attachments, func(a *sarif.Attachment, b *sarif.Attachment) int {
return strings.Compare(*(a.Description.Text), *(b.Description.Text))
})
slices.SortFunc(report.Runs[0].Results[0].Attachments, func(a *sarif.Attachment, b *sarif.Attachment) int {
return strings.Compare(*(a.Description.Text), *(b.Description.Text))
})

require.Equal(t, expected, report)
}

func Test_FromDraconRawIssuesRun(t *testing.T) {
Expand Down

0 comments on commit 338e01b

Please sign in to comment.