2
0
mirror of synced 2025-02-24 15:28:28 +00:00
mobile/cmd/gobind/gobind_test.go
Elias Naur d8b1e1aab8 bind,misc: guard reverse generated import with mobile os tags
Running go get golang.org/x/mobile/... results in errors because the
go tool fails to find the reverse generated Java ("Java/...") and
Objective-C ("ObjC/...") packages. Work around the errors by adding
the android and ios tags, respectively, to files importing those
packages.

The gobind gradle plugin is updated to pass along GOOS=android to
ensure the gobind tool continues to build Android reverse packages.

Fixes golang/go#17750

Change-Id: Id66a3c6cdfe249c6ed494192eb12195d6509332f
Reviewed-on: https://go-review.googlesource.com/34956
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-17 14:22:42 +00:00

72 lines
1.7 KiB
Go

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"os/exec"
"testing"
)
var tests = []struct {
name string
lang string
pkg string
goos string
}{
{"ObjC-Testpkg", "objc", "golang.org/x/mobile/bind/testpkg", ""},
{"Java-Testpkg", "java", "golang.org/x/mobile/bind/testpkg", ""},
{"Go-Testpkg", "go", "golang.org/x/mobile/bind/testpkg", ""},
{"Java-Javapkg", "java", "golang.org/x/mobile/bind/testpkg/javapkg", "android"},
{"Go-Javapkg", "go", "golang.org/x/mobile/bind/testpkg/javapkg", "android"},
}
func installGobind() error {
if out, err := exec.Command("go", "install", "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
return fmt.Errorf("gobind install failed: %v: %s", err, out)
}
return nil
}
func runGobind(lang, pkg, goos string) error {
cmd := exec.Command("gobind", "-lang", lang, pkg)
if goos != "" {
cmd.Env = append(os.Environ(), "GOOS="+goos)
}
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("gobind -lang %s %s failed: %v: %s", lang, pkg, err, out)
}
return nil
}
func TestGobind(t *testing.T) {
if err := installGobind(); err != nil {
t.Fatal(err)
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := runGobind(test.lang, test.pkg, test.goos); err != nil {
t.Error(err)
}
})
}
}
func BenchmarkGobind(b *testing.B) {
if err := installGobind(); err != nil {
b.Fatal(err)
}
for _, test := range tests {
b.Run(test.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := runGobind(test.lang, test.pkg, test.goos); err != nil {
b.Error(err)
}
}
})
}
}