1
0
mirror of https://github.com/golang/go synced 2024-09-29 01:34:32 -06:00

misc/cgo/testcarchive: 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 TestPIE to instead
use 'go build', and switches TestInstall and TestCachedInstall
(which appear to be explicitly testing 'go install') to explicitly
request GOPATH mode (which does have a well-defined install target).

For #37015.

Change-Id: Ifb24657d2781d1e35cf40078e8e3ebf56aab9cc8
Reviewed-on: https://go-review.googlesource.com/c/go/+/416954
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
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-11 13:06:56 -04:00 committed by Gopher Robot
parent bf5898ef53
commit ad641e8521

View File

@ -205,6 +205,7 @@ func genHeader(t *testing.T, header, dir string) {
func testInstall(t *testing.T, exe, libgoa, libgoh string, buildcmd ...string) { func testInstall(t *testing.T, exe, libgoa, libgoh string, buildcmd ...string) {
t.Helper() t.Helper()
cmd := exec.Command(buildcmd[0], buildcmd[1:]...) cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
cmd.Env = append(cmd.Environ(), "GO111MODULE=off") // 'go install' only works in GOPATH mode
t.Log(buildcmd) t.Log(buildcmd)
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
t.Logf("%s", out) t.Logf("%s", out)
@ -238,7 +239,7 @@ func testInstall(t *testing.T, exe, libgoa, libgoh string, buildcmd ...string) {
binArgs := append(cmdToRun(exe), "arg1", "arg2") binArgs := append(cmdToRun(exe), "arg1", "arg2")
cmd = exec.Command(binArgs[0], binArgs[1:]...) cmd = exec.Command(binArgs[0], binArgs[1:]...)
if runtime.Compiler == "gccgo" { if runtime.Compiler == "gccgo" {
cmd.Env = append(os.Environ(), "GCCGO=1") cmd.Env = append(cmd.Environ(), "GCCGO=1")
} }
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
t.Logf("%s", out) t.Logf("%s", out)
@ -822,9 +823,15 @@ func TestPIE(t *testing.T) {
t.Skipf("skipping PIE test on %s", GOOS) t.Skipf("skipping PIE test on %s", GOOS)
} }
libgoa := "libgo.a"
if runtime.Compiler == "gccgo" {
libgoa = "liblibgo.a"
}
if !testWork { if !testWork {
defer func() { defer func() {
os.Remove("testp" + exeSuffix) os.Remove("testp" + exeSuffix)
os.Remove(libgoa)
os.RemoveAll(filepath.Join(GOPATH, "pkg")) os.RemoveAll(filepath.Join(GOPATH, "pkg"))
}() }()
} }
@ -837,18 +844,13 @@ func TestPIE(t *testing.T) {
// be running this test in a GOROOT owned by root.) // be running this test in a GOROOT owned by root.)
genHeader(t, "p.h", "./p") genHeader(t, "p.h", "./p")
cmd := exec.Command("go", "install", "-buildmode=c-archive", "./libgo") cmd := exec.Command("go", "build", "-buildmode=c-archive", "./libgo")
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
t.Logf("%s", out) t.Logf("%s", out)
t.Fatal(err) t.Fatal(err)
} }
libgoa := "libgo.a" ccArgs := append(cc, "-fPIE", "-pie", "-o", "testp"+exeSuffix, "main.c", "main_unix.c", libgoa)
if runtime.Compiler == "gccgo" {
libgoa = "liblibgo.a"
}
ccArgs := append(cc, "-fPIE", "-pie", "-o", "testp"+exeSuffix, "main.c", "main_unix.c", filepath.Join(libgodir, libgoa))
if runtime.Compiler == "gccgo" { if runtime.Compiler == "gccgo" {
ccArgs = append(ccArgs, "-lgo") ccArgs = append(ccArgs, "-lgo")
} }
@ -1035,6 +1037,7 @@ func TestCachedInstall(t *testing.T) {
buildcmd := []string{"go", "install", "-buildmode=c-archive", "./libgo"} buildcmd := []string{"go", "install", "-buildmode=c-archive", "./libgo"}
cmd := exec.Command(buildcmd[0], buildcmd[1:]...) cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
cmd.Env = append(cmd.Environ(), "GO111MODULE=off") // 'go install' only works in GOPATH mode
t.Log(buildcmd) t.Log(buildcmd)
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
t.Logf("%s", out) t.Logf("%s", out)
@ -1050,6 +1053,7 @@ func TestCachedInstall(t *testing.T) {
} }
cmd = exec.Command(buildcmd[0], buildcmd[1:]...) cmd = exec.Command(buildcmd[0], buildcmd[1:]...)
cmd.Env = append(cmd.Environ(), "GO111MODULE=off")
t.Log(buildcmd) t.Log(buildcmd)
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
t.Logf("%s", out) t.Logf("%s", out)