github.com/cyrilou242/gomobile-java@v0.0.0-20220215185836-09daef25a210/cmd/gomobile/build_test.go (about)

     1  // Copyright 2015 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  	"bytes"
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"runtime"
    14  	"strings"
    15  	"testing"
    16  	"text/template"
    17  )
    18  
    19  func TestRFC1034Label(t *testing.T) {
    20  	tests := []struct {
    21  		in, want string
    22  	}{
    23  		{"a", "a"},
    24  		{"123", "-23"},
    25  		{"a.b.c", "a-b-c"},
    26  		{"a-b", "a-b"},
    27  		{"a:b", "a-b"},
    28  		{"a?b", "a-b"},
    29  		{"αβγ", "---"},
    30  		{"💩", "--"},
    31  		{"My App", "My-App"},
    32  		{"...", ""},
    33  		{".-.", "--"},
    34  	}
    35  
    36  	for _, tc := range tests {
    37  		if got := rfc1034Label(tc.in); got != tc.want {
    38  			t.Errorf("rfc1034Label(%q) = %q, want %q", tc.in, got, tc.want)
    39  		}
    40  	}
    41  }
    42  
    43  func TestAndroidPkgName(t *testing.T) {
    44  	tests := []struct {
    45  		in, want string
    46  	}{
    47  		{"a", "a"},
    48  		{"a123", "a123"},
    49  		{"a.b.c", "a_b_c"},
    50  		{"a-b", "a_b"},
    51  		{"a:b", "a_b"},
    52  		{"a?b", "a_b"},
    53  		{"αβγ", "go___"},
    54  		{"💩", "go_"},
    55  		{"My App", "My_App"},
    56  		{"...", "go___"},
    57  		{".-.", "go___"},
    58  		{"abstract", "abstract_"},
    59  		{"Abstract", "Abstract"},
    60  		{"12345", "go12345"},
    61  	}
    62  
    63  	for _, tc := range tests {
    64  		if got := androidPkgName(tc.in); got != tc.want {
    65  			t.Errorf("len %d", len(tc.in))
    66  			t.Errorf("androidPkgName(%q) = %q, want %q", tc.in, got, tc.want)
    67  		}
    68  	}
    69  }
    70  
    71  func TestAndroidBuild(t *testing.T) {
    72  	if runtime.GOOS == "android" || runtime.GOOS == "ios" {
    73  		t.Skipf("not available on %s", runtime.GOOS)
    74  	}
    75  	buf := new(bytes.Buffer)
    76  	defer func() {
    77  		xout = os.Stderr
    78  		buildN = false
    79  		buildX = false
    80  	}()
    81  	xout = buf
    82  	buildN = true
    83  	buildX = true
    84  	buildO = "basic.apk"
    85  	buildTarget = "android/arm"
    86  	gopath = filepath.ToSlash(filepath.SplitList(goEnv("GOPATH"))[0])
    87  	if goos == "windows" {
    88  		os.Setenv("HOMEDRIVE", "C:")
    89  	}
    90  	cmdBuild.flag.Parse([]string{"github.com/cyrilou242/gomobile-java/example/basic"})
    91  	oldTags := buildTags
    92  	buildTags = []string{"tag1"}
    93  	defer func() {
    94  		buildTags = oldTags
    95  	}()
    96  	err := runBuild(cmdBuild)
    97  	if err != nil {
    98  		t.Log(buf.String())
    99  		t.Fatal(err)
   100  	}
   101  
   102  	diff, err := diffOutput(buf.String(), androidBuildTmpl)
   103  	if err != nil {
   104  		t.Fatalf("computing diff failed: %v", err)
   105  	}
   106  	if diff != "" {
   107  		t.Errorf("unexpected output:\n%s", diff)
   108  	}
   109  }
   110  
   111  var androidBuildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
   112  WORK=$WORK
   113  mkdir -p $WORK/lib/armeabi-v7a
   114  GOOS=android GOARCH=arm CC=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang CXX=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang++ CGO_ENABLED=1 GOARM=7 go build -tags tag1 -x -buildmode=c-shared -o $WORK/lib/armeabi-v7a/libbasic.so github.com/cyrilou242/gomobile-java/example/basic
   115  `))
   116  
   117  func TestParseBuildTarget(t *testing.T) {
   118  	wantAndroid := "android/" + strings.Join(platformArchs("android"), ",android/")
   119  
   120  	tests := []struct {
   121  		in      string
   122  		wantErr bool
   123  		want    string
   124  	}{
   125  		{"android", false, wantAndroid},
   126  		{"android,android/arm", false, wantAndroid},
   127  		{"android/arm", false, "android/arm"},
   128  
   129  		{"ios", false, "ios/arm64,iossimulator/arm64,iossimulator/amd64"},
   130  		{"ios,ios/arm64", false, "ios/arm64"},
   131  		{"ios/arm64", false, "ios/arm64"},
   132  
   133  		{"iossimulator", false, "iossimulator/arm64,iossimulator/amd64"},
   134  		{"iossimulator/amd64", false, "iossimulator/amd64"},
   135  
   136  		{"macos", false, "macos/arm64,macos/amd64"},
   137  		{"macos,ios/arm64", false, "macos/arm64,macos/amd64,ios/arm64"},
   138  		{"macos/arm64", false, "macos/arm64"},
   139  		{"macos/amd64", false, "macos/amd64"},
   140  
   141  		{"maccatalyst", false, "maccatalyst/arm64,maccatalyst/amd64"},
   142  		{"maccatalyst,ios/arm64", false, "maccatalyst/arm64,maccatalyst/amd64,ios/arm64"},
   143  		{"maccatalyst/arm64", false, "maccatalyst/arm64"},
   144  		{"maccatalyst/amd64", false, "maccatalyst/amd64"},
   145  
   146  		{"", true, ""},
   147  		{"linux", true, ""},
   148  		{"android/x86", true, ""},
   149  		{"android/arm5", true, ""},
   150  		{"ios/mips", true, ""},
   151  		{"android,ios", true, ""},
   152  		{"ios,android", true, ""},
   153  		{"ios/amd64", true, ""},
   154  	}
   155  
   156  	for _, tc := range tests {
   157  		t.Run(tc.in, func(t *testing.T) {
   158  			targets, err := parseBuildTarget(tc.in)
   159  			var s []string
   160  			for _, t := range targets {
   161  				s = append(s, t.String())
   162  			}
   163  			got := strings.Join(s, ",")
   164  			if tc.wantErr {
   165  				if err == nil {
   166  					t.Errorf("-target=%q; want error, got (%q, nil)", tc.in, got)
   167  				}
   168  				return
   169  			}
   170  			if err != nil || got != tc.want {
   171  				t.Errorf("-target=%q; want (%q, nil), got (%q, %v)", tc.in, tc.want, got, err)
   172  			}
   173  		})
   174  	}
   175  }
   176  
   177  func TestRegexImportGolangXPackage(t *testing.T) {
   178  	tests := []struct {
   179  		in      string
   180  		want    string
   181  		wantLen int
   182  	}{
   183  		{"ffffffff t github.com/cyrilou242/gomobile-java", "github.com/cyrilou242/gomobile-java", 2},
   184  		{"ffffffff t github.com/example/repo/vendor/github.com/cyrilou242/gomobile-java", "github.com/cyrilou242/gomobile-java", 2},
   185  		{"ffffffff t github.com/example/github.com/cyrilou242/gomobile-java", "", 0},
   186  		{"ffffffff t github.com/example/repo", "", 0},
   187  		{"ffffffff t github.com/example/repo/vendor", "", 0},
   188  		{"ffffffff t _github.com/cyrilou242/gomobile-java/app", "github.com/cyrilou242/gomobile-java/app", 2},
   189  	}
   190  
   191  	for _, tc := range tests {
   192  		res := nmRE.FindStringSubmatch(tc.in)
   193  		if len(res) != tc.wantLen {
   194  			t.Errorf("nmRE returned unexpected result for %q: want len(res) = %d, got %d",
   195  				tc.in, tc.wantLen, len(res))
   196  			continue
   197  		}
   198  		if tc.wantLen == 0 {
   199  			continue
   200  		}
   201  		if res[1] != tc.want {
   202  			t.Errorf("nmRE returned unexpected result. want (%v), got (%v)",
   203  				tc.want, res[1])
   204  		}
   205  	}
   206  }
   207  
   208  func TestBuildWithGoModules(t *testing.T) {
   209  	if runtime.GOOS == "android" || runtime.GOOS == "ios" {
   210  		t.Skipf("gomobile are not available on %s", runtime.GOOS)
   211  	}
   212  
   213  	dir, err := ioutil.TempDir("", "gomobile-test")
   214  	if err != nil {
   215  		t.Fatal(err)
   216  	}
   217  	defer os.RemoveAll(dir)
   218  
   219  	if out, err := exec.Command("go", "build", "-o="+dir, "github.com/cyrilou242/gomobile-java/cmd/gomobile").CombinedOutput(); err != nil {
   220  		t.Fatalf("%v: %s", err, string(out))
   221  	}
   222  	path := dir
   223  	if p := os.Getenv("PATH"); p != "" {
   224  		path += string(filepath.ListSeparator) + p
   225  	}
   226  
   227  	for _, target := range []string{"android", "ios"} {
   228  		t.Run(target, func(t *testing.T) {
   229  			switch target {
   230  			case "android":
   231  				androidHome := os.Getenv("ANDROID_HOME")
   232  				if androidHome == "" {
   233  					t.Skip("ANDROID_HOME not found, skipping bind")
   234  				}
   235  				if _, err := androidAPIPath(); err != nil {
   236  					t.Skip("No android API platform found in $ANDROID_HOME, skipping bind")
   237  				}
   238  			case "ios":
   239  				if !xcodeAvailable() {
   240  					t.Skip("Xcode is missing")
   241  				}
   242  			}
   243  
   244  			var out string
   245  			switch target {
   246  			case "android":
   247  				out = filepath.Join(dir, "basic.apk")
   248  			case "ios":
   249  				out = filepath.Join(dir, "Basic.app")
   250  			}
   251  
   252  			tests := []struct {
   253  				Name string
   254  				Path string
   255  				Dir  string
   256  			}{
   257  				{
   258  					Name: "Absolute Path",
   259  					Path: "github.com/cyrilou242/gomobile-java/example/basic",
   260  				},
   261  				{
   262  					Name: "Relative Path",
   263  					Path: "./example/basic",
   264  					Dir:  filepath.Join("..", ".."),
   265  				},
   266  			}
   267  
   268  			for _, tc := range tests {
   269  				tc := tc
   270  				t.Run(tc.Name, func(t *testing.T) {
   271  					args := []string{"build", "-target=" + target, "-o=" + out}
   272  					if target == "ios" {
   273  						args = append(args, "-bundleid=org.golang.gomobiletest")
   274  					}
   275  					args = append(args, tc.Path)
   276  					cmd := exec.Command(filepath.Join(dir, "gomobile"), args...)
   277  					cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
   278  					cmd.Dir = tc.Dir
   279  					if out, err := cmd.CombinedOutput(); err != nil {
   280  						t.Errorf("gomobile build failed: %v\n%s", err, string(out))
   281  					}
   282  				})
   283  			}
   284  		})
   285  	}
   286  }