From 4422994c55854f1ed673f6f28b7b09610aeac717 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Thu, 3 Oct 2024 19:59:15 +0200 Subject: [PATCH] feat(go-bindgen): add `test` flag Signed-off-by: Roman Volosatovs --- .gitignore | 7 +++--- cmd/wadge-bindgen-go/main.go | 24 ++++++++++++++++--- ...ndings.wadge.go => bindings.wadge_test.go} | 2 +- examples/go/http/http_test.go | 2 +- ...ndings.wadge.go => bindings.wadge_test.go} | 2 +- tests/go/sync/sync_test.go | 2 +- ...ndings.wadge.go => bindings.wadge_test.go} | 2 +- tests/go/wasi/wasi_test.go | 2 +- 8 files changed, 31 insertions(+), 12 deletions(-) rename examples/go/http/{bindings.wadge.go => bindings.wadge_test.go} (99%) rename tests/go/sync/{bindings.wadge.go => bindings.wadge_test.go} (99%) rename tests/go/wasi/{bindings.wadge.go => bindings.wadge_test.go} (99%) diff --git a/.gitignore b/.gitignore index ed17c3a..1440c30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -/target -*.wasm *.a *.lib -bindings.wadge.go +*.wasm /go/internal/tests/*/bindings/* +/target +bindings.wadge.go +bindings.wadge_test.go diff --git a/cmd/wadge-bindgen-go/main.go b/cmd/wadge-bindgen-go/main.go index 3e6e616..08f76d4 100644 --- a/cmd/wadge-bindgen-go/main.go +++ b/cmd/wadge-bindgen-go/main.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "errors" "flag" "fmt" "go/ast" @@ -19,9 +20,10 @@ import ( ) var ( - output = flag.String("output", "bindings.wadge.go", "output file path from the root of the package directory") gofmt = flag.Bool("gofmt", true, "whether to format the generated code") - pkgName = flag.String("package", "", "package name, defaults to test package at path specified") + output = flag.String("output", "", "output file path from the root of the package directory. Defaults to `bindings.wadge.go` if `test` is not set and `bindings.wadge_test.go` if it is") + pkgName = flag.String("package", "", "package name, defaults to package at path specified or the corresponding test package if `test` is set") + test = flag.Bool("test", false, "whether to run in test mode, which will set output and package flags accordingly") unsafePointerTy = &ast.SelectorExpr{ X: &ast.Ident{Name: "unsafe"}, @@ -170,7 +172,15 @@ func importType(fs *token.FileSet, imports map[string]*ast.Ident, expr *ast.Expr } func generate(path string) error { - fpath := filepath.Join(path, *output) + out := *output + if out == "" { + if *test { + out = "bindings.wadge_test.go" + } else { + out = "bindings.wadge.go" + } + } + fpath := filepath.Join(path, out) if err := os.RemoveAll(fpath); err != nil { return fmt.Errorf("failed to remove file at `%s`: %w", fpath, err) } @@ -456,6 +466,11 @@ func generate(path string) error { name := *pkgName if name == "" { name = pkg.Name + if *test { + name = fmt.Sprintf("%s_test", pkg.Name) + } else { + name = pkg.Name + } } importSpecs := []*ast.ImportSpec{ { @@ -551,6 +566,9 @@ func run() error { if len(args) == 0 { args = []string{"."} } + if *test && *pkgName != "" && *output != "" { + return errors.New("setting `test` to true has no effect, when both `package` and `output` are set") + } for _, path := range args { if err := generate(path); err != nil { return fmt.Errorf("failed to generate bindings for `%s`:\n%w", path, err) diff --git a/examples/go/http/bindings.wadge.go b/examples/go/http/bindings.wadge_test.go similarity index 99% rename from examples/go/http/bindings.wadge.go rename to examples/go/http/bindings.wadge_test.go index b810c7a..b2a3f5d 100644 --- a/examples/go/http/bindings.wadge.go +++ b/examples/go/http/bindings.wadge_test.go @@ -2,7 +2,7 @@ // Code generated by wadge-bindgen-go DO NOT EDIT -package main +package main_test import ( github_com__bytecodealliance__wasm___tools___go__cm "github.com/bytecodealliance/wasm-tools-go/cm" diff --git a/examples/go/http/http_test.go b/examples/go/http/http_test.go index 14b3df6..7b51895 100644 --- a/examples/go/http/http_test.go +++ b/examples/go/http/http_test.go @@ -1,4 +1,4 @@ -//go:generate go run go.wasmcloud.dev/wadge/cmd/wadge-bindgen-go +//go:generate go run go.wasmcloud.dev/wadge/cmd/wadge-bindgen-go -test package main_test diff --git a/tests/go/sync/bindings.wadge.go b/tests/go/sync/bindings.wadge_test.go similarity index 99% rename from tests/go/sync/bindings.wadge.go rename to tests/go/sync/bindings.wadge_test.go index f3623f9..200b3cb 100644 --- a/tests/go/sync/bindings.wadge.go +++ b/tests/go/sync/bindings.wadge_test.go @@ -2,7 +2,7 @@ // Code generated by wadge-bindgen-go DO NOT EDIT -package sync +package sync_test import ( github_com__bytecodealliance__wasm___tools___go__cm "github.com/bytecodealliance/wasm-tools-go/cm" diff --git a/tests/go/sync/sync_test.go b/tests/go/sync/sync_test.go index 3bdc17a..446e275 100644 --- a/tests/go/sync/sync_test.go +++ b/tests/go/sync/sync_test.go @@ -1,4 +1,4 @@ -//go:generate go run go.wasmcloud.dev/wadge/cmd/wadge-bindgen-go +//go:generate go run go.wasmcloud.dev/wadge/cmd/wadge-bindgen-go -test //go:generate cargo build -p sync-test-component --target wasm32-unknown-unknown //go:generate cp ../../../target/wasm32-unknown-unknown/debug/sync_test_component.wasm component.wasm diff --git a/tests/go/wasi/bindings.wadge.go b/tests/go/wasi/bindings.wadge_test.go similarity index 99% rename from tests/go/wasi/bindings.wadge.go rename to tests/go/wasi/bindings.wadge_test.go index 12cdd68..3f49e35 100644 --- a/tests/go/wasi/bindings.wadge.go +++ b/tests/go/wasi/bindings.wadge_test.go @@ -2,7 +2,7 @@ // Code generated by wadge-bindgen-go DO NOT EDIT -package wasi +package wasi_test import ( github_com__bytecodealliance__wasm___tools___go__cm "github.com/bytecodealliance/wasm-tools-go/cm" diff --git a/tests/go/wasi/wasi_test.go b/tests/go/wasi/wasi_test.go index 0f249d1..bf3a7f3 100644 --- a/tests/go/wasi/wasi_test.go +++ b/tests/go/wasi/wasi_test.go @@ -1,4 +1,4 @@ -//go:generate go run go.wasmcloud.dev/wadge/cmd/wadge-bindgen-go +//go:generate go run go.wasmcloud.dev/wadge/cmd/wadge-bindgen-go -test //go:generate cargo build -p wasi-test-component --target wasm32-wasip1 //go:generate cp ../../../target/wasm32-wasip1/debug/wasi_test_component.wasm component.wasm