-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvenom_output.go
200 lines (176 loc) · 4.94 KB
/
venom_output.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
package venom
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/fatih/color"
tap "github.com/mndrix/tap-go"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
func init() {
color.NoColor = true
if os.Getenv("IS_TTY") == "" || strings.ToLower(os.Getenv("IS_TTY")) == "true" || os.Getenv("IS_TTY") == "1" {
color.NoColor = false
}
}
// OutputResult output result to sdtout, files...
func (v *Venom) OutputResult() error {
if v.OutputDir == "" {
return nil
}
for i := range v.Tests.TestSuites {
tcFiltered := []TestCase{}
for _, tc := range v.Tests.TestSuites[i].TestCases {
if tc.IsEvaluated {
tcFiltered = append(tcFiltered, tc)
}
}
v.Tests.TestSuites[i].TestCases = tcFiltered
testsResult := &Tests{
TestSuites: []TestSuite{v.Tests.TestSuites[i]},
Status: v.Tests.Status,
NbTestsuitesFail: v.Tests.NbTestsuitesFail,
NbTestsuitesPass: v.Tests.NbTestsuitesPass,
NbTestsuitesSkip: v.Tests.NbTestsuitesSkip,
Duration: v.Tests.Duration,
Start: v.Tests.Start,
End: v.Tests.End,
}
var data []byte
var err error
switch v.OutputFormat {
case "json":
data, err = json.MarshalIndent(testsResult, "", " ")
if err != nil {
log.Fatalf("Error: cannot format output json (%s)", err)
}
case "tap":
data, err = outputTapFormat(*testsResult)
if err != nil {
log.Fatalf("Error: cannot format output tap (%s)", err)
}
case "yml", "yaml":
data, err = yaml.Marshal(testsResult)
if err != nil {
log.Fatalf("Error: cannot format output yaml (%s)", err)
}
case "xml":
data, err = outputXMLFormat(*testsResult)
if err != nil {
log.Fatalf("Error: cannot format output xml (%s)", err)
}
case "html":
log.Fatalf("Error: you have to use the --html-report flag")
}
fname := strings.TrimSuffix(v.Tests.TestSuites[i].Filepath, filepath.Ext(v.Tests.TestSuites[i].Filepath))
fname = strings.ReplaceAll(fname, "/", "_")
filename := path.Join(v.OutputDir, "test_results_"+fname+"."+v.OutputFormat)
if err := os.WriteFile(filename, data, 0600); err != nil {
return fmt.Errorf("Error while creating file %s: %v", filename, err)
}
v.PrintFunc("Writing file %s\n", filename)
}
if v.HtmlReport {
testsResult := &Tests{
TestSuites: v.Tests.TestSuites,
Status: v.Tests.Status,
NbTestsuitesFail: v.Tests.NbTestsuitesFail,
NbTestsuitesPass: v.Tests.NbTestsuitesPass,
NbTestsuitesSkip: v.Tests.NbTestsuitesSkip,
Duration: v.Tests.Duration,
Start: v.Tests.Start,
End: v.Tests.End,
}
data, err := outputHTML(testsResult)
if err != nil {
log.Fatalf("Error: cannot format output html (%s)", err)
}
var filename = filepath.Join(v.OutputDir, computeOutputFilename("test_results.html"))
v.PrintFunc("Writing html file %s\n", filename)
if err := os.WriteFile(filename, data, 0600); err != nil {
return fmt.Errorf("Error while creating file %s: %v", filename, err)
}
}
return nil
}
func outputTapFormat(tests Tests) ([]byte, error) {
tapValue := tap.New()
buf := new(bytes.Buffer)
tapValue.Writer = buf
var total int
for _, ts := range tests.TestSuites {
for _, tc := range ts.TestCases {
total++
name := ts.Name + " / " + tc.Name
if len(tc.Skipped) > 0 {
tapValue.Skip(1, name)
continue
}
for _, testStepResult := range tc.TestStepResults {
if len(testStepResult.Errors) > 0 {
tapValue.Fail(name)
for _, e := range testStepResult.Errors {
tapValue.Diagnosticf("Error: %s", e.Value)
}
continue
}
}
tapValue.Pass(name)
}
}
tapValue.Header(total)
return buf.Bytes(), nil
}
func outputXMLFormat(tests Tests) ([]byte, error) {
testsXML := TestsXML{}
for _, ts := range tests.TestSuites {
tsXML := TestSuiteXML{
Name: ts.Name,
Package: ts.Filepath,
}
for _, tc := range ts.TestCases {
switch tc.Status {
case StatusFail:
tsXML.Errors++
case StatusSkip:
tsXML.Skipped++
}
tsXML.Total++
failuresXML := []FailureXML{}
systemout := InnerResult{}
systemerr := InnerResult{}
for _, result := range tc.TestStepResults {
for _, failure := range result.Errors {
failuresXML = append(failuresXML, FailureXML{
Value: failure.Value,
})
}
systemout.Value += result.Systemout
systemerr.Value += result.Systemerr
}
tcXML := TestCaseXML{
Classname: ts.Filename,
Errors: failuresXML,
Name: tc.Name,
Skipped: tc.Skipped,
Systemout: systemout,
Systemerr: systemerr,
Time: tc.Duration,
}
tsXML.TestCases = append(tsXML.TestCases, tcXML)
}
testsXML.TestSuites = append(testsXML.TestSuites, tsXML)
}
dataxml, errm := xml.MarshalIndent(testsXML, "", " ")
if errm != nil {
log.Fatalf("Error: cannot format xml output: %s", errm)
}
data := append([]byte(`<?xml version="1.0" encoding="utf-8"?>`), dataxml...)
return data, nil
}