github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/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  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"strconv"
    13  	"strings"
    14  	"testing"
    15  	"text/template"
    16  )
    17  
    18  var gopath string
    19  
    20  func TestInit(t *testing.T) {
    21  	buf := new(bytes.Buffer)
    22  	gopathorig := os.Getenv("GOPATH")
    23  	defer func() {
    24  		xout = os.Stderr
    25  		buildN = false
    26  		buildX = false
    27  		initU = false
    28  		os.Setenv("GOPATH", gopathorig)
    29  	}()
    30  	xout = buf
    31  	buildN = true
    32  	buildX = true
    33  	initU = true
    34  
    35  	// Test that first GOPATH element is chosen correctly.
    36  	gopath = "/GOPATH1"
    37  	paths := []string{gopath, "/path2", "/path3"}
    38  	if goos == "windows" {
    39  		gopath = filepath.ToSlash(`C:\GOPATH1`)
    40  		paths = []string{gopath, `C:\PATH2`, `C:\PATH3`}
    41  	}
    42  	os.Setenv("GOPATH", strings.Join(paths, string(os.PathListSeparator)))
    43  	os.Setenv("GOROOT_BOOTSTRAP", "go1.4")
    44  	if goos == "windows" {
    45  		os.Setenv("HOMEDRIVE", "C:")
    46  	}
    47  
    48  	err := runInit(cmdInit)
    49  	if err != nil {
    50  		t.Log(buf.String())
    51  		t.Fatal(err)
    52  	}
    53  
    54  	diff, err := diffOutput(buf.String(), initTmpl)
    55  	if err != nil {
    56  		t.Fatalf("computing diff failed: %v", err)
    57  	}
    58  	if diff != "" {
    59  		t.Errorf("unexpected output:\n%s", diff)
    60  	}
    61  }
    62  
    63  func diffOutput(got string, wantTmpl *template.Template) (string, error) {
    64  	got = filepath.ToSlash(got)
    65  
    66  	wantBuf := new(bytes.Buffer)
    67  	data := outputData{
    68  		NDK:       ndkVersion,
    69  		GOOS:      goos,
    70  		GOARCH:    goarch,
    71  		GOPATH:    gopath,
    72  		NDKARCH:   ndkarch,
    73  		Xproj:     projPbxproj,
    74  		Xcontents: contentsJSON,
    75  		Xinfo:     infoplistTmplData{BundleID: "org.golang.todo.basic", Name: "Basic"},
    76  		NumCPU:    strconv.Itoa(runtime.NumCPU()),
    77  	}
    78  	if goos == "windows" {
    79  		data.EXE = ".exe"
    80  	}
    81  	if err := wantTmpl.Execute(wantBuf, data); err != nil {
    82  		return "", err
    83  	}
    84  	want := wantBuf.String()
    85  	if got != want {
    86  		return diff(got, want)
    87  	}
    88  	return "", nil
    89  }
    90  
    91  type outputData struct {
    92  	NDK       string
    93  	GOOS      string
    94  	GOARCH    string
    95  	GOPATH    string
    96  	NDKARCH   string
    97  	EXE       string // .extension for executables. (ex. ".exe" for windows)
    98  	Xproj     string
    99  	Xcontents string
   100  	Xinfo     infoplistTmplData
   101  	NumCPU    string
   102  }
   103  
   104  var initTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
   105  mkdir -p $GOMOBILE/android-{{.NDK}}
   106  WORK={{.GOPATH}}/pkg/gomobile/work
   107  mkdir -p $GOMOBILE/dl
   108  curl -o$GOMOBILE/dl/gomobile-{{.NDK}}-{{.GOOS}}-{{.NDKARCH}}.tar.gz https://dl.google.com/go/mobile/gomobile-{{.NDK}}-{{.GOOS}}-{{.NDKARCH}}.tar.gz
   109  tar xfz $GOMOBILE/dl/gomobile-{{.NDK}}-{{.GOOS}}-{{.NDKARCH}}.tar.gz
   110  mkdir -p $GOMOBILE/android-{{.NDK}}/arm/sysroot/usr
   111  mv $WORK/android-{{.NDK}}/platforms/android-15/arch-arm/usr/include $GOMOBILE/android-{{.NDK}}/arm/sysroot/usr/include
   112  mv $WORK/android-{{.NDK}}/platforms/android-15/arch-arm/usr/lib $GOMOBILE/android-{{.NDK}}/arm/sysroot/usr/lib
   113  mv $WORK/android-{{.NDK}}/toolchains/arm-linux-androideabi-4.8/prebuilt/{{.GOOS}}-{{.NDKARCH}}/bin $GOMOBILE/android-{{.NDK}}/arm/bin
   114  mv $WORK/android-{{.NDK}}/toolchains/arm-linux-androideabi-4.8/prebuilt/{{.GOOS}}-{{.NDKARCH}}/lib $GOMOBILE/android-{{.NDK}}/arm/lib
   115  mv $WORK/android-{{.NDK}}/toolchains/arm-linux-androideabi-4.8/prebuilt/{{.GOOS}}-{{.NDKARCH}}/libexec $GOMOBILE/android-{{.NDK}}/arm/libexec
   116  mkdir -p $GOMOBILE/android-{{.NDK}}/arm/arm-linux-androideabi/bin
   117  ln -s $GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-ld{{.EXE}} $GOMOBILE/android-{{.NDK}}/arm/arm-linux-androideabi/bin/ld{{.EXE}}
   118  ln -s $GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-as{{.EXE}} $GOMOBILE/android-{{.NDK}}/arm/arm-linux-androideabi/bin/as{{.EXE}}
   119  ln -s $GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} $GOMOBILE/android-{{.NDK}}/arm/arm-linux-androideabi/bin/gcc{{.EXE}}
   120  ln -s $GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} $GOMOBILE/android-{{.NDK}}/arm/arm-linux-androideabi/bin/g++{{.EXE}}
   121  mkdir -p $GOMOBILE/dl
   122  curl -o$GOMOBILE/dl/gomobile-openal-soft-1.16.0.1.tar.gz https://dl.google.com/go/mobile/gomobile-openal-soft-1.16.0.1.tar.gz
   123  tar xfz $GOMOBILE/dl/gomobile-openal-soft-1.16.0.1.tar.gz
   124  mv $WORK/openal/include/AL $GOMOBILE/android-{{.NDK}}/arm/sysroot/usr/include/AL
   125  mkdir -p $GOMOBILE/android-{{.NDK}}/openal
   126  mv $WORK/openal/lib $GOMOBILE/android-{{.NDK}}/openal/lib{{if eq .GOOS "darwin"}}
   127  go install -p={{.NumCPU}} -x github.com/c-darwin/mobile/gl
   128  go install -p={{.NumCPU}} -x github.com/c-darwin/mobile/app
   129  go install -p={{.NumCPU}} -x github.com/c-darwin/mobile/exp/app/debug {{end}}
   130  GOOS=android GOARCH=arm GOARM=7 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 go install -p={{.NumCPU}} -pkgdir=$GOMOBILE/pkg_android_arm -x std
   131  {{if eq .GOOS "darwin"}}GOOS=darwin GOARCH=arm GOARM=7 CC=clang-iphoneos CXX=clang-iphoneos CGO_CFLAGS=-isysroot=iphoneos -arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -arch armv7 CGO_ENABLED=1 go install -p={{.NumCPU}} -pkgdir=$GOMOBILE/pkg_darwin_arm -x std
   132  GOOS=darwin GOARCH=arm64 CC=clang-iphoneos CXX=clang-iphoneos CGO_CFLAGS=-isysroot=iphoneos -arch arm64 CGO_LDFLAGS=-isysroot=iphoneos -arch arm64 CGO_ENABLED=1 go install -p={{.NumCPU}} -pkgdir=$GOMOBILE/pkg_darwin_arm64 -x std
   133  GOOS=darwin GOARCH=amd64 CC=clang-iphonesimulator CXX=clang-iphonesimulator CGO_CFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=6.1 -arch x86_64 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=6.1 -arch x86_64 CGO_ENABLED=1 go install -p={{.NumCPU}} -tags=ios -pkgdir=$GOMOBILE/pkg_darwin_amd64 -x std
   134  {{end}}go version > $GOMOBILE/version
   135  rm -r -f "$WORK"
   136  `))