-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjunit.go
135 lines (115 loc) · 2.83 KB
/
junit.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
package junit
import (
"bytes"
"encoding/xml"
"fmt"
"math"
"os"
"runtime"
"sync/atomic"
"time"
"github.com/aphistic/sweet"
)
func roundTime(duration time.Duration) float64 {
seconds := float64(duration) / float64(time.Second)
ms := math.Floor(seconds * 10000)
return ms / 10000
}
type JUnitPlugin struct {
suites *testSuites
output string
}
var _ sweet.Plugin = &JUnitPlugin{}
func NewPlugin() *JUnitPlugin {
return &JUnitPlugin{
suites: newTestSuites(),
}
}
func (p *JUnitPlugin) Name() string {
return "JUnit Output"
}
func (p *JUnitPlugin) Options() *sweet.PluginOptions {
return &sweet.PluginOptions{
Prefix: "junit",
Options: map[string]*sweet.PluginOption{
"output": &sweet.PluginOption{
Help: "Results of the test run will be written in JUnit format to the path provided",
},
},
}
}
func (p *JUnitPlugin) SetOption(name, value string) {
if name == "output" {
p.output = value
}
}
func (p *JUnitPlugin) Starting() {
}
func (p *JUnitPlugin) SuiteStarting(suite string) {
s := p.suites.GetSuite(suite)
s.AddProperty("go.version", runtime.Version())
}
func (p *JUnitPlugin) TestStarting(testName *sweet.TestName) {
}
func (p *JUnitPlugin) TestPassed(testName *sweet.TestName, stats *sweet.TestPassedStats) {
s := p.suites.GetSuite(testName.SuiteName)
atomic.AddInt64(&s.Tests, 1)
s.AddTestCase(&testCase{
Name: testName.String(),
ClassName: testName.SuiteName,
Time: roundTime(stats.Time),
})
}
func (p *JUnitPlugin) TestFailed(testName *sweet.TestName, stats *sweet.TestFailedStats) {
s := p.suites.GetSuite(testName.SuiteName)
atomic.AddInt64(&s.Tests, 1)
atomic.AddInt64(&s.Failures, 1)
tc := &testCase{
Name: testName.String(),
ClassName: testName.SuiteName,
Time: roundTime(stats.Time),
}
file := "<unknown>"
line := 0
if len(stats.Frames) > 0 {
file = stats.Frames[0].File
line = stats.Frames[0].Line
}
tc.SetFailure(file, line, stats.Message)
s.AddTestCase(tc)
}
func (p *JUnitPlugin) TestSkipped(testName *sweet.TestName, stats *sweet.TestSkippedStats) {
}
func (p *JUnitPlugin) SuiteFinished(suite string, stats *sweet.SuiteFinishedStats) {
s := p.suites.GetSuite(suite)
s.Time = roundTime(stats.Time)
}
func (p *JUnitPlugin) Finished() {
if p.output == "" {
return
}
of, err := os.OpenFile(p.output, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Unable to open file for JUnit output: %s\n", err)
os.Exit(1)
}
defer of.Close()
x, err := p.generateXML()
if err != nil {
fmt.Printf("Error generating xml: %s", err)
return
}
of.WriteString(xml.Header)
of.WriteString(x)
of.WriteString("\n")
}
func (p *JUnitPlugin) generateXML() (string, error) {
buffer := &bytes.Buffer{}
enc := xml.NewEncoder(buffer)
enc.Indent("", " ")
err := enc.Encode(p.suites)
if err != nil {
return "", err
}
return buffer.String(), nil
}