-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_loader_test.go
60 lines (51 loc) · 1.3 KB
/
module_loader_test.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
package scripts_test
import (
"embed"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
scripts "github.com/txix-open/isp-script"
)
//go:embed test_modules
var fs embed.FS
func TestEmbedFsModules(t *testing.T) {
require := require.New(t)
engine := scripts.NewEngine(
scripts.WithModuleLoader(
scripts.NewFsModuleLoader(fs),
),
)
src, err := scripts.NewScript([]byte(`
const module = require("./test_modules/a.js");
module.a();
`))
require.NoError(err)
result, err := engine.Execute(src, nil)
require.NoError(err)
require.EqualValues("hello world", result)
}
func TestStaticModules(t *testing.T) {
require := require.New(t)
moduleData, err := os.ReadFile("test_modules/b.js")
require.NoError(err)
engine := scripts.NewEngine(
scripts.WithModuleLoader(
scripts.NewStaticModuleLoader(
scripts.NewStaticModule("b.js", moduleData),
),
),
)
src, err := scripts.NewScript([]byte(`(function() {
const module = require("./b.js");
return module.b();
})();
`))
require.NoError(err)
result, err := engine.Execute(src, nil, scripts.WithTimeout(100*time.Second))
require.NoError(err)
require.EqualValues("hello world", result)
result, err = engine.Execute(src, nil, scripts.WithTimeout(100*time.Second))
require.NoError(err)
require.EqualValues("hello world", result)
}