-
Notifications
You must be signed in to change notification settings - Fork 0
/
venom_output.go
241 lines (218 loc) · 6.43 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
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
230
231
232
233
234
235
236
237
238
239
240
241
package venom
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/fatih/color"
tap "github.com/mndrix/tap-go"
"github.com/pkg/errors"
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
}
}
// CleanUpSecrets This method tries to hide all the sensitive variables
func (v *Venom) CleanUpSecrets(testSuite TestSuite) TestSuite {
for _, testCase := range testSuite.TestCases {
ctx := v.processSecrets(context.Background(), &testSuite, &testCase)
for _, result := range testCase.TestStepResults {
for k, v := range result.ComputedVars {
if !strings.HasPrefix(k, "venom.") {
result.ComputedVars[k] = HideSensitive(ctx, v)
}
}
for k, v := range result.InputVars {
if !strings.HasPrefix(k, "venom.") {
result.InputVars[k] = HideSensitive(ctx, v)
}
}
for k, v := range testCase.TestCaseInput.Vars {
if !strings.HasPrefix(k, "venom.") {
testCase.TestCaseInput.Vars[k] = HideSensitive(ctx, v)
}
}
result.Raw = HideSensitive(ctx, fmt.Sprint(result.Raw))
result.Interpolated = HideSensitive(ctx, fmt.Sprint(result.Interpolated))
result.Systemout = HideSensitive(ctx, result.Systemout)
result.Systemerr = HideSensitive(ctx, result.Systemerr)
}
}
return testSuite
}
// OutputResult output result to sdtout, files...
func (v *Venom) OutputResult() error {
if v.OutputDir == "" {
return nil
}
cleanedTs := []TestSuite{}
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
ts := v.CleanUpSecrets(v.Tests.TestSuites[i])
cleanedTs = append(cleanedTs, ts)
testsResult := &Tests{
TestSuites: []TestSuite{ts},
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 {
return errors.Wrapf(err, "Error: cannot format output json (%s)", err)
}
case "tap":
data, err = outputTapFormat(*testsResult)
if err != nil {
return errors.Wrapf(err, "Error: cannot format output tap (%s)", err)
}
case "yml", "yaml":
data, err = yaml.Marshal(testsResult)
if err != nil {
return errors.Wrapf(err, "Error: cannot format output yaml (%s)", err)
}
case "xml":
data, err = outputXMLFormat(*testsResult, v.Verbose)
if err != nil {
return errors.Wrapf(err, "Error: cannot format output xml (%s)", err)
}
case "html":
return errors.New("Error: you have to use the --html-report flag")
}
fname := strings.TrimSuffix(ts.Filepath, filepath.Ext(ts.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: cleanedTs,
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 {
return errors.Wrapf(err, "Error: cannot format output html")
}
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 errors.Wrapf(err, "Error while creating file %s", filename)
}
}
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, verbose int) ([]byte, error) {
testsXML := TestsXML{}
for _, ts := range tests.TestSuites {
tsXML := TestSuiteXML{
Name: ts.Name,
Package: ts.Filepath,
Time: fmt.Sprintf("%f", ts.Duration),
}
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,
})
}
if len(result.Errors) > 0 {
appendCleanValue(&systemout.Value, result.Systemout)
} else if verbose > 1 {
appendCleanValue(&systemout.Value, result.Systemout)
}
appendCleanValue(&systemerr.Value, result.Systemerr)
}
tcXML := TestCaseXML{
Classname: ts.Filename,
Errors: failuresXML,
Name: tc.Name,
Skipped: tc.Skipped,
Systemout: systemout,
Systemerr: systemerr,
Time: tc.Duration,
ID: tc.ID,
}
tsXML.TestCases = append(tsXML.TestCases, tcXML)
}
testsXML.TestSuites = append(testsXML.TestSuites, tsXML)
}
dataxml, err := xml.MarshalIndent(testsXML, "", " ")
if err != nil {
errors.Wrapf(err, "Error: cannot format xml output")
}
data := append([]byte(`<?xml version="1.0" encoding="utf-8"?>`), dataxml...)
return data, nil
}
func appendCleanValue(dest *string, source string) {
cleanedValue := strings.ReplaceAll(source, "\x03", "")
*dest += cleanedValue
}