-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (168 loc) · 4.54 KB
/
main.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
package main
import (
"embed"
"encoding/xml"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"text/template"
)
// xmlPath is the path to the JUnit XML file.
// serverPort is the port to serve the dashboard on.
// export is the flag to render the dashboard to stdout.
var (
xmlPath string
serverPort string
export bool
version string
vFlag bool
)
//go:embed dashboard.html
var f embed.FS
type TestSuites struct {
XMLName xml.Name `xml:"testsuites"`
TestSuites []TestSuite `xml:"testsuite"`
}
type TestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Errors string `xml:"errors,attr"`
Failures string `xml:"failures,attr"`
Skipped string `xml:"skipped,attr,omitempty"`
Tests string `xml:"tests,attr"`
Time string `xml:"time,attr"`
Timestamp string `xml:"timestamp,attr"`
Hostname string `xml:"hostname,attr"`
TestCases []TestCase `xml:"testcase"`
}
// GetSuccessCount returns the number of successful tests.
func (ts *TestSuite) GetSuccessCount() int {
intCov := func(s string) int {
i, err := strconv.Atoi(s)
checkError(err)
return i
}
tests := intCov(ts.Tests)
failures := intCov(ts.Failures)
errors := intCov(ts.Errors)
// Not all test suites have skipped tests.
if ts.Skipped == "" {
return tests - failures - errors
}
skipped := intCov(ts.Skipped)
return tests - failures - errors - skipped
}
type TestCase struct {
XMLName xml.Name `xml:"testcase"`
ClassName string `xml:"classname,attr"`
Name string `xml:"name,attr"`
Time string `xml:"time,attr"`
Failure Failure `xml:"failure,omitempty"`
Error Error `xml:"error,omitempty"`
Skipped Skipped `xml:"skipped,omitempty"`
}
type Failure struct {
XMLName xml.Name `xml:"failure"`
Message string `xml:"message,attr"`
Value string `xml:",chardata"`
}
type Error struct {
XMLName xml.Name `xml:"error"`
Message string `xml:"message,attr"`
Value string `xml:",chardata"`
}
type Skipped struct {
XMLName xml.Name `xml:"skipped"`
Type string `xml:"type,attr"`
Message string `xml:"message,attr"`
}
// checkError checks if an error occurred and if so, it logs it and exits the program.
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
// serve serves the dashboard.
func serve(tmpl *template.Template, ts *TestSuite) error {
// Create a new temporary html file.
tmpFile, err := ioutil.TempFile("", "dashboard.*.html")
if err != nil {
return err
}
defer tmpFile.Close()
defer os.Remove(tmpFile.Name())
// Write the dashboard template to the tmp file.
err = tmpl.Execute(tmpFile, ts)
if err != nil {
return err
}
// Serve the dashboard.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, tmpFile.Name())
})
log.Printf("Serving the dashboard at http://localhost:%s/", serverPort)
log.Println("Press CTRL+C to stop the server.")
err = http.ListenAndServe(fmt.Sprintf(":%s", serverPort), nil)
if err != nil {
return err
}
return nil
}
// expStdout renders the dashboard to stdout.
func expStdout(tmpl *template.Template, ts *TestSuite) error {
err := tmpl.Execute(os.Stdout, ts)
if err != nil {
return err
}
return nil
}
func main() {
// Parse the command line flags.
flag.StringVar(&xmlPath, "f", "", "Path to the JUnit XML file.")
flag.StringVar(&serverPort, "p", "8080", "Port to serve the dashboard on.")
flag.BoolVar(&export, "e", false, "Render to stdout.")
flag.BoolVar(&vFlag, "v", false, "Print the version.")
flag.Parse()
// Print the version.
if vFlag {
fmt.Println("Version:", version)
os.Exit(0)
}
// Check if the xml file exists and read the contents.
if xmlPath == "" {
log.Fatal("Please provide a path to the JUnit XML file.")
}
if _, err := os.Stat(xmlPath); errors.Is(err, os.ErrNotExist) {
log.Fatal("The JUnit XML file does not exist in the given path.")
}
data, err := os.ReadFile(xmlPath)
checkError(err)
// Unmarshal the xml file contents into a TestSuites struct.
var testSuites TestSuites
err = xml.Unmarshal(data, &testSuites)
checkError(err)
// Create a new dashboard template.
funcMap := template.FuncMap{
"inc": func(i int) int {
return i + 1
},
}
b, err := f.ReadFile("dashboard.html")
checkError(err)
tmpl, err := template.New("dashboard").Funcs(funcMap).Parse(string(b))
checkError(err)
// Render the dashboard template.
ts := testSuites.TestSuites[0]
if export {
err := expStdout(tmpl, &ts)
checkError(err)
} else {
err := serve(tmpl, &ts)
checkError(err)
}
}