mirror of
https://github.com/golang/go.git
synced 2025-05-29 03:11:26 +00:00
cmd/go: fix and re-enable build_trimpath test
The test was comparing a binary built from a list of files to a test build from a named package. That should not (and did not) work. The test now compares two binaries built the same way in different directories. Also add a portion of the test for GOPATH and fix the gccgo portion of the test (verified manually). Fixes #35435 Change-Id: I2535a0011c9d97d2274e5550ae277302dbb91e6f Reviewed-on: https://go-review.googlesource.com/c/go/+/208234 Run-TryBot: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
941ac9ce70
commit
8324acadfe
119
src/cmd/go/testdata/script/build_trimpath.txt
vendored
119
src/cmd/go/testdata/script/build_trimpath.txt
vendored
@ -1,64 +1,92 @@
|
|||||||
[short] skip
|
[short] skip
|
||||||
env GO111MODULE=on
|
|
||||||
|
|
||||||
# A binary built without -trimpath should contain the current workspace
|
# Set up two identical directories that can be used as GOPATH.
|
||||||
|
env GO111MODULE=on
|
||||||
|
mkdir $WORK/a/src/paths $WORK/b/src/paths
|
||||||
|
cp paths.go $WORK/a/src/paths
|
||||||
|
cp paths.go $WORK/b/src/paths
|
||||||
|
cp go.mod $WORK/a/src/paths/
|
||||||
|
cp go.mod $WORK/b/src/paths/
|
||||||
|
|
||||||
|
|
||||||
|
# A binary built without -trimpath should contain the module root dir
|
||||||
# and GOROOT for debugging and stack traces.
|
# and GOROOT for debugging and stack traces.
|
||||||
cd a
|
cd $WORK/a/src/paths
|
||||||
go build -o $WORK/paths-a.exe paths.go
|
go build -o $WORK/paths-dbg.exe .
|
||||||
exec $WORK/paths-a.exe $WORK/paths-a.exe
|
exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
|
||||||
stdout 'binary contains GOPATH: true'
|
stdout 'binary contains module root: true'
|
||||||
stdout 'binary contains GOROOT: true'
|
stdout 'binary contains GOROOT: true'
|
||||||
|
|
||||||
# A binary built with -trimpath should not contain the current workspace
|
# A binary built with -trimpath should not contain the current workspace
|
||||||
# or GOROOT.
|
# or GOROOT.
|
||||||
go build -trimpath -o $WORK/paths-a.exe paths.go
|
go build -trimpath -o $WORK/paths-a.exe .
|
||||||
exec $WORK/paths-a.exe $WORK/paths-a.exe
|
exec $WORK/paths-a.exe $WORK/paths-a.exe
|
||||||
stdout 'binary contains GOPATH: false'
|
stdout 'binary contains module root: false'
|
||||||
stdout 'binary contains GOROOT: false'
|
stdout 'binary contains GOROOT: false'
|
||||||
|
|
||||||
# A binary from an external module built with -trimpath should not contain
|
# A binary from an external module built with -trimpath should not contain
|
||||||
# the current workspace or GOROOT.
|
# the current workspace or GOROOT.
|
||||||
cd $WORK
|
|
||||||
go get -trimpath rsc.io/fortune
|
go get -trimpath rsc.io/fortune
|
||||||
exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
|
exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
|
||||||
stdout 'binary contains GOPATH: false'
|
stdout 'binary contains module root: false'
|
||||||
stdout 'binary contains GOROOT: false'
|
stdout 'binary contains GOROOT: false'
|
||||||
|
go mod edit -droprequire rsc.io/fortune
|
||||||
|
|
||||||
# Two binaries built from identical packages in different directories
|
# Two binaries built from identical packages in different directories
|
||||||
# should be identical.
|
# should be identical.
|
||||||
# TODO(golang.org/issue/35435): at the moment, they are not.
|
cd $WORK/b/src/paths
|
||||||
#mkdir $GOPATH/src/b
|
go build -trimpath -o $WORK/paths-b.exe
|
||||||
#cp $GOPATH/src/a/go.mod $GOPATH/src/b/go.mod
|
cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
|
||||||
#cp $GOPATH/src/a/paths.go $GOPATH/src/b/paths.go
|
|
||||||
#cd $GOPATH/src/b
|
|
||||||
#go build -trimpath -o $WORK/paths-b.exe .
|
|
||||||
#cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
|
|
||||||
|
|
||||||
|
|
||||||
|
# Same sequence of tests but in GOPATH mode.
|
||||||
|
# A binary built without -trimpath should contain GOPATH and GOROOT.
|
||||||
|
env GO111MODULE=off
|
||||||
|
cd $WORK
|
||||||
|
env GOPATH=$WORK/a
|
||||||
|
go build -o paths-dbg.exe paths
|
||||||
|
exec ./paths-dbg.exe paths-dbg.exe
|
||||||
|
stdout 'binary contains GOPATH: true'
|
||||||
|
stdout 'binary contains GOROOT: true'
|
||||||
|
|
||||||
|
# A binary built with -trimpath should not contain GOPATH or GOROOT.
|
||||||
|
go build -trimpath -o paths-a.exe paths
|
||||||
|
exec ./paths-a.exe paths-a.exe
|
||||||
|
stdout 'binary contains GOPATH: false'
|
||||||
|
stdout 'binary contains GOROOT: false'
|
||||||
|
|
||||||
|
# Two binaries built from identical packages in different GOPATH roots
|
||||||
|
# should be identical.
|
||||||
|
env GOPATH=$WORK/b
|
||||||
|
go build -trimpath -o paths-b.exe paths
|
||||||
|
cmp -q paths-a.exe paths-b.exe
|
||||||
|
|
||||||
|
|
||||||
|
# Same sequence of tests but with gccgo.
|
||||||
|
# gccgo does not support builds in module mode.
|
||||||
[!exec:gccgo] stop
|
[!exec:gccgo] stop
|
||||||
|
env GOPATH=$WORK/a
|
||||||
|
|
||||||
# A binary built with gccgo without -trimpath should contain the current
|
# A binary built with gccgo without -trimpath should contain the current
|
||||||
# GOPATH and GOROOT.
|
# GOPATH and GOROOT.
|
||||||
env GO111MODULE=off # The current released gccgo does not support builds in module mode.
|
go build -compiler=gccgo -o paths-dbg.exe paths
|
||||||
cd $GOPATH/src/a
|
exec ./paths-dbg.exe paths-dbg.exe
|
||||||
go build -compiler=gccgo -o $WORK/gccgo-paths-a.exe .
|
|
||||||
exec $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-a.exe
|
|
||||||
stdout 'binary contains GOPATH: true'
|
stdout 'binary contains GOPATH: true'
|
||||||
stdout 'binary contains GOROOT: false' # gccgo doesn't load std from GOROOT.
|
stdout 'binary contains GOROOT: false' # gccgo doesn't load std from GOROOT.
|
||||||
|
|
||||||
# A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
|
# A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
|
||||||
go build -compiler=gccgo -trimpath -o $WORK/gccgo-paths-b.exe .
|
go build -compiler=gccgo -trimpath -o paths-a.exe paths
|
||||||
exec $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-b.exe
|
exec ./paths-a.exe paths-a.exe
|
||||||
stdout 'binary contains GOPATH: false'
|
stdout 'binary contains GOPATH: false'
|
||||||
stdout 'binary contains GOROOT: false'
|
stdout 'binary contains GOROOT: false'
|
||||||
|
|
||||||
# Two binaries built from identical packages in different directories
|
# Two binaries built from identical packages in different directories
|
||||||
# should be identical.
|
# should be identical.
|
||||||
# TODO(golang.org/issue/35435): at the moment, they are not.
|
env GOPATH=$WORK/b
|
||||||
#cd ../b
|
go build -compiler=gccgo -trimpath -o paths-b.exe paths
|
||||||
#go build -compiler=gccgo -trimpath -o $WORK/gccgo-paths-b.exe .
|
cmp -q paths-a.exe paths-b.exe
|
||||||
#cmp -q $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-b.exe
|
|
||||||
|
|
||||||
-- $GOPATH/src/a/paths.go --
|
-- paths.go --
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -67,7 +95,9 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -77,17 +107,26 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
gopath := []byte(filepath.ToSlash(os.Getenv("GOPATH")))
|
if os.Getenv("GO111MODULE") == "on" {
|
||||||
if len(gopath) == 0 {
|
out, err := exec.Command("go", "env", "GOMOD").Output()
|
||||||
log.Fatal("GOPATH not set")
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
modRoot := filepath.Dir(strings.TrimSpace(string(out)))
|
||||||
|
check(data, "module root", modRoot)
|
||||||
|
} else {
|
||||||
|
check(data, "GOPATH", os.Getenv("GOPATH"))
|
||||||
}
|
}
|
||||||
fmt.Printf("binary contains GOPATH: %v\n", bytes.Contains(data, gopath))
|
check(data, "GOROOT", os.Getenv("GOROOT"))
|
||||||
|
|
||||||
goroot := []byte(filepath.ToSlash(os.Getenv("GOROOT")))
|
|
||||||
if len(goroot) == 0 {
|
|
||||||
log.Fatal("GOROOT not set")
|
|
||||||
}
|
|
||||||
fmt.Printf("binary contains GOROOT: %v\n", bytes.Contains(data, goroot))
|
|
||||||
}
|
}
|
||||||
-- $GOPATH/src/a/go.mod --
|
|
||||||
module example.com/a
|
func check(data []byte, desc, dir string) {
|
||||||
|
containsDir := bytes.Contains(data, []byte(dir))
|
||||||
|
containsSlashDir := bytes.Contains(data, []byte(filepath.ToSlash(dir)))
|
||||||
|
fmt.Printf("binary contains %s: %v\n", desc, containsDir || containsSlashDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
-- go.mod --
|
||||||
|
module paths
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
Loading…
x
Reference in New Issue
Block a user