2019-11-13 10:25:09 -07:00
|
|
|
// 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"
|
|
|
|
"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",
|
2020-09-01 18:01:00 -06:00
|
|
|
"bitfields",
|
2019-11-13 10:25:09 -07:00
|
|
|
"issue8478",
|
|
|
|
"fieldtypedef",
|
2020-03-29 16:38:09 -06:00
|
|
|
"issue37479",
|
|
|
|
"issue37621",
|
2020-05-01 00:05:51 -06:00
|
|
|
"issue38649",
|
2020-06-11 13:01:52 -06:00
|
|
|
"issue39534",
|
2021-09-15 17:11:37 -06:00
|
|
|
"issue48396",
|
2019-11-13 10:25:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoDefs(t *testing.T) {
|
|
|
|
testdata, err := filepath.Abs("testdata")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
gopath, err := os.MkdirTemp("", "testgodefs-gopath")
|
2019-11-13 10:25:09 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, fp+"_defs.go"), out, 0644); err != nil {
|
2019-11-13 10:25:09 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
main, err := os.ReadFile(filepath.Join("testdata", "main.go"))
|
2019-11-13 10:25:09 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "main.go"), main, 0644); err != nil {
|
2019-11-13 10:25:09 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module testgodefs\ngo 1.14\n"), 0644); err != nil {
|
2019-11-13 10:25:09 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|