-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfs_source_test.go
More file actions
448 lines (384 loc) · 12.6 KB
/
fs_source_test.go
File metadata and controls
448 lines (384 loc) · 12.6 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2026 Aaron Alpar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wile_test
import (
"context"
"testing"
"testing/fstest"
qt "github.com/frankban/quicktest"
"github.com/aalpar/wile"
)
// TestWithSourceFS_Include verifies that (include ...) loads a file from
// a virtual filesystem provided via WithSourceFS.
//
// Note: We use (include ...) rather than (load ...) because load is a
// runtime primitive in the eval extension (internal package, not
// importable from an external test). include exercises the same
// FSFileResolver.ResolveAndOpen path at compile time.
func TestWithSourceFS_Include(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"helper.scm": &fstest.MapFile{
Data: []byte(`(define helper-val 42)`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(include "helper.scm") helper-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "42")
}
// TestWithSourceFS_NestedInclude verifies that nested includes resolve
// relative paths correctly within the virtual filesystem. main.scm
// includes sub/helper.scm, and definitions from the nested file are
// visible at the top level.
func TestWithSourceFS_NestedInclude(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"main.scm": &fstest.MapFile{
Data: []byte(`(include "sub/helper.scm")`),
},
"sub/helper.scm": &fstest.MapFile{
Data: []byte(`(define nested-val 99)`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(include "main.scm") nested-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "99")
}
// TestWithSourceFS_LibraryImport verifies that the R7RS library system
// can find and import .sld library files from a virtual filesystem.
func TestWithSourceFS_LibraryImport(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"lib/mylib.sld": &fstest.MapFile{
Data: []byte(`(define-library (mylib)
(export greet)
(begin
(define greet "hello from fs")))`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
wile.WithLibraryPaths("lib"),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(import (mylib)) greet`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, `"hello from fs"`)
}
// TestWithSourceFS_IncludeInLibrary verifies that (include ...) inside a
// define-library form resolves files relative to the library's directory
// within the virtual filesystem.
func TestWithSourceFS_IncludeInLibrary(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"lib/mylib.sld": &fstest.MapFile{
Data: []byte(`(define-library (mylib)
(export compute)
(include "impl.scm"))`),
},
"lib/impl.scm": &fstest.MapFile{
Data: []byte(`(begin (define compute 777))`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
wile.WithLibraryPaths("lib"),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(import (mylib)) compute`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "777")
}
// TestWithSourceFS_IncludeRejectsAbsolutePath verifies that the
// FSFileResolver rejects absolute paths. Absolute paths have no meaning
// in a virtual filesystem.
func TestWithSourceFS_IncludeRejectsAbsolutePath(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"dummy.scm": &fstest.MapFile{
Data: []byte(`(define x 1)`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
)
c.Assert(err, qt.IsNil)
_, err = engine.Eval(ctx, engine.MustParse(ctx, `(include "/absolute/path.scm")`))
c.Assert(err, qt.IsNotNil)
}
// TestWithSourceFS_NotSet_UsesOSFilesystem verifies that without
// WithSourceFS, the engine falls back to the OS filesystem. Including a
// nonexistent file should produce an error (proving the OS resolver is
// used, not a nil/missing resolver).
func TestWithSourceFS_NotSet_UsesOSFilesystem(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
engine, err := wile.NewEngine(ctx, wile.WithSafeExtensions())
c.Assert(err, qt.IsNil)
_, err = engine.Eval(ctx, engine.MustParse(ctx, `(include "definitely-nonexistent-file.scm")`))
c.Assert(err, qt.IsNotNil)
}
// TestWithSourceFS_LibraryScmFallback verifies that the library loader
// falls back to .scm when no .sld file exists. This exercises the
// .sld-then-.scm resolution path in LoadLibrary.
func TestWithSourceFS_LibraryScmFallback(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"lib/mylib.scm": &fstest.MapFile{
Data: []byte(`(define-library (mylib)
(export val)
(begin (define val 123)))`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
wile.WithLibraryPaths("lib"),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(import (mylib)) val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "123")
}
// TestWithSourceFS_TransitiveLibraryImport verifies that a library can
// import another library and re-export values. Library (derived) imports
// (base) and defines derived-val in terms of base-val.
func TestWithSourceFS_TransitiveLibraryImport(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"lib/base.sld": &fstest.MapFile{
Data: []byte(`(define-library (base)
(export base-val)
(begin (define base-val 10)))`),
},
"lib/derived.sld": &fstest.MapFile{
Data: []byte(`(define-library (derived)
(import (base))
(export derived-val)
(begin (define derived-val (+ base-val 5))))`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
wile.WithLibraryPaths("lib"),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(import (derived)) derived-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "15")
}
// TestWithSourceFS_MultipleFS verifies that multiple WithSourceFS calls
// create a chain where files are found across different virtual filesystems.
// fs1 has helper.scm, fs2 has utils.scm — both resolve.
func TestWithSourceFS_MultipleFS(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fs1 := fstest.MapFS{
"helper.scm": &fstest.MapFile{
Data: []byte(`(define helper-val 10)`),
},
}
fs2 := fstest.MapFS{
"utils.scm": &fstest.MapFile{
Data: []byte(`(define utils-val 20)`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fs1),
wile.WithSourceFS(fs2),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx,
`(include "helper.scm") (include "utils.scm") (+ helper-val utils-val)`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "30")
}
// TestWithSourceFS_ChainPriority verifies that when two filesystems both
// contain the same file, the first one in chain order wins.
func TestWithSourceFS_ChainPriority(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fs1 := fstest.MapFS{
"config.scm": &fstest.MapFile{
Data: []byte(`(define config-val "from-fs1")`),
},
}
fs2 := fstest.MapFS{
"config.scm": &fstest.MapFile{
Data: []byte(`(define config-val "from-fs2")`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fs1),
wile.WithSourceFS(fs2),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(include "config.scm") config-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, `"from-fs1"`)
}
// TestWithSourceOS_Fallback verifies that WithSourceOS appends the
// OS filesystem as the last resolver. A nonexistent file in the virtual
// FS falls through to OS, which also fails — proving the chain tried both.
func TestWithSourceOS_Fallback(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"present.scm": &fstest.MapFile{
Data: []byte(`(define present-val 1)`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
wile.WithSourceOS(),
)
c.Assert(err, qt.IsNil)
// File in virtual FS resolves.
result, err := engine.EvalMultiple(ctx, `(include "present.scm") present-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "1")
// File not in virtual FS falls through to OS, which also fails.
_, err = engine.Eval(ctx, engine.MustParse(ctx, `(include "definitely-nonexistent-chain-test.scm")`))
c.Assert(err, qt.IsNotNil)
}
// TestWithSourceFS_ExcludesOSByDefault verifies that when WithSourceFS is used
// without WithSourceOS, the OS filesystem is NOT consulted. Only the
// virtual filesystem is searched.
func TestWithSourceFS_ExcludesOSByDefault(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"only-this.scm": &fstest.MapFile{
Data: []byte(`(define only-val 42)`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
)
c.Assert(err, qt.IsNil)
// File in virtual FS resolves.
result, err := engine.EvalMultiple(ctx, `(include "only-this.scm") only-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "42")
}
// TestWithSourceFS_LibraryAcrossLayers verifies that the library loader
// searches across multiple virtual filesystems in chain order.
func TestWithSourceFS_LibraryAcrossLayers(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fs1 := fstest.MapFS{
"lib/base.sld": &fstest.MapFile{
Data: []byte(`(define-library (base)
(export base-val)
(begin (define base-val 100)))`),
},
}
fs2 := fstest.MapFS{
"lib/extra.sld": &fstest.MapFile{
Data: []byte(`(define-library (extra)
(import (base))
(export extra-val)
(begin (define extra-val (+ base-val 50))))`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fs1),
wile.WithSourceFS(fs2),
wile.WithLibraryPaths("lib"),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(import (extra)) extra-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "150")
}
// TestWithSourceFS_DeeplyNestedInclude verifies that three levels of
// nested includes resolve correctly: a.scm -> sub/b.scm -> sub/deep/c.scm.
// Each include is relative to the including file's directory, not the
// original evaluation root.
func TestWithSourceFS_DeeplyNestedInclude(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"a.scm": &fstest.MapFile{
Data: []byte(`(include "sub/b.scm")`),
},
"sub/b.scm": &fstest.MapFile{
Data: []byte(`(include "deep/c.scm")`),
},
"sub/deep/c.scm": &fstest.MapFile{
Data: []byte(`(define deep-val 333)`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(include "a.scm") deep-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "333")
}
// TestWithSourceFS_IncludeDotDotTraversal verifies that a library's
// (include ...) directive can use ".." to reach files outside its own
// directory. The library at lib/mylib.sld includes ../shared/common.scm,
// which resolves to shared/common.scm in the virtual filesystem.
func TestWithSourceFS_IncludeDotDotTraversal(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
fsys := fstest.MapFS{
"shared/common.scm": &fstest.MapFile{
Data: []byte(`(define common-val 55)`),
},
"lib/mylib.sld": &fstest.MapFile{
Data: []byte(`(define-library (mylib)
(export common-val)
(include "../shared/common.scm"))`),
},
}
engine, err := wile.NewEngine(ctx,
wile.WithSafeExtensions(),
wile.WithSourceFS(fsys),
wile.WithLibraryPaths("lib"),
)
c.Assert(err, qt.IsNil)
result, err := engine.EvalMultiple(ctx, `(import (mylib)) common-val`)
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "55")
}