Skip to content

Commit

Permalink
misc/cgo/testcshared: don't rely on an erroneous install target in tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Jul 13, 2022
1 parent c006b7a commit feada53
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions misc/cgo/testcshared/cshared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -373,6 +384,7 @@ func createHeadersOnce(t *testing.T) {
headersErr = createHeaders()
})
if headersErr != nil {
t.Helper()
t.Fatal(headersErr)
}
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit feada53

Please sign in to comment.