-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestcase.go
155 lines (137 loc) · 3.47 KB
/
testcase.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
package cfft
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"github.com/aereal/jsondiff"
"github.com/itchyny/gojq"
)
type TestCase struct {
Name string `json:"name" yaml:"name"`
Event string `json:"event" yaml:"event"`
Expect string `json:"expect" yaml:"expect"`
Ignore string `json:"ignore" yaml:"ignore"`
Env map[string]string `json:"env" yaml:"env"`
id int
event *CFFEvent
expect *CFFExpect
ignore *gojq.Query
}
type CFFExpect struct {
Request *CFFRequest `json:"request,omitempty"`
Reponse *CFFResponse `json:"response,omitempty"`
}
func (e *CFFExpect) ToMap() map[string]any {
b, err := json.Marshal(e)
if err != nil {
panic(err)
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
panic(err)
}
return m
}
func (c *TestCase) EventBytes() []byte {
return c.event.Bytes()
}
func (c *TestCase) ExpectBytes() []byte {
b, _ := json.Marshal(c.expect)
return b
}
func (c *TestCase) Identifier() string {
if c.Name != "" {
return c.Name
}
return fmt.Sprintf("[%d]", c.id)
}
func (c *TestCase) Setup(ctx context.Context, readFile func(string) ([]byte, error)) error {
for k, v := range c.Env {
df := localEnv(k, v)
defer df()
}
eventBytes, err := readFile(c.Event)
if err != nil {
return fmt.Errorf("failed to read event object, %w", err)
}
var event CFFEvent
if err := json.Unmarshal(eventBytes, &event); err != nil {
return fmt.Errorf("failed to parse event object as CFF event object, %w", err)
}
c.event = &event
if len(c.Expect) > 0 {
// expect is optional
expectBytes, err := readFile(c.Expect)
if err != nil {
return fmt.Errorf("failed to read expect object, %w", err)
}
if len(expectBytes) == 0 {
return fmt.Errorf("expect object is empty")
} else {
slog.Debug(f("expect object: %s", string(expectBytes)))
}
var expect CFFExpect
if err := json.Unmarshal(expectBytes, &expect); err != nil {
return fmt.Errorf("failed to parse expect object, %w", err)
}
c.expect = &expect
}
if len(c.Ignore) > 0 {
// ignore is optional
q, err := gojq.Parse(c.Ignore)
if err != nil {
return fmt.Errorf("failed to parse ignore query, %w", err)
}
c.ignore = q
}
return nil
}
func (c *TestCase) Run(ctx context.Context, output []byte, logger *slog.Logger) error {
logger.Debug(f("function output: %s", string(output)))
if c.expect == nil {
logger.Info("no expected value. skipping checking function output")
return nil
}
logger.Info("checking function output with expected value")
result := &CFFExpect{}
if err := json.Unmarshal(output, result); err != nil {
return fmt.Errorf("failed to parse function output, %w", err)
}
var options []jsondiff.Option
if c.ignore != nil {
options = append(options, jsondiff.Ignore(c.ignore))
}
diff, err := jsondiff.Diff(
&jsondiff.Input{Name: "expect", X: c.expect.ToMap()},
&jsondiff.Input{Name: "actual", X: result.ToMap()},
options...,
)
if err != nil {
return fmt.Errorf("failed to diff, %w", err)
}
if diff != "" {
fmt.Print(coloredDiff(diff))
return fmt.Errorf("expect and actual are not equal")
} else {
logger.Info("expect and actual are equal")
}
return nil
}
func localEnv(key, value string) func() {
prevValue, ok := os.LookupEnv(key)
if err := os.Setenv(key, value); err != nil {
slog.Error("cannot set environment variable:", "error", err)
panic(err)
}
if ok {
return func() {
os.Setenv(key, prevValue)
}
} else {
return func() {
os.Unsetenv(key)
}
}
}