github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/printers/junitxml_test.go (about) 1 //nolint:dupl 2 package printers 3 4 import ( 5 "bytes" 6 "context" 7 "go/token" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/golangci/golangci-lint/pkg/result" 14 ) 15 16 func TestJunitXML_Print(t *testing.T) { 17 issues := []result.Issue{ 18 { 19 FromLinter: "linter-a", 20 Severity: "warning", 21 Text: "some issue", 22 Pos: token.Position{ 23 Filename: "path/to/filea.go", 24 Offset: 2, 25 Line: 10, 26 Column: 4, 27 }, 28 }, 29 { 30 FromLinter: "linter-b", 31 Severity: "error", 32 Text: "another issue", 33 SourceLines: []string{ 34 "func foo() {", 35 "\tfmt.Println(\"bar\")", 36 "}", 37 }, 38 Pos: token.Position{ 39 Filename: "path/to/fileb.go", 40 Offset: 5, 41 Line: 300, 42 Column: 9, 43 }, 44 }, 45 } 46 47 buf := new(bytes.Buffer) 48 printer := NewJunitXML(buf) 49 50 err := printer.Print(context.Background(), issues) 51 require.NoError(t, err) 52 53 expected := `<testsuites> 54 <testsuite name="path/to/filea.go" tests="1" errors="0" failures="1"> 55 <testcase name="linter-a" classname="path/to/filea.go:10:4"> 56 <failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue 57 Category: linter-a 58 File: path/to/filea.go 59 Line: 10 60 Details: ]]></failure> 61 </testcase> 62 </testsuite> 63 <testsuite name="path/to/fileb.go" tests="1" errors="0" failures="1"> 64 <testcase name="linter-b" classname="path/to/fileb.go:300:9"> 65 <failure message="path/to/fileb.go:300:9: another issue" type="error"><![CDATA[error: another issue 66 Category: linter-b 67 File: path/to/fileb.go 68 Line: 300 69 Details: func foo() { 70 fmt.Println("bar") 71 }]]></failure> 72 </testcase> 73 </testsuite> 74 </testsuites>` 75 76 assert.Equal(t, expected, buf.String()) 77 }