Skip to content

Commit

Permalink
feat(go-bindgen): add test flag
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed Oct 3, 2024
1 parent 7985084 commit 4422994
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 12 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/target
*.wasm
*.a
*.lib
bindings.wadge.go
*.wasm
/go/internal/tests/*/bindings/*
/target
bindings.wadge.go
bindings.wadge_test.go
24 changes: 21 additions & 3 deletions cmd/wadge-bindgen-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"errors"
"flag"
"fmt"
"go/ast"
Expand All @@ -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"},
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/go/http/http_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/go/sync/sync_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/go/wasi/wasi_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 4422994

Please sign in to comment.