-
Notifications
You must be signed in to change notification settings - Fork 118
/
report.go
229 lines (205 loc) · 6.2 KB
/
report.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*Package junitxml creates a JUnit XML report from a testjson.Execution.
*/
package junitxml
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"gotest.tools/gotestsum/internal/log"
"gotest.tools/gotestsum/testjson"
)
// JUnitTestSuites is a collection of JUnit test suites.
type JUnitTestSuites struct {
XMLName xml.Name `xml:"testsuites"`
Name string `xml:"name,attr,omitempty"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Time string `xml:"time,attr"`
Suites []JUnitTestSuite
}
// JUnitTestSuite is a single JUnit test suite which may contain many
// testcases.
type JUnitTestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Time string `xml:"time,attr"`
Name string `xml:"name,attr"`
Properties []JUnitProperty `xml:"properties>property,omitempty"`
TestCases []JUnitTestCase
Timestamp string `xml:"timestamp,attr"`
}
// JUnitTestCase is a single test case with its result.
type JUnitTestCase struct {
XMLName xml.Name `xml:"testcase"`
Classname string `xml:"classname,attr"`
Name string `xml:"name,attr"`
Time string `xml:"time,attr"`
SkipMessage *JUnitSkipMessage `xml:"skipped,omitempty"`
Failure *JUnitFailure `xml:"failure,omitempty"`
}
// JUnitSkipMessage contains the reason why a testcase was skipped.
type JUnitSkipMessage struct {
Message string `xml:"message,attr"`
}
// JUnitProperty represents a key/value pair used to define properties.
type JUnitProperty struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
// JUnitFailure contains data related to a failed test.
type JUnitFailure struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Contents string `xml:",chardata"`
}
// Config used to write a junit XML document.
type Config struct {
ProjectName string
FormatTestSuiteName FormatFunc
FormatTestCaseClassname FormatFunc
HideEmptyPackages bool
// This is used for tests to have a consistent timestamp
customTimestamp string
customElapsed string
}
// FormatFunc converts a string from one format into another.
type FormatFunc func(string) string
// Write creates an XML document and writes it to out.
func Write(out io.Writer, exec *testjson.Execution, cfg Config) error {
if err := write(out, generate(exec, cfg)); err != nil {
return fmt.Errorf("failed to write JUnit XML: %v", err)
}
return nil
}
func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites {
cfg = configWithDefaults(cfg)
version := goVersion()
suites := JUnitTestSuites{
Name: cfg.ProjectName,
Tests: exec.Total(),
Failures: len(exec.Failed()),
Errors: len(exec.Errors()),
Time: formatDurationAsSeconds(time.Since(exec.Started())),
}
if cfg.customElapsed != "" {
suites.Time = cfg.customElapsed
}
for _, pkgname := range exec.Packages() {
pkg := exec.Package(pkgname)
if cfg.HideEmptyPackages && pkg.IsEmpty() {
continue
}
junitpkg := JUnitTestSuite{
Name: cfg.FormatTestSuiteName(pkgname),
Tests: pkg.Total,
Time: formatDurationAsSeconds(pkg.Elapsed()),
Properties: packageProperties(version),
TestCases: packageTestCases(pkg, cfg.FormatTestCaseClassname),
Failures: len(pkg.Failed),
Timestamp: cfg.customTimestamp,
}
if cfg.customTimestamp == "" {
junitpkg.Timestamp = exec.Started().Format(time.RFC3339)
}
suites.Suites = append(suites.Suites, junitpkg)
}
return suites
}
func configWithDefaults(cfg Config) Config {
noop := func(v string) string {
return v
}
if cfg.FormatTestSuiteName == nil {
cfg.FormatTestSuiteName = noop
}
if cfg.FormatTestCaseClassname == nil {
cfg.FormatTestCaseClassname = noop
}
return cfg
}
func formatDurationAsSeconds(d time.Duration) string {
return fmt.Sprintf("%f", d.Seconds())
}
func packageProperties(goVersion string) []JUnitProperty {
return []JUnitProperty{
{Name: "go.version", Value: goVersion},
}
}
// goVersion returns the version as reported by the go binary in PATH. This
// version will not be the same as runtime.Version, which is always the version
// of go used to build the gotestsum binary.
//
// To skip the os/exec call set the GOVERSION environment variable to the
// desired value.
func goVersion() string {
if version, ok := os.LookupEnv("GOVERSION"); ok {
return version
}
log.Debugf("exec: go version")
cmd := exec.Command("go", "version")
out, err := cmd.Output()
if err != nil {
log.Warnf("Failed to lookup go version for junit xml: %v", err)
return "unknown"
}
return strings.TrimPrefix(strings.TrimSpace(string(out)), "go version ")
}
func packageTestCases(pkg *testjson.Package, formatClassname FormatFunc) []JUnitTestCase {
cases := []JUnitTestCase{}
if pkg.TestMainFailed() {
var buf bytes.Buffer
pkg.WriteOutputTo(&buf, 0) //nolint:errcheck
jtc := newJUnitTestCase(testjson.TestCase{Test: "TestMain"}, formatClassname)
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: buf.String(),
}
cases = append(cases, jtc)
}
for _, tc := range pkg.Failed {
jtc := newJUnitTestCase(tc, formatClassname)
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: strings.Join(pkg.OutputLines(tc), ""),
}
cases = append(cases, jtc)
}
for _, tc := range pkg.Skipped {
jtc := newJUnitTestCase(tc, formatClassname)
jtc.SkipMessage = &JUnitSkipMessage{
Message: strings.Join(pkg.OutputLines(tc), ""),
}
cases = append(cases, jtc)
}
for _, tc := range pkg.Passed {
jtc := newJUnitTestCase(tc, formatClassname)
cases = append(cases, jtc)
}
return cases
}
func newJUnitTestCase(tc testjson.TestCase, formatClassname FormatFunc) JUnitTestCase {
return JUnitTestCase{
Classname: formatClassname(tc.Package),
Name: tc.Test.Name(),
Time: formatDurationAsSeconds(tc.Elapsed),
}
}
func write(out io.Writer, suites JUnitTestSuites) error {
doc, err := xml.MarshalIndent(suites, "", "\t")
if err != nil {
return err
}
_, err = out.Write([]byte(xml.Header))
if err != nil {
return err
}
_, err = out.Write(doc)
return err
}