github.com/corfe83/mobile@v0.0.0-20220928034243-9edc37f43fac/cmd/gomobile/init_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 "io/ioutil" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "strings" 14 "testing" 15 "text/template" 16 ) 17 18 var gopath string 19 20 func TestInit(t *testing.T) { 21 if _, err := exec.LookPath("diff"); err != nil { 22 t.Skip("command diff not found, skipping") 23 } 24 buf := new(bytes.Buffer) 25 gopathorig := os.Getenv("GOPATH") 26 defer func() { 27 xout = os.Stderr 28 buildN = false 29 buildX = false 30 os.Setenv("GOPATH", gopathorig) 31 }() 32 xout = buf 33 buildN = true 34 buildX = true 35 36 // Test that first GOPATH element is chosen correctly. 37 var err error 38 gopath, err = ioutil.TempDir("", "gomobile-test") 39 if err != nil { 40 t.Fatal(err) 41 } 42 paths := []string{gopath, "/path2", "/path3"} 43 if goos == "windows" { 44 gopath = filepath.ToSlash(`C:\GOPATH1`) 45 paths = []string{gopath, `C:\PATH2`, `C:\PATH3`} 46 } 47 os.Setenv("GOPATH", strings.Join(paths, string(os.PathListSeparator))) 48 os.Setenv("GOROOT_BOOTSTRAP", "go1.4") 49 if goos == "windows" { 50 os.Setenv("HOMEDRIVE", "C:") 51 } 52 53 emptymod, err := ioutil.TempDir("", "gomobile-test") 54 if err != nil { 55 t.Fatal(err) 56 } 57 defer os.RemoveAll(emptymod) 58 59 // Create go.mod, but without Go files. 60 f, err := os.Create(filepath.Join(emptymod, "go.mod")) 61 if err != nil { 62 t.Fatal(err) 63 } 64 defer f.Close() 65 if _, err := f.WriteString("module example.com/m\n"); err != nil { 66 t.Fatal(err) 67 } 68 if err := f.Sync(); err != nil { 69 t.Fatal(err) 70 } 71 72 dirs := []struct { 73 dir string 74 name string 75 }{ 76 { 77 dir: ".", 78 name: "current", 79 }, 80 { 81 dir: emptymod, 82 name: "emptymod", 83 }, 84 } 85 for _, dir := range dirs { 86 dir := dir 87 t.Run(dir.name, func(t *testing.T) { 88 wd, err := os.Getwd() 89 if err != nil { 90 t.Fatal(err) 91 } 92 if err := os.Chdir(dir.dir); err != nil { 93 t.Fatal(err) 94 } 95 defer os.Chdir(wd) 96 97 if err := runInit(cmdInit); err != nil { 98 t.Log(buf.String()) 99 t.Fatal(err) 100 } 101 102 if dir.name == "emptymod" { 103 return 104 } 105 106 diff, err := diffOutput(buf.String(), initTmpl) 107 if err != nil { 108 t.Fatalf("computing diff failed: %v", err) 109 } 110 if diff != "" { 111 t.Errorf("unexpected output:\n%s", diff) 112 } 113 }) 114 } 115 } 116 117 func diffOutput(got string, wantTmpl *template.Template) (string, error) { 118 got = filepath.ToSlash(got) 119 120 wantBuf := new(bytes.Buffer) 121 data, err := defaultOutputData() 122 if err != nil { 123 return "", err 124 } 125 if err := wantTmpl.Execute(wantBuf, data); err != nil { 126 return "", err 127 } 128 want := wantBuf.String() 129 if got != want { 130 return diff(got, want) 131 } 132 return "", nil 133 } 134 135 type outputData struct { 136 GOOS string 137 GOARCH string 138 GOPATH string 139 NDKARCH string 140 EXE string // .extension for executables. (ex. ".exe" for windows) 141 Xproj string 142 Xcontents string 143 Xinfo infoplistTmplData 144 } 145 146 func defaultOutputData() (outputData, error) { 147 projPbxproj := new(bytes.Buffer) 148 if err := projPbxprojTmpl.Execute(projPbxproj, projPbxprojTmplData{ 149 BitcodeEnabled: bitcodeEnabled, 150 }); err != nil { 151 return outputData{}, err 152 } 153 154 data := outputData{ 155 GOOS: goos, 156 GOARCH: goarch, 157 GOPATH: gopath, 158 NDKARCH: archNDK(), 159 Xproj: string(projPbxproj.Bytes()), 160 Xcontents: contentsJSON, 161 Xinfo: infoplistTmplData{BundleID: "org.golang.todo.basic", Name: "Basic"}, 162 } 163 if goos == "windows" { 164 data.EXE = ".exe" 165 } 166 return data, nil 167 } 168 169 var initTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile 170 rm -r -f "$GOMOBILE" 171 mkdir -p $GOMOBILE 172 WORK={{.GOPATH}}/pkg/gomobile/work 173 go install -x golang.org/x/mobile/cmd/gobind 174 cp $OPENAL_PATH/include/AL/al.h $GOMOBILE/include/AL/al.h 175 mkdir -p $GOMOBILE/include/AL 176 cp $OPENAL_PATH/include/AL/alc.h $GOMOBILE/include/AL/alc.h 177 mkdir -p $GOMOBILE/include/AL 178 mkdir -p $WORK/build/armeabi 179 PWD=$WORK/build/armeabi cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=armv7a-linux-androideabi16 180 PWD=$WORK/build/armeabi $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make 181 cp $WORK/build/armeabi/libopenal.so $GOMOBILE/lib/armeabi-v7a/libopenal.so 182 mkdir -p $GOMOBILE/lib/armeabi-v7a 183 mkdir -p $WORK/build/arm64 184 PWD=$WORK/build/arm64 cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=aarch64-linux-android21 185 PWD=$WORK/build/arm64 $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make 186 cp $WORK/build/arm64/libopenal.so $GOMOBILE/lib/arm64-v8a/libopenal.so 187 mkdir -p $GOMOBILE/lib/arm64-v8a 188 mkdir -p $WORK/build/x86 189 PWD=$WORK/build/x86 cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=i686-linux-android16 190 PWD=$WORK/build/x86 $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make 191 cp $WORK/build/x86/libopenal.so $GOMOBILE/lib/x86/libopenal.so 192 mkdir -p $GOMOBILE/lib/x86 193 mkdir -p $WORK/build/x86_64 194 PWD=$WORK/build/x86_64 cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=x86_64-linux-android21 195 PWD=$WORK/build/x86_64 $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make 196 cp $WORK/build/x86_64/libopenal.so $GOMOBILE/lib/x86_64/libopenal.so 197 mkdir -p $GOMOBILE/lib/x86_64 198 rm -r -f "$WORK" 199 `))