[go] cmd/go: fix TestScript/build_trimpath_goroot when built with a mismatched GOROOT_FINAL

89 views
Skip to first unread message

Bryan Mills (Gerrit)

unread,
Apr 11, 2022, 11:13:33 PM4/11/22
to Bryan Mills, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gopher Robot, Russ Cox, Michael Matloob, Heschi Kreinick, golang-co...@googlegroups.com

Bryan Mills submitted this change.

View Change



3 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:

```
The name of the file: src/cmd/go/testdata/script/build_trimpath_goroot.txt
Insertions: 1, Deletions: 1.

@@ -46,7 +46,7 @@

# If a correct GOROOT is baked in to the 'go' command itself, 'go run' and
# 'go test' should not implicitly set GOROOT in the process environment
-# (because that could mask an unexpected production depenency on the GOROOT
+# (because that could mask an unexpected production dependency on the GOROOT
# environment variable), but 'go generate' should (because the generator may
# reasonably expect to be able to locate the GOROOT for which it is generating
# code).
```

Approvals: Russ Cox: Looks good to me, approved Bryan Mills: Run TryBots Gopher Robot: TryBots succeeded
cmd/go: fix TestScript/build_trimpath_goroot when built with a mismatched GOROOT_FINAL

Fixes #52236.
Updates #51461.

Change-Id: Ie91e0256afd45e9bbd60fd8cdc696363027ab696
Reviewed-on: https://go-review.googlesource.com/c/go/+/399156
Run-TryBot: Bryan Mills <bcm...@google.com>
Reviewed-by: Russ Cox <r...@golang.org>
TryBot-Result: Gopher Robot <go...@golang.org>
---
M src/cmd/go/go_test.go
M src/cmd/go/script_test.go
M src/cmd/go/testdata/script/README
M src/cmd/go/testdata/script/build_trimpath_goroot.txt
4 files changed, 65 insertions(+), 13 deletions(-)

diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go
index 426228a..b17c776 100644
--- a/src/cmd/go/go_test.go
+++ b/src/cmd/go/go_test.go
@@ -78,6 +78,10 @@
// (temp) directory.
var testGOROOT string

+// testGOROOT_FINAL is the GOROOT_FINAL with which the test binary is assumed to
+// have been built.
+var testGOROOT_FINAL = os.Getenv("GOROOT_FINAL")
+
var testGOCACHE string

var testGo string
diff --git a/src/cmd/go/script_test.go b/src/cmd/go/script_test.go
index 76c542f..6254cf97 100644
--- a/src/cmd/go/script_test.go
+++ b/src/cmd/go/script_test.go
@@ -175,7 +175,7 @@
"GOPROXY=" + proxyURL,
"GOPRIVATE=",
"GOROOT=" + testGOROOT,
- "GOROOT_FINAL=" + os.Getenv("GOROOT_FINAL"), // causes spurious rebuilds and breaks the "stale" built-in if not propagated
+ "GOROOT_FINAL=" + testGOROOT_FINAL, // causes spurious rebuilds and breaks the "stale" built-in if not propagated
"GOTRACEBACK=system",
"TESTGO_GOROOT=" + testGOROOT,
"GOSUMDB=" + testSumDBVerifierKey,
@@ -385,6 +385,8 @@
}
}
}
+ case "mismatched-goroot":
+ ok = testGOROOT_FINAL != "" && testGOROOT_FINAL != testGOROOT
default:
if strings.HasPrefix(cond.tag, "exec:") {
prog := cond.tag[len("exec:"):]
diff --git a/src/cmd/go/testdata/script/README b/src/cmd/go/testdata/script/README
index 17b582d..85e575d 100644
--- a/src/cmd/go/testdata/script/README
+++ b/src/cmd/go/testdata/script/README
@@ -90,6 +90,8 @@
- [exec:prog] for whether prog is available for execution (found by exec.LookPath)
- [GODEBUG:value] for whether value is one of the comma-separated entries in the GODEBUG variable
- [buildmode:value] for whether -buildmode=value is supported
+ - [trimpath] for whether the 'go' binary was built with -trimpath
+ - [mismatched-goroot] for whether the test's GOROOT_FINAL does not match the real GOROOT

A condition can be negated: [!short] means to run the rest of the line
when testing.Short() is false. Multiple conditions may be given for a single
diff --git a/src/cmd/go/testdata/script/build_trimpath_goroot.txt b/src/cmd/go/testdata/script/build_trimpath_goroot.txt
index 7b870ab..91e5107 100644
--- a/src/cmd/go/testdata/script/build_trimpath_goroot.txt
+++ b/src/cmd/go/testdata/script/build_trimpath_goroot.txt
@@ -8,23 +8,51 @@
# TODO(#51483): when runtime.GOROOT() returns the empty string,
# go/build should default to 'go env GOROOT' instead.

-env GOROOT=
env GOROOT_FINAL=

+[trimpath] env GOROOT=
[trimpath] ! go env GOROOT
[trimpath] stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
+[trimpath] env GOROOT=$TESTGO_GOROOT
+
+[short] stop
+
+# With GOROOT still set but GOROOT_FINAL unset, 'go build' and 'go test -c'
+# should cause runtime.GOROOT() to report either the correct GOROOT
+# (without -trimpath) or no GOROOT at all (with -trimpath).
+
+go build -o example.exe .
+go build -trimpath -o example-trimpath.exe .
+go test -c -o example.test.exe .
+go test -trimpath -c -o example.test-trimpath.exe .
+
+env GOROOT=
+
+exec ./example.exe
+stdout '^GOROOT '$TESTGO_GOROOT'$'
+stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
+
+! exec ./example-trimpath.exe
+stdout '^GOROOT $'
+stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\n\z'
+
+exec ./example.test.exe -test.v
+stdout '^GOROOT '$TESTGO_GOROOT'$'
+stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
+
+! exec ./example.test-trimpath.exe -test.v
+stdout '^GOROOT $'
+stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$'
+
+# If a correct GOROOT is baked in to the 'go' command itself, 'go run' and
+# 'go test' should not implicitly set GOROOT in the process environment
+# (because that could mask an unexpected production dependency on the GOROOT
+# environment variable), but 'go generate' should (because the generator may
+# reasonably expect to be able to locate the GOROOT for which it is generating
+# code).
+
[trimpath] stop
-
-
-[short] skip
-
-go run .
-stdout '^GOROOT '$TESTGO_GOROOT'$'
-stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
-
-go test -v .
-stdout '^GOROOT '$TESTGO_GOROOT'$'
-stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
+[mismatched-goroot] stop

! go run -trimpath .
stdout '^GOROOT $'

To view, visit change 399156. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie91e0256afd45e9bbd60fd8cdc696363027ab696
Gerrit-Change-Number: 399156
Gerrit-PatchSet: 5
Gerrit-Owner: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Heschi Kreinick <hes...@google.com>
Gerrit-CC: Michael Matloob <mat...@golang.org>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages