-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathjunit.go
88 lines (72 loc) · 2.1 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
package outputs
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"strconv"
"time"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/util"
"github.com/fatih/color"
)
type JUnit struct{}
func (r JUnit) Output(w io.Writer, results <-chan []resource.TestResult,
startTime time.Time, outConfig util.OutputConfig) (exitCode int) {
color.NoColor = true
var testCount, failed, skipped int
// ISO8601 timeformat
timestamp := time.Now().Format(time.RFC3339)
var summary map[int]string
summary = make(map[int]string)
for resultGroup := range results {
for _, testResult := range resultGroup {
m := struct2map(testResult)
duration := strconv.FormatFloat(m["duration"].(float64)/1000/1000/1000, 'f', 3, 64)
summary[testCount] = "<testcase name=\"" +
testResult.ResourceType + " " +
escapeString(testResult.ResourceId) + " " +
testResult.Property + "\" " +
"time=\"" + duration + "\">\n"
if testResult.Result == resource.FAIL {
summary[testCount] += "<system-err>" +
escapeString(humanizeResult2(testResult)) +
"</system-err>\n"
summary[testCount] += "<failure>" +
escapeString(humanizeResult2(testResult)) +
"</failure>\n</testcase>\n"
failed++
} else {
if testResult.Result == resource.SKIP {
summary[testCount] += "<skipped/>"
skipped++
}
summary[testCount] += "<system-out>" +
escapeString(humanizeResult2(testResult)) +
"</system-out>\n</testcase>\n"
}
testCount++
}
}
duration := time.Since(startTime)
fmt.Fprintln(w, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
fmt.Fprintf(w, "<testsuite name=\"goss\" errors=\"0\" tests=\"%d\" "+
"failures=\"%d\" skipped=\"%d\" time=\"%.3f\" timestamp=\"%s\">\n",
testCount, failed, skipped, duration.Seconds(), timestamp)
for i := 0; i < testCount; i++ {
fmt.Fprintf(w, "%s", summary[i])
}
fmt.Fprintln(w, "</testsuite>")
if failed > 0 {
return 1
}
return 0
}
func init() {
RegisterOutputer("junit", &JUnit{}, []string{})
}
func escapeString(str string) string {
buffer := new(bytes.Buffer)
xml.EscapeText(buffer, []byte(str))
return buffer.String()
}