forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasi_snapshot_preview1_test.go
59 lines (50 loc) · 1.63 KB
/
wasi_snapshot_preview1_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
package wasi_snapshot_preview1_test
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"testing"
"time"
"github.com/tetratelabs/wazero"
)
func TestMain(m *testing.M) {
var err error
if wasmGo, err = compileWasip1Wasm(); err != nil {
println("go: skipping due compilation error:", err)
}
os.Exit(m.Run())
}
// runtimeCfg is a shared runtime configuration for tests within this package to reduce the compilation time of the binary.
var runtimeCfg = wazero.NewRuntimeConfig().WithCompilationCache(wazero.NewCompilationCache())
// compileWasip1Wasm allows us to generate a binary with runtime.GOOS=wasip1
// and runtime.GOARCH=wasm. This intentionally does so on-demand, because the
// wasm is too big to check in.
func compileWasip1Wasm() ([]byte, error) {
// Prepare the working directory.
workdir, err := os.MkdirTemp("", "wasi")
if err != nil {
return nil, err
}
defer os.RemoveAll(workdir)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
binPath := path.Join(workdir, "wasi.wasm")
cmd := exec.CommandContext(ctx, "go", "build", "-o", binPath, ".") //nolint:gosec
cmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm")
cmd.Dir = "testdata/go"
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("couldn't compile %s: %w", string(out), err)
}
bin, err := os.ReadFile(binPath) //nolint:gosec
if err != nil {
return nil, err
}
// Proactively compile the binary to reduce the test time.
r := wazero.NewRuntimeWithConfig(context.Background(), runtimeCfg)
defer r.Close(context.Background())
_, err = r.CompileModule(context.Background(), bin)
return bin, err
}