...
1
16
17
18
19 package junit
20
21 import (
22 "encoding/xml"
23 "fmt"
24
25 "golang.org/x/tools/cover"
26
27 "edge-infra.dev/third_party/gopherage/pkg/cov/junit/calculation"
28 )
29
30
31 type Property struct {
32 Name string `xml:"name,attr"`
33 Value string `xml:"value,attr"`
34 }
35
36
37 type Properties struct {
38 PropertyList []Property `xml:"property"`
39 }
40
41
42 type TestCase struct {
43 ClassName string `xml:"class_name,attr"`
44 Name string `xml:"name,attr"`
45 Time string `xml:"time,attr"`
46 Failure bool `xml:"failure,omitempty"`
47 PropertyList Properties `xml:"properties"`
48 }
49
50
51 type Testsuite struct {
52 XMLName string `xml:"testsuite"`
53 Testcases []TestCase `xml:"testcase"`
54 }
55
56 func (ts *Testsuite) addTestCase(coverageTargetName string, ratio, threshold float32) {
57 testCase := TestCase{
58 ClassName: "go_coverage",
59 Name: coverageTargetName,
60 Time: "0",
61 Failure: ratio < threshold,
62 PropertyList: Properties{
63 PropertyList: []Property{
64 {
65 Name: "coverage",
66 Value: fmt.Sprintf("%.1f", ratio*100),
67 },
68 },
69 },
70 }
71 ts.Testcases = append(ts.Testcases, testCase)
72 }
73
74
75
76 func toTestsuite(covList *calculation.CoverageList, coverageThreshold float32) Testsuite {
77 ts := Testsuite{}
78 ts.addTestCase("OVERALL", covList.Ratio(), coverageThreshold)
79
80 for _, cov := range covList.Group {
81 ts.addTestCase(cov.Name, cov.Ratio(), coverageThreshold)
82 }
83
84 for _, dir := range covList.ListDirectories() {
85 dirCov := covList.Subset(dir)
86 ts.addTestCase(dir, dirCov.Ratio(), coverageThreshold)
87 }
88 return ts
89 }
90
91
92
93 func ProfileToTestsuiteXML(profiles []*cover.Profile, coverageThreshold float32) ([]byte, error) {
94 covList := calculation.ProduceCovList(profiles)
95
96 ts := toTestsuite(covList, coverageThreshold)
97 return xml.MarshalIndent(ts, "", " ")
98 }
99
View as plain text