]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/testgodefs: convert test from bash to Go
authorBryan C. Mills <bcmills@google.com>
Wed, 13 Nov 2019 17:25:09 +0000 (12:25 -0500)
committerBryan C. Mills <bcmills@google.com>
Wed, 13 Nov 2019 20:53:39 +0000 (20:53 +0000)
The bash version of the test wrote intermediate files to its testdata directory.

Updates #28387
Updates #30316
Fixes #35536

Change-Id: Ib81b547d3c43e90df713a2172c8f399fefb53c68
Reviewed-on: https://go-review.googlesource.com/c/go/+/206901
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
misc/cgo/testgodefs/test.bash [deleted file]
misc/cgo/testgodefs/testgodefs_test.go [new file with mode: 0644]
src/cmd/dist/test.go

diff --git a/misc/cgo/testgodefs/test.bash b/misc/cgo/testgodefs/test.bash
deleted file mode 100755 (executable)
index e4ce2ee..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env bash
-
-# Copyright 2014 The Go Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style
-# license that can be found in the LICENSE file.
-
-# We are testing cgo -godefs, which translates Go files that use
-# import "C" into Go files with Go definitions of types defined in the
-# import "C" block.  Add more tests here.
-FILE_PREFIXES="anonunion issue8478 fieldtypedef"
-
-cd testdata
-
-RM=
-for FP in $FILE_PREFIXES
-do
-  go tool cgo -godefs -srcdir . ${FP}.go > ${FP}_defs.go
-  RM="${RM} ${FP}_defs.go"
-done
-
-go build -o testgodefs . && ./testgodefs
-EXIT=$?
-rm -rf _obj testgodefs ${RM}
-exit $EXIT
diff --git a/misc/cgo/testgodefs/testgodefs_test.go b/misc/cgo/testgodefs/testgodefs_test.go
new file mode 100644 (file)
index 0000000..c02c3ff
--- /dev/null
@@ -0,0 +1,83 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package testgodefs
+
+import (
+       "bytes"
+       "io/ioutil"
+       "os"
+       "os/exec"
+       "path/filepath"
+       "strings"
+       "testing"
+)
+
+// We are testing cgo -godefs, which translates Go files that use
+// import "C" into Go files with Go definitions of types defined in the
+// import "C" block.  Add more tests here.
+var filePrefixes = []string{
+       "anonunion",
+       "issue8478",
+       "fieldtypedef",
+}
+
+func TestGoDefs(t *testing.T) {
+       testdata, err := filepath.Abs("testdata")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       gopath, err := ioutil.TempDir("", "testgodefs-gopath")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(gopath)
+
+       dir := filepath.Join(gopath, "src", "testgodefs")
+       if err := os.MkdirAll(dir, 0755); err != nil {
+               t.Fatal(err)
+       }
+
+       for _, fp := range filePrefixes {
+               cmd := exec.Command("go", "tool", "cgo",
+                       "-godefs",
+                       "-srcdir", testdata,
+                       "-objdir", dir,
+                       fp+".go")
+               cmd.Stderr = new(bytes.Buffer)
+
+               out, err := cmd.Output()
+               if err != nil {
+                       t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
+               }
+
+               if err := ioutil.WriteFile(filepath.Join(dir, fp+"_defs.go"), out, 0644); err != nil {
+                       t.Fatal(err)
+               }
+       }
+
+       main, err := ioutil.ReadFile(filepath.Join("testdata", "main.go"))
+       if err != nil {
+               t.Fatal(err)
+       }
+       if err := ioutil.WriteFile(filepath.Join(dir, "main.go"), main, 0644); err != nil {
+               t.Fatal(err)
+       }
+
+       if err := ioutil.WriteFile(filepath.Join(dir, "go.mod"), []byte("module testgodefs\ngo 1.14\n"), 0644); err != nil {
+               t.Fatal(err)
+       }
+
+       // Use 'go run' to build and run the resulting binary in a single step,
+       // instead of invoking 'go build' and the resulting binary separately, so that
+       // this test can pass on mobile builders, which do not copy artifacts back
+       // from remote invocations.
+       cmd := exec.Command("go", "run", ".")
+       cmd.Env = append(os.Environ(), "GOPATH="+gopath)
+       cmd.Dir = dir
+       if out, err := cmd.CombinedOutput(); err != nil {
+               t.Fatalf("%s [%s]: %v\n%s", strings.Join(cmd.Args, " "), dir, err, out)
+       }
+}
index 036b9353f844cf9c2fb85445deb825be7fcba319..9488b9744c1a8abba205457e88a57ff32d1769ce 100644 (file)
@@ -673,15 +673,13 @@ func (t *tester) registerTests() {
                })
        }
 
-       if t.hasBash() && t.cgoEnabled && goos != "android" && goos != "darwin" {
-               t.registerTest("testgodefs", "../misc/cgo/testgodefs", "./test.bash")
-       }
-
        // Don't run these tests with $GO_GCFLAGS because most of them
        // assume that they can run "go install" with no -gcflags and not
        // recompile the entire standard library. If make.bash ran with
        // special -gcflags, that's not true.
        if t.cgoEnabled && gogcflags == "" {
+               t.registerHostTest("testgodefs", "../misc/cgo/testgodefs", "misc/cgo/testgodefs", ".")
+
                t.registerTest("testso", "../misc/cgo/testso", t.goTest(), t.timeout(600), ".")
                t.registerTest("testsovar", "../misc/cgo/testsovar", t.goTest(), t.timeout(600), ".")
                if t.supportedBuildmode("c-archive") {