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