From 2c6ea15e35f0f3c317fb05d7712bfab7c1cc9899 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 1 Mar 2019 10:54:22 +0100 Subject: [PATCH] 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 Reviewed-by: Brad Fitzpatrick --- bind/java/seq_test.go | 55 ++++++++++++++++++++++++++------------- cmd/gobind/gobind_test.go | 35 ++++++++++++++----------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/bind/java/seq_test.go b/bind/java/seq_test.go index 3f5de90..51ec8bd 100644 --- a/bind/java/seq_test.go +++ b/bind/java/seq_test.go @@ -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) diff --git a/cmd/gobind/gobind_test.go b/cmd/gobind/gobind_test.go index f2ff706..3f0739c 100644 --- a/cmd/gobind/gobind_test.go +++ b/cmd/gobind/gobind_test.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "io/ioutil" + "log" "os" "os/exec" "path/filepath" @@ -28,15 +29,28 @@ var tests = []struct { {"Go-Javapkg", "go,java,objc", "golang.org/x/mobile/bind/testdata/cgopkg", "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) +var gobindBin string + +func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + bin, err := ioutil.TempFile("", "*.exe") + if err != nil { + log.Fatal(err) } - return nil + bin.Close() + gobindBin = bin.Name() + defer os.Remove(gobindBin) + 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) + } + return m.Run() } func runGobind(lang, pkg, goos string) error { - cmd := exec.Command("gobind", "-lang", lang, pkg) + cmd := exec.Command(gobindBin, "-lang", lang, pkg) if goos != "" { cmd.Env = append(os.Environ(), "GOOS="+goos) cmd.Env = append(os.Environ(), "CGO_ENABLED=1") @@ -48,9 +62,6 @@ func runGobind(lang, pkg, goos string) error { } 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 { @@ -61,9 +72,6 @@ func TestGobind(t *testing.T) { } func TestDocs(t *testing.T) { - if err := installGobind(); err != nil { - t.Fatal(err) - } // Create a fake package for doc.go tmpdir, err := ioutil.TempDir("", "gobind-test-") if err != nil { @@ -86,7 +94,7 @@ type Struct struct{ const comment = "This is a comment." for _, lang := range []string{"java", "objc"} { - cmd := exec.Command("gobind", "-lang", lang, "doctest") + cmd := exec.Command(gobindBin, "-lang", lang, "doctest") cmd.Env = append(os.Environ(), "GOROOT="+tmpdir) out, err := cmd.CombinedOutput() if err != nil { @@ -100,9 +108,6 @@ type Struct struct{ } 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++ {