github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/printers/json_test.go (about)

     1  package printers
     2  
     3  import (
     4  	"bytes"
     5  	"go/token"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/vanstinator/golangci-lint/pkg/result"
    12  )
    13  
    14  func TestJSON_Print(t *testing.T) {
    15  	issues := []result.Issue{
    16  		{
    17  			FromLinter: "linter-a",
    18  			Severity:   "warning",
    19  			Text:       "some issue",
    20  			Pos: token.Position{
    21  				Filename: "path/to/filea.go",
    22  				Offset:   2,
    23  				Line:     10,
    24  				Column:   4,
    25  			},
    26  		},
    27  		{
    28  			FromLinter: "linter-b",
    29  			Severity:   "error",
    30  			Text:       "another issue",
    31  			SourceLines: []string{
    32  				"func foo() {",
    33  				"\tfmt.Println(\"bar\")",
    34  				"}",
    35  			},
    36  			Pos: token.Position{
    37  				Filename: "path/to/fileb.go",
    38  				Offset:   5,
    39  				Line:     300,
    40  				Column:   9,
    41  			},
    42  		},
    43  	}
    44  
    45  	buf := new(bytes.Buffer)
    46  
    47  	printer := NewJSON(nil, buf)
    48  
    49  	err := printer.Print(issues)
    50  	require.NoError(t, err)
    51  
    52  	//nolint:lll
    53  	expected := `{"Issues":[{"FromLinter":"linter-a","Text":"some issue","Severity":"warning","SourceLines":null,"Replacement":null,"Pos":{"Filename":"path/to/filea.go","Offset":2,"Line":10,"Column":4},"ExpectNoLint":false,"ExpectedNoLintLinter":""},{"FromLinter":"linter-b","Text":"another issue","Severity":"error","SourceLines":["func foo() {","\tfmt.Println(\"bar\")","}"],"Replacement":null,"Pos":{"Filename":"path/to/fileb.go","Offset":5,"Line":300,"Column":9},"ExpectNoLint":false,"ExpectedNoLintLinter":""}],"Report":null}
    54  `
    55  
    56  	assert.Equal(t, expected, buf.String())
    57  }