build_test.go 7.2 KB

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