github.com/F4RD1N/gomobile@v1.0.1/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  
    30  	if ndk, err := ndkRoot(); err == nil {
    31  		t.Errorf("expected error but got %q", ndk)
    32  	}
    33  
    34  	sdkNDK := filepath.Join(home, "ndk-bundle")
    35  	envNDK := filepath.Join(home, "android-ndk")
    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  	os.Setenv("ANDROID_NDK_HOME", envNDK)
    44  
    45  	if ndk, _ := ndkRoot(); ndk != sdkNDK {
    46  		t.Errorf("got %q want %q", ndk, sdkNDK)
    47  	}
    48  
    49  	os.Unsetenv("ANDROID_HOME")
    50  
    51  	if ndk, _ := ndkRoot(); ndk != envNDK {
    52  		t.Errorf("got %q want %q", ndk, envNDK)
    53  	}
    54  
    55  	os.RemoveAll(envNDK)
    56  
    57  	if ndk, err := ndkRoot(); err == nil {
    58  		t.Errorf("expected error but got %q", ndk)
    59  	}
    60  
    61  	os.Setenv("ANDROID_HOME", home)
    62  
    63  	if ndk, _ := ndkRoot(); ndk != sdkNDK {
    64  		t.Errorf("got %q want %q", ndk, sdkNDK)
    65  	}
    66  }