From 11e824273b3758320922e962e56703dcb42ad135 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 16 Aug 2024 16:26:31 +0200 Subject: [PATCH 1/4] feat: add extended-junit-xml format --- .golangci.next.reference.yml | 1 + jsonschema/golangci.next.jsonschema.json | 1 + pkg/config/output.go | 2 + pkg/printers/junitxml.go | 17 +++++-- pkg/printers/junitxml_test.go | 60 ++++++++++++++++++++---- pkg/printers/printer.go | 4 +- 6 files changed, 72 insertions(+), 13 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index e08464bdd6b8..7d2efcce5538 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -70,6 +70,7 @@ output: # - `checkstyle` # - `code-climate` # - `junit-xml` + # - `extended-junit-xml` # - `github-actions` # - `teamcity` # - `sarif` diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index f8233a77759d..c0d8ab21f160 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -520,6 +520,7 @@ "checkstyle", "code-climate", "junit-xml", + "extended-junit-xml", "github-actions", "teamcity", "sarif" diff --git a/pkg/config/output.go b/pkg/config/output.go index 592e293e0b06..122a6c1e8103 100644 --- a/pkg/config/output.go +++ b/pkg/config/output.go @@ -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" @@ -32,6 +33,7 @@ var AllOutputFormats = []string{ OutFormatCodeClimate, OutFormatHTML, OutFormatJunitXML, + OutFormatExtendedJunitXML, OutFormatGithubActions, OutFormatTeamCity, OutFormatSarif, diff --git a/pkg/printers/junitxml.go b/pkg/printers/junitxml.go index 3e3f82f5805c..7d0a703b0a5e 100644 --- a/pkg/printers/junitxml.go +++ b/pkg/printers/junitxml.go @@ -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 { @@ -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 { @@ -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 } diff --git a/pkg/printers/junitxml_test.go b/pkg/printers/junitxml_test.go index d5b4bc1f62de..8c2adbef5ca5 100644 --- a/pkg/printers/junitxml_test.go +++ b/pkg/printers/junitxml_test.go @@ -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 := ` + testCases := []struct { + desc string + extended bool + expected string + }{ + { + desc: "basic", + expected: ` -` +`, + }, + { + desc: "extended/complete", + extended: true, + expected: ` + + + + + + + + + + +`, + }, + } + + 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()) + }) + } } diff --git a/pkg/printers/printer.go b/pkg/printers/printer.go index 53db01220e31..6abd41ff79d9 100644 --- a/pkg/printers/printer.go +++ b/pkg/printers/printer.go @@ -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: From e5a80c81d60092b53dff3225f50005a24a2c2ebc Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 25 Aug 2024 17:41:07 +0200 Subject: [PATCH 2/4] chore: extended-junit-xml -> junit-xml-extended --- .golangci.next.reference.yml | 2 +- jsonschema/golangci.next.jsonschema.json | 2 +- pkg/config/output.go | 4 ++-- pkg/printers/printer.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 7d2efcce5538..48e3b710d433 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -70,7 +70,7 @@ output: # - `checkstyle` # - `code-climate` # - `junit-xml` - # - `extended-junit-xml` + # - `junit-xml-extended` # - `github-actions` # - `teamcity` # - `sarif` diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index c0d8ab21f160..319da8d930cf 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -520,7 +520,7 @@ "checkstyle", "code-climate", "junit-xml", - "extended-junit-xml", + "junit-xml-extended", "github-actions", "teamcity", "sarif" diff --git a/pkg/config/output.go b/pkg/config/output.go index 122a6c1e8103..6a26d5773e76 100644 --- a/pkg/config/output.go +++ b/pkg/config/output.go @@ -17,7 +17,7 @@ const ( OutFormatCodeClimate = "code-climate" OutFormatHTML = "html" OutFormatJunitXML = "junit-xml" - OutFormatExtendedJunitXML = "extended-junit-xml" + OutFormatJunitXMLExtended = "junit-xml-extended" OutFormatGithubActions = "github-actions" // Deprecated OutFormatTeamCity = "teamcity" OutFormatSarif = "sarif" @@ -33,7 +33,7 @@ var AllOutputFormats = []string{ OutFormatCodeClimate, OutFormatHTML, OutFormatJunitXML, - OutFormatExtendedJunitXML, + OutFormatJunitXMLExtended, OutFormatGithubActions, OutFormatTeamCity, OutFormatSarif, diff --git a/pkg/printers/printer.go b/pkg/printers/printer.go index 6abd41ff79d9..06c515c846fe 100644 --- a/pkg/printers/printer.go +++ b/pkg/printers/printer.go @@ -131,7 +131,7 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error p = NewHTML(w) case config.OutFormatJunitXML: p = NewJunitXML(false, w) - case config.OutFormatExtendedJunitXML: + case config.OutFormatJunitXMLExtended: p = NewJunitXML(true, w) case config.OutFormatGithubActions: p = NewGitHubAction(w) From 89633473d6ca41e1d91577e4318366e136acc841 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 25 Aug 2024 17:46:43 +0200 Subject: [PATCH 3/4] chore: lint --- pkg/printers/junitxml_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/printers/junitxml_test.go b/pkg/printers/junitxml_test.go index 8c2adbef5ca5..e4dfde195dfa 100644 --- a/pkg/printers/junitxml_test.go +++ b/pkg/printers/junitxml_test.go @@ -101,7 +101,6 @@ Details: func foo() { } for _, test := range testCases { - test := test t.Run(test.desc, func(t *testing.T) { t.Parallel() From 64708b84f07b6783399940ff24e08b5d09e41875 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 25 Aug 2024 18:01:54 +0200 Subject: [PATCH 4/4] feat: simplify switch --- pkg/printers/printer.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/printers/printer.go b/pkg/printers/printer.go index 06c515c846fe..20be02e0158e 100644 --- a/pkg/printers/printer.go +++ b/pkg/printers/printer.go @@ -115,7 +115,7 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error switch format { case config.OutFormatJSON: p = NewJSON(c.reportData, w) - case config.OutFormatColoredLineNumber, config.OutFormatLineNumber: + case config.OutFormatLineNumber, config.OutFormatColoredLineNumber: p = NewText(c.cfg.PrintIssuedLine, format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName, c.log.Child(logutils.DebugKeyTextPrinter), w) @@ -129,10 +129,8 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error p = NewCodeClimate(w) case config.OutFormatHTML: p = NewHTML(w) - case config.OutFormatJunitXML: - p = NewJunitXML(false, w) - case config.OutFormatJunitXMLExtended: - p = NewJunitXML(true, w) + case config.OutFormatJunitXML, config.OutFormatJunitXMLExtended: + p = NewJunitXML(format == config.OutFormatJunitXMLExtended, w) case config.OutFormatGithubActions: p = NewGitHubAction(w) case config.OutFormatTeamCity: