Repository navigation
Expand file tree
/
Copy pathsurface_test.go
More file actions
247 lines (224 loc) · 7.96 KB
/
Copy pathsurface_test.go
File metadata and controls
247 lines (224 loc) · 7.96 KB
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
package starbox_test
// DescribeSurface (STAR-9 / BOX-09 / R3) tests, grouped by concern:
// - TestDescribeSurfaceBuiltin builtin set modules + member enumeration
// - TestDescribeSurfaceCustom AddModule{Data,Functions}/AddModuleLoader origins + members
// - TestDescribeSurfaceGlobals global name + type reporting
// - TestDescribeSurfaceScriptDynamic script (.star) and dynamic modules report name-only
// - TestDescribeSurfaceSideEffectFree no script run; configuration and a later Run still work
// - TestDescribeSurfaceDedupNamedBuiltin AddNamedModules builtins: overlap dedups, new name appears once
import (
"reflect"
"testing"
"github.com/1set/starbox"
"github.com/1set/starlet"
"go.starlark.net/starlark"
)
// findModule returns the ModuleSurface with the given name, or false.
func findModule(sf starbox.Surface, name string) (starbox.ModuleSurface, bool) {
for _, m := range sf.Modules {
if m.Name == name {
return m, true
}
}
return starbox.ModuleSurface{}, false
}
func contains(ss []string, want string) bool {
for _, s := range ss {
if s == want {
return true
}
}
return false
}
func TestDescribeSurfaceBuiltin(t *testing.T) {
b := starbox.New("surface-builtin")
b.SetModuleSet(starbox.SafeModuleSet)
sf, err := b.DescribeSurface()
if err != nil {
t.Fatalf("DescribeSurface: %v", err)
}
// A Module-shape builtin exposes its members.
if m, ok := findModule(sf, "math"); !ok {
t.Error("math not in Safe surface")
} else {
if m.Origin != starbox.OriginBuiltin {
t.Errorf("math origin: want builtin, got %q", m.Origin)
}
if !contains(m.Members, "sqrt") || !contains(m.Members, "pi") {
t.Errorf("math members missing sqrt/pi: %v", m.Members)
}
}
// A flat-shape builtin (go_idiomatic) exposes its top-level names.
if m, ok := findModule(sf, "go_idiomatic"); !ok {
t.Error("go_idiomatic not in Safe surface")
} else if !contains(m.Members, "sleep") || !contains(m.Members, "exit") {
t.Errorf("go_idiomatic members missing sleep/exit: %v", m.Members)
}
// The pure newcomers admitted to Safe carry members too.
if m, ok := findModule(sf, "json"); !ok || !contains(m.Members, "encode") {
t.Errorf("json surface wrong: %+v ok=%v", m, ok)
}
// net must NOT be in the Safe surface (BOX-05).
if _, ok := findModule(sf, "net"); ok {
t.Error("net leaked into Safe surface")
}
}
func TestDescribeSurfaceCustom(t *testing.T) {
b := starbox.New("surface-custom")
b.AddModuleData("conf", starlark.StringDict{
"host": starlark.String("localhost"),
"port": starlark.MakeInt(8080),
})
b.AddModuleFunctions("ops", starbox.FuncMap{
"ping": func(_ *starlark.Thread, _ *starlark.Builtin, _ starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
return starlark.None, nil
},
})
// An opaque loader: members cannot be known without invoking it, so nil.
b.AddModuleLoader("opaque", func() (starlark.StringDict, error) {
return starlark.StringDict{"secret": starlark.None}, nil
})
sf, err := b.DescribeSurface()
if err != nil {
t.Fatalf("DescribeSurface: %v", err)
}
if m, ok := findModule(sf, "conf"); !ok {
t.Error("conf module missing")
} else if m.Origin != starbox.OriginCustom || !reflect.DeepEqual(m.Members, []string{"host", "port"}) {
t.Errorf("conf surface wrong: %+v", m)
}
if m, ok := findModule(sf, "ops"); !ok {
t.Error("ops module missing")
} else if !reflect.DeepEqual(m.Members, []string{"ping"}) {
t.Errorf("ops members: want [ping], got %v", m.Members)
}
if m, ok := findModule(sf, "opaque"); !ok {
t.Error("opaque module missing")
} else if m.Origin != starbox.OriginCustom || m.Members != nil {
t.Errorf("opaque surface: want custom/nil members, got %+v", m)
}
}
func TestDescribeSurfaceGlobals(t *testing.T) {
b := starbox.New("surface-globals")
b.AddKeyValue("count", 42) // raw Go int
b.AddKeyValue("name", "Star") // raw Go string
b.AddBuiltin("greet", func(_ *starlark.Thread, _ *starlark.Builtin, _ starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
return starlark.None, nil
})
b.AddKeyStarlarkValue("seven", starlark.MakeInt(7))
sf, err := b.DescribeSurface()
if err != nil {
t.Fatalf("DescribeSurface: %v", err)
}
want := map[string]string{
"count": "int",
"name": "string",
"greet": "builtin_function_or_method",
"seven": "int",
}
got := make(map[string]string, len(sf.Globals))
for _, g := range sf.Globals {
got[g.Name] = g.Type
}
for name, typ := range want {
if got[name] != typ {
t.Errorf("global %q: want type %q, got %q", name, typ, got[name])
}
}
}
func TestDescribeSurfaceScriptDynamic(t *testing.T) {
b := starbox.New("surface-sd")
b.AddModuleScript("helper", `value = 1`)
b.AddNamedModules("mystery") // not builtin, not custom -> routes to dynamic
b.SetDynamicModuleLoader(func(string) (starlet.ModuleLoader, error) { return nil, nil })
sf, err := b.DescribeSurface()
if err != nil {
t.Fatalf("DescribeSurface: %v", err)
}
if m, ok := findModule(sf, "helper.star"); !ok {
t.Error("script module helper.star missing")
} else if m.Origin != starbox.OriginScript || m.Members != nil {
t.Errorf("script surface: want script/nil, got %+v", m)
}
if m, ok := findModule(sf, "mystery"); !ok {
t.Error("dynamic module mystery missing")
} else if m.Origin != starbox.OriginDynamic || m.Members != nil {
t.Errorf("dynamic surface: want dynamic/nil, got %+v", m)
}
}
func TestDescribeSurfaceSideEffectFree(t *testing.T) {
b := starbox.New("surface-pure")
b.SetPrintFunc(noopPrint)
b.SetModuleSet(starbox.SafeModuleSet)
b.AddKeyValue("word", "World")
// Describing the surface before any run must not execute the script or
// lock out further configuration.
if _, err := b.DescribeSurface(); err != nil {
t.Fatalf("DescribeSurface before run: %v", err)
}
// A normal run still works afterwards (DescribeSurface did not mark hasExec).
out, err := b.Run(hereDoc(`x = word.upper()`))
if err != nil {
t.Fatalf("Run after DescribeSurface: %v", err)
}
if out["x"] != "WORLD" {
t.Errorf("want x=WORLD, got %v", out["x"])
}
// And the surface is still describable after the run.
if _, err := b.DescribeSurface(); err != nil {
t.Errorf("DescribeSurface after run: %v", err)
}
}
func TestDescribeSurfaceEdges(t *testing.T) {
// Unknown module set surfaces the same error Run would return.
bad := starbox.New("surface-bad")
bad.SetModuleSet(starbox.ModuleSetName("nope"))
if _, err := bad.DescribeSurface(); err == nil {
t.Error("expected error for unknown module set, got nil")
}
// A nil host global is reported as the Starlark None type.
b := starbox.New("surface-nil")
b.AddKeyValue("zilch", nil)
sf, err := b.DescribeSurface()
if err != nil {
t.Fatalf("DescribeSurface: %v", err)
}
var found bool
for _, g := range sf.Globals {
if g.Name == "zilch" {
found = true
if g.Type != "NoneType" {
t.Errorf("nil global type: want NoneType, got %q", g.Type)
}
}
}
if !found {
t.Error("nil global zilch missing from surface")
}
}
// TestDescribeSurfaceDedupNamedBuiltin covers the AddNamedModules-of-builtins
// paths: a name that overlaps the active set must appear exactly once (the
// dedup guard), and a named builtin outside the set must be reported once.
func TestDescribeSurfaceDedupNamedBuiltin(t *testing.T) {
b := starbox.New("surface-named")
b.SetModuleSet(starbox.SafeModuleSet) // includes math, not http
b.AddNamedModules("math", "http") // math overlaps (dedup); http is a new named builtin
sf, err := b.DescribeSurface()
if err != nil {
t.Fatalf("DescribeSurface: %v", err)
}
count := map[string]int{}
for _, m := range sf.Modules {
count[m.Name]++
}
if count["math"] != 1 {
t.Errorf("math should appear once (dedup across set + named), got %d", count["math"])
}
if count["http"] != 1 {
t.Errorf("http (named builtin outside the set) should appear once, got %d", count["http"])
}
// the overlapping name keeps its builtin origin.
if m, ok := findModule(sf, "math"); !ok || m.Origin != starbox.OriginBuiltin {
t.Errorf("math origin = %v (found=%v), want OriginBuiltin", m.Origin, ok)
}
}