Skip to content

Commit feada53

Browse files
Bryan C. Millsgopherbot
authored andcommitted
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>
1 parent c006b7a commit feada53

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

misc/cgo/testcshared/cshared_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,22 @@ func testMain(m *testing.M) int {
151151
// The installation directory format varies depending on the platform.
152152
output, err := exec.Command("go", "list",
153153
"-buildmode=c-shared",
154-
"-installsuffix", "testcshared",
155154
"-f", "{{.Target}}",
156-
"./libgo").CombinedOutput()
155+
"runtime/cgo").CombinedOutput()
157156
if err != nil {
158157
log.Panicf("go list failed: %v\n%s", err, output)
159158
}
160-
target := string(bytes.TrimSpace(output))
161-
libgoname = filepath.Base(target)
162-
installdir = filepath.Dir(target)
163-
libSuffix = strings.TrimPrefix(filepath.Ext(target), ".")
159+
runtimeCgoTarget := string(bytes.TrimSpace(output))
160+
libSuffix = strings.TrimPrefix(filepath.Ext(runtimeCgoTarget), ".")
161+
162+
defer func() {
163+
if installdir != "" {
164+
err := os.RemoveAll(installdir)
165+
if err != nil {
166+
log.Panic(err)
167+
}
168+
}
169+
}()
164170

165171
return m.Run()
166172
}
@@ -284,8 +290,13 @@ func createHeaders() error {
284290
}
285291

286292
// Generate a C header file for libgo itself.
287-
args = []string{"go", "install", "-buildmode=c-shared",
288-
"-installsuffix", "testcshared", "./libgo"}
293+
installdir, err = os.MkdirTemp("", "testcshared")
294+
if err != nil {
295+
return err
296+
}
297+
libgoname = "libgo." + libSuffix
298+
299+
args = []string{"go", "build", "-buildmode=c-shared", "-o", filepath.Join(installdir, libgoname), "./libgo"}
289300
cmd = exec.Command(args[0], args[1:]...)
290301
out, err = cmd.CombinedOutput()
291302
if err != nil {
@@ -373,6 +384,7 @@ func createHeadersOnce(t *testing.T) {
373384
headersErr = createHeaders()
374385
})
375386
if headersErr != nil {
387+
t.Helper()
376388
t.Fatal(headersErr)
377389
}
378390
}
@@ -705,12 +717,15 @@ func TestCachedInstall(t *testing.T) {
705717
copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "libgo", "libgo.go"), filepath.Join("libgo", "libgo.go"))
706718
copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "p", "p.go"), filepath.Join("p", "p.go"))
707719

708-
env := append(os.Environ(), "GOPATH="+tmpdir, "GOBIN="+filepath.Join(tmpdir, "bin"))
709-
710720
buildcmd := []string{"go", "install", "-x", "-buildmode=c-shared", "-installsuffix", "testcshared", "./libgo"}
711721

712722
cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
713723
cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
724+
env := append(cmd.Environ(),
725+
"GOPATH="+tmpdir,
726+
"GOBIN="+filepath.Join(tmpdir, "bin"),
727+
"GO111MODULE=off", // 'go install' only works in GOPATH mode
728+
)
714729
cmd.Env = env
715730
t.Log(buildcmd)
716731
out, err := cmd.CombinedOutput()

0 commit comments

Comments
 (0)