Skip to content

Commit

Permalink
Build TinyGo module without docker. Disable Wasmer tests. Fix linter …
Browse files Browse the repository at this point in the history
…issues. (#63)
  • Loading branch information
pkedy authored Jan 21, 2025
1 parent 4be2434 commit 17eb7d5
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 130 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: '1.20'
go-version: '1.23.5'
- uses: acifani/setup-tinygo@v2
with:
tinygo-version: '0.34.0'

- name: Set up Rust
uses: actions-rs/toolchain@v1
Expand Down
3 changes: 1 addition & 2 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"time"

"github.com/wapc/wapc-go"
"github.com/wapc/wapc-go/engines/wasmer"
"github.com/wapc/wapc-go/engines/wasmtime"
"github.com/wapc/wapc-go/engines/wazero"
)

var ctx = context.Background()

var engines = []wapc.Engine{
wasmer.Engine(),
// wasmer.Engine(),
wasmtime.Engine(),
wazero.Engine(),
}
Expand Down
44 changes: 22 additions & 22 deletions engines/wasmer/example_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package wasmer

import (
"context"
"log"
// import (
// "context"
// "log"

"github.com/wasmerio/wasmer-go/wasmer"
// "github.com/wasmerio/wasmer-go/wasmer"

"github.com/wapc/wapc-go"
)
// "github.com/wapc/wapc-go"
// )

// This shows how to customize the underlying wasmer engine used by waPC.
func Example_custom() {
// Set up the context used to instantiate the engine.
ctx := context.Background()
// // This shows how to customize the underlying wasmer engine used by waPC.
// func Example_custom() {
// // Set up the context used to instantiate the engine.
// ctx := context.Background()

// Configure waPC to use a specific wasmer feature.
e := EngineWithRuntime(func() (*wasmer.Engine, error) {
return wasmer.NewEngineWithConfig(wasmer.NewConfig().UseDylibEngine()), nil
})
// // Configure waPC to use a specific wasmer feature.
// e := EngineWithRuntime(func() (*wasmer.Engine, error) {
// return wasmer.NewEngineWithConfig(wasmer.NewConfig().UseDylibEngine()), nil
// })

// Instantiate a module normally.
m, err := e.New(ctx, wapc.NoOpHostCallHandler, guest, mc)
if err != nil {
log.Panicf("Error creating module - %v\n", err)
}
defer m.Close(ctx)
// // Instantiate a module normally.
// m, err := e.New(ctx, wapc.NoOpHostCallHandler, guest, mc)
// if err != nil {
// log.Panicf("Error creating module - %v\n", err)
// }
// defer m.Close(ctx)

// Output:
}
// // Output:
// }
192 changes: 96 additions & 96 deletions engines/wasmer/wasmer_test.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
package wasmer

import (
"context"
"errors"
"log"
"os"
"testing"

"github.com/wasmerio/wasmer-go/wasmer"

"github.com/wapc/wapc-go"
)

// testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")

var guest []byte
var mc = &wapc.ModuleConfig{
Logger: wapc.PrintlnLogger,
Stdout: os.Stdout,
Stderr: os.Stderr,
}

// TestMain ensures we can read the example wasm prior to running unit tests.
func TestMain(m *testing.M) {
var err error
guest, err = os.ReadFile("../../testdata/go/hello.wasm")
if err != nil {
log.Panicln(err)
}
os.Exit(m.Run())
}

func TestEngine_WithEngine(t *testing.T) {
t.Run("ok", func(t *testing.T) {
expected := wasmer.NewEngine()

e := EngineWithRuntime(func() (*wasmer.Engine, error) {
return expected, nil
})

m, err := e.New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
if err != nil {
t.Errorf("Error creating module - %v", err)
}
defer m.Close(testCtx)

if have := m.(*Module).engine; have != expected {
t.Errorf("Unexpected engine, have %v, expected %v", have, expected)
}
})

t.Run("nil not ok", func(t *testing.T) {
expectedErr := "function set by WithEngine returned nil"
e := EngineWithRuntime(func() (*wasmer.Engine, error) {
return nil, errors.New(expectedErr)
})

if _, err := e.New(testCtx, wapc.NoOpHostCallHandler, guest, mc); err.Error() != expectedErr {
t.Errorf("Unexpected error, have %v, expected %v", err, expectedErr)
}
})
}

func TestModule_Unwrap(t *testing.T) {
m, err := EngineWithRuntime(DefaultRuntime).New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
if err != nil {
t.Errorf("Error creating module - %v", err)
}
defer m.Close(testCtx)

mod := m.(*Module)
expected := mod.module
if have := mod.Unwrap(); have != expected {
t.Errorf("Unexpected module, have %v, expected %v", have, expected)
}
}

