forked from ovh/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
venom_output.go
187 lines (168 loc) · 4.91 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
package venom
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"reflect"
"regexp"
"strings"
"time"
"github.com/acarl005/stripansi"
"github.com/fatih/color"
dump "github.com/fsamin/go-dump"
tap "github.com/mndrix/tap-go"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
var regexpSlug = regexp.MustCompile("[^a-z0-9]+")
func slug(s string) string {
return strings.Trim(regexpSlug.ReplaceAllString(strings.ToLower(s), "-"), "-")
}
// OutputResult output result to sdtout, files...
func (v *Venom) OutputResult(tests Tests, elapsed time.Duration) error {
var data []byte
var err error
v.outputResume(tests, elapsed)
cleanOutputColors(&tests)
switch v.OutputFormat {
case "json":
data, err = json.MarshalIndent(tests, "", " ")
if err != nil {
log.Fatalf("Error: cannot format output json (%s)", err)
}
case "tap":
data, err = outputTapFormat(tests)
if err != nil {
log.Fatalf("Error: cannot format output tap (%s)", err)
}
case "yml", "yaml":
data, err = yaml.Marshal(tests)
if err != nil {
log.Fatalf("Error: cannot format output yaml (%s)", err)
}
default:
dataxml, errm := xml.MarshalIndent(tests, "", " ")
if errm != nil {
log.Fatalf("Error: cannot format xml output: %s", errm)
}
data = append([]byte(`<?xml version="1.0" encoding="utf-8"?>`), dataxml...)
}
if v.OutputDir != "" {
v.PrintFunc("\n") // new line to display files written
filename := v.OutputDir + "/test_results." + v.OutputFormat
if err := ioutil.WriteFile(filename, data, 0644); err != nil {
return fmt.Errorf("Error while creating file %s: %v", filename, err)
}
v.PrintFunc("Writing file %s\n", filename)
for _, ts := range tests.TestSuites {
for _, tc := range ts.TestCases {
for _, f := range tc.Failures {
filename := v.OutputDir + "/" + slug(ts.ShortName) + "." + slug(tc.Name) + ".dump"
sdump := &bytes.Buffer{}
dumpEncoder := dump.NewEncoder(sdump)
dumpEncoder.ExtraFields.DetailedMap = false
dumpEncoder.ExtraFields.DetailedStruct = false
dumpEncoder.ExtraFields.Len = false
dumpEncoder.ExtraFields.Type = false
dumpEncoder.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultLowerCaseFormatter()}
//Try to pretty print only the result
var smartPrinted bool
for k, v := range f.Result {
if k == "result" && reflect.TypeOf(v).Kind() != reflect.String {
dumpEncoder.Fdump(v)
smartPrinted = true
break
}
}
//If not succeed print all the stuff
if !smartPrinted {
dumpEncoder.Fdump(f.Result)
}
output := f.Value + "\n ------ Result: \n" + sdump.String() + "\n ------ Variables:\n"
for k, v := range ts.Templater.Values {
output += fmt.Sprintf("%s:%s\n", k, v)
}
if err := ioutil.WriteFile(filename, []byte(output), 0644); err != nil {
return fmt.Errorf("Error while creating file %s: %v", filename, err)
}
v.PrintFunc("File %s is written\n", filename)
}
}
}
}
return nil
}
func outputTapFormat(tests Tests) ([]byte, error) {
tapValue := tap.New()
buf := new(bytes.Buffer)
tapValue.Writer = buf
tapValue.Header(tests.Total)
for _, ts := range tests.TestSuites {
for _, tc := range ts.TestCases {
name := ts.Name + " / " + tc.Name
if len(tc.Skipped) > 0 {
tapValue.Skip(1, name)
continue
}
if len(tc.Errors) > 0 {
tapValue.Fail(name)
for _, e := range tc.Errors {
tapValue.Diagnosticf("Error: %s", e.Value)
}
continue
}
if len(tc.Failures) > 0 {
tapValue.Fail(name)
for _, e := range tc.Failures {
tapValue.Diagnosticf("Failure: %s", e.Value)
}
continue
}
tapValue.Pass(name)
}
}
return buf.Bytes(), nil
}
func (v *Venom) outputResume(tests Tests, elapsed time.Duration) {
red := color.New(color.FgRed).SprintFunc()
totalTestCases := 0
totalTestSteps := 0
v.PrintFunc("\n")
for _, t := range tests.TestSuites {
totalTestCases += len(t.TestCases)
for _, tc := range t.TestCases {
totalTestSteps += len(tc.TestSteps)
}
if t.Failures > 0 || t.Errors > 0 {
v.PrintFunc("%s %s\n", red("FAILED"), t.Name)
for _, tc := range t.TestCases {
for _, f := range tc.Failures {
v.PrintFunc("%s\n", f.Value)
}
for _, f := range tc.Errors {
v.PrintFunc("%s\n", f.Value)
}
}
}
}
}
func cleanOutputColors(tests *Tests) {
testSuites := make([]TestSuite, 0, len(tests.TestSuites))
for _, testSuite := range tests.TestSuites {
testCases := make([]TestCase, 0, len(testSuite.TestCases))
for _, testCase := range testSuite.TestCases {
failures := make([]Failure, 0, len(testCase.Failures))
for _, failure := range testCase.Failures {
failure.Value = stripansi.Strip(failure.Value)
failures = append(failures, failure)
}
testCase.Failures = failures
testCases = append(testCases, testCase)
}
testSuite.TestCases = testCases
testSuites = append(testSuites, testSuite)
}
tests.TestSuites = testSuites
}