Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

junit-xml output #557

Merged
merged 1 commit into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ Usage:
golangci-lint run [flags]

Flags:
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate (default "colored-line-number")
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml (default "colored-line-number")
--print-issued-lines Print lines of code with issue (default true)
--print-linter-name Print linter name in issue line (default true)
--issues-exit-code int Exit code when issues were found (default 1)
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) {
p = printers.NewCheckstyle()
case config.OutFormatCodeClimate:
p = printers.NewCodeClimate()
case config.OutFormatJunitXML:
p = printers.NewJunitXML()
default:
return nil, fmt.Errorf("unknown output format %s", format)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
OutFormatTab = "tab"
OutFormatCheckstyle = "checkstyle"
OutFormatCodeClimate = "code-climate"
OutFormatJunitXML = "junit-xml"
)

var OutFormats = []string{
Expand All @@ -23,6 +24,7 @@ var OutFormats = []string{
OutFormatTab,
OutFormatCheckstyle,
OutFormatCodeClimate,
OutFormatJunitXML,
}

type ExcludePattern struct {
Expand Down
68 changes: 68 additions & 0 deletions pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package printers

import (
"context"
"encoding/xml"
"strings"

"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

type testSuitesXML struct {
XMLName xml.Name `xml:"testsuites"`
TestSuites []testSuiteXML
}

type testSuiteXML struct {
XMLName xml.Name `xml:"testsuite"`
Suite string `xml:"name,attr"`
TestCases []testCaseXML `xml:"testcase"`
}

type testCaseXML struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Status string `xml:"status,attr"`
}

type JunitXML struct {
}

func NewJunitXML() *JunitXML {
return &JunitXML{}
}

func (JunitXML) Print(ctx context.Context, issues <-chan result.Issue) error {
suites := make(map[string]testSuiteXML) // use a map to group-by "FromLinter"

for i := range issues {
fromLinter := i.FromLinter
testSuite := suites[fromLinter]
testSuite.Suite = fromLinter

var source string
for _, line := range i.SourceLines {
source += strings.TrimSpace(line) + "; "
}
tc := testCaseXML{Name: i.Text,
ClassName: i.Pos.String(),
Status: strings.TrimSuffix(source, "; "),
}

testSuite.TestCases = append(testSuite.TestCases, tc)
suites[fromLinter] = testSuite
}

var res testSuitesXML
for _, val := range suites {
res.TestSuites = append(res.TestSuites, val)
}

enc := xml.NewEncoder(logutils.StdOut)
enc.Indent("", " ")
if err := enc.Encode(res); err != nil {
return err
}
return nil
}