forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.go
195 lines (167 loc) · 5.28 KB
/
execute.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
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package execution
import (
"strconv"
"time"
"github.com/getgauge/gauge/skel"
"fmt"
"strings"
"os"
"sync"
"runtime/debug"
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/env"
"github.com/getgauge/gauge/execution/event"
"github.com/getgauge/gauge/execution/rerun"
"github.com/getgauge/gauge/execution/result"
"github.com/getgauge/gauge/gauge"
"github.com/getgauge/gauge/logger"
"github.com/getgauge/gauge/manifest"
"github.com/getgauge/gauge/plugin"
"github.com/getgauge/gauge/plugin/install"
"github.com/getgauge/gauge/reporter"
"github.com/getgauge/gauge/runner"
"github.com/getgauge/gauge/util"
"github.com/getgauge/gauge/validation"
)
var NumberOfExecutionStreams int
var InParallel bool
type suiteExecutor interface {
run() *result.SuiteResult
}
type executor interface {
execute(i gauge.Item, r result.Result)
}
type executionInfo struct {
manifest *manifest.Manifest
specs *gauge.SpecCollection
runner runner.Runner
pluginHandler plugin.Handler
errMaps *gauge.BuildErrors
inParallel bool
numberOfStreams int
stream int
}
func newExecutionInfo(s *gauge.SpecCollection, r runner.Runner, ph plugin.Handler, e *gauge.BuildErrors, p bool, stream int) *executionInfo {
m, err := manifest.ProjectManifest()
if err != nil {
logger.Fatalf(err.Error())
}
return &executionInfo{
manifest: m,
specs: s,
runner: r,
pluginHandler: ph,
errMaps: e,
inParallel: p,
numberOfStreams: NumberOfExecutionStreams,
stream: stream,
}
}
func ExecuteSpecs(specDirs []string) int {
err := validateFlags()
if err != nil {
logger.Fatalf(err.Error())
}
if config.CheckUpdates() {
i := &install.UpdateFacade{}
i.BufferUpdateDetails()
defer i.PrintUpdateBuffer()
}
skel.SetupPlugins()
res := validation.ValidateSpecs(specDirs, false)
if len(res.Errs) > 0 {
return 1
}
if res.SpecCollection.Size() < 1 {
logger.Infof("No specifications found in %s.", strings.Join(specDirs, ", "))
res.Runner.Kill()
if res.ParseOk {
return 0
}
return 1
}
event.InitRegistry()
wg := &sync.WaitGroup{}
reporter.ListenExecutionEvents(wg)
rerun.ListenFailedScenarios(wg)
if util.ConvertToBool(os.Getenv(env.SaveExecutionResult), env.SaveExecutionResult, false) {
ListenSuiteEndAndSaveResult(wg)
}
defer wg.Wait()
defer recoverPanic()
ei := newExecutionInfo(res.SpecCollection, res.Runner, nil, res.ErrMap, InParallel, 0)
e := newExecution(ei)
return printExecutionStatus(e.run(), res.ParseOk)
}
func recoverPanic() {
if r := recover(); r != nil {
logger.Infof("%v\n%s", r, string(debug.Stack()))
os.Exit(1)
}
}
func Execute(s *gauge.SpecCollection, r runner.Runner, ph plugin.Handler, e *gauge.BuildErrors, p bool, n int) {
newExecution(newExecutionInfo(s, r, ph, e, p, n)).run()
}
func newExecution(executionInfo *executionInfo) suiteExecutor {
if executionInfo.inParallel {
return newParallelExecution(executionInfo)
}
return newSimpleExecution(executionInfo, true)
}
func printExecutionStatus(suiteResult *result.SuiteResult, isParsingOk bool) int {
nSkippedSpecs := suiteResult.SpecsSkippedCount
var nExecutedSpecs int
if len(suiteResult.SpecResults) != 0 {
nExecutedSpecs = len(suiteResult.SpecResults) - nSkippedSpecs
}
nFailedSpecs := suiteResult.SpecsFailedCount
nPassedSpecs := nExecutedSpecs - nFailedSpecs
nExecutedScenarios := 0
nFailedScenarios := 0
nPassedScenarios := 0
nSkippedScenarios := 0
for _, specResult := range suiteResult.SpecResults {
nExecutedScenarios += specResult.ScenarioCount
nFailedScenarios += specResult.ScenarioFailedCount
nSkippedScenarios += specResult.ScenarioSkippedCount
}
nExecutedScenarios -= nSkippedScenarios
nPassedScenarios = nExecutedScenarios - nFailedScenarios
if nExecutedScenarios < 0 {
nExecutedScenarios = 0
}
if nPassedScenarios < 0 {
nPassedScenarios = 0
}
logger.Infof("Specifications:\t%d executed\t%d passed\t%d failed\t%d skipped", nExecutedSpecs, nPassedSpecs, nFailedSpecs, nSkippedSpecs)
logger.Infof("Scenarios:\t%d executed\t%d passed\t%d failed\t%d skipped", nExecutedScenarios, nPassedScenarios, nFailedScenarios, nSkippedScenarios)
logger.Infof("\nTotal time taken: %s", time.Millisecond*time.Duration(suiteResult.ExecutionTime))
if suiteResult.IsFailed || !isParsingOk {
return 1
}
return 0
}
func validateFlags() error {
if !InParallel {
return nil
}
if NumberOfExecutionStreams < 1 {
return fmt.Errorf("Invalid input(%s) to --n flag.", strconv.Itoa(NumberOfExecutionStreams))
}
if !isValidStrategy(Strategy) {
return fmt.Errorf("Invalid input(%s) to --strategy flag.", Strategy)
}
return nil
}