Skip to content

Commit

Permalink
feat: add extended-junit-xml format
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Aug 16, 2024
1 parent adbdfdb commit 977b49c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
1 change: 1 addition & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ output:
# - `checkstyle`
# - `code-climate`
# - `junit-xml`
# - `extended-junit-xml`
# - `github-actions`
# - `teamcity`
# - `sarif`
Expand Down
1 change: 1 addition & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@
"checkstyle",
"code-climate",
"junit-xml",
"extended-junit-xml",
"github-actions",
"teamcity",
"sarif"
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
OutFormatCodeClimate = "code-climate"
OutFormatHTML = "html"
OutFormatJunitXML = "junit-xml"
OutFormatExtendedJunitXML = "extended-junit-xml"
OutFormatGithubActions = "github-actions" // Deprecated
OutFormatTeamCity = "teamcity"
OutFormatSarif = "sarif"
Expand All @@ -32,6 +33,7 @@ var AllOutputFormats = []string{
OutFormatCodeClimate,
OutFormatHTML,
OutFormatJunitXML,
OutFormatExtendedJunitXML,
OutFormatGithubActions,
OutFormatTeamCity,
OutFormatSarif,
Expand Down
17 changes: 14 additions & 3 deletions pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type testCaseXML struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Failure failureXML `xml:"failure"`
File string `xml:"file,attr,omitempty"`
Line int `xml:"line,attr,omitempty"`
}

type failureXML struct {
Expand All @@ -39,11 +41,15 @@ type failureXML struct {
}

type JunitXML struct {
w io.Writer
extended bool
w io.Writer
}

func NewJunitXML(w io.Writer) *JunitXML {
return &JunitXML{w: w}
func NewJunitXML(extended bool, w io.Writer) *JunitXML {
return &JunitXML{
extended: extended,
w: w,
}
}

func (p JunitXML) Print(issues []result.Issue) error {
Expand All @@ -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)
suites[suiteName] = testSuite
}
Expand Down
60 changes: 51 additions & 9 deletions pkg/printers/junitxml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ func TestJunitXML_Print(t *testing.T) {
},
}

buf := new(bytes.Buffer)
printer := NewJunitXML(buf)

err := printer.Print(issues)
require.NoError(t, err)

expected := `<testsuites>
testCases := []struct {
desc string
extended bool
expected string
}{
{
desc: "basic",
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">
<failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue
Expand All @@ -69,7 +70,48 @@ Details: func foo() {
}]]></failure>
</testcase>
</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>`,
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

assert.Equal(t, expected, buf.String())
buf := new(bytes.Buffer)
printer := NewJunitXML(test.extended, buf)

err := printer.Print(issues)
require.NoError(t, err)

assert.Equal(t, test.expected, buf.String())
})
}
}
4 changes: 3 additions & 1 deletion pkg/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
case config.OutFormatHTML:
p = NewHTML(w)
case config.OutFormatJunitXML:
p = NewJunitXML(w)
p = NewJunitXML(false, w)
case config.OutFormatExtendedJunitXML:
p = NewJunitXML(true, w)
case config.OutFormatGithubActions:
p = NewGitHubAction(w)
case config.OutFormatTeamCity:
Expand Down

0 comments on commit 977b49c

Please sign in to comment.