cmd/gobind,cmd/gomobile: don't go install binaries

Use a temporary location for the gobind and gomobile binaries
during tests.

This avoids depending on the go install path being in PATH and avoids
overwriting any existing gobind or gomobile binary the user have installed.

For the android x/mobile builder.

Change-Id: I98a6767fcff642a7143efc3eeb2cb3aa1f1719cc
Reviewed-on: https://go-review.googlesource.com/c/164797
Run-TryBot: Elias Naur <mail@eliasnaur.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Elias Naur
2019-03-01 16:17:44 +00:00
parent ca80213619
commit 2c6ea15e35
2 changed files with 57 additions and 33 deletions
+37 -18
View File
@@ -8,16 +8,51 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"golang.org/x/mobile/internal/importers/java"
)
var gomobileBin string
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
// Build gomobile and gobind and put them into PATH.
binDir, err := ioutil.TempDir("", "bind-java-test-")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(binDir)
exe := ""
if runtime.GOOS == "windows" {
exe = ".exe"
}
gomobileBin = filepath.Join(binDir, "gomobile"+exe)
gobindBin := filepath.Join(binDir, "gobind"+exe)
if out, err := exec.Command("go", "build", "-o", gomobileBin, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
log.Fatalf("gomobile build failed: %v: %s", err, out)
}
if out, err := exec.Command("go", "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
log.Fatalf("gobind build failed: %v: %s", err, out)
}
PATH := os.Getenv("PATH")
if PATH != "" {
PATH += string(filepath.ListSeparator)
}
PATH += binDir
os.Setenv("PATH", PATH)
return m.Run()
}
func TestClasses(t *testing.T) {
if !java.IsAvailable() {
t.Skipf("java importer is not available")
@@ -71,22 +106,6 @@ func runTest(t *testing.T, pkgNames []string, javaPkg, javaCls string) {
if sdk := os.Getenv("ANDROID_HOME"); sdk == "" {
t.Skip("ANDROID_HOME environment var not set, skipping")
}
gomobile, err := exec.LookPath("gomobile")
if err != nil {
t.Log("go install gomobile")
if _, err := run("go install golang.org/x/mobile/cmd/gomobile"); err != nil {
t.Fatalf("gomobile install failed: %v", err)
}
if gomobile, err = exec.LookPath("gomobile"); err != nil {
t.Fatalf("gomobile install failed: %v", err)
}
t.Log("gomobile init")
start := time.Now()
if _, err := run(gomobile + " init"); err != nil {
t.Fatalf("gomobile init failed: %v", err)
}
t.Logf("gomobile init took %v", time.Since(start))
}
cwd, err := os.Getwd()
if err != nil {
@@ -116,7 +135,7 @@ func runTest(t *testing.T, pkgNames []string, javaPkg, javaCls string) {
args = append(args, "-javapkg", javaPkg)
}
args = append(args, pkgNames...)
buf, err := exec.Command(gomobile, args...).CombinedOutput()
buf, err := exec.Command(gomobileBin, args...).CombinedOutput()
if err != nil {
t.Logf("%s", buf)
t.Fatalf("failed to run gomobile bind: %v", err)