-
Notifications
You must be signed in to change notification settings - Fork 1
/
testloader.go
113 lines (104 loc) · 3.58 KB
/
testloader.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
package internal
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
"go.starlark.net/starlarktest"
"go.starlark.net/syntax"
)
// ModuleLoadFunc is a function that loads a Starlark module and returns the module's string dict.
type ModuleLoadFunc func() (starlark.StringDict, error)
// ThreadLoadFunc is a function that loads a Starlark module by name, usually used by the Starlark thread.
type ThreadLoadFunc func(thread *starlark.Thread, module string) (starlark.StringDict, error)
var initTestOnce sync.Once
// NewAssertLoader creates a Starlark thread loader that loads a module by name or asserts.star for testing.
func NewAssertLoader(moduleName string, loader ModuleLoadFunc) ThreadLoadFunc {
initTestOnce.Do(func() {
starlarktest.DataFile = func(pkgdir, filename string) string {
_, currFileName, _, ok := runtime.Caller(1)
if !ok {
return ""
}
return filepath.Join(filepath.Dir(currFileName), filename)
}
})
// for assert loader
return func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
switch module {
case moduleName:
if loader == nil {
return nil, fmt.Errorf("nil module")
}
d, err := loader()
if err != nil {
// failed to load
return nil, err
}
// Aligned with starlet/module.go: GetLazyLoader() function
// extract all members of module from dict like `{name: module}` or `{name: struct}`
if len(d) == 1 {
m, found := d[moduleName]
if found {
if mm, ok := m.(*starlarkstruct.Module); ok && mm != nil {
return mm.Members, nil
} else if sm, ok := m.(*starlarkstruct.Struct); ok && sm != nil {
sd := make(starlark.StringDict)
sm.ToStringDict(sd)
return sd, nil
}
}
}
return d, nil
case "struct.star":
return starlark.StringDict{
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
}, nil
case "module.star":
return starlark.StringDict{
"module": starlark.NewBuiltin("module", starlarkstruct.MakeModule),
}, nil
case "assert.star":
return starlarktest.LoadAssertModule()
case "freeze.star":
return starlark.StringDict{
"freeze": starlark.NewBuiltin("freeze", freezeValue),
}, nil
}
return nil, fmt.Errorf("invalid module")
}
}
// ExecModuleWithErrorTest executes a Starlark script with a module loader and compares the error with the expected error.
func ExecModuleWithErrorTest(t *testing.T, name string, loader ModuleLoadFunc, script string, wantErr string, predecl starlark.StringDict) (starlark.StringDict, error) {
thread := &starlark.Thread{Load: NewAssertLoader(name, loader), Print: func(_ *starlark.Thread, msg string) { t.Log("※", msg) }}
starlarktest.SetReporter(thread, t)
header := `load('assert.star', 'assert')`
opts := syntax.FileOptions{
Set: true,
}
out, err := starlark.ExecFileOptions(&opts, thread, name+"_test.star", []byte(header+"\n"+script), predecl)
if err != nil {
if wantErr == "" {
if ee, ok := err.(*starlark.EvalError); ok {
t.Errorf("got unexpected starlark error: '%v'", ee.Backtrace())
} else {
t.Errorf("got unexpected error: '%v'", err)
}
} else if wantErr != "" && !strings.Contains(err.Error(), wantErr) {
t.Errorf("got mismatched error: '%v', want: '%v'", err, wantErr)
}
}
return out, err
}
func freezeValue(thread *starlark.Thread, bn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var v starlark.Value
if err := starlark.UnpackArgs(bn.Name(), args, kwargs, "v", &v); err != nil {
return nil, err
}
v.Freeze()
return v, nil
}