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

     1  package printers
     2  
     3  import (
     4  	"bytes"
     5  	"go/token"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/vanstinator/golangci-lint/pkg/result"
    13  )
    14  
    15  func TestCheckstyle_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  	printer := NewCheckstyle(buf)
    48  
    49  	err := printer.Print(issues)
    50  	require.NoError(t, err)
    51  
    52  	//nolint:lll
    53  	expected := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<checkstyle version=\"5.0\">\n  <file name=\"path/to/filea.go\">\n    <error column=\"4\" line=\"10\" message=\"some issue\" severity=\"warning\" source=\"linter-a\"></error>\n  </file>\n  <file name=\"path/to/fileb.go\">\n    <error column=\"9\" line=\"300\" message=\"another issue\" severity=\"error\" source=\"linter-b\"></error>\n  </file>\n</checkstyle>\n"
    54  
    55  	assert.Equal(t, expected, strings.ReplaceAll(buf.String(), "\r", ""))
    56  }