mirror of
https://github.com/golang/go
synced 2025-05-01 02:44:01 +00:00
misc/cgo/test: simplify for module mode
When we were first introducing module mode, CL 163418 moved many of the tests in misc/cgo/test into their own test binary under testdata so misc/cgo/test continued to work in both GOPATH mode and module mode. This introduce a somewhat complicated test driver into misc/cgo/test. Since the misc/cgo/test test had to invoke "go test" as a subprocess, this required care to thread any build flags down into the subprocess. The output from any failures of the sub-process was also less than ideal. Now that we don't have to worry about running these in GOPATH mode any more, this CL moves all of the tests back into misc/cgo/test and drops the test driver. There are two slight complications: - Test41761 was added after this split and has a C type "S" that's also present in misc/cgo/test itself. We rename that to keep that test working. - TestCgo in go/internal/srcimporter now fails to import misc/cgo/test because misc/cgo/test now contains imports of other "misc" module packages and the importer it sets up isn't configured to allow that. We fix this by setting up a build context that's configured for this. Preparation for #37486. Change-Id: I3c4f73540e0482bbd493823cca44b0ce7fac01f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/447355 Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
misc/cgo/test
cgo_linux_test.gocgo_test.gogcc68255.go
gcc68255
issue20266.goissue20266
issue23555.goissue23555a
issue23555b
issue24161_darwin_test.goissue24161arg
issue24161e0
issue24161e1
issue24161e2
issue24161res
issue26213
issue26430.goissue26430
issue26743.goissue26743
issue27054
issue27340.goissue27340
issue29563.goissue29563
issue30527.goissue30527
issue41761.goissue41761a
issue43639.goissue43639
issue52611.goissue52611a
issue52611b
issue8756.goissue8756
issue8828.goissue8828
issue9026.goissue9026
issue9400
asm_386.sasm_amd64x.sasm_arm.sasm_arm64.sasm_loong64.sasm_mips64x.sasm_mipsx.sasm_ppc64x.sasm_riscv64.sasm_s390x.sgccgo.gostubs.go
issue9400_linux.goissue9510.goissue9510a
issue9510b
overlaydir_test.gopkg_test.gotest26213.gotestdata
src/go/internal/srcimporter
@ -32,4 +32,5 @@ func TestSetgidStress(t *testing.T) {
|
||||
|
||||
func Test1435(t *testing.T) { test1435(t) }
|
||||
func Test6997(t *testing.T) { test6997(t) }
|
||||
func Test9400(t *testing.T) { test9400(t) }
|
||||
func TestBuildID(t *testing.T) { testBuildID(t) }
|
||||
|
@ -34,7 +34,10 @@ func Test7978(t *testing.T) { test7978(t) }
|
||||
func Test8092(t *testing.T) { test8092(t) }
|
||||
func Test8517(t *testing.T) { test8517(t) }
|
||||
func Test8694(t *testing.T) { test8694(t) }
|
||||
func Test8756(t *testing.T) { test8756(t) }
|
||||
func Test8811(t *testing.T) { test8811(t) }
|
||||
func Test9026(t *testing.T) { test9026(t) }
|
||||
func Test9510(t *testing.T) { test9510(t) }
|
||||
func Test9557(t *testing.T) { test9557(t) }
|
||||
func Test10303(t *testing.T) { test10303(t, 10) }
|
||||
func Test11925(t *testing.T) { test11925(t) }
|
||||
@ -45,6 +48,7 @@ func Test17537(t *testing.T) { test17537(t) }
|
||||
func Test18126(t *testing.T) { test18126(t) }
|
||||
func Test18720(t *testing.T) { test18720(t) }
|
||||
func Test20129(t *testing.T) { test20129(t) }
|
||||
func Test20266(t *testing.T) { test20266(t) }
|
||||
func Test20369(t *testing.T) { test20369(t) }
|
||||
func Test20910(t *testing.T) { test20910(t) }
|
||||
func Test21708(t *testing.T) { test21708(t) }
|
||||
@ -55,6 +59,7 @@ func Test23356(t *testing.T) { test23356(t) }
|
||||
func Test24206(t *testing.T) { test24206(t) }
|
||||
func Test25143(t *testing.T) { test25143(t) }
|
||||
func Test26066(t *testing.T) { test26066(t) }
|
||||
func Test26213(t *testing.T) { test26213(t) }
|
||||
func Test27660(t *testing.T) { test27660(t) }
|
||||
func Test28896(t *testing.T) { test28896(t) }
|
||||
func Test30065(t *testing.T) { test30065(t) }
|
||||
@ -84,6 +89,7 @@ func TestNamedEnum(t *testing.T) { testNamedEnum(t) }
|
||||
func TestCastToEnum(t *testing.T) { testCastToEnum(t) }
|
||||
func TestErrno(t *testing.T) { testErrno(t) }
|
||||
func TestFpVar(t *testing.T) { testFpVar(t) }
|
||||
func TestGCC68255(t *testing.T) { testGCC68255(t) }
|
||||
func TestHandle(t *testing.T) { testHandle(t) }
|
||||
func TestHelpers(t *testing.T) { testHelpers(t) }
|
||||
func TestLibgcc(t *testing.T) { testLibgcc(t) }
|
||||
|
@ -7,7 +7,7 @@ package cgotest
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cgotest/gcc68255"
|
||||
"misc/cgo/test/gcc68255"
|
||||
)
|
||||
|
||||
func testGCC68255(t *testing.T) {
|
@ -7,5 +7,7 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
import _ "cgotest/issue23555a"
|
||||
import _ "cgotest/issue23555b"
|
||||
import (
|
||||
_ "misc/cgo/test/issue23555a"
|
||||
_ "misc/cgo/test/issue23555b"
|
||||
)
|
@ -7,11 +7,11 @@ package cgotest
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cgotest/issue24161arg"
|
||||
"cgotest/issue24161e0"
|
||||
"cgotest/issue24161e1"
|
||||
"cgotest/issue24161e2"
|
||||
"cgotest/issue24161res"
|
||||
"misc/cgo/test/issue24161arg"
|
||||
"misc/cgo/test/issue24161e0"
|
||||
"misc/cgo/test/issue24161e1"
|
||||
"misc/cgo/test/issue24161e2"
|
||||
"misc/cgo/test/issue24161res"
|
||||
)
|
||||
|
||||
func Test24161Arg(t *testing.T) {
|
@ -7,4 +7,4 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
import _ "cgotest/issue26430"
|
||||
import _ "misc/cgo/test/issue26430"
|
@ -7,4 +7,4 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
import _ "cgotest/issue26743"
|
||||
import _ "misc/cgo/test/issue26743"
|
@ -7,6 +7,6 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
import "cgotest/issue27340"
|
||||
import "misc/cgo/test/issue27340"
|
||||
|
||||
var issue27340Var = issue27340.Issue27340GoFunc
|
@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
// Issue 29563: internal linker fails on duplicate weak symbols.
|
||||
@ -9,4 +10,4 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
import _ "cgotest/issue29563"
|
||||
import _ "misc/cgo/test/issue29563"
|
@ -7,7 +7,7 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
import "cgotest/issue30527"
|
||||
import "misc/cgo/test/issue30527"
|
||||
|
||||
func issue30527G() {
|
||||
issue30527.G(nil)
|
@ -5,16 +5,16 @@
|
||||
package cgotest
|
||||
|
||||
/*
|
||||
typedef struct S S;
|
||||
typedef struct S41761 S41761;
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"cgotest/issue41761a"
|
||||
"misc/cgo/test/issue41761a"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func test41761(t *testing.T) {
|
||||
var x issue41761a.T
|
||||
_ = (*C.struct_S)(x.X)
|
||||
_ = (*C.struct_S41761)(x.X)
|
||||
}
|
@ -5,10 +5,10 @@
|
||||
package issue41761a
|
||||
|
||||
/*
|
||||
typedef struct S S;
|
||||
typedef struct S41761 S41761;
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type T struct {
|
||||
X *C.S
|
||||
X *C.S41761
|
||||
}
|
@ -4,6 +4,6 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
// Issue 43639: No runtime test needed, make sure package cgotest/issue43639 compiles well.
|
||||
// Issue 43639: No runtime test needed, make sure package misc/cgo/test/issue43639 compiles well.
|
||||
|
||||
import _ "cgotest/issue43639"
|
||||
import _ "misc/cgo/test/issue43639"
|
@ -8,6 +8,6 @@
|
||||
package cgotest
|
||||
|
||||
import (
|
||||
_ "cgotest/issue52611a"
|
||||
_ "cgotest/issue52611b"
|
||||
_ "misc/cgo/test/issue52611a"
|
||||
_ "misc/cgo/test/issue52611b"
|
||||
)
|
@ -8,7 +8,7 @@ import "C"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cgotest/issue8756"
|
||||
"misc/cgo/test/issue8756"
|
||||
)
|
||||
|
||||
func test8756(t *testing.T) {
|
@ -9,7 +9,7 @@
|
||||
|
||||
package cgotest
|
||||
|
||||
import "cgotest/issue8828"
|
||||
import "misc/cgo/test/issue8828"
|
||||
|
||||
func p() {
|
||||
issue8828.Bar()
|
@ -3,7 +3,7 @@ package cgotest
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cgotest/issue9026"
|
||||
"misc/cgo/test/issue9026"
|
||||
)
|
||||
|
||||
func test9026(t *testing.T) { issue9026.Test(t) }
|
@ -19,7 +19,7 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"cgotest/issue9400"
|
||||
"misc/cgo/test/issue9400"
|
||||
)
|
||||
|
||||
func test9400(t *testing.T) {
|
@ -11,8 +11,8 @@ import (
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"cgotest/issue9510a"
|
||||
"cgotest/issue9510b"
|
||||
"misc/cgo/test/issue9510a"
|
||||
"misc/cgo/test/issue9510b"
|
||||
)
|
||||
|
||||
func test9510(t *testing.T) {
|
@ -1,78 +0,0 @@
|
||||
// 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 cgotest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
|
||||
//
|
||||
// TODO: Once we no longer need to support the misc module in GOPATH mode,
|
||||
// factor this function out into a package to reduce duplication.
|
||||
func overlayDir(dstRoot, srcRoot string) error {
|
||||
dstRoot = filepath.Clean(dstRoot)
|
||||
if err := os.MkdirAll(dstRoot, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
srcRoot, err := filepath.Abs(srcRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
|
||||
if err != nil || srcPath == srcRoot {
|
||||
return err
|
||||
}
|
||||
|
||||
suffix := strings.TrimPrefix(srcPath, srcRoot)
|
||||
for len(suffix) > 0 && suffix[0] == filepath.Separator {
|
||||
suffix = suffix[1:]
|
||||
}
|
||||
dstPath := filepath.Join(dstRoot, suffix)
|
||||
|
||||
perm := info.Mode() & os.ModePerm
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
info, err = os.Stat(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
perm = info.Mode() & os.ModePerm
|
||||
}
|
||||
|
||||
// Always copy directories (don't symlink them).
|
||||
// If we add a file in the overlay, we don't want to add it in the original.
|
||||
if info.IsDir() {
|
||||
return os.MkdirAll(dstPath, perm|0200)
|
||||
}
|
||||
|
||||
// If the OS supports symlinks, use them instead of copying bytes.
|
||||
if err := os.Symlink(srcPath, dstPath); err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Otherwise, copy the bytes.
|
||||
src, err := os.Open(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(dst, src)
|
||||
if closeErr := dst.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
// 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 cgotest
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestCrossPackageTests compiles and runs tests that depend on imports of other
|
||||
// local packages, using source code stored in the testdata directory.
|
||||
//
|
||||
// The tests in the misc directory tree do not have a valid import path in
|
||||
// GOPATH mode, so they previously used relative imports. However, relative
|
||||
// imports do not work in module mode. In order to make the test work in both
|
||||
// modes, we synthesize a GOPATH in which the module paths are equivalent, and
|
||||
// run the tests as a subprocess.
|
||||
//
|
||||
// If and when we no longer support these tests in GOPATH mode, we can remove
|
||||
// this shim and move the tests currently located in testdata back into the
|
||||
// parent directory.
|
||||
func TestCrossPackageTests(t *testing.T) {
|
||||
switch runtime.GOOS {
|
||||
case "android":
|
||||
t.Skip("Can't exec cmd/go subprocess on Android.")
|
||||
case "ios":
|
||||
switch runtime.GOARCH {
|
||||
case "arm64":
|
||||
t.Skip("Can't exec cmd/go subprocess on iOS.")
|
||||
}
|
||||
case "linux":
|
||||
if _, err := os.Stat("/etc/alpine-release"); err == nil {
|
||||
t.Skip("skipping failing test on alpine - go.dev/issue/39857")
|
||||
}
|
||||
}
|
||||
|
||||
GOPATH, err := os.MkdirTemp("", "cgotest")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(GOPATH)
|
||||
|
||||
modRoot := filepath.Join(GOPATH, "src", "cgotest")
|
||||
if err := overlayDir(modRoot, "testdata"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgotest\n"), 0666); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("go", "test")
|
||||
if testing.Verbose() {
|
||||
cmd.Args = append(cmd.Args, "-v")
|
||||
}
|
||||
if testing.Short() {
|
||||
cmd.Args = append(cmd.Args, "-short")
|
||||
}
|
||||
cmd.Dir = modRoot
|
||||
cmd.Env = append(os.Environ(), "GOPATH="+GOPATH, "PWD="+cmd.Dir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
|
||||
} else {
|
||||
t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ package cgotest
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cgotest/issue26213"
|
||||
"misc/cgo/test/issue26213"
|
||||
)
|
||||
|
||||
func test26213(t *testing.T) {
|
9
misc/cgo/test/testdata/cgo_linux_test.go
vendored
9
misc/cgo/test/testdata/cgo_linux_test.go
vendored
@ -1,9 +0,0 @@
|
||||
// Copyright 2012 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 cgotest
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test9400(t *testing.T) { test9400(t) }
|
18
misc/cgo/test/testdata/cgo_test.go
vendored
18
misc/cgo/test/testdata/cgo_test.go
vendored
@ -1,18 +0,0 @@
|
||||
// Copyright 2011 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 cgotest
|
||||
|
||||
import "testing"
|
||||
|
||||
// The actual test functions are in non-_test.go files
|
||||
// so that they can use cgo (import "C").
|
||||
// These wrappers are here for gotest to find.
|
||||
|
||||
func Test8756(t *testing.T) { test8756(t) }
|
||||
func Test9026(t *testing.T) { test9026(t) }
|
||||
func Test9510(t *testing.T) { test9510(t) }
|
||||
func Test20266(t *testing.T) { test20266(t) }
|
||||
func Test26213(t *testing.T) { test26213(t) }
|
||||
func TestGCC68255(t *testing.T) { testGCC68255(t) }
|
@ -243,8 +243,10 @@ func TestCgo(t *testing.T) {
|
||||
testenv.MustHaveGoBuild(t)
|
||||
testenv.MustHaveCGO(t)
|
||||
|
||||
importer := New(&build.Default, token.NewFileSet(), make(map[string]*types.Package))
|
||||
_, err := importer.ImportFrom("./misc/cgo/test", testenv.GOROOT(t), 0)
|
||||
buildCtx := build.Default
|
||||
buildCtx.Dir = filepath.Join(testenv.GOROOT(t), "misc")
|
||||
importer := New(&buildCtx, token.NewFileSet(), make(map[string]*types.Package))
|
||||
_, err := importer.ImportFrom("./cgo/test", buildCtx.Dir, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Import failed: %v", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user