fix: init empty result slice for SARIF printer (#4758)

Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
This commit is contained in:
Zxilly 2024-05-28 00:04:54 +08:00 committed by GitHub
parent 6456cbda82
commit 817316680f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 0 deletions

View File

@ -70,6 +70,7 @@ func NewSarif(w io.Writer) *Sarif {
func (p Sarif) Print(issues []result.Issue) error {
run := sarifRun{}
run.Tool.Driver.Name = "golangci-lint"
run.Results = make([]sarifResult, 0)
for i := range issues {
issue := issues[i]

View File

@ -65,3 +65,17 @@ func TestSarif_Print(t *testing.T) {
assert.Equal(t, expected, buf.String())
}
func TestSarif_Print_empty(t *testing.T) {
buf := new(bytes.Buffer)
printer := NewSarif(buf)
err := printer.Print(nil)
require.NoError(t, err)
expected := `{"version":"2.1.0","$schema":"https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json","runs":[{"tool":{"driver":{"name":"golangci-lint"}},"results":[]}]}
`
assert.Equal(t, expected, buf.String())
}