Skip to content

Commit 4f207b1

Browse files
ldezuudashr
authored andcommitted
feat: add junit-xml-extended format (golangci#4918)
1 parent 87ee8b8 commit 4f207b1

File tree

6 files changed

+71
-15
lines changed

6 files changed

+71
-15
lines changed

.golangci.next.reference.yml

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ output:
7070
# - `checkstyle`
7171
# - `code-climate`
7272
# - `junit-xml`
73+
# - `junit-xml-extended`
7374
# - `github-actions`
7475
# - `teamcity`
7576
# - `sarif`

jsonschema/golangci.next.jsonschema.json

+1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@
522522
"checkstyle",
523523
"code-climate",
524524
"junit-xml",
525+
"junit-xml-extended",
525526
"github-actions",
526527
"teamcity",
527528
"sarif"

pkg/config/output.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
OutFormatCodeClimate = "code-climate"
1818
OutFormatHTML = "html"
1919
OutFormatJunitXML = "junit-xml"
20+
OutFormatJunitXMLExtended = "junit-xml-extended"
2021
OutFormatGithubActions = "github-actions" // Deprecated
2122
OutFormatTeamCity = "teamcity"
2223
OutFormatSarif = "sarif"
@@ -32,6 +33,7 @@ var AllOutputFormats = []string{
3233
OutFormatCodeClimate,
3334
OutFormatHTML,
3435
OutFormatJunitXML,
36+
OutFormatJunitXMLExtended,
3537
OutFormatGithubActions,
3638
OutFormatTeamCity,
3739
OutFormatSarif,

pkg/printers/junitxml.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type testCaseXML struct {
3030
Name string `xml:"name,attr"`
3131
ClassName string `xml:"classname,attr"`
3232
Failure failureXML `xml:"failure"`
33+
File string `xml:"file,attr,omitempty"`
34+
Line int `xml:"line,attr,omitempty"`
3335
}
3436

3537
type failureXML struct {
@@ -39,11 +41,15 @@ type failureXML struct {
3941
}
4042

4143
type JunitXML struct {
42-
w io.Writer
44+
extended bool
45+
w io.Writer
4346
}
4447

45-
func NewJunitXML(w io.Writer) *JunitXML {
46-
return &JunitXML{w: w}
48+
func NewJunitXML(extended bool, w io.Writer) *JunitXML {
49+
return &JunitXML{
50+
extended: extended,
51+
w: w,
52+
}
4753
}
4854

4955
func (p JunitXML) Print(issues []result.Issue) error {
@@ -68,6 +74,11 @@ func (p JunitXML) Print(issues []result.Issue) error {
6874
},
6975
}
7076

77+
if p.extended {
78+
tc.File = i.Pos.Filename
79+
tc.Line = i.Pos.Line
80+
}
81+
7182
testSuite.TestCases = append(testSuite.TestCases, tc)
7283
suites[suiteName] = testSuite
7384
}

pkg/printers/junitxml_test.go

+50-9
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ func TestJunitXML_Print(t *testing.T) {
4242
},
4343
}
4444

45-
buf := new(bytes.Buffer)
46-
printer := NewJunitXML(buf)
47-
48-
err := printer.Print(issues)
49-
require.NoError(t, err)
50-
51-
expected := `<testsuites>
45+
testCases := []struct {
46+
desc string
47+
extended bool
48+
expected string
49+
}{
50+
{
51+
desc: "basic",
52+
expected: `<testsuites>
5253
<testsuite name="path/to/filea.go" tests="1" errors="0" failures="1">
5354
<testcase name="linter-a" classname="path/to/filea.go:10:4">
5455
<failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue
@@ -69,7 +70,47 @@ Details: func foo() {
6970
}]]></failure>
7071
</testcase>
7172
</testsuite>
72-
</testsuites>`
73+
</testsuites>`,
74+
},
75+
{
76+
desc: "extended/complete",
77+
extended: true,
78+
expected: `<testsuites>
79+
<testsuite name="path/to/filea.go" tests="1" errors="0" failures="1">
80+
<testcase name="linter-a" classname="path/to/filea.go:10:4" file="path/to/filea.go" line="10">
81+
<failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue
82+
Category: linter-a
83+
File: path/to/filea.go
84+
Line: 10
85+
Details: ]]></failure>
86+
</testcase>
87+
</testsuite>
88+
<testsuite name="path/to/fileb.go" tests="1" errors="0" failures="1">
89+
<testcase name="linter-b" classname="path/to/fileb.go:300:9" file="path/to/fileb.go" line="300">
90+
<failure message="path/to/fileb.go:300:9: another issue" type="error"><![CDATA[error: another issue
91+
Category: linter-b
92+
File: path/to/fileb.go
93+
Line: 300
94+
Details: func foo() {
95+
fmt.Println("bar")
96+
}]]></failure>
97+
</testcase>
98+
</testsuite>
99+
</testsuites>`,
100+
},
101+
}
102+
103+
for _, test := range testCases {
104+
t.Run(test.desc, func(t *testing.T) {
105+
t.Parallel()
73106

74-
assert.Equal(t, expected, buf.String())
107+
buf := new(bytes.Buffer)
108+
printer := NewJunitXML(test.extended, buf)
109+
110+
err := printer.Print(issues)
111+
require.NoError(t, err)
112+
113+
assert.Equal(t, test.expected, buf.String())
114+
})
115+
}
75116
}

pkg/printers/printer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
115115
switch format {
116116
case config.OutFormatJSON:
117117
p = NewJSON(c.reportData, w)
118-
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
118+
case config.OutFormatLineNumber, config.OutFormatColoredLineNumber:
119119
p = NewText(c.cfg.PrintIssuedLine,
120120
format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName,
121121
c.log.Child(logutils.DebugKeyTextPrinter), w)
@@ -129,8 +129,8 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
129129
p = NewCodeClimate(w)
130130
case config.OutFormatHTML:
131131
p = NewHTML(w)
132-
case config.OutFormatJunitXML:
133-
p = NewJunitXML(w)
132+
case config.OutFormatJunitXML, config.OutFormatJunitXMLExtended:
133+
p = NewJunitXML(format == config.OutFormatJunitXMLExtended, w)
134134
case config.OutFormatGithubActions:
135135
p = NewGitHubAction(w)
136136
case config.OutFormatTeamCity:

0 commit comments

Comments
 (0)