From fdaa226929c56f52667a6f240e3ce716187686ee Mon Sep 17 00:00:00 2001 From: Sonia Hamilton Date: Mon, 20 May 2019 10:02:20 +1000 Subject: [PATCH 1/4] junit-xml output --- pkg/commands/run.go | 2 ++ pkg/config/config.go | 2 ++ pkg/printers/junitxml.go | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 pkg/printers/junitxml.go diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 42faf7e2400a..521a1071cf51 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -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) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 720c5e312e2d..9a113fe6d892 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -14,6 +14,7 @@ const ( OutFormatTab = "tab" OutFormatCheckstyle = "checkstyle" OutFormatCodeClimate = "code-climate" + OutFormatJunitXML = "junit-xml" ) var OutFormats = []string{ @@ -23,6 +24,7 @@ var OutFormats = []string{ OutFormatTab, OutFormatCheckstyle, OutFormatCodeClimate, + OutFormatJunitXML, } type ExcludePattern struct { diff --git a/pkg/printers/junitxml.go b/pkg/printers/junitxml.go new file mode 100644 index 000000000000..eabf4f339caf --- /dev/null +++ b/pkg/printers/junitxml.go @@ -0,0 +1,73 @@ +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 { + //debug := logutils.Debug("junitxml") + //debug("starting") + suites := make(map[string]testSuiteXML) // use a map to group-by "FromLinter" + + for i := range issues { + i := i + //debug("%+v", i) + + 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 result testSuitesXML + for _, val := range suites { + result.TestSuites = append(result.TestSuites, val) + } + + enc := xml.NewEncoder(logutils.StdOut) + enc.Indent("", " ") + if err := enc.Encode(result); err != nil { + return err + } + return nil +} From da11e9068b9b9c5c18b7bc2709bc522d25502d4a Mon Sep 17 00:00:00 2001 From: Sonia Hamilton Date: Mon, 20 May 2019 14:08:25 +1000 Subject: [PATCH 2/4] remove debug comments --- pkg/printers/junitxml.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/printers/junitxml.go b/pkg/printers/junitxml.go index eabf4f339caf..938368c11a35 100644 --- a/pkg/printers/junitxml.go +++ b/pkg/printers/junitxml.go @@ -34,14 +34,10 @@ func NewJunitXML() *JunitXML { } func (JunitXML) Print(ctx context.Context, issues <-chan result.Issue) error { - //debug := logutils.Debug("junitxml") - //debug("starting") suites := make(map[string]testSuiteXML) // use a map to group-by "FromLinter" for i := range issues { i := i - //debug("%+v", i) - fromLinter := i.FromLinter testSuite := suites[fromLinter] testSuite.Suite = fromLinter From 3c44a8a26284297eb57c7d04f999a9f839aaadf3 Mon Sep 17 00:00:00 2001 From: Sonia Hamilton Date: Mon, 20 May 2019 14:08:42 +1000 Subject: [PATCH 3/4] shadowed variables --- pkg/printers/junitxml.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/printers/junitxml.go b/pkg/printers/junitxml.go index 938368c11a35..99d354a009ee 100644 --- a/pkg/printers/junitxml.go +++ b/pkg/printers/junitxml.go @@ -37,7 +37,6 @@ 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 { - i := i fromLinter := i.FromLinter testSuite := suites[fromLinter] testSuite.Suite = fromLinter @@ -55,14 +54,14 @@ func (JunitXML) Print(ctx context.Context, issues <-chan result.Issue) error { suites[fromLinter] = testSuite } - var result testSuitesXML + var res testSuitesXML for _, val := range suites { - result.TestSuites = append(result.TestSuites, val) + res.TestSuites = append(res.TestSuites, val) } enc := xml.NewEncoder(logutils.StdOut) enc.Indent("", " ") - if err := enc.Encode(result); err != nil { + if err := enc.Encode(res); err != nil { return err } return nil From 3319d715efff80dd4054b219fe5d4d6f192c6d49 Mon Sep 17 00:00:00 2001 From: Sonia Hamilton Date: Mon, 20 May 2019 14:25:46 +1000 Subject: [PATCH 4/4] update readme junit-xml --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cc3dc1d358f7..e170e171fc4c 100644 --- a/README.md +++ b/README.md @@ -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)