forked from zoncoen/scenarigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario_test.go
59 lines (54 loc) · 1.38 KB
/
scenario_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
package scenarigo
import (
"bytes"
"os"
"testing"
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/plugin"
"github.com/zoncoen/scenarigo/reporter"
"github.com/zoncoen/scenarigo/schema"
)
func TestRunScenario_Context_ScenarioFilepath(t *testing.T) {
path := createTempScenario(t, `
steps:
- ref: '{{plugins.getScenarioFilepath}}'
`)
sceanrios, err := schema.LoadScenarios(path)
if err != nil {
t.Fatalf("failed to load scenario: %s", err)
}
if len(sceanrios) != 1 {
t.Fatalf("unexpected scenario length: %d", len(sceanrios))
}
var (
got string
log bytes.Buffer
)
ok := reporter.Run(func(rptr reporter.Reporter) {
ctx := context.New(rptr).WithPlugins(map[string]interface{}{
"getScenarioFilepath": plugin.StepFunc(func(ctx *context.Context, step *schema.Step) *context.Context {
got = ctx.ScenarioFilepath()
return ctx
}),
})
RunScenario(ctx, sceanrios[0])
}, reporter.WithWriter(&log))
if !ok {
t.Fatalf("scenario failed:\n%s", log.String())
}
if got != path {
t.Errorf("invalid filepath: %q", got)
}
}
func createTempScenario(t *testing.T, scenario string) string {
t.Helper()
f, err := os.CreateTemp("", "*.yaml")
if err != nil {
t.Fatalf("failed to create temp file: %s", err)
}
defer f.Close()
if _, err := f.WriteString(scenario); err != nil {
t.Fatalf("failed to write scenario: %s", err)
}
return f.Name()
}