github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/printers/codeclimate_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 TestCodeClimate_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  			FromLinter: "linter-c",
    45  			Text:       "issue c",
    46  			SourceLines: []string{
    47  				"func foo() {",
    48  				"\tfmt.Println(\"ccc\")",
    49  				"}",
    50  			},
    51  			Pos: token.Position{
    52  				Filename: "path/to/filec.go",
    53  				Offset:   6,
    54  				Line:     200,
    55  				Column:   2,
    56  			},
    57  		},
    58  	}
    59  
    60  	buf := new(bytes.Buffer)
    61  	printer := NewCodeClimate(buf)
    62  
    63  	err := printer.Print(issues)
    64  	require.NoError(t, err)
    65  
    66  	//nolint:lll
    67  	expected := `[{"description":"linter-a: some issue","severity":"warning","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","severity":"error","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: issue c","severity":"critical","fingerprint":"BEE6E9FBB6BFA4B7DB9FB036697FB036","location":{"path":"path/to/filec.go","lines":{"begin":200}}}]
    68  `
    69  
    70  	assert.Equal(t, expected, buf.String())
    71  }