github.com/SkycoinProject/gomobile@v0.0.0-20190312151609-d3739f865fa6/cmd/gobind/gobind_test.go (about) 1 // Copyright 2016 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 "fmt" 10 "io/ioutil" 11 "log" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "testing" 17 ) 18 19 var tests = []struct { 20 name string 21 lang string 22 pkg string 23 goos string 24 // reverse is true if the test needs to generate reverse bindings using 25 // external tools such as javap. 26 reverse bool 27 }{ 28 {"ObjC-Testpkg", "objc", "golang.org/x/mobile/bind/testdata/testpkg", "", false}, 29 {"Java-Testpkg", "java", "golang.org/x/mobile/bind/testdata/testpkg", "", false}, 30 {"Go-Testpkg", "go", "golang.org/x/mobile/bind/testdata/testpkg", "", false}, 31 {"Java-Javapkg", "java", "golang.org/x/mobile/bind/testdata/testpkg/javapkg", "android", true}, 32 {"Go-Javapkg", "go", "golang.org/x/mobile/bind/testdata/testpkg/javapkg", "android", true}, 33 {"Go-Javapkg", "go,java,objc", "golang.org/x/mobile/bind/testdata/cgopkg", "android", false}, 34 } 35 36 var gobindBin string 37 38 func TestMain(m *testing.M) { 39 os.Exit(testMain(m)) 40 } 41 42 func testMain(m *testing.M) int { 43 bin, err := ioutil.TempFile("", "*.exe") 44 if err != nil { 45 log.Fatal(err) 46 } 47 bin.Close() 48 defer os.Remove(bin.Name()) 49 if runtime.GOOS != "android" { 50 if out, err := exec.Command("go", "build", "-o", bin.Name(), "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil { 51 log.Fatalf("gobind build failed: %v: %s", err, out) 52 } 53 gobindBin = bin.Name() 54 } 55 return m.Run() 56 } 57 58 func runGobind(t testing.TB, lang, pkg, goos string) error { 59 if gobindBin == "" { 60 t.Skipf("gobind is not available on %s", runtime.GOOS) 61 } 62 cmd := exec.Command(gobindBin, "-lang", lang, pkg) 63 if goos != "" { 64 cmd.Env = append(os.Environ(), "GOOS="+goos) 65 cmd.Env = append(os.Environ(), "CGO_ENABLED=1") 66 } 67 if out, err := cmd.CombinedOutput(); err != nil { 68 return fmt.Errorf("gobind -lang %s %s failed: %v: %s", lang, pkg, err, out) 69 } 70 return nil 71 } 72 73 func TestGobind(t *testing.T) { 74 _, javapErr := exec.LookPath("javap") 75 for _, test := range tests { 76 t.Run(test.name, func(t *testing.T) { 77 if test.reverse && javapErr != nil { 78 t.Skip("reverse bind test requires javap which is not available") 79 } 80 if err := runGobind(t, test.lang, test.pkg, test.goos); err != nil { 81 t.Error(err) 82 } 83 }) 84 } 85 } 86 87 func TestDocs(t *testing.T) { 88 if gobindBin == "" { 89 t.Skipf("gobind is not available on %s", runtime.GOOS) 90 } 91 // Create a fake package for doc.go 92 tmpdir, err := ioutil.TempDir("", "gobind-test-") 93 if err != nil { 94 t.Fatal(err) 95 } 96 defer os.RemoveAll(tmpdir) 97 docPkg := filepath.Join(tmpdir, "src", "doctest") 98 if err := os.MkdirAll(docPkg, 0700); err != nil { 99 t.Fatal(err) 100 } 101 const docsrc = ` 102 package doctest 103 104 // This is a comment. 105 type Struct struct{ 106 }` 107 if err := ioutil.WriteFile(filepath.Join(docPkg, "doc.go"), []byte(docsrc), 0700); err != nil { 108 t.Fatal(err) 109 } 110 111 const comment = "This is a comment." 112 for _, lang := range []string{"java", "objc"} { 113 cmd := exec.Command(gobindBin, "-lang", lang, "doctest") 114 cmd.Env = append(os.Environ(), "GOROOT="+tmpdir) 115 out, err := cmd.CombinedOutput() 116 if err != nil { 117 t.Errorf("gobind -lang %s failed: %v: %s", lang, err, out) 118 continue 119 } 120 if bytes.Index(out, []byte(comment)) == -1 { 121 t.Errorf("gobind output for language %s did not contain the comment %q", lang, comment) 122 } 123 } 124 } 125 126 func BenchmarkGobind(b *testing.B) { 127 _, javapErr := exec.LookPath("javap") 128 for _, test := range tests { 129 b.Run(test.name, func(b *testing.B) { 130 if test.reverse && javapErr != nil { 131 b.Skip("reverse bind test requires javap which is not available") 132 } 133 for i := 0; i < b.N; i++ { 134 if err := runGobind(b, test.lang, test.pkg, test.goos); err != nil { 135 b.Error(err) 136 } 137 } 138 }) 139 } 140 }