2
0
mirror of synced 2025-02-25 15:55:27 +00:00
mobile/cmd/gomobile/env_test.go
Changkun Ou d82874c7e6 cmd/gomobile: fix flaky TestNdkRoot test
The current TestNdkRoot may fail if ANDROID_HOME and ANDROID_NDK_HOME
are both set from the environment. This is because ndkRoot will first
check ANDROID_HOME, if fails, then check ANDROID_NDK_HOME.

The test was intended to reset the ANDROID_HOME and expecting an error
for fetching ANDROID_NDK_HOME. However, if ANDROID_NDK_HOME is set,
the ndkRoot will not return an error, which may fail the test.

This change moves the environment settings around to permit TestNdkRoot
to pass.

Change-Id: I17ce6dbeec186d35f7a4ba2d8275a43a862c306d
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/346152
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dmitri Shuralyov <dmitshur@golang.org>
2021-08-31 15:17:20 +00:00

65 lines
1.4 KiB
Go

// Copyright 2019 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 (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestNdkRoot(t *testing.T) {
home, err := ioutil.TempDir("", "gomobile-test-")
if err != nil {
t.Fatal(err)
}
homeorig := os.Getenv("ANDROID_HOME")
ndkhomeorig := os.Getenv("ANDROID_NDK_HOME")
defer func() {
os.Setenv("ANDROID_HOME", homeorig)
os.Setenv("ANDROID_NDK_HOME", ndkhomeorig)
os.RemoveAll(home)
}()
os.Setenv("ANDROID_HOME", home)
sdkNDK := filepath.Join(home, "ndk-bundle")
envNDK := filepath.Join(home, "android-ndk")
os.Setenv("ANDROID_NDK_HOME", envNDK)
if ndk, err := ndkRoot(); err == nil {
t.Errorf("expected error but got %q", ndk)
}
for _, dir := range []string{sdkNDK, envNDK} {
if err := os.Mkdir(dir, 0755); err != nil {
t.Fatalf("couldn't mkdir %q", dir)
}
}
if ndk, _ := ndkRoot(); ndk != sdkNDK {
t.Errorf("got %q want %q", ndk, sdkNDK)
}
os.Unsetenv("ANDROID_HOME")
if ndk, _ := ndkRoot(); ndk != envNDK {
t.Errorf("got %q want %q", ndk, envNDK)
}
os.RemoveAll(envNDK)
if ndk, err := ndkRoot(); err == nil {
t.Errorf("expected error but got %q", ndk)
}
os.Setenv("ANDROID_HOME", home)
if ndk, _ := ndkRoot(); ndk != sdkNDK {
t.Errorf("got %q want %q", ndk, sdkNDK)
}
}