build_test.go 7.2 KB

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