forked from vdobler/ht
/
suite.go
246 lines (209 loc) · 5.84 KB
/
suite.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
242
243
244
245
246
// Copyright 2016 Volker Dobler. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package suite
import (
"errors"
"fmt"
"io/ioutil"
"log"
"strconv"
"time"
"github.com/vdobler/ht/cookiejar"
"github.com/vdobler/ht/ht"
)
// A Suite is a collection of Tests which can be executed sequentily with the
// result captured.
type Suite struct {
Name string // Name of the Suite.
Description string // Description of what's going on here.
KeepCookies bool // KeepCookies in a cookie jar common to all Tests.
Status ht.Status // Status is the overall status of the whole suite.
Error error // Error encountered during execution of the suite.
Started time.Time // Start of the execution.
Duration time.Duration // Duration of the execution.
Tests []*ht.Test // The Tests to execute
Variables map[string]string // The initial variable assignemnt
FinalVariables map[string]string // The final set of variables.
Jar *cookiejar.Jar // The cookie jar used
Log *log.Logger // The logger used.
scope map[string]string
tests []*RawTest
}
func shouldRun(t int, rs *RawSuite, s *Suite) bool {
if !rs.tests[t].IsEnabled() {
return false
}
// Stop execution on errors during setup
for i := 0; i < len(rs.Setup) && i < len(s.Tests); i++ {
if s.Tests[i].Status > ht.Pass {
return false
}
}
return true
}
func newScope(outer, inner map[string]string, auto bool) map[string]string {
// 1. Copy of outer scope
scope := make(map[string]string, len(outer)+len(inner)+2)
for gn, gv := range outer {
scope[gn] = gv
}
if auto {
scope["COUNTER"] = strconv.Itoa(<-GetCounter)
scope["RANDOM"] = strconv.Itoa(100000 + ht.RandomIntn(900000))
}
replacer := varReplacer(scope)
// 2. Merging inner defaults, allow substitutions from outer scope
for name, val := range inner {
if _, ok := scope[name]; ok {
// Variable name exists in outer scope, do not
// overwrite with suite defaults.
continue
}
scope[name] = replacer.Replace(val)
}
return scope
}
// NewFromRaw sets up a new Suite from rs, read to be Iterated.
func NewFromRaw(rs *RawSuite, global map[string]string, jar *cookiejar.Jar, logger *log.Logger) *Suite {
// Create cookie jar if needed.
if rs.KeepCookies {
if jar == nil {
// Make own, private-use jar.
jar, _ = cookiejar.New(nil)
}
} else {
jar = nil
}
if logger == nil {
logger = log.New(ioutil.Discard, "", 0)
}
suite := &Suite{
KeepCookies: rs.KeepCookies,
Status: ht.NotRun,
Error: nil,
Tests: make([]*ht.Test, 0, len(rs.tests)),
Variables: make(map[string]string),
FinalVariables: make(map[string]string),
Jar: jar,
Log: logger,
tests: rs.tests,
}
suite.scope = newScope(global, rs.Variables, true)
suite.scope["SUITE_DIR"] = rs.File.Dirname()
suite.scope["SUITE_NAME"] = rs.File.Basename()
replacer := varReplacer(suite.scope)
suite.Name = replacer.Replace(rs.Name)
suite.Description = replacer.Replace(rs.Description)
for n, v := range suite.scope {
suite.Variables[n] = v
}
return suite
}
// A Executor is responsible for executing the given test during the
// Iterate'ion of a Suite. It should return nil if execution should continue
// and ErrAbortExecution to stop further iteration.
type Executor func(test *ht.Test) error
var (
ErrAbortExecution = errors.New("Abort Execution")
)
// Iterate the suite through the given executor.
func (suite *Suite) Iterate(executor Executor) {
now := time.Now()
now = now.Add(-time.Duration(now.Nanosecond()))
suite.Started = now
overall := ht.NotRun
errors := ht.ErrorList{}
for _, rt := range suite.tests {
// suite.Log.Printf("Executing Test %q\n", rt.File.Name)
callScope := newScope(suite.scope, rt.contextVars, true)
testScope := newScope(callScope, rt.Variables, false)
testScope["TEST_DIR"] = rt.File.Dirname()
testScope["TEST_NAME"] = rt.File.Basename()
test, err := rt.ToTest(testScope)
if err != nil {
test.Status = ht.Bogus
test.Error = err
}
test.Jar = suite.Jar
test.Log = suite.Log
exstat := executor(test)
if test.Status == ht.Pass {
suite.updateVariables(test)
}
suite.Tests = append(suite.Tests, test)
if test.Status > overall {
overall = test.Status
}
if err := test.Error; err != nil {
errors = append(errors, err)
}
if exstat == ErrAbortExecution {
break
}
}
suite.Duration = time.Since(suite.Started)
clip := suite.Duration.Nanoseconds() % 1000000
suite.Duration -= time.Duration(clip)
suite.Status = overall
if len(errors) == 0 {
suite.Error = nil
} else {
suite.Error = errors
}
for n, v := range suite.scope {
suite.FinalVariables[n] = v
}
}
func (suite *Suite) updateVariables(test *ht.Test) {
if test.Status != ht.Pass {
return
}
for varname, value := range test.Extract() {
if old, ok := suite.scope[varname]; ok {
if value != old {
suite.Log.Printf("Updating variable %q to %q\n",
varname, value)
} else {
suite.Log.Printf("Keeping variable %q as %q\n",
varname, value)
}
} else {
suite.Log.Printf("Setting variable %q to %q\n",
varname, value)
}
suite.scope[varname] = value
}
}
func (suite *Suite) updateStatus(test *ht.Test) {
if test.Status <= suite.Status {
return
}
suite.Status = test.Status
if test.Error != nil {
suite.Error = test.Error
}
}
// Stats counts the test results of s.
func (suite *Suite) Stats() (notRun int, skipped int, passed int, failed int, errored int, bogus int) {
for _, tr := range suite.Tests {
switch tr.Status {
case ht.NotRun:
notRun++
case ht.Skipped:
skipped++
case ht.Pass:
passed++
case ht.Fail:
failed++
case ht.Error:
errored++
case ht.Bogus:
bogus++
default:
panic(fmt.Sprintf("No such Status %d in suite %q test %q",
tr.Status, suite.Name, tr.Name))
}
}
return
}