misc/cgo/testcshared: don't rely on an erroneous install target in tests

Non-main packages in module mode should not be installed to
GOPATH/pkg, but due to #37015 they were installed there anyway.

This change switches the 'go install' command in createHeaders to
instead use 'go build' (with an extension determined by the install
target for 'runtime/cgo', which is well-defined at least for the
moment), and switches TestCachedInstall (which appears to be
explicitly testing 'go install') to explicitly request GOPATH mode
(which provides a well-defined install target for the library).

This change follows a similar structure to CL 416954.

For #37015.

Change-Id: I22ae4af0f0d4c50adc9e0f0dc279859d1f258cc8
Reviewed-on: https://go-review.googlesource.com/c/go/+/417096
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Bryan C. Mills 2022-07-12 13:43:02 -04:00 committed by Gopher Robot
parent c006b7ac27
commit feada53661

View file

@ -151,16 +151,22 @@ func testMain(m *testing.M) int {
// The installation directory format varies depending on the platform.
output, err := exec.Command("go", "list",
"-buildmode=c-shared",
"-installsuffix", "testcshared",
"-f", "{{.Target}}",
"./libgo").CombinedOutput()
"runtime/cgo").CombinedOutput()
if err != nil {
log.Panicf("go list failed: %v\n%s", err, output)
}
target := string(bytes.TrimSpace(output))
libgoname = filepath.Base(target)
installdir = filepath.Dir(target)
libSuffix = strings.TrimPrefix(filepath.Ext(target), ".")
runtimeCgoTarget := string(bytes.TrimSpace(output))
libSuffix = strings.TrimPrefix(filepath.Ext(runtimeCgoTarget), ".")
defer func() {
if installdir != "" {
err := os.RemoveAll(installdir)
if err != nil {
log.Panic(err)
}
}
}()
return m.Run()
}
@ -284,8 +290,13 @@ func createHeaders() error {
}
// Generate a C header file for libgo itself.
args = []string{"go", "install", "-buildmode=c-shared",
"-installsuffix", "testcshared", "./libgo"}
installdir, err = os.MkdirTemp("", "testcshared")
if err != nil {
return err
}
libgoname = "libgo." + libSuffix
args = []string{"go", "build", "-buildmode=c-shared", "-o", filepath.Join(installdir, libgoname), "./libgo"}
cmd = exec.Command(args[0], args[1:]...)
out, err = cmd.CombinedOutput()
if err != nil {
@ -373,6 +384,7 @@ func createHeadersOnce(t *testing.T) {
headersErr = createHeaders()
})
if headersErr != nil {
t.Helper()
t.Fatal(headersErr)
}
}
@ -705,12 +717,15 @@ func TestCachedInstall(t *testing.T) {
copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "libgo", "libgo.go"), filepath.Join("libgo", "libgo.go"))
copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "p", "p.go"), filepath.Join("p", "p.go"))
env := append(os.Environ(), "GOPATH="+tmpdir, "GOBIN="+filepath.Join(tmpdir, "bin"))
buildcmd := []string{"go", "install", "-x", "-buildmode=c-shared", "-installsuffix", "testcshared", "./libgo"}
cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
env := append(cmd.Environ(),
"GOPATH="+tmpdir,
"GOBIN="+filepath.Join(tmpdir, "bin"),
"GO111MODULE=off", // 'go install' only works in GOPATH mode
)
cmd.Env = env
t.Log(buildcmd)
out, err := cmd.CombinedOutput()