Skip to content

Commit 5c86bfc

Browse files
soniahjirfag
authored andcommitted
junit-xml output
1 parent 7f91ce8 commit 5c86bfc

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ Usage:
460460
golangci-lint run [flags]
461461
462462
Flags:
463-
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate (default "colored-line-number")
463+
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml (default "colored-line-number")
464464
--print-issued-lines Print lines of code with issue (default true)
465465
--print-linter-name Print linter name in issue line (default true)
466466
--issues-exit-code int Exit code when issues were found (default 1)

pkg/commands/run.go

+2
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) {
372372
p = printers.NewCheckstyle()
373373
case config.OutFormatCodeClimate:
374374
p = printers.NewCodeClimate()
375+
case config.OutFormatJunitXML:
376+
p = printers.NewJunitXML()
375377
default:
376378
return nil, fmt.Errorf("unknown output format %s", format)
377379
}

pkg/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
OutFormatTab = "tab"
1515
OutFormatCheckstyle = "checkstyle"
1616
OutFormatCodeClimate = "code-climate"
17+
OutFormatJunitXML = "junit-xml"
1718
)
1819

1920
var OutFormats = []string{
@@ -23,6 +24,7 @@ var OutFormats = []string{
2324
OutFormatTab,
2425
OutFormatCheckstyle,
2526
OutFormatCodeClimate,
27+
OutFormatJunitXML,
2628
}
2729

2830
type ExcludePattern struct {

pkg/printers/junitxml.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package printers
2+
3+
import (
4+
"context"
5+
"encoding/xml"
6+
"strings"
7+
8+
"github.com/golangci/golangci-lint/pkg/logutils"
9+
"github.com/golangci/golangci-lint/pkg/result"
10+
)
11+
12+
type testSuitesXML struct {
13+
XMLName xml.Name `xml:"testsuites"`
14+
TestSuites []testSuiteXML
15+
}
16+
17+
type testSuiteXML struct {
18+
XMLName xml.Name `xml:"testsuite"`
19+
Suite string `xml:"name,attr"`
20+
TestCases []testCaseXML `xml:"testcase"`
21+
}
22+
23+
type testCaseXML struct {
24+
Name string `xml:"name,attr"`
25+
ClassName string `xml:"classname,attr"`
26+
Status string `xml:"status,attr"`
27+
}
28+
29+
type JunitXML struct {
30+
}
31+
32+
func NewJunitXML() *JunitXML {
33+
return &JunitXML{}
34+
}
35+
36+
func (JunitXML) Print(ctx context.Context, issues <-chan result.Issue) error {
37+
suites := make(map[string]testSuiteXML) // use a map to group-by "FromLinter"
38+
39+
for i := range issues {
40+
fromLinter := i.FromLinter
41+
testSuite := suites[fromLinter]
42+
testSuite.Suite = fromLinter
43+
44+
var source string
45+
for _, line := range i.SourceLines {
46+
source += strings.TrimSpace(line) + "; "
47+
}
48+
tc := testCaseXML{Name: i.Text,
49+
ClassName: i.Pos.String(),
50+
Status: strings.TrimSuffix(source, "; "),
51+
}
52+
53+
testSuite.TestCases = append(testSuite.TestCases, tc)
54+
suites[fromLinter] = testSuite
55+
}
56+
57+
var res testSuitesXML
58+
for _, val := range suites {
59+
res.TestSuites = append(res.TestSuites, val)
60+
}
61+
62+
enc := xml.NewEncoder(logutils.StdOut)
63+
enc.Indent("", " ")
64+
if err := enc.Encode(res); err != nil {
65+
return err
66+
}
67+
return nil
68+
}

0 commit comments

Comments
 (0)