forked from zoncoen/scenarigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.go
153 lines (145 loc) · 3.79 KB
/
step.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
package scenarigo
import (
"fmt"
"path/filepath"
"time"
"github.com/zoncoen/scenarigo/assert"
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/errors"
"github.com/zoncoen/scenarigo/plugin"
"github.com/zoncoen/scenarigo/reporter"
"github.com/zoncoen/scenarigo/schema"
)
func runStep(ctx *context.Context, scenario *schema.Scenario, s *schema.Step, stepIdx int) *context.Context {
if s.Vars != nil {
vars, err := ctx.ExecuteTemplate(s.Vars)
if err != nil {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.WrapPath(
err,
fmt.Sprintf("steps[%d].vars", stepIdx),
"invalid vars",
),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
ctx = ctx.WithVars(vars)
}
if s.Secrets != nil {
secrets, err := ctx.ExecuteTemplate(s.Secrets)
if err != nil {
ctx.Reporter().Fatalf(
"invalid secrets: %s",
errors.WithNodeAndColored(
errors.WithPath(err, fmt.Sprintf("steps[%d].secrets", stepIdx)),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
ctx = ctx.WithSecrets(secrets)
}
if s.Include != "" {
baseDir := filepath.Dir(scenario.Filepath())
include := filepath.Join(baseDir, s.Include)
scenarios, err := schema.LoadScenarios(include)
if err != nil {
ctx.Reporter().Fatalf(`failed to include "%s" as step: %s`, s.Include, err)
}
if len(scenarios) != 1 {
ctx.Reporter().Fatalf(`failed to include "%s" as step: must be a scenario`, s.Include)
}
testName, err := filepath.Rel(baseDir, include)
if err != nil {
ctx.Reporter().Fatalf(`failed to include "%s" as step: %s`, s.Include, err)
}
currentNode := ctx.Node()
ctx.Reporter().Run(testName, func(rptr reporter.Reporter) {
ctx = RunScenario(ctx.WithReporter(rptr).WithNode(scenarios[0].Node), scenarios[0])
})
if ctx.Reporter().Failed() {
ctx.Reporter().FailNow()
}
// back node to current node
ctx = ctx.WithNode(currentNode)
return ctx
}
if s.Ref != nil {
x, err := ctx.ExecuteTemplate(s.Ref)
if err != nil {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.WrapPathf(
err,
fmt.Sprintf("steps[%d].ref", stepIdx),
`failed to reference "%s" as step`, s.Ref,
),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
stp, ok := x.(plugin.Step)
if !ok {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.ErrorPathf(
fmt.Sprintf("steps[%d].ref", stepIdx),
`failed to reference "%s" as step: not implement plugin.Step interface`, s.Ref,
),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
startTime := time.Now()
ctx = stp.Run(ctx, s)
ctx.Reporter().Logf("Run %s: elapsed time %f sec", s.Ref, time.Since(startTime).Seconds())
return ctx
}
return invokeAndAssert(ctx, s, stepIdx)
}
func invokeAndAssert(ctx *context.Context, s *schema.Step, stepIdx int) *context.Context {
reqTime := time.Now()
newCtx, resp, err := s.Request.Invoke(ctx)
ctx.Reporter().Logf("elapsed time: %f sec", time.Since(reqTime).Seconds())
if err != nil {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.WithPath(err, fmt.Sprintf("steps[%d].request", stepIdx)),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
assertion, err := s.Expect.Build(newCtx)
if err != nil {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.WithPath(err, fmt.Sprintf("steps[%d].expect", stepIdx)),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
if err := assertion.Assert(resp); err != nil {
err = errors.WithNodeAndColored(
errors.WithPath(err, fmt.Sprintf("steps[%d].expect", stepIdx)),
ctx.Node(),
ctx.EnabledColor(),
)
var assertErr *assert.Error
if errors.As(err, &assertErr) {
for _, err := range assertErr.Errors {
ctx.Reporter().Error(err)
}
} else {
ctx.Reporter().Error(err)
}
ctx.Reporter().FailNow()
}
return newCtx
}