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

     1  //nolint:dupl
     2  package printers
     3  
     4  import (
     5  	"bytes"
     6  	"go/token"
     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 TestJunitXML_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 := NewJunitXML(buf)
    48  
    49  	err := printer.Print(issues)
    50  	require.NoError(t, err)
    51  
    52  	expected := `<testsuites>
    53    <testsuite name="path/to/filea.go" tests="1" errors="0" failures="1">
    54      <testcase name="linter-a" classname="path/to/filea.go:10:4">
    55        <failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue
    56  Category: linter-a
    57  File: path/to/filea.go
    58  Line: 10
    59  Details: ]]></failure>
    60      </testcase>
    61    </testsuite>
    62    <testsuite name="path/to/fileb.go" tests="1" errors="0" failures="1">
    63      <testcase name="linter-b" classname="path/to/fileb.go:300:9">
    64        <failure message="path/to/fileb.go:300:9: another issue" type="error"><![CDATA[error: another issue
    65  Category: linter-b
    66  File: path/to/fileb.go
    67  Line: 300
    68  Details: func foo() {
    69  	fmt.Println("bar")
    70  }]]></failure>
    71      </testcase>
    72    </testsuite>
    73  </testsuites>`
    74  
    75  	assert.Equal(t, expected, buf.String())
    76  }