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