-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathscenario_test.go
123 lines (117 loc) · 2.81 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
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
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()
}
func TestExecuteIf(t *testing.T) {
t.Run("success", func(t *testing.T) {
tests := map[string]struct {
vars map[string]any
expr string
expect bool
}{
"empty": {
expect: true,
},
"no vars": {
expr: "{{true}}",
expect: true,
},
"with vars": {
vars: map[string]any{
"foo": true,
},
expr: "{{vars.foo}}",
expect: true,
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
ctx := context.FromT(t).WithVars(test.vars)
got, err := executeIf(ctx, test.expr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if expect := test.expect; got != expect {
t.Errorf("expected %t but got %t", expect, got)
}
})
}
})
t.Run("failure", func(t *testing.T) {
tests := map[string]struct {
expr string
expectError string
}{
"invalid template": {
expr: "{{",
expectError: `failed to execute: failed to parse "{{": col 3: expected '}}', found 'EOF'`,
},
"not bool": {
expr: "{{1}}",
expectError: "must be bool but got int64",
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
ctx := context.FromT(t)
if _, err := executeIf(ctx, test.expr); err == nil {
t.Fatal("no error")
} else if got, expect := err.Error(), test.expectError; got != expect {
t.Errorf("expected %q but got %q", expect, got)
}
})
}
})
}