github.com/SkycoinProject/gomobile@v0.0.0-20190312151609-d3739f865fa6/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 "runtime" 12 "strings" 13 "testing" 14 "text/template" 15 ) 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 34 for _, tc := range tests { 35 if got := rfc1034Label(tc.in); got != tc.want { 36 t.Errorf("rfc1034Label(%q) = %q, want %q", tc.in, got, tc.want) 37 } 38 } 39 } 40 41 func TestAndroidPkgName(t *testing.T) { 42 tests := []struct { 43 in, want string 44 }{ 45 {"a", "a"}, 46 {"a123", "a123"}, 47 {"a.b.c", "a_b_c"}, 48 {"a-b", "a_b"}, 49 {"a:b", "a_b"}, 50 {"a?b", "a_b"}, 51 {"αβγ", "go___"}, 52 {"💩", "go_"}, 53 {"My App", "My_App"}, 54 {"...", "go___"}, 55 {".-.", "go___"}, 56 {"abstract", "abstract_"}, 57 {"Abstract", "Abstract"}, 58 {"12345", "go12345"}, 59 } 60 61 for _, tc := range tests { 62 if got := androidPkgName(tc.in); got != tc.want { 63 t.Errorf("len %d", len(tc.in)) 64 t.Errorf("androidPkgName(%q) = %q, want %q", tc.in, got, tc.want) 65 } 66 } 67 } 68 69 func TestAndroidBuild(t *testing.T) { 70 if runtime.GOOS == "android" { 71 t.Skip("not available on Android") 72 } 73 buf := new(bytes.Buffer) 74 defer func() { 75 xout = os.Stderr 76 buildN = false 77 buildX = false 78 }() 79 xout = buf 80 buildN = true 81 buildX = true 82 buildO = "basic.apk" 83 buildTarget = "android/arm" 84 gopath = filepath.ToSlash(filepath.SplitList(goEnv("GOPATH"))[0]) 85 if goos == "windows" { 86 os.Setenv("HOMEDRIVE", "C:") 87 } 88 cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"}) 89 oldTags := ctx.BuildTags 90 ctx.BuildTags = []string{"tag1"} 91 defer func() { 92 ctx.BuildTags = oldTags 93 }() 94 err := runBuild(cmdBuild) 95 if err != nil { 96 t.Log(buf.String()) 97 t.Fatal(err) 98 } 99 100 diff, err := diffOutput(buf.String(), androidBuildTmpl) 101 if err != nil { 102 t.Fatalf("computing diff failed: %v", err) 103 } 104 if diff != "" { 105 t.Errorf("unexpected output:\n%s", diff) 106 } 107 } 108 109 var androidBuildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile 110 WORK=$WORK 111 mkdir -p $WORK/lib/armeabi-v7a 112 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 113 `)) 114 115 func TestParseBuildTargetFlag(t *testing.T) { 116 archs := strings.Join(allArchs, ",") 117 118 tests := []struct { 119 in string 120 wantErr bool 121 wantOS string 122 wantArchs string 123 }{ 124 {"android", false, "android", archs}, 125 {"android,android/arm", false, "android", archs}, 126 {"android/arm", false, "android", "arm"}, 127 128 {"ios", false, "darwin", archs}, 129 {"ios,ios/arm", false, "darwin", archs}, 130 {"ios/arm", false, "darwin", "arm"}, 131 {"ios/amd64", false, "darwin", "amd64"}, 132 133 {"", true, "", ""}, 134 {"linux", true, "", ""}, 135 {"android/x86", true, "", ""}, 136 {"android/arm5", true, "", ""}, 137 {"ios/mips", true, "", ""}, 138 {"android,ios", true, "", ""}, 139 {"ios,android", true, "", ""}, 140 } 141 142 for _, tc := range tests { 143 gotOS, gotArchs, err := parseBuildTarget(tc.in) 144 if tc.wantErr { 145 if err == nil { 146 t.Errorf("-target=%q; want error, got (%q, %q, nil)", tc.in, gotOS, gotArchs) 147 } 148 continue 149 } 150 if err != nil || gotOS != tc.wantOS || strings.Join(gotArchs, ",") != tc.wantArchs { 151 t.Errorf("-target=%q; want (%v, [%v], nil), got (%q, %q, %v)", 152 tc.in, tc.wantOS, tc.wantArchs, gotOS, gotArchs, err) 153 } 154 } 155 } 156 157 func TestRegexImportGolangXPackage(t *testing.T) { 158 tests := []struct { 159 in string 160 want string 161 wantLen int 162 }{ 163 {"ffffffff t golang.org/x/mobile", "golang.org/x/mobile", 2}, 164 {"ffffffff t github.com/example/repo/vendor/golang.org/x/mobile", "golang.org/x/mobile", 2}, 165 {"ffffffff t github.com/example/golang.org/x/mobile", "", 0}, 166 {"ffffffff t github.com/example/repo", "", 0}, 167 {"ffffffff t github.com/example/repo/vendor", "", 0}, 168 } 169 170 for _, tc := range tests { 171 res := nmRE.FindStringSubmatch(tc.in) 172 if len(res) != tc.wantLen { 173 t.Errorf("nmRE returned unexpected result for %q: want len(res) = %d, got %d", 174 tc.in, tc.wantLen, len(res)) 175 continue 176 } 177 if tc.wantLen == 0 { 178 continue 179 } 180 if res[1] != tc.want { 181 t.Errorf("nmRE returned unexpected result. want (%v), got (%v)", 182 tc.want, res[1]) 183 } 184 } 185 }