1
1
package c_test
2
2
3
3
import (
4
+ "bytes"
5
+ "context"
6
+ "fmt"
4
7
"os"
5
8
"os/exec"
6
9
"testing"
7
10
8
11
"github.com/bazelbuild/rules_go/go/runfiles"
9
12
"github.com/stretchr/testify/assert"
13
+ "github.com/tetratelabs/wazero"
14
+ "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
10
15
)
11
16
12
17
func TestYadda (t * testing.T ) {
@@ -16,11 +21,36 @@ func TestYadda(t *testing.T) {
16
21
if err != nil {
17
22
t .Fatalf ("unable to locate guest binary: %v" , err )
18
23
}
19
-
20
- got , err := exec .Command (binary ).CombinedOutput ()
24
+ var got []byte
25
+ switch os .Getenv ("EXECUTOR" ) {
26
+ case "NATIVE" :
27
+ got , err = exec .Command (binary ).CombinedOutput ()
28
+ case "WASI" :
29
+ got , err = runWasi (binary )
30
+ default :
31
+ err = fmt .Errorf ("unknown executor: %q" , os .Getenv ("EXECUTOR" ))
32
+ }
21
33
if err != nil {
22
34
t .Fatalf ("run %q: %v" , binary , err )
23
35
}
24
36
25
37
assert .Regexp (t , string (want ), string (got ))
26
38
}
39
+
40
+ func runWasi (binary string ) ([]byte , error ) {
41
+ ctx := context .Background ()
42
+ r := wazero .NewRuntime (ctx )
43
+ defer r .Close (ctx )
44
+ buf := & bytes.Buffer {}
45
+ config := wazero .NewModuleConfig ().WithStdout (buf ).WithStderr (buf ).WithArgs ("wasi" )
46
+ wasi_snapshot_preview1 .MustInstantiate (ctx , r )
47
+ bin , err := os .ReadFile (binary )
48
+ if err != nil {
49
+ return nil , fmt .Errorf ("unable to read guest binary: %v" , err )
50
+ }
51
+ _ , err = r .InstantiateWithConfig (ctx , bin , config )
52
+ if err != nil {
53
+ return nil , fmt .Errorf ("unable to create instantiate module: %v" , err )
54
+ }
55
+ return buf .Bytes (), nil
56
+ }
0 commit comments