forked from zoncoen/scenarigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
61 lines (56 loc) · 1.29 KB
/
setup.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
package scenarigo
import (
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/plugin"
)
type setupFuncList []setupFunc
type setupFunc struct {
name string
f plugin.SetupFunc
}
type teardownFunc struct {
name string
f func(*plugin.Context)
}
func (sl setupFuncList) setup(ctx *plugin.Context) (*plugin.Context, func(*plugin.Context)) {
if len(sl) == 0 {
return ctx, func(_ *plugin.Context) {}
}
var teardowns []teardownFunc
setupCtx := ctx
ctx.Run("setup", func(ctx *plugin.Context) {
for _, setup := range sl {
if ctx.Reporter().Failed() {
break
}
newCtx := ctx
ctx.Run(setup.name, func(ctx *context.Context) {
ctx, teardown := setup.f(ctx)
if ctx != nil {
newCtx = ctx
}
if teardown != nil {
teardowns = append(teardowns, teardownFunc{
name: setup.name,
f: teardown,
})
}
})
ctx = newCtx.WithReporter(ctx.Reporter())
}
setupCtx = ctx
})
ctx = setupCtx.WithReporter(ctx.Reporter())
if len(teardowns) == 0 {
return ctx, func(_ *plugin.Context) {}
}
return ctx, func(ctx *plugin.Context) {
ctx.Run("teardown", func(ctx *plugin.Context) {
for i := len(teardowns) - 1; i >= 0; i-- {
ctx.Run(teardowns[i].name, func(ctx *context.Context) {
teardowns[i].f(ctx)
})
}
})
}
}