-
Notifications
You must be signed in to change notification settings - Fork 344
/
specExecutor.go
414 lines (381 loc) · 13.8 KB
/
specExecutor.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE in the project root for license information.
*----------------------------------------------------------------*/
package execution
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/getgauge/gauge-proto/go/gauge_messages"
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/execution/event"
"github.com/getgauge/gauge/execution/result"
"github.com/getgauge/gauge/filter"
"github.com/getgauge/gauge/gauge"
"github.com/getgauge/gauge/logger"
"github.com/getgauge/gauge/parser"
"github.com/getgauge/gauge/plugin"
"github.com/getgauge/gauge/runner"
"github.com/getgauge/gauge/validation"
)
type specExecutor struct {
specification *gauge.Specification
runner runner.Runner
pluginHandler plugin.Handler
currentExecutionInfo *gauge_messages.ExecutionInfo
specResult *result.SpecResult
errMap *gauge.BuildErrors
stream int
scenarioExecutor executor
}
func newSpecExecutor(s *gauge.Specification, r runner.Runner, ph plugin.Handler, e *gauge.BuildErrors, stream int) *specExecutor {
ei := &gauge_messages.ExecutionInfo{
CurrentSpec: &gauge_messages.SpecInfo{
Name: s.Heading.Value,
FileName: s.FileName,
IsFailed: false,
Tags: getTagValue(s.Tags)},
ProjectName: filepath.Base(config.ProjectRoot),
NumberOfExecutionStreams: int32(NumberOfExecutionStreams),
RunnerId: int32(stream),
ExecutionArgs: gauge.ConvertToProtoExecutionArg(ExecutionArgs),
}
return &specExecutor{
specification: s,
runner: r,
pluginHandler: ph,
errMap: e,
stream: stream,
currentExecutionInfo: ei,
scenarioExecutor: newScenarioExecutor(r, ph, ei, e, s.Contexts, s.TearDownSteps, stream),
}
}
func (e *specExecutor) execute(executeBefore, execute, executeAfter bool) *result.SpecResult {
e.specResult = gauge.NewSpecResult(e.specification)
if e.runner.Info().Killed {
e.specResult.SetSkipped(true)
return e.specResult
}
if errs, ok := e.errMap.SpecErrs[e.specification]; ok {
if hasParseError(errs) {
e.failSpec()
return e.specResult
}
}
lookup, err := e.dataTableLookup()
if err != nil {
logger.Fatalf(true, "Failed to resolve Specifications : %s", err.Error())
}
resolvedSpecItems, err := resolveItems(e.specification.GetSpecItems(), lookup, e.setSkipInfo)
if err != nil {
logger.Fatalf(true, "Failed to resolve Specifications : %s", err.Error())
}
e.specResult.AddSpecItems(resolvedSpecItems)
if executeBefore {
event.Notify(event.NewExecutionEvent(event.SpecStart, e.specification, e.specResult, e.stream, e.currentExecutionInfo))
if _, ok := e.errMap.SpecErrs[e.specification]; !ok {
if res := e.initSpecDataStore(); res.GetFailed() {
e.skipSpecForError(fmt.Errorf("Failed to initialize spec datastore. Error: %s", res.GetErrorMessage()))
} else {
e.notifyBeforeSpecHook()
}
} else {
e.specResult.SetSkipped(true)
e.specResult.Errors = e.convertErrors(e.errMap.SpecErrs[e.specification])
}
}
if execute && !e.specResult.GetFailed() {
if e.specification.DataTable.Table.GetRowCount() == 0 {
others, tableDriven := parser.FilterTableRelatedScenarios(e.specification.Scenarios, func(s *gauge.Scenario) bool {
return s.ScenarioDataTableRow.IsInitialized()
})
results, err := e.executeScenarios(others)
if err != nil {
logger.Fatalf(true, "Failed to resolve Specifications : %s", err.Error())
}
e.specResult.AddScenarioResults(results)
scnMap := make(map[int]bool)
for _, s := range tableDriven {
if _, ok := scnMap[s.Span.Start]; !ok {
scnMap[s.Span.Start] = true
}
r, err := e.executeScenario(s)
if err != nil {
logger.Fatalf(true, "Failed to resolve Specifications : %s", err.Error())
}
e.specResult.AddTableDrivenScenarioResult(r, gauge.ConvertToProtoTable(s.DataTable.Table),
s.ScenarioDataTableRowIndex, s.SpecDataTableRowIndex, s.SpecDataTableRow.IsInitialized())
}
e.specResult.ScenarioCount += len(scnMap)
} else {
err := e.executeSpec()
if err != nil {
logger.Fatalf(true, "Failed to execute Specification %s : %s", e.specification.Heading.Value, err.Error())
}
}
}
e.specResult.SetSkipped(e.specResult.Skipped || e.specResult.ScenarioSkippedCount == len(e.specification.Scenarios))
if executeAfter {
if _, ok := e.errMap.SpecErrs[e.specification]; !ok {
e.notifyAfterSpecHook()
}
event.Notify(event.NewExecutionEvent(event.SpecEnd, e.specification, e.specResult, e.stream, e.currentExecutionInfo))
}
return e.specResult
}
func (e *specExecutor) executeTableRelatedScenarios(scenarios []*gauge.Scenario) error {
if len(scenarios) > 0 {
index := e.specification.Scenarios[0].SpecDataTableRowIndex
sceRes, err := e.executeScenarios(scenarios)
if err != nil {
return err
}
specResult := [][]result.Result{sceRes}
e.specResult.AddTableRelatedScenarioResult(specResult, index)
}
return nil
}
func (e *specExecutor) executeSpec() error {
parser.GetResolvedDataTablerows(e.specification.DataTable.Table)
nonTableRelatedScenarios, tableRelatedScenarios := parser.FilterTableRelatedScenarios(e.specification.Scenarios, func(s *gauge.Scenario) bool {
return s.SpecDataTableRow.IsInitialized()
})
res, err := e.executeScenarios(nonTableRelatedScenarios)
if err != nil {
return err
}
e.specResult.AddScenarioResults(res)
err = e.executeTableRelatedScenarios(tableRelatedScenarios)
if err != nil {
return err
}
return nil
}
func (e *specExecutor) initSpecDataStore() *gauge_messages.ProtoExecutionResult {
initSpecDataStoreMessage := &gauge_messages.Message{MessageType: gauge_messages.Message_SpecDataStoreInit,
SpecDataStoreInitRequest: &gauge_messages.SpecDataStoreInitRequest{Stream: int32(e.stream)}}
return e.runner.ExecuteAndGetStatus(initSpecDataStoreMessage)
}
func (e *specExecutor) notifyBeforeSpecHook() {
m := &gauge_messages.Message{MessageType: gauge_messages.Message_SpecExecutionStarting,
SpecExecutionStartingRequest: &gauge_messages.SpecExecutionStartingRequest{CurrentExecutionInfo: e.currentExecutionInfo, Stream: int32(e.stream)}}
e.pluginHandler.NotifyPlugins(m)
res := executeHook(m, e.specResult, e.runner)
e.specResult.ProtoSpec.PreHookMessages = res.Message
e.specResult.ProtoSpec.PreHookScreenshotFiles = res.ScreenshotFiles
if res.GetFailed() {
setSpecFailure(e.currentExecutionInfo)
handleHookFailure(e.specResult, res, result.AddPreHook)
}
m.SpecExecutionStartingRequest.SpecResult = gauge.ConvertToProtoSpecResult(e.specResult)
e.pluginHandler.NotifyPlugins(m)
}
func (e *specExecutor) notifyAfterSpecHook() {
e.currentExecutionInfo.CurrentScenario = nil
m := &gauge_messages.Message{MessageType: gauge_messages.Message_SpecExecutionEnding,
SpecExecutionEndingRequest: &gauge_messages.SpecExecutionEndingRequest{CurrentExecutionInfo: e.currentExecutionInfo, Stream: int32(e.stream)}}
res := executeHook(m, e.specResult, e.runner)
e.specResult.ProtoSpec.PostHookMessages = res.Message
e.specResult.ProtoSpec.PostHookScreenshotFiles = res.ScreenshotFiles
if res.GetFailed() {
setSpecFailure(e.currentExecutionInfo)
handleHookFailure(e.specResult, res, result.AddPostHook)
}
m.SpecExecutionEndingRequest.SpecResult = gauge.ConvertToProtoSpecResult(e.specResult)
e.pluginHandler.NotifyPlugins(m)
}
func (e *specExecutor) skipSpecForError(err error) {
logger.Errorf(true, err.Error())
validationError := validation.NewStepValidationError(&gauge.Step{LineNo: e.specification.Heading.LineNo, LineText: e.specification.Heading.Value},
err.Error(), e.specification.FileName, nil, "")
for _, scenario := range e.specification.Scenarios {
e.errMap.ScenarioErrs[scenario] = []error{validationError}
}
e.errMap.SpecErrs[e.specification] = []error{validationError}
e.specResult.Errors = e.convertErrors(e.errMap.SpecErrs[e.specification])
e.specResult.SetSkipped(true)
}
func (e *specExecutor) failSpec() {
e.specResult.Errors = e.convertErrors(e.errMap.SpecErrs[e.specification])
e.specResult.SetFailure()
}
func (e *specExecutor) convertErrors(specErrors []error) []*gauge_messages.Error {
var errors []*gauge_messages.Error
for _, e := range specErrors {
switch err := e.(type) {
case parser.ParseError:
errors = append(errors, &gauge_messages.Error{
Message: err.Error(),
LineNumber: int32(err.LineNo),
Filename: err.FileName,
Type: gauge_messages.Error_PARSE_ERROR,
})
case validation.StepValidationError, validation.SpecValidationError:
errors = append(errors, &gauge_messages.Error{
Message: e.Error(),
Type: gauge_messages.Error_VALIDATION_ERROR,
})
}
}
return errors
}
func (e *specExecutor) setSkipInfo(protoStep *gauge_messages.ProtoStep, step *gauge.Step) {
protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{}
protoStep.StepExecutionResult.Skipped = false
if _, ok := e.errMap.StepErrs[step]; ok {
protoStep.StepExecutionResult.Skipped = true
protoStep.StepExecutionResult.SkippedReason = "Step implementation not found"
}
}
func (e *specExecutor) getItemsForScenarioExecution(steps []*gauge.Step) ([]*gauge_messages.ProtoItem, error) {
items := make([]gauge.Item, len(steps))
for i, context := range steps {
items[i] = context
}
lookup, err := e.dataTableLookup()
if err != nil {
return nil, err
}
return resolveItems(items, lookup, e.setSkipInfo)
}
func (e *specExecutor) dataTableLookup() (*gauge.ArgLookup, error) {
l := new(gauge.ArgLookup)
err := l.ReadDataTableRow(e.specification.DataTable.Table, 0)
return l, err
}
func (e *specExecutor) executeScenarios(scenarios []*gauge.Scenario) ([]result.Result, error) {
var scenarioResults []result.Result
for _, scenario := range scenarios {
sceResult, err := e.executeScenario(scenario)
if err != nil {
return nil, err
}
scenarioResults = append(scenarioResults, sceResult)
}
return scenarioResults, nil
}
func (e *specExecutor) executeScenario(scenario *gauge.Scenario) (*result.ScenarioResult, error) {
var scenarioResult *result.ScenarioResult
shouldRetry := RetryOnlyTags == ""
if !shouldRetry {
spec := e.specification
tagValues := make([]string, 0)
if spec.Tags != nil {
tagValues = spec.Tags.Values()
}
specFilter := filter.NewScenarioFilterBasedOnTags(tagValues, RetryOnlyTags)
shouldRetry = !(specFilter.Filter(scenario))
}
retriesCount := 0
for i := 0; i < MaxRetriesCount; i++ {
e.currentExecutionInfo.CurrentScenario = &gauge_messages.ScenarioInfo{
Name: scenario.Heading.Value,
Tags: getTagValue(scenario.Tags),
IsFailed: false,
}
scenarioResult = &result.ScenarioResult{
ProtoScenario: gauge.NewProtoScenario(scenario),
ScenarioDataTableRow: gauge.ConvertToProtoTable(&scenario.ScenarioDataTableRow),
ScenarioDataTableRowIndex: scenario.ScenarioDataTableRowIndex,
ScenarioDataTable: gauge.ConvertToProtoTable(scenario.DataTable.Table),
}
if err := e.addAllItemsForScenarioExecution(scenario, scenarioResult); err != nil {
return nil, err
}
e.scenarioExecutor.execute(scenario, scenarioResult)
retriesCount++
if scenarioResult.ProtoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_SKIPPED {
e.specResult.ScenarioSkippedCount++
}
if !(shouldRetry && scenarioResult.GetFailed()) {
break
}
}
scenarioResult.ProtoScenario.RetriesCount = int64(retriesCount)
return scenarioResult, nil
}
func (e *specExecutor) addAllItemsForScenarioExecution(scenario *gauge.Scenario, scenarioResult *result.ScenarioResult) error {
contexts, err := e.getItemsForScenarioExecution(e.specification.Contexts)
if err != nil {
return err
}
scenarioResult.AddContexts(contexts)
tearDownSteps, err := e.getItemsForScenarioExecution(e.specification.TearDownSteps)
if err != nil {
return err
}
scenarioResult.AddTearDownSteps(tearDownSteps)
lookup, err := e.dataTableLookup()
if err != nil {
return err
}
if scenario.ScenarioDataTableRow.IsInitialized() {
parser.GetResolvedDataTablerows(&scenario.ScenarioDataTableRow)
if err = lookup.ReadDataTableRow(&scenario.ScenarioDataTableRow, 0); err != nil {
return err
}
}
items, err := resolveItems(scenario.Items, lookup, e.setSkipInfo)
if err != nil {
return err
}
scenarioResult.AddItems(items)
return nil
}
func getTagValue(tags *gauge.Tags) []string {
var tagValues []string
if tags != nil {
tagValues = append(tagValues, tags.Values()...)
}
return tagValues
}
func setSpecFailure(executionInfo *gauge_messages.ExecutionInfo) {
executionInfo.CurrentSpec.IsFailed = true
}
func shouldExecuteForRow(i int) bool {
if len(tableRowsIndexes) < 1 {
return true
}
for _, index := range tableRowsIndexes {
if index == i {
return true
}
}
return false
}
func getDataTableRows(tableRows string) (tableRowIndexes []int) {
switch {
case strings.TrimSpace(tableRows) == "":
return
case strings.Contains(tableRows, "-"):
indexes := strings.Split(tableRows, "-")
startRow, _ := strconv.Atoi(strings.TrimSpace(indexes[0]))
endRow, _ := strconv.Atoi(strings.TrimSpace(indexes[1]))
for i := startRow - 1; i < endRow; i++ {
tableRowIndexes = append(tableRowIndexes, i)
}
default:
indexes := strings.Split(tableRows, ",")
for _, i := range indexes {
rowNumber, _ := strconv.Atoi(strings.TrimSpace(i))
tableRowIndexes = append(tableRowIndexes, rowNumber-1)
}
}
return
}
func executeHook(message *gauge_messages.Message, execTimeTracker result.ExecTimeTracker, r runner.Runner) *gauge_messages.ProtoExecutionResult {
executionResult := r.ExecuteAndGetStatus(message)
execTimeTracker.AddExecTime(executionResult.GetExecutionTime())
return executionResult
}
func hasParseError(errs []error) bool {
for _, e := range errs {
if _, ok := e.(parser.ParseError); ok {
return true
}
}
return false
}