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 // TestCrossPackageTests compiles and runs tests that depend on imports of other
17 // local packages, using source code stored in the testdata directory.
19 // The tests in the misc directory tree do not have a valid import path in
20 // GOPATH mode, so they previously used relative imports. However, relative
21 // imports do not work in module mode. In order to make the test work in both
22 // modes, we synthesize a GOPATH in which the module paths are equivalent, and
23 // run the tests as a subprocess.
25 // If and when we no longer support these tests in GOPATH mode, we can remove
26 // this shim and move the tests currently located in testdata back into the
28 func TestCrossPackageTests(t
*testing
.T
) {
31 t
.Skip("Can't exec cmd/go subprocess on Android.")
33 switch runtime
.GOARCH
{
35 t
.Skip("Can't exec cmd/go subprocess on iOS.")
39 GOPATH
, err
:= os
.MkdirTemp("", "cgotest")
43 defer os
.RemoveAll(GOPATH
)
45 modRoot
:= filepath
.Join(GOPATH
, "src", "cgotest")
46 if err
:= overlayDir(modRoot
, "testdata"); err
!= nil {
49 if err
:= os
.WriteFile(filepath
.Join(modRoot
, "go.mod"), []byte("module cgotest\n"), 0666); err
!= nil {
53 cmd
:= exec
.Command("go", "test")
54 if testing
.Verbose() {
55 cmd
.Args
= append(cmd
.Args
, "-v")
58 cmd
.Args
= append(cmd
.Args
, "-short")
61 cmd
.Env
= append(os
.Environ(), "GOPATH="+GOPATH
, "PWD="+cmd
.Dir
)
62 out
, err
:= cmd
.CombinedOutput()
64 t
.Logf("%s:\n%s", strings
.Join(cmd
.Args
, " "), out
)
66 t
.Fatalf("%s: %s\n%s", strings
.Join(cmd
.Args
, " "), err
, out
)