-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
178 lines (155 loc) · 3.6 KB
/
process.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
package venom
import (
"fmt"
"strings"
"sync"
log "github.com/sirupsen/logrus"
)
func (v *Venom) init() error {
v.testsuites = []TestSuite{}
switch v.LogLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "error":
log.SetLevel(log.WarnLevel)
default:
log.SetLevel(log.WarnLevel)
}
log.SetOutput(v.LogOutput)
return nil
}
// Parse parses tests suite to check context and variables
func (v *Venom) Parse(path []string, exclude []string) error {
if err := v.init(); err != nil {
return err
}
filesPath, err := getFilesPath(path, exclude)
if err != nil {
return err
}
if err := v.readFiles(filesPath); err != nil {
return err
}
missingVars := []string{}
extractedVars := []string{}
for i := range v.testsuites {
ts := &v.testsuites[i]
log.Info("Parsing testsuite ", ts.Package)
tvars, textractedVars, err := v.parseTestSuite(ts)
if err != nil {
return err
}
for k := range ts.Vars {
textractedVars = append(textractedVars, k)
}
for _, k := range tvars {
var found bool
for i := 0; i < len(missingVars); i++ {
if missingVars[i] == k {
found = true
break
}
}
if !found {
missingVars = append(missingVars, k)
}
}
for _, k := range textractedVars {
var found bool
for i := 0; i < len(extractedVars); i++ {
if extractedVars[i] == k {
found = true
break
}
}
if !found {
extractedVars = append(extractedVars, k)
}
}
}
reallyMissingVars := []string{}
for _, k := range missingVars {
var varExtracted bool
for _, e := range extractedVars {
if strings.HasPrefix(k, e) {
varExtracted = true
}
}
if !varExtracted {
var ignored bool
// ignore {{.venom.var..}}
if strings.HasPrefix(k, "venom.") {
continue
}
for _, i := range v.IgnoreVariables {
if strings.HasPrefix(k, i) {
ignored = true
break
}
}
if !ignored {
reallyMissingVars = append(reallyMissingVars, k)
}
}
}
if len(reallyMissingVars) > 0 {
return fmt.Errorf("Missing variables %v", reallyMissingVars)
}
return nil
}
// Process runs tests suite and return a Tests result
func (v *Venom) Process(path []string, exclude []string) (*Tests, error) {
if err := v.init(); err != nil {
return nil, err
}
filesPath, err := getFilesPath(path, exclude)
if err != nil {
return nil, err
}
if err := v.readFiles(filesPath); err != nil {
return nil, err
}
chanEnd := make(chan *TestSuite, 1)
parallels := make(chan *TestSuite, v.Parallel) //Run testsuite in parrallel
wg := sync.WaitGroup{}
testsResult := &Tests{}
wg.Add(len(filesPath))
chanToRun := make(chan *TestSuite, len(filesPath)+1)
go v.computeStats(testsResult, chanEnd, &wg)
go func() {
for ts := range chanToRun {
parallels <- ts
go func(ts *TestSuite) {
v.runTestSuite(ts)
chanEnd <- ts
<-parallels
}(ts)
}
}()
for i := range v.testsuites {
chanToRun <- &v.testsuites[i]
}
wg.Wait()
return testsResult, nil
}
func (v *Venom) computeStats(testsResult *Tests, chanEnd <-chan *TestSuite, wg *sync.WaitGroup) {
for t := range chanEnd {
testsResult.TestSuites = append(testsResult.TestSuites, *t)
if t.Failures > 0 || t.Errors > 0 {
testsResult.TotalKO += (t.Failures + t.Errors)
} else {
testsResult.TotalOK += len(t.TestCases) - (t.Failures + t.Errors)
}
if t.Skipped > 0 {
testsResult.TotalSkipped += t.Skipped
}
testsResult.Total = testsResult.TotalKO + testsResult.TotalOK + testsResult.TotalSkipped
wg.Done()
}
}
func rightPad(s string, padStr string, pLen int) string {
o := s + strings.Repeat(padStr, pLen)
return o[0:pLen]
}