forked from ovh/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
venom_output.go
100 lines (89 loc) · 2.16 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
package venom
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
"github.com/fatih/color"
tap "github.com/mndrix/tap-go"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
func init() {
if 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(tests Tests, elapsed time.Duration) error {
if v.OutputDir == "" {
return nil
}
var data []byte
var err error
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...)
}
filename := path.Join(v.OutputDir, "test_results."+v.OutputFormat)
if err := ioutil.WriteFile(filename, data, 0600); err != nil {
return fmt.Errorf("Error while creating file %s: %v", filename, err)
}
v.PrintFunc("Writing file %s\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
}