func TestInstance_Unwrap(t *testing.T) {
m, err := EngineWithRuntime(DefaultRuntime).New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
if err != nil {
t.Errorf("Error creating module - %v", err)
}
defer m.Close(testCtx)

i, err := m.Instantiate(testCtx)
if err != nil {
t.Errorf("Error creating instance - %v", err)
}
defer m.Close(testCtx)

inst := i.(*Instance)
expected := inst.inst
if have := inst.Unwrap(); have != expected {
t.Errorf("Unexpected instance, have %v, expected %v", have, expected)
}
}
// import (
// "context"
// "errors"
// "log"
// "os"
// "testing"

// "github.com/wasmerio/wasmer-go/wasmer"

// "github.com/wapc/wapc-go"
// )

// // testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
// var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")

// var guest []byte
// var mc = &wapc.ModuleConfig{
// Logger: wapc.PrintlnLogger,
// Stdout: os.Stdout,
// Stderr: os.Stderr,
// }

// // TestMain ensures we can read the example wasm prior to running unit tests.
// func TestMain(m *testing.M) {
// var err error
// guest, err = os.ReadFile("../../testdata/go/hello.wasm")
// if err != nil {
// log.Panicln(err)
// }
// os.Exit(m.Run())
// }

// func TestEngine_WithEngine(t *testing.T) {
// t.Run("ok", func(t *testing.T) {
// expected := wasmer.NewEngine()

// e := EngineWithRuntime(func() (*wasmer.Engine, error) {
// return expected, nil
// })

// m, err := e.New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
// if err != nil {
// t.Errorf("Error creating module - %v", err)
// }
// defer m.Close(testCtx)

// if have := m.(*Module).engine; have != expected {
// t.Errorf("Unexpected engine, have %v, expected %v", have, expected)
// }
// })

// t.Run("nil not ok", func(t *testing.T) {
// expectedErr := "function set by WithEngine returned nil"
// e := EngineWithRuntime(func() (*wasmer.Engine, error) {
// return nil, errors.New(expectedErr)
// })

// if _, err := e.New(testCtx, wapc.NoOpHostCallHandler, guest, mc); err.Error() != expectedErr {
// t.Errorf("Unexpected error, have %v, expected %v", err, expectedErr)
// }
// })
// }

// func TestModule_Unwrap(t *testing.T) {
// m, err := EngineWithRuntime(DefaultRuntime).New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
// if err != nil {
// t.Errorf("Error creating module - %v", err)
// }
// defer m.Close(testCtx)

// mod := m.(*Module)
// expected := mod.module
// if have := mod.Unwrap(); have != expected {
// t.Errorf("Unexpected module, have %v, expected %v", have, expected)
// }
// }

// func TestInstance_Unwrap(t *testing.T) {
// m, err := EngineWithRuntime(DefaultRuntime).New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
// if err != nil {
// t.Errorf("Error creating module - %v", err)
// }
// defer m.Close(testCtx)

// i, err := m.Instantiate(testCtx)
// if err != nil {
// t.Errorf("Error creating instance - %v", err)
// }
// defer m.Close(testCtx)

// inst := i.(*Instance)
// expected := inst.inst
// if have := inst.Unwrap(); have != expected {
// t.Errorf("Unexpected instance, have %v, expected %v", have, expected)
// }
// }
4 changes: 3 additions & 1 deletion engines/wasmtime/wasmtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/wapc/wapc-go"
)

type testKey struct{}

// testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
var testCtx = context.WithValue(context.Background(), testKey{}, "arbitrary")

var guest []byte
var mc = &wapc.ModuleConfig{
Expand Down
8 changes: 4 additions & 4 deletions engines/wazero/wazero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"github.com/wapc/wapc-go"
)

type testKey struct{}

// testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
var testCtx = context.WithValue(context.Background(), testKey{}, "arbitrary")

var guest []byte
var mc = &wapc.ModuleConfig{
Expand Down Expand Up @@ -102,9 +104,7 @@ func TestEngineWithRuntime(t *testing.T) {

if _, err := wasi_snapshot_preview1.Instantiate(testCtx, r); err != nil {
_ = r.Close(testCtx)
if err != nil {
t.Errorf("Error creating module - %v", err)
}
t.Errorf("Error creating module - %v", err)
}

// TinyGo doesn't need the AssemblyScript host functions which are
Expand Down
2 changes: 1 addition & 1 deletion hello/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ build:
@echo "----------"
@echo "Building Go wasm Guest"
@echo "----------"
docker run -v `pwd`/:/build -w /build tinygo/tinygo:0.25.0 tinygo build -o hello.wasm -scheduler=none --no-debug -target=wasi main.go
tinygo build -o hello.wasm -scheduler=none --no-debug -target=wasi main.go
2 changes: 1 addition & 1 deletion testdata/go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ build:
@echo "----------"
@echo "Building Go wasm Guest"
@echo "----------"
docker run -v `pwd`/:/build -w /build tinygo/tinygo:0.25.0 tinygo build -o hello.wasm -scheduler=none --no-debug -target=wasi main.go
tinygo build -o hello.wasm -scheduler=none --no-debug -target=wasi main.go

0 comments on commit 17eb7d5

Please sign in to comment.