github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/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 "os" 10 "path/filepath" 11 "strings" 12 "testing" 13 "text/template" 14 ) 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 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 40 func TestAndroidPkgName(t *testing.T) { 41 tests := []struct { 42 in, want string 43 }{ 44 {"a", "a"}, 45 {"a123", "a123"}, 46 {"a.b.c", "a_b_c"}, 47 {"a-b", "a_b"}, 48 {"a:b", "a_b"}, 49 {"a?b", "a_b"}, 50 {"αβγ", "go___"}, 51 {"💩", "go_"}, 52 {"My App", "My_App"}, 53 {"...", "go___"}, 54 {".-.", "go___"}, 55 {"abstract", "abstract_"}, 56 {"Abstract", "Abstract"}, 57 } 58 59 for _, tc := range tests { 60 if got := androidPkgName(tc.in); got != tc.want { 61 t.Errorf("len %d", len(tc.in)) 62 t.Errorf("androidPkgName(%q) = %q, want %q", tc.in, got, tc.want) 63 } 64 } 65 } 66 67 func TestAndroidBuild(t *testing.T) { 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" 79 gopath = filepath.ToSlash(filepath.SplitList(os.Getenv("GOPATH"))[0]) 80 if goos == "windows" { 81 os.Setenv("HOMEDRIVE", "C:") 82 } 83 cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"}) 84 ctx.BuildTags = []string{"tag1"} 85 err := runBuild(cmdBuild) 86 if err != nil { 87 t.Log(buf.String()) 88 t.Fatal(err) 89 } 90 91 diff, err := diffOutput(buf.String(), androidBuildTmpl) 92 if err != nil { 93 t.Fatalf("computing diff failed: %v", err) 94 } 95 if diff != "" { 96 t.Errorf("unexpected output:\n%s", diff) 97 } 98 } 99 100 var androidBuildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile 101 WORK=$WORK 102 mkdir -p $WORK/lib/armeabi-v7a 103 GOOS=android GOARCH=arm CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} CGO_ENABLED=1 GOARM=7 go build -p={{.NumCPU}} -pkgdir=$GOMOBILE/pkg_android_arm -tags="tag1" -x -buildmode=c-shared -o $WORK/lib/armeabi-v7a/libbasic.so golang.org/x/mobile/example/basic 104 `)) 105 106 func TestParseBuildTargetFlag(t *testing.T) { 107 androidArchs := "arm" 108 iosArchs := "arm,arm64,amd64" 109 110 tests := []struct { 111 in string 112 wantErr bool 113 wantOS string 114 wantArchs string 115 }{ 116 {"android", false, "android", androidArchs}, 117 {"android,android/arm", false, "android", androidArchs}, 118 {"android/arm", false, "android", "arm"}, 119 120 {"ios", false, "darwin", iosArchs}, 121 {"ios,ios/arm", false, "darwin", iosArchs}, 122 {"ios/arm", false, "darwin", "arm"}, 123 {"ios/amd64", false, "darwin", "amd64"}, 124 125 {"", true, "", ""}, 126 {"linux", true, "", ""}, 127 {"android/x86", true, "", ""}, 128 {"android/arm5", true, "", ""}, 129 {"ios/mips", true, "", ""}, 130 {"android,ios", true, "", ""}, 131 {"ios,android", true, "", ""}, 132 } 133 134 for _, tc := range tests { 135 gotOS, gotArchs, err := parseBuildTarget(tc.in) 136 if tc.wantErr { 137 if err == nil { 138 t.Errorf("-target=%q; want error, got (%q, %q, nil)", tc.in, gotOS, gotArchs) 139 } 140 continue 141 } 142 if err != nil || gotOS != tc.wantOS || strings.Join(gotArchs, ",") != tc.wantArchs { 143 t.Errorf("-target=%q; want (%v, [%v], nil), got (%q, %q, %v)", 144 tc.in, tc.wantOS, tc.wantArchs, gotOS, gotArchs, err) 145 } 146 } 147 }