github.com/cyrilou242/gomobile-java@v0.0.0-20220215185836-09daef25a210/cmd/gomobile/env_test.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "testing" 12 ) 13 14 func TestNdkRoot(t *testing.T) { 15 home, err := ioutil.TempDir("", "gomobile-test-") 16 if err != nil { 17 t.Fatal(err) 18 } 19 20 homeorig := os.Getenv("ANDROID_HOME") 21 ndkhomeorig := os.Getenv("ANDROID_NDK_HOME") 22 defer func() { 23 os.Setenv("ANDROID_HOME", homeorig) 24 os.Setenv("ANDROID_NDK_HOME", ndkhomeorig) 25 os.RemoveAll(home) 26 }() 27 28 os.Setenv("ANDROID_HOME", home) 29 sdkNDK := filepath.Join(home, "ndk-bundle") 30 envNDK := filepath.Join(home, "android-ndk") 31 os.Setenv("ANDROID_NDK_HOME", envNDK) 32 33 if ndk, err := ndkRoot(); err == nil { 34 t.Errorf("expected error but got %q", ndk) 35 } 36 37 for _, dir := range []string{sdkNDK, envNDK} { 38 if err := os.Mkdir(dir, 0755); err != nil { 39 t.Fatalf("couldn't mkdir %q", dir) 40 } 41 } 42 43 if ndk, _ := ndkRoot(); ndk != sdkNDK { 44 t.Errorf("got %q want %q", ndk, sdkNDK) 45 } 46 47 os.Unsetenv("ANDROID_HOME") 48 49 if ndk, _ := ndkRoot(); ndk != envNDK { 50 t.Errorf("got %q want %q", ndk, envNDK) 51 } 52 53 os.RemoveAll(envNDK) 54 55 if ndk, err := ndkRoot(); err == nil { 56 t.Errorf("expected error but got %q", ndk) 57 } 58 59 os.Setenv("ANDROID_HOME", home) 60 61 if ndk, _ := ndkRoot(); ndk != sdkNDK { 62 t.Errorf("got %q want %q", ndk, sdkNDK) 63 } 64 }