github.com/acrespo/mobile@v0.0.0-20190107162257-dc0771356504/cmd/gomobile/build_darwin_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  	"testing"
    12  	"text/template"
    13  )
    14  
    15  func TestIOSBuild(t *testing.T) {
    16  	defer func() {
    17  		xout = os.Stderr
    18  		buildN = false
    19  		buildX = false
    20  	}()
    21  	buildN = true
    22  	buildX = true
    23  	buildTarget = "ios"
    24  	gopath = filepath.SplitList(goEnv("GOPATH"))[0]
    25  	oldTags := ctx.BuildTags
    26  	ctx.BuildTags = []string{"tag1"}
    27  	defer func() {
    28  		ctx.BuildTags = oldTags
    29  	}()
    30  	tests := []struct {
    31  		pkg  string
    32  		main bool
    33  	}{
    34  		{"golang.org/x/mobile/example/basic", true},
    35  		{"golang.org/x/mobile/bind/testdata/testpkg", false},
    36  	}
    37  	for _, test := range tests {
    38  		buf := new(bytes.Buffer)
    39  		xout = buf
    40  		if test.main {
    41  			buildO = "basic.app"
    42  		} else {
    43  			buildO = ""
    44  		}
    45  		cmdBuild.flag.Parse([]string{test.pkg})
    46  		err := runBuild(cmdBuild)
    47  		if err != nil {
    48  			t.Log(buf.String())
    49  			t.Fatal(err)
    50  		}
    51  
    52  		teamID, err := detectTeamID()
    53  		if err != nil {
    54  			t.Fatalf("detecting team ID failed: %v", err)
    55  		}
    56  
    57  		data := struct {
    58  			outputData
    59  			TeamID string
    60  			Pkg    string
    61  			Main   bool
    62  		}{
    63  			outputData: defaultOutputData(),
    64  			TeamID:     teamID,
    65  			Pkg:        test.pkg,
    66  			Main:       test.main,
    67  		}
    68  
    69  		got := filepath.ToSlash(buf.String())
    70  
    71  		wantBuf := new(bytes.Buffer)
    72  
    73  		if err := iosBuildTmpl.Execute(wantBuf, data); err != nil {
    74  			t.Fatalf("computing diff failed: %v", err)
    75  		}
    76  
    77  		diff, err := diff(got, wantBuf.String())
    78  
    79  		if err != nil {
    80  			t.Fatalf("computing diff failed: %v", err)
    81  		}
    82  		if diff != "" {
    83  			t.Errorf("unexpected output:\n%s", diff)
    84  		}
    85  	}
    86  }
    87  
    88  var iosBuildTmpl = template.Must(infoplistTmpl.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
    89  WORK=$WORK{{if .Main}}
    90  mkdir -p $WORK/main.xcodeproj
    91  echo "{{.Xproj}}" > $WORK/main.xcodeproj/project.pbxproj
    92  mkdir -p $WORK/main
    93  echo "{{template "infoplist" .Xinfo}}" > $WORK/main/Info.plist
    94  mkdir -p $WORK/main/Images.xcassets/AppIcon.appiconset
    95  echo "{{.Xcontents}}" > $WORK/main/Images.xcassets/AppIcon.appiconset/Contents.json{{end}}
    96  GOARM=7 GOOS=darwin GOARCH=arm CC=clang-iphoneos CXX=clang-iphoneos CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=6.1 -arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=6.1 -arch armv7 CGO_ENABLED=1 go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/arm {{end}}{{.Pkg}}
    97  GOOS=darwin GOARCH=arm64 CC=clang-iphoneos CXX=clang-iphoneos CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=6.1 -arch arm64 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=6.1 -arch arm64 CGO_ENABLED=1 go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/arm64 {{end}}{{.Pkg}}
    98  GOOS=darwin GOARCH=386 CC=clang-iphonesimulator CXX=clang-iphonesimulator CGO_CFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=6.1 -arch i386 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=6.1 -arch i386 CGO_ENABLED=1 go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/386 {{end}}{{.Pkg}}
    99  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 build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/amd64 {{end}}{{.Pkg}}{{if .Main}}
   100  xcrun lipo -o $WORK/main/main -create $WORK/arm $WORK/arm64 $WORK/386 $WORK/amd64
   101  mkdir -p $WORK/main/assets
   102  xcrun xcodebuild -configuration Release -project $WORK/main.xcodeproj -allowProvisioningUpdates DEVELOPMENT_TEAM={{.TeamID}}
   103  mv $WORK/build/Release-iphoneos/main.app basic.app{{end}}
   104  `))