forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
124 lines (110 loc) · 2.46 KB
/
run.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
package godog
import (
"fmt"
"os"
)
type initializer func(*Suite)
type runner struct {
stopOnFailure bool
features []*feature
fmt Formatter // needs to support concurrency
initializer initializer
}
func (r *runner) concurrent(rate int) (failed bool) {
queue := make(chan int, rate)
for i, ft := range r.features {
queue <- i // reserve space in queue
go func(fail *bool, feat *feature) {
defer func() {
<-queue // free a space in queue
}()
if r.stopOnFailure && *fail {
return
}
suite := &Suite{
fmt: r.fmt,
stopOnFailure: r.stopOnFailure,
features: []*feature{feat},
}
r.initializer(suite)
suite.run()
if suite.failed {
*fail = true
}
}(&failed, ft)
}
// wait until last are processed
for i := 0; i < rate; i++ {
queue <- i
}
close(queue)
// print summary
r.fmt.Summary()
return
}
func (r *runner) run() (failed bool) {
suite := &Suite{
fmt: r.fmt,
stopOnFailure: r.stopOnFailure,
features: r.features,
}
r.initializer(suite)
suite.run()
r.fmt.Summary()
return suite.failed
}
// Run creates and runs the feature suite.
// uses contextInitializer to register contexts
//
// the concurrency option allows runner to
// initialize a number of suites to be run
// separately. Only progress formatter
// is supported when concurrency level is
// higher than 1
//
// contextInitializer must be able to register
// the step definitions and event handlers.
func Run(contextInitializer func(suite *Suite)) int {
var defs, sof, noclr bool
var tags, format string
var concurrency int
flagSet := FlagSet(&format, &tags, &defs, &sof, &noclr, &concurrency)
err := flagSet.Parse(os.Args[1:])
fatal(err)
if defs {
s := &Suite{}
contextInitializer(s)
s.printStepDefinitions()
return 0
}
paths := flagSet.Args()
if len(paths) == 0 {
inf, err := os.Stat("features")
if err == nil && inf.IsDir() {
paths = []string{"features"}
}
}
if concurrency > 1 && format != "progress" {
fatal(fmt.Errorf("when concurrency level is higher than 1, only progress format is supported"))
}
formatter, err := findFmt(format)
fatal(err)
features, err := parseFeatures(tags, paths)
fatal(err)
r := runner{
fmt: formatter,
initializer: contextInitializer,
features: features,
stopOnFailure: sof,
}
var failed bool
if concurrency > 1 {
failed = r.concurrent(concurrency)
} else {
failed = r.run()
}
if failed {
return 1
}
return 0
}