feat: add junit-xml-extended format (#4918)

This commit is contained in:
Ludovic Fernandez 2024-09-04 17:01:06 +02:00 committed by GitHub
parent 2fcfe26fdb
commit 0275389a64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 71 additions and 15 deletions

View File

@ -70,6 +70,7 @@ output:
# - `checkstyle` # - `checkstyle`
# - `code-climate` # - `code-climate`
# - `junit-xml` # - `junit-xml`
# - `junit-xml-extended`
# - `github-actions` # - `github-actions`
# - `teamcity` # - `teamcity`
# - `sarif` # - `sarif`

View File

@ -521,6 +521,7 @@
"checkstyle", "checkstyle",
"code-climate", "code-climate",
"junit-xml", "junit-xml",
"junit-xml-extended",
"github-actions", "github-actions",
"teamcity", "teamcity",
"sarif" "sarif"

View File

@ -17,6 +17,7 @@ const (
OutFormatCodeClimate = "code-climate" OutFormatCodeClimate = "code-climate"
OutFormatHTML = "html" OutFormatHTML = "html"
OutFormatJunitXML = "junit-xml" OutFormatJunitXML = "junit-xml"
OutFormatJunitXMLExtended = "junit-xml-extended"
OutFormatGithubActions = "github-actions" // Deprecated OutFormatGithubActions = "github-actions" // Deprecated
OutFormatTeamCity = "teamcity" OutFormatTeamCity = "teamcity"
OutFormatSarif = "sarif" OutFormatSarif = "sarif"
@ -32,6 +33,7 @@ var AllOutputFormats = []string{
OutFormatCodeClimate, OutFormatCodeClimate,
OutFormatHTML, OutFormatHTML,
OutFormatJunitXML, OutFormatJunitXML,
OutFormatJunitXMLExtended,
OutFormatGithubActions, OutFormatGithubActions,
OutFormatTeamCity, OutFormatTeamCity,
OutFormatSarif, OutFormatSarif,

View File

@ -30,6 +30,8 @@ type testCaseXML struct {
Name string `xml:"name,attr"` Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"` ClassName string `xml:"classname,attr"`
Failure failureXML `xml:"failure"` Failure failureXML `xml:"failure"`
File string `xml:"file,attr,omitempty"`
Line int `xml:"line,attr,omitempty"`
} }
type failureXML struct { type failureXML struct {
@ -39,11 +41,15 @@ type failureXML struct {
} }
type JunitXML struct { type JunitXML struct {
w io.Writer extended bool
w io.Writer
} }
func NewJunitXML(w io.Writer) *JunitXML { func NewJunitXML(extended bool, w io.Writer) *JunitXML {
return &JunitXML{w: w} return &JunitXML{
extended: extended,
w: w,
}
} }
func (p JunitXML) Print(issues []result.Issue) error { func (p JunitXML) Print(issues []result.Issue) error {
@ -68,6 +74,11 @@ func (p JunitXML) Print(issues []result.Issue) error {
}, },
} }
if p.extended {
tc.File = i.Pos.Filename
tc.Line = i.Pos.Line
}
testSuite.TestCases = append(testSuite.TestCases, tc) testSuite.TestCases = append(testSuite.TestCases, tc)
suites[suiteName] = testSuite suites[suiteName] = testSuite
} }

View File

@ -42,13 +42,14 @@ func TestJunitXML_Print(t *testing.T) {
}, },
} }
buf := new(bytes.Buffer) testCases := []struct {
printer := NewJunitXML(buf) desc string
extended bool
err := printer.Print(issues) expected string
require.NoError(t, err) }{
{
expected := `<testsuites> desc: "basic",
expected: `<testsuites>
<testsuite name="path/to/filea.go" tests="1" errors="0" failures="1"> <testsuite name="path/to/filea.go" tests="1" errors="0" failures="1">
<testcase name="linter-a" classname="path/to/filea.go:10:4"> <testcase name="linter-a" classname="path/to/filea.go:10:4">
<failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue <failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue
@ -69,7 +70,47 @@ Details: func foo() {
}]]></failure> }]]></failure>
</testcase> </testcase>
</testsuite> </testsuite>
</testsuites>` </testsuites>`,
},
{
desc: "extended/complete",
extended: true,
expected: `<testsuites>
<testsuite name="path/to/filea.go" tests="1" errors="0" failures="1">
<testcase name="linter-a" classname="path/to/filea.go:10:4" file="path/to/filea.go" line="10">
<failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue
Category: linter-a
File: path/to/filea.go
Line: 10
Details: ]]></failure>
</testcase>
</testsuite>
<testsuite name="path/to/fileb.go" tests="1" errors="0" failures="1">
<testcase name="linter-b" classname="path/to/fileb.go:300:9" file="path/to/fileb.go" line="300">
<failure message="path/to/fileb.go:300:9: another issue" type="error"><![CDATA[error: another issue
Category: linter-b
File: path/to/fileb.go
Line: 300
Details: func foo() {
fmt.Println("bar")
}]]></failure>
</testcase>
</testsuite>
</testsuites>`,
},
}
assert.Equal(t, expected, buf.String()) for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
buf := new(bytes.Buffer)
printer := NewJunitXML(test.extended, buf)
err := printer.Print(issues)
require.NoError(t, err)
assert.Equal(t, test.expected, buf.String())
})
}
} }

View File

@ -115,7 +115,7 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
switch format { switch format {
case config.OutFormatJSON: case config.OutFormatJSON:
p = NewJSON(c.reportData, w) p = NewJSON(c.reportData, w)
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber: case config.OutFormatLineNumber, config.OutFormatColoredLineNumber:
p = NewText(c.cfg.PrintIssuedLine, p = NewText(c.cfg.PrintIssuedLine,
format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName, format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName,
c.log.Child(logutils.DebugKeyTextPrinter), w) c.log.Child(logutils.DebugKeyTextPrinter), w)
@ -129,8 +129,8 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
p = NewCodeClimate(w) p = NewCodeClimate(w)
case config.OutFormatHTML: case config.OutFormatHTML:
p = NewHTML(w) p = NewHTML(w)
case config.OutFormatJunitXML: case config.OutFormatJunitXML, config.OutFormatJunitXMLExtended:
p = NewJunitXML(w) p = NewJunitXML(format == config.OutFormatJunitXMLExtended, w)
case config.OutFormatGithubActions: case config.OutFormatGithubActions:
p = NewGitHubAction(w) p = NewGitHubAction(w)
case config.OutFormatTeamCity: case config.OutFormatTeamCity: