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:
Austin Clements 2022-10-21 15:18:14 -04:00
parent 7e88b866f9
commit b079640404
75 changed files with 43 additions and 208 deletions

View file

@ -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) }

View file

@ -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) }

View file

@ -7,7 +7,7 @@ package cgotest
import (
"testing"
"cgotest/gcc68255"
"misc/cgo/test/gcc68255"
)
func testGCC68255(t *testing.T) {

View file

@ -7,5 +7,7 @@
package cgotest
import _ "cgotest/issue23555a"
import _ "cgotest/issue23555b"
import (
_ "misc/cgo/test/issue23555a"
_ "misc/cgo/test/issue23555b"
)

View file

@ -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) {

View file

@ -7,4 +7,4 @@
package cgotest
import _ "cgotest/issue26430"
import _ "misc/cgo/test/issue26430"

View file

@ -7,4 +7,4 @@
package cgotest
import _ "cgotest/issue26743"
import _ "misc/cgo/test/issue26743"

View file

@ -7,6 +7,6 @@
package cgotest
import "cgotest/issue27340"
import "misc/cgo/test/issue27340"
var issue27340Var = issue27340.Issue27340GoFunc

View file

@ -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"

View file

@ -7,7 +7,7 @@
package cgotest
import "cgotest/issue30527"
import "misc/cgo/test/issue30527"
func issue30527G() {
issue30527.G(nil)

View file

@ -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)
}

View file

@ -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
}

View file

@ -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"

View file

@ -8,6 +8,6 @@
package cgotest
import (
_ "cgotest/issue52611a"
_ "cgotest/issue52611b"
_ "misc/cgo/test/issue52611a"
_ "misc/cgo/test/issue52611b"
)

View file

@ -8,7 +8,7 @@ import "C"
import (
"testing"
"cgotest/issue8756"
"misc/cgo/test/issue8756"
)
func test8756(t *testing.T) {

View file

@ -9,7 +9,7 @@
package cgotest
import "cgotest/issue8828"
import "misc/cgo/test/issue8828"
func p() {
issue8828.Bar()

View file

@ -3,7 +3,7 @@ package cgotest
import (
"testing"
"cgotest/issue9026"
"misc/cgo/test/issue9026"
)
func test9026(t *testing.T) { issue9026.Test(t) }

View file

@ -19,7 +19,7 @@ import (
"sync/atomic"
"testing"
"cgotest/issue9400"
"misc/cgo/test/issue9400"
)
func test9400(t *testing.T) {

View file

@ -11,8 +11,8 @@ import (
"runtime"
"testing"
"cgotest/issue9510a"
"cgotest/issue9510b"
"misc/cgo/test/issue9510a"
"misc/cgo/test/issue9510b"
)
func test9510(t *testing.T) {

View file

@ -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
})
}

View file

@ -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)
}
}

View file

@ -7,7 +7,7 @@ package cgotest
import (
"testing"
"cgotest/issue26213"
"misc/cgo/test/issue26213"
)
func test26213(t *testing.T) {

View file

@ -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) }

View file

@ -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) }

View file

@ -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)
}