all: skip or fix tests for GOOS=android

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 <mail@eliasnaur.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Elias Naur 2019-03-01 21:40:45 +01:00
parent 3ac37b7f94
commit b8c6dab863
7 changed files with 63 additions and 27 deletions

View File

@ -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")

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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

View File

@ -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;

View File

@ -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)