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