forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.go
277 lines (233 loc) · 6.28 KB
/
run_test.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
package godog
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/DATA-DOG/godog/colors"
"github.com/DATA-DOG/godog/gherkin"
)
func okStep() error {
return nil
}
func TestPrintsStepDefinitions(t *testing.T) {
var buf bytes.Buffer
w := colors.Uncolored(&buf)
s := &Suite{}
steps := []string{
"^passing step$",
`^with name "([^"])"`,
}
for _, step := range steps {
s.Step(step, okStep)
}
s.printStepDefinitions(w)
out := buf.String()
ref := `okStep`
for i, def := range strings.Split(strings.TrimSpace(out), "\n") {
if idx := strings.Index(def, steps[i]); idx == -1 {
t.Fatalf(`step "%s" was not found in output`, steps[i])
}
if idx := strings.Index(def, ref); idx == -1 {
t.Fatalf(`step definition reference "%s" was not found in output: "%s"`, ref, def)
}
}
}
func TestPrintsNoStepDefinitionsIfNoneFound(t *testing.T) {
var buf bytes.Buffer
w := colors.Uncolored(&buf)
s := &Suite{}
s.printStepDefinitions(w)
out := strings.TrimSpace(buf.String())
if out != "there were no contexts registered, could not find any step definition.." {
t.Fatalf("expected output does not match to: %s", out)
}
}
func TestFailsOrPassesBasedOnStrictModeWhenHasPendingSteps(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
r := runner{
fmt: progressFunc("progress", ioutil.Discard),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() error { return ErrPending })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
r.strict = true
if !r.run() {
t.Fatal("the suite should have failed")
}
}
func TestFailsOrPassesBasedOnStrictModeWhenHasUndefinedSteps(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
r := runner{
fmt: progressFunc("progress", ioutil.Discard),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
// two - is undefined
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
r.strict = true
if !r.run() {
t.Fatal("the suite should have failed")
}
}
func TestShouldFailOnError(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
r := runner{
fmt: progressFunc("progress", ioutil.Discard),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() error { return fmt.Errorf("error") })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
}
func TestFailsWithConcurrencyOptionError(t *testing.T) {
stderr, closer := bufErrorPipe(t)
defer closer()
defer stderr.Close()
opt := Options{
Format: "pretty",
Paths: []string{"features/load:6"},
Concurrency: 2,
Output: ioutil.Discard,
}
status := RunWithOptions("fails", func(_ *Suite) {}, opt)
if status != exitOptionError {
t.Fatalf("expected exit status to be 2, but was: %d", status)
}
closer()
b, err := ioutil.ReadAll(stderr)
if err != nil {
t.Fatal(err)
}
out := strings.TrimSpace(string(b))
if out != `format "pretty" does not support concurrent execution` {
t.Fatalf("unexpected error output: \"%s\"", out)
}
}
func TestFailsWithUnknownFormatterOptionError(t *testing.T) {
stderr, closer := bufErrorPipe(t)
defer closer()
defer stderr.Close()
opt := Options{
Format: "unknown",
Paths: []string{"features/load:6"},
Output: ioutil.Discard,
}
status := RunWithOptions("fails", func(_ *Suite) {}, opt)
if status != exitOptionError {
t.Fatalf("expected exit status to be 2, but was: %d", status)
}
closer()
b, err := ioutil.ReadAll(stderr)
if err != nil {
t.Fatal(err)
}
out := strings.TrimSpace(string(b))
if !strings.Contains(out, `unregistered formatter name: "unknown", use one of`) {
t.Fatalf("unexpected error output: %q", out)
}
}
func TestFailsWithOptionErrorWhenLookingForFeaturesInUnavailablePath(t *testing.T) {
stderr, closer := bufErrorPipe(t)
defer closer()
defer stderr.Close()
opt := Options{
Format: "progress",
Paths: []string{"unavailable"},
Output: ioutil.Discard,
}
status := RunWithOptions("fails", func(_ *Suite) {}, opt)
if status != exitOptionError {
t.Fatalf("expected exit status to be 2, but was: %d", status)
}
closer()
b, err := ioutil.ReadAll(stderr)
if err != nil {
t.Fatal(err)
}
out := strings.TrimSpace(string(b))
if out != `feature path "unavailable" is not available` {
t.Fatalf("unexpected error output: \"%s\"", out)
}
}
func TestByDefaultRunsFeaturesPath(t *testing.T) {
opt := Options{
Format: "progress",
Output: ioutil.Discard,
Strict: true,
}
status := RunWithOptions("fails", func(_ *Suite) {}, opt)
// should fail in strict mode due to undefined steps
if status != exitFailure {
t.Fatalf("expected exit status to be 1, but was: %d", status)
}
opt.Strict = false
status = RunWithOptions("succeeds", func(_ *Suite) {}, opt)
// should succeed in non strict mode due to undefined steps
if status != exitSuccess {
t.Fatalf("expected exit status to be 0, but was: %d", status)
}
}
func bufErrorPipe(t *testing.T) (io.ReadCloser, func()) {
stderr := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stderr = w
return r, func() {
w.Close()
os.Stderr = stderr
}
}
func TestFeatureFilePathParser(t *testing.T) {
type Case struct {
input string
path string
line int
}
cases := []Case{
{"/home/test.feature", "/home/test.feature", -1},
{"/home/test.feature:21", "/home/test.feature", 21},
{"test.feature", "test.feature", -1},
{"test.feature:2", "test.feature", 2},
{"", "", -1},
{"/c:/home/test.feature", "/c:/home/test.feature", -1},
{"/c:/home/test.feature:3", "/c:/home/test.feature", 3},
{"D:\\home\\test.feature:3", "D:\\home\\test.feature", 3},
}
for i, c := range cases {
p, ln := extractFeaturePathLine(c.input)
if p != c.path {
t.Fatalf(`result path "%s" != "%s" at %d`, p, c.path, i)
}
if ln != c.line {
t.Fatalf(`result line "%d" != "%d" at %d`, ln, c.line, i)
}
}
}