github.com/acrespo/mobile@v0.0.0-20190107162257-dc0771356504/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 {"12345", "go12345"}, 58 } 59 60 for _, tc := range tests { 61 if got := androidPkgName(tc.in); got != tc.want { 62 t.Errorf("len %d", len(tc.in)) 63 t.Errorf("androidPkgName(%q) = %q, want %q", tc.in, got, tc.want) 64 } 65 } 66 } 67 68 func TestAndroidBuild(t *testing.T) { 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 := ctx.BuildTags 86 ctx.BuildTags = []string{"tag1"} 87 defer func() { 88 ctx.BuildTags = oldTags 89 }() 90 err := runBuild(cmdBuild) 91 if err != nil { 92 t.Log(buf.String()) 93 t.Fatal(err) 94 } 95 96 diff, err := diffOutput(buf.String(), androidBuildTmpl) 97 if err != nil { 98 t.Fatalf("computing diff failed: %v", err) 99 } 100 if diff != "" { 101 t.Errorf("unexpected output:\n%s", diff) 102 } 103 } 104 105 var androidBuildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile 106 WORK=$WORK 107 mkdir -p $WORK/lib/armeabi-v7a 108 GOOS=android GOARCH=arm CC=$GOMOBILE/ndk-toolchains/arm/bin/arm-linux-androideabi-clang CXX=$GOMOBILE/ndk-toolchains/arm/bin/arm-linux-androideabi-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 109 `)) 110 111 func TestParseBuildTargetFlag(t *testing.T) { 112 archs := strings.Join(allArchs, ",") 113 114 tests := []struct { 115 in string 116 wantErr bool 117 wantOS string 118 wantArchs string 119 }{ 120 {"android", false, "android", archs}, 121 {"android,android/arm", false, "android", archs}, 122 {"android/arm", false, "android", "arm"}, 123 124 {"ios", false, "darwin", archs}, 125 {"ios,ios/arm", false, "darwin", archs}, 126 {"ios/arm", false, "darwin", "arm"}, 127 {"ios/amd64", false, "darwin", "amd64"}, 128 129 {"", true, "", ""}, 130 {"linux", true, "", ""}, 131 {"android/x86", true, "", ""}, 132 {"android/arm5", true, "", ""}, 133 {"ios/mips", true, "", ""}, 134 {"android,ios", true, "", ""}, 135 {"ios,android", true, "", ""}, 136 } 137 138 for _, tc := range tests { 139 gotOS, gotArchs, err := parseBuildTarget(tc.in) 140 if tc.wantErr { 141 if err == nil { 142 t.Errorf("-target=%q; want error, got (%q, %q, nil)", tc.in, gotOS, gotArchs) 143 } 144 continue 145 } 146 if err != nil || gotOS != tc.wantOS || strings.Join(gotArchs, ",") != tc.wantArchs { 147 t.Errorf("-target=%q; want (%v, [%v], nil), got (%q, %q, %v)", 148 tc.in, tc.wantOS, tc.wantArchs, gotOS, gotArchs, err) 149 } 150 } 151 } 152 153 func TestRegexImportGolangXPackage(t *testing.T) { 154 tests := []struct { 155 in string 156 want string 157 wantLen int 158 }{ 159 {"ffffffff t golang.org/x/mobile", "golang.org/x/mobile", 2}, 160 {"ffffffff t github.com/example/repo/vendor/golang.org/x/mobile", "golang.org/x/mobile", 2}, 161 {"ffffffff t github.com/example/golang.org/x/mobile", "", 0}, 162 {"ffffffff t github.com/example/repo", "", 0}, 163 {"ffffffff t github.com/example/repo/vendor", "", 0}, 164 } 165 166 for _, tc := range tests { 167 res := nmRE.FindStringSubmatch(tc.in) 168 if len(res) != tc.wantLen { 169 t.Errorf("nmRE returned unexpected result for %q: want len(res) = %d, got %d", 170 tc.in, tc.wantLen, len(res)) 171 continue 172 } 173 if tc.wantLen == 0 { 174 continue 175 } 176 if res[1] != tc.want { 177 t.Errorf("nmRE returned unexpected result. want (%v), got (%v)", 178 tc.want, res[1]) 179 } 180 } 181 }