forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjunitxml.go
102 lines (85 loc) · 2.26 KB
/
junitxml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package printers
import (
"context"
"encoding/xml"
"fmt"
"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"`
Tests int `xml:"tests,attr"`
Errors int `xml:"errors,attr"`
Failures int `xml:"failures,attr"`
TestCases []testCaseXML `xml:"testcase"`
}
type testCaseXML struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Failure failureXML `xml:"failure"`
}
type failureXML struct {
Message string `xml:"message,attr"`
Content string `xml:",cdata"`
}
type JunitXML struct {
}
func NewJunitXML() *JunitXML {
return &JunitXML{}
}
func (j JunitXML) Print(ctx context.Context, issues []result.Issue) error {
suites := make(map[string]testSuiteXML) // use a map to group by file
for ind := range issues {
i := &issues[ind]
suiteName := i.FilePath()
testSuite := suites[suiteName]
testSuite.Suite = i.FilePath()
testSuite.Tests++
testSuite.Failures++
content := strings.Join(i.SourceLines, "\n")
content += j.getSuggestedFix(&issues[ind])
tc := testCaseXML{
Name: i.FromLinter,
ClassName: i.Pos.String(),
Failure: failureXML{
Message: i.Text,
Content: content,
},
}
testSuite.TestCases = append(testSuite.TestCases, tc)
suites[suiteName] = 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
}
func (j JunitXML) getSuggestedFix(i *result.Issue) string {
var text string
if len(i.SuggestedFixes) > 0 {
for _, fix := range i.SuggestedFixes {
text += fmt.Sprintf("%s\n", strings.TrimSpace(fix.Message))
var suggestedEdits []string
for _, textEdit := range fix.TextEdits {
suggestedEdits = append(suggestedEdits, strings.TrimSpace(textEdit.NewText))
}
text += strings.Join(suggestedEdits, "\n") + "\n"
}
}
if text != "" {
return fmt.Sprintf("\n\n%s", text)
}
return ""
}