1 // Copyright 2019 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.
16 // We are testing cgo -godefs, which translates Go files that use
17 // import "C" into Go files with Go definitions of types defined in the
18 // import "C" block. Add more tests here.
19 var filePrefixes
= []string{
31 func TestGoDefs(t
*testing
.T
) {
32 testdata
, err
:= filepath
.Abs("testdata")
37 gopath
, err
:= os
.MkdirTemp("", "testgodefs-gopath")
41 defer os
.RemoveAll(gopath
)
43 dir
:= filepath
.Join(gopath
, "src", "testgodefs")
44 if err
:= os
.MkdirAll(dir
, 0755); err
!= nil {
48 for _
, fp
:= range filePrefixes
{
49 cmd
:= exec
.Command("go", "tool", "cgo",
54 cmd
.Stderr
= new(bytes
.Buffer
)
56 out
, err
:= cmd
.Output()
58 t
.Fatalf("%s: %v\n%s", strings
.Join(cmd
.Args
, " "), err
, cmd
.Stderr
)
61 if err
:= os
.WriteFile(filepath
.Join(dir
, fp
+"_defs.go"), out
, 0644); err
!= nil {
66 main
, err
:= os
.ReadFile(filepath
.Join("testdata", "main.go"))
70 if err
:= os
.WriteFile(filepath
.Join(dir
, "main.go"), main
, 0644); err
!= nil {
74 if err
:= os
.WriteFile(filepath
.Join(dir
, "go.mod"), []byte("module testgodefs\ngo 1.14\n"), 0644); err
!= nil {
78 // Use 'go run' to build and run the resulting binary in a single step,
79 // instead of invoking 'go build' and the resulting binary separately, so that
80 // this test can pass on mobile builders, which do not copy artifacts back
81 // from remote invocations.
82 cmd
:= exec
.Command("go", "run", ".")
83 cmd
.Env
= append(os
.Environ(), "GOPATH="+gopath
)
85 if out
, err
:= cmd
.CombinedOutput(); err
!= nil {
86 t
.Fatalf("%s [%s]: %v\n%s", strings
.Join(cmd
.Args
, " "), dir
, err
, out
)