Skip to content

Commit 2805ac5

Browse files
committed
test: validate sarif output with json schema
1 parent e95ac1b commit 2805ac5

File tree

4 files changed

+2913
-0
lines changed

4 files changed

+2913
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ require (
9090
github.com/ryanrolds/sqlclosecheck v0.5.1
9191
github.com/sanposhiho/wastedassign/v2 v2.0.7
9292
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
93+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
9394
github.com/sashamelentyev/interfacebloat v1.1.0
9495
github.com/sashamelentyev/usestdlibvars v1.27.0
9596
github.com/securego/gosec/v2 v2.20.1-0.20240525090044-5f0084eb01a9

go.sum

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/printers/sarif_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,38 @@ package printers
33
import (
44
"bytes"
55
"go/token"
6+
"io"
67
"testing"
78

9+
"github.com/santhosh-tekuri/jsonschema/v6"
810
"github.com/stretchr/testify/assert"
911
"github.com/stretchr/testify/require"
1012

1113
"github.com/golangci/golangci-lint/pkg/result"
1214
)
1315

16+
var sarifSchema *jsonschema.Schema
17+
18+
func RequireSarifSchema(t *testing.T) *jsonschema.Schema {
19+
t.Helper()
20+
if sarifSchema == nil {
21+
c := jsonschema.NewCompiler()
22+
var err error
23+
sarifSchema, err = c.Compile("./testdata/sarif-2.1.0-rtm.6.json")
24+
require.NoError(t, err)
25+
}
26+
return sarifSchema
27+
}
28+
29+
func ValidateSarifSchema(t *testing.T, reader io.Reader) {
30+
t.Helper()
31+
inst, err := jsonschema.UnmarshalJSON(reader)
32+
require.NoError(t, err)
33+
34+
err = RequireSarifSchema(t).Validate(inst)
35+
require.NoError(t, err)
36+
}
37+
1438
func TestSarif_Print(t *testing.T) {
1539
issues := []result.Issue{
1640
{
@@ -74,6 +98,7 @@ func TestSarif_Print(t *testing.T) {
7498
`
7599

76100
assert.Equal(t, expected, buf.String())
101+
ValidateSarifSchema(t, buf)
77102
}
78103

79104
func TestSarif_Print_empty(t *testing.T) {
@@ -88,4 +113,5 @@ func TestSarif_Print_empty(t *testing.T) {
88113
`
89114

90115
assert.Equal(t, expected, buf.String())
116+
ValidateSarifSchema(t, buf)
91117
}

0 commit comments

Comments
 (0)