From b8c6dab863a6bb7fceeb28cd882e272336b5d63d Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 1 Mar 2019 21:40:45 +0100 Subject: [PATCH] all: skip or fix tests for GOOS=android MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some x/mobile tests are designed to run from a host with a device or emulator attached. Some fail if they run directly from a device, which is the case when GOOS=android. Fix the tests by skipping them or adjusting them to work on GOOS=android. Remove gomobile environment naïve variable expansion for $HOME; on Android devices HOME=/ so every path separator is replaced with $HOME. Fixes golang/go#30482 Change-Id: I553e708226922f6284163f0b7d7b1011a9502e34 Reviewed-on: https://go-review.googlesource.com/c/164799 Run-TryBot: Elias Naur Reviewed-by: Brad Fitzpatrick --- bind/java/seq_test.go | 31 +++++++++++++++++------------- cmd/gobind/gobind_test.go | 23 +++++++++++++++------- cmd/gomobile/bind_test.go | 4 ++++ cmd/gomobile/build.go | 3 --- cmd/gomobile/build_test.go | 4 ++++ exp/sensor/android.c | 18 +++++++++++++---- exp/sprite/portable/affine_test.go | 7 +++++++ 7 files changed, 63 insertions(+), 27 deletions(-) diff --git a/bind/java/seq_test.go b/bind/java/seq_test.go index 51ec8bd..5ac8c1f 100644 --- a/bind/java/seq_test.go +++ b/bind/java/seq_test.go @@ -36,20 +36,22 @@ func testMain(m *testing.M) int { 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 runtime.GOOS != "android" { + 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) } - 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() } @@ -99,6 +101,9 @@ func TestJavaSeqBench(t *testing.T) { // This requires the gradle command in PATH and // the Android SDK whose path is available through ANDROID_HOME environment variable. func runTest(t *testing.T, pkgNames []string, javaPkg, javaCls string) { + if gomobileBin == "" { + t.Skipf("no gomobile on %s", runtime.GOOS) + } gradle, err := exec.LookPath("gradle") if err != nil { t.Skip("command gradle not found, skipping") diff --git a/cmd/gobind/gobind_test.go b/cmd/gobind/gobind_test.go index 3f0739c..7302f56 100644 --- a/cmd/gobind/gobind_test.go +++ b/cmd/gobind/gobind_test.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "testing" ) @@ -41,15 +42,20 @@ func testMain(m *testing.M) int { log.Fatal(err) } 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) + defer os.Remove(bin.Name()) + if runtime.GOOS != "android" { + if out, err := exec.Command("go", "build", "-o", bin.Name(), "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil { + log.Fatalf("gobind build failed: %v: %s", err, out) + } + gobindBin = bin.Name() } return m.Run() } -func runGobind(lang, pkg, goos string) error { +func runGobind(t testing.TB, lang, pkg, goos string) error { + if gobindBin == "" { + t.Skipf("gobind is not available on %s", runtime.GOOS) + } cmd := exec.Command(gobindBin, "-lang", lang, pkg) if goos != "" { cmd.Env = append(os.Environ(), "GOOS="+goos) @@ -64,7 +70,7 @@ func runGobind(lang, pkg, goos string) error { func TestGobind(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - if err := runGobind(test.lang, test.pkg, test.goos); err != nil { + if err := runGobind(t, test.lang, test.pkg, test.goos); err != nil { t.Error(err) } }) @@ -72,6 +78,9 @@ func TestGobind(t *testing.T) { } func TestDocs(t *testing.T) { + if gobindBin == "" { + t.Skipf("gobind is not available on %s", runtime.GOOS) + } // Create a fake package for doc.go tmpdir, err := ioutil.TempDir("", "gobind-test-") if err != nil { @@ -111,7 +120,7 @@ func BenchmarkGobind(b *testing.B) { 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 { + if err := runGobind(b, test.lang, test.pkg, test.goos); err != nil { b.Error(err) } } diff --git a/cmd/gomobile/bind_test.go b/cmd/gomobile/bind_test.go index 88c2aef..0e3d496 100644 --- a/cmd/gomobile/bind_test.go +++ b/cmd/gomobile/bind_test.go @@ -9,12 +9,16 @@ import ( "os" "path" "path/filepath" + "runtime" "strings" "testing" "text/template" ) func TestImportPackagesPathCleaning(t *testing.T) { + if runtime.GOOS == "android" { + t.Skip("not available on Android") + } slashPath := "golang.org/x/mobile/example/bind/hello/" pkgs, err := importPackages([]string{slashPath}) if err != nil { diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go index 9c9b28d..45f950a 100644 --- a/cmd/gomobile/build.go +++ b/cmd/gomobile/build.go @@ -213,9 +213,6 @@ func printcmd(format string, args ...interface{}) { if gopath := goEnv("GOPATH"); gopath != "" { cmd = strings.Replace(cmd, gopath, "$GOPATH", -1) } - if env := os.Getenv("HOME"); env != "" { - cmd = strings.Replace(cmd, env, "$HOME", -1) - } if env := os.Getenv("HOMEPATH"); env != "" { cmd = strings.Replace(cmd, env, "$HOMEPATH", -1) } diff --git a/cmd/gomobile/build_test.go b/cmd/gomobile/build_test.go index 1bce42f..76a820d 100644 --- a/cmd/gomobile/build_test.go +++ b/cmd/gomobile/build_test.go @@ -8,6 +8,7 @@ import ( "bytes" "os" "path/filepath" + "runtime" "strings" "testing" "text/template" @@ -66,6 +67,9 @@ func TestAndroidPkgName(t *testing.T) { } func TestAndroidBuild(t *testing.T) { + if runtime.GOOS == "android" { + t.Skip("not available on Android") + } buf := new(bytes.Buffer) defer func() { xout = os.Stderr diff --git a/exp/sensor/android.c b/exp/sensor/android.c index 369a259..119c5eb 100644 --- a/exp/sensor/android.c +++ b/exp/sensor/android.c @@ -16,8 +16,18 @@ ASensorEventQueue* queue = NULL; ALooper* looper = NULL; +static ASensorManager* getSensorManager() { + #pragma clang diagnostic push + // Builders convert C warnings to errors, so suppress the + // error from ASensorManager_getInstance being deprecated + // in Android 26. + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + return ASensorManager_getInstance(); + #pragma clang diagnostic pop +} + void GoAndroid_createManager() { - ASensorManager* manager = ASensorManager_getInstance(); + ASensorManager* manager = getSensorManager(); looper = ALooper_forThread(); if (looper == NULL) { looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); @@ -26,7 +36,7 @@ void GoAndroid_createManager() { } int GoAndroid_enableSensor(int s, int32_t usec) { - ASensorManager* manager = ASensorManager_getInstance(); + ASensorManager* manager = getSensorManager(); const ASensor* sensor = ASensorManager_getDefaultSensor(manager, s); if (sensor == NULL) { return 1; @@ -37,7 +47,7 @@ int GoAndroid_enableSensor(int s, int32_t usec) { } void GoAndroid_disableSensor(int s) { - ASensorManager* manager = ASensorManager_getInstance(); + ASensorManager* manager = getSensorManager(); const ASensor* sensor = ASensorManager_getDefaultSensor(manager, s); ASensorEventQueue_disableSensor(queue, sensor); } @@ -67,7 +77,7 @@ int GoAndroid_readQueue(int n, int32_t* types, int64_t* timestamps, float* vecto } void GoAndroid_destroyManager() { - ASensorManager* manager = ASensorManager_getInstance(); + ASensorManager* manager = getSensorManager(); ASensorManager_destroyEventQueue(manager, queue); queue = NULL; looper = NULL; diff --git a/exp/sprite/portable/affine_test.go b/exp/sprite/portable/affine_test.go index d8feb11..908933a 100644 --- a/exp/sprite/portable/affine_test.go +++ b/exp/sprite/portable/affine_test.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "math" "os" + "runtime" "testing" "golang.org/x/mobile/event/size" @@ -20,6 +21,9 @@ import ( ) func TestAffine(t *testing.T) { + if runtime.GOOS == "android" { + t.Skip("testdata not available on Android") + } f, err := os.Open("../../../testdata/testpattern.png") if err != nil { t.Fatal(err) @@ -100,6 +104,9 @@ func TestAffine(t *testing.T) { } func TestAffineMask(t *testing.T) { + if runtime.GOOS == "android" { + t.Skip("testdata not available on Android") + } f, err := os.Open("../../../testdata/testpattern.png") if err != nil { t.Fatal(err)