Debian Codegraph @

cmd/gomobile/build_test.go

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