-
Notifications
You must be signed in to change notification settings - Fork 20
/
script.go
297 lines (252 loc) · 7.61 KB
/
script.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package overflow
import (
"encoding/json"
"fmt"
"io"
"testing"
"github.com/bjartek/underflow"
"github.com/enescakir/emoji"
"github.com/fatih/color"
"github.com/hexops/autogold"
"github.com/onflow/cadence"
"github.com/onflow/flowkit/v2"
"github.com/pkg/errors"
"github.com/sanity-io/litter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xeipuuv/gojsonpointer"
)
// Scripts
//
// A read only interaction against the flow blockchain
// a type used for composing scripts
type OverflowScriptFunction func(filename string, opts ...OverflowInteractionOption) *OverflowScriptResult
// a type used for composing scripts
type OverflowScriptOptsFunction func(opts ...OverflowInteractionOption) *OverflowScriptResult
// compose interactionOptions into a new Script function
func (o *OverflowState) ScriptFN(outerOpts ...OverflowInteractionOption) OverflowScriptFunction {
return func(filename string, opts ...OverflowInteractionOption) *OverflowScriptResult {
outerOpts = append(outerOpts, opts...)
return o.Script(filename, outerOpts...)
}
}
// compose fileName and interactionOptions into a new Script function
func (o *OverflowState) ScriptFileNameFN(filename string, outerOpts ...OverflowInteractionOption) OverflowScriptOptsFunction {
return func(opts ...OverflowInteractionOption) *OverflowScriptResult {
outerOpts = append(outerOpts, opts...)
return o.Script(filename, outerOpts...)
}
}
// run a script with the given code/filanem an options
func (o *OverflowState) Script(filename string, opts ...OverflowInteractionOption) *OverflowScriptResult {
interaction := o.BuildInteraction(filename, "script", opts...)
result := interaction.runScript()
if interaction.PrintOptions != nil && !interaction.NoLog {
result.Print()
}
if o.StopOnError && result.Err != nil {
result.PrintArguments(nil)
panic(result.Err)
}
return result
}
func (fbi *OverflowInteractionBuilder) runScript() *OverflowScriptResult {
o := fbi.Overflow
osc := &OverflowScriptResult{Input: fbi}
if fbi.Error != nil {
osc.Err = fbi.Error
return osc
}
filePath := fmt.Sprintf("%s/%s.cdc", fbi.BasePath, fbi.FileName)
o.Log.Reset()
script := flowkit.Script{
Code: fbi.TransactionCode,
Args: fbi.Arguments,
Location: filePath,
}
sc := fbi.ScriptQuery
if fbi.ScriptQuery == nil {
sc = &flowkit.ScriptQuery{
Latest: true,
}
}
result, err := o.Flowkit.ExecuteScript(fbi.Ctx, script, *sc)
osc.Result = result
osc.Output = underflow.CadenceValueToInterfaceWithOption(result, fbi.Overflow.UnderflowOptions)
if err != nil {
osc.Err = errors.Wrapf(err, "scriptFileName:%s", fbi.FileName)
}
//}
var logMessage []OverflowEmulatorLogMessage
dec := json.NewDecoder(o.Log)
for {
var doc OverflowEmulatorLogMessage
err := dec.Decode(&doc)
if err == io.EOF {
// all done
break
}
if err != nil {
osc.Err = err
}
logMessage = append(logMessage, doc)
}
o.Log.Reset()
osc.Log = logMessage
return osc
}
// result after running a script
type OverflowScriptResult struct {
Err error
Result cadence.Value
Output interface{}
Input *OverflowInteractionBuilder
Log []OverflowEmulatorLogMessage
}
func (osr *OverflowScriptResult) PrintArguments(t *testing.T) {
args := osr.Input.NamedCadenceArguments
maxLength := 0
for name := range args {
if len(name) > maxLength {
maxLength = len(name)
}
}
format := fmt.Sprintf("%%%ds -> %%v", maxLength)
for name, arg := range args {
value, err := underflow.CadenceValueToJsonStringWithOption(arg, osr.Input.Overflow.UnderflowOptions)
if err != nil {
panic(err)
}
printOrLog(t, fmt.Sprintf(format, name, value))
}
}
// get the script as json
func (osr *OverflowScriptResult) GetAsJson() (string, error) {
if osr.Err != nil {
return "", errors.Wrapf(osr.Err, "script: %s", osr.Input.FileName)
}
j, err := json.MarshalIndent(osr.Output, "", " ")
if err != nil {
return "", errors.Wrapf(err, "script: %s", osr.Input.FileName)
}
return string(j), nil
}
// get the script as interface{}
func (osr *OverflowScriptResult) GetAsInterface() (interface{}, error) {
if osr.Err != nil {
return nil, errors.Wrapf(osr.Err, "script: %s", osr.Input.FileName)
}
return osr.Output, nil
}
// Assert that a jsonPointer into the result is an error
func (osr *OverflowScriptResult) AssertWithPointerError(t *testing.T, pointer string, message string) *OverflowScriptResult {
t.Helper()
_, err := osr.GetWithPointer(pointer)
assert.Error(t, err)
assert.ErrorContains(t, err, message, "output", litter.Sdump(osr.Output))
return osr
}
// Assert that a jsonPointer into the result is equal to the given value
func (osr *OverflowScriptResult) AssertWithPointer(t *testing.T, pointer string, value interface{}) *OverflowScriptResult {
t.Helper()
result, err := osr.GetWithPointer(pointer)
assert.NoError(t, err)
assert.Equal(t, value, result, "output", litter.Sdump(osr.Output))
return osr
}
// Assert that a jsonPointer into the result is equal to the given autogold Want
func (osr *OverflowScriptResult) AssertWithPointerWant(t *testing.T, pointer string, want autogold.Value) *OverflowScriptResult {
t.Helper()
result, err := osr.GetWithPointer(pointer)
assert.NoError(t, err)
switch result.(type) {
case []interface{}, map[interface{}]interface{}:
want.Equal(t, litter.Sdump(result))
default:
want.Equal(t, result)
}
return osr
}
// Assert that the length of a jsonPointer is equal to length
func (osr *OverflowScriptResult) AssertLengthWithPointer(t *testing.T, pointer string, length int) *OverflowScriptResult {
t.Helper()
require.NoError(t, osr.Err)
osr.Print()
result, err := osr.GetWithPointer(pointer)
require.NoError(t, err)
switch res := result.(type) {
case []interface{}:
assert.Equal(t, length, len(res), litter.Sdump(osr.Output))
case map[interface{}]interface{}:
assert.Equal(t, length, len(res), litter.Sdump(osr.Output))
default:
assert.Equal(t, length, len(fmt.Sprintf("%v", res)), litter.Sdump(osr.Output))
}
return osr
}
// Marshal the script output as the given sent in type
func (osr *OverflowScriptResult) MarshalAs(marshalTo interface{}) error {
if osr.Err != nil {
return osr.Err
}
bytes, err := json.Marshal(osr.Output)
if err != nil {
return err
}
err = json.Unmarshal(bytes, marshalTo)
if err != nil {
return err
}
return nil
}
// Marshal the given jsonPointer as the given type
func (osr *OverflowScriptResult) MarshalPointerAs(pointer string, marshalTo interface{}) error {
ptr, err := gojsonpointer.NewJsonPointer(pointer)
if err != nil {
return err
}
result, _, err := ptr.Get(osr.Output)
if err != nil {
return err
}
bytes, err := json.Marshal(result)
if err != nil {
return err
}
err = json.Unmarshal(bytes, marshalTo)
if err != nil {
return err
}
return nil
}
// get the given jsonPointer as interface{}
func (osr *OverflowScriptResult) GetWithPointer(pointer string) (interface{}, error) {
ptr, err := gojsonpointer.NewJsonPointer(pointer)
if err != nil {
return nil, err
}
result, _, err := ptr.Get(osr.Output)
return result, err
}
// Assert that the result is equal to the given autogold.Want
func (osr *OverflowScriptResult) AssertWant(t *testing.T, want autogold.Value) *OverflowScriptResult {
t.Helper()
assert.NoError(t, osr.Err)
switch osr.Output.(type) {
case []interface{}, map[interface{}]interface{}:
want.Equal(t, litter.Sdump(osr.Output))
default:
want.Equal(t, osr.Output)
}
return osr
}
// Print the result
func (osr *OverflowScriptResult) Print() *OverflowScriptResult {
json, err := osr.GetAsJson()
if err != nil {
color.Red(err.Error())
return osr
}
fmt.Printf("%v Script %s run result:%v\n", emoji.Star, osr.Input.Name, json)
return osr
}