Skip to content

test: validate sarif output with json schema #4875

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ require (
github.com/ryanrolds/sqlclosecheck v0.5.1
github.com/sanposhiho/wastedassign/v2 v2.0.7
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
github.com/sashamelentyev/interfacebloat v1.1.0
github.com/sashamelentyev/usestdlibvars v1.27.0
github.com/securego/gosec/v2 v2.20.1-0.20240525090044-5f0084eb01a9
Expand Down
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions pkg/printers/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@ package printers
import (
"bytes"
"go/token"
"io"
"testing"

"github.com/santhosh-tekuri/jsonschema/v6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/result"
)

var sarifSchema *jsonschema.Schema

func RequireSarifSchema(t *testing.T) *jsonschema.Schema {
t.Helper()
if sarifSchema == nil {
c := jsonschema.NewCompiler()
var err error
sarifSchema, err = c.Compile("./testdata/sarif-2.1.0-rtm.6.json")
require.NoError(t, err)
}
return sarifSchema
}

func ValidateSarifSchema(t *testing.T, reader io.Reader) {
t.Helper()
inst, err := jsonschema.UnmarshalJSON(reader)
require.NoError(t, err)

err = RequireSarifSchema(t).Validate(inst)
require.NoError(t, err)
}

func TestSarif_Print(t *testing.T) {
issues := []result.Issue{
{
Expand Down Expand Up @@ -74,6 +98,7 @@ func TestSarif_Print(t *testing.T) {
`

assert.Equal(t, expected, buf.String())
ValidateSarifSchema(t, buf)
}

func TestSarif_Print_empty(t *testing.T) {
Expand All @@ -88,4 +113,5 @@ func TestSarif_Print_empty(t *testing.T) {
`

assert.Equal(t, expected, buf.String())
ValidateSarifSchema(t, buf)
}
Loading
Loading