mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime/debug: do not require a GOROOT/src prefix in TestStack
When paths are trimmed, the reported file locations begin with the package import path (not GOROOT/src). Updates #51461. Change-Id: Ia6814f970aee11f3d933e75c75136d679d19e220 Reviewed-on: https://go-review.googlesource.com/c/go/+/391815 Trust: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
0fca8a8f25
commit
c6244b5909
1 changed files with 69 additions and 13 deletions
|
|
@ -5,11 +5,26 @@
|
||||||
package debug_test
|
package debug_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"internal/testenv"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
. "runtime/debug"
|
. "runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
if os.Getenv("GO_RUNTIME_DEBUG_TEST_DUMP_GOROOT") != "" {
|
||||||
|
fmt.Println(runtime.GOROOT())
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
|
||||||
type T int
|
type T int
|
||||||
|
|
||||||
func (t *T) ptrmethod() []byte {
|
func (t *T) ptrmethod() []byte {
|
||||||
|
|
@ -43,23 +58,64 @@ func TestStack(t *testing.T) {
|
||||||
if len(lines) < 6 {
|
if len(lines) < 6 {
|
||||||
t.Fatal("too few lines")
|
t.Fatal("too few lines")
|
||||||
}
|
}
|
||||||
n := 0
|
|
||||||
frame := func(line, code string) {
|
// If built with -trimpath, file locations should start with package paths.
|
||||||
check(t, lines[n], code)
|
// Otherwise, file locations should start with a GOROOT/src prefix
|
||||||
n++
|
// (for whatever value of GOROOT is baked into the binary, not the one
|
||||||
check(t, lines[n], line)
|
// that may be set in the environment).
|
||||||
n++
|
fileGoroot := ""
|
||||||
|
if envGoroot := os.Getenv("GOROOT"); envGoroot != "" {
|
||||||
|
// Since GOROOT is set explicitly in the environment, we can't be certain
|
||||||
|
// that it is the same GOROOT value baked into the binary, and we can't
|
||||||
|
// change the value in-process because runtime.GOROOT uses the value from
|
||||||
|
// initial (not current) environment. Spawn a subprocess to determine the
|
||||||
|
// real baked-in GOROOT.
|
||||||
|
t.Logf("found GOROOT %q from environment; checking embedded GOROOT value", envGoroot)
|
||||||
|
testenv.MustHaveExec(t)
|
||||||
|
exe, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
n++
|
cmd := exec.Command(exe)
|
||||||
frame("src/runtime/debug/stack.go", "runtime/debug.Stack")
|
cmd.Env = append(os.Environ(), "GOROOT=", "GO_RUNTIME_DEBUG_TEST_DUMP_GOROOT=1")
|
||||||
frame("src/runtime/debug/stack_test.go", "runtime/debug_test.(*T).ptrmethod")
|
out, err := cmd.Output()
|
||||||
frame("src/runtime/debug/stack_test.go", "runtime/debug_test.T.method")
|
if err != nil {
|
||||||
frame("src/runtime/debug/stack_test.go", "runtime/debug_test.TestStack")
|
t.Fatal(err)
|
||||||
frame("src/testing/testing.go", "")
|
}
|
||||||
|
fileGoroot = string(bytes.TrimSpace(out))
|
||||||
|
} else {
|
||||||
|
// Since GOROOT is not set in the environment, its value (if any) must come
|
||||||
|
// from the path embedded in the binary.
|
||||||
|
fileGoroot = runtime.GOROOT()
|
||||||
|
}
|
||||||
|
filePrefix := ""
|
||||||
|
if fileGoroot != "" {
|
||||||
|
filePrefix = filepath.ToSlash(fileGoroot) + "/src/"
|
||||||
}
|
}
|
||||||
|
|
||||||
func check(t *testing.T, line, has string) {
|
n := 0
|
||||||
if !strings.Contains(line, has) {
|
frame := func(file, code string) {
|
||||||
t.Errorf("expected %q in %q", has, line)
|
t.Helper()
|
||||||
|
|
||||||
|
line := lines[n]
|
||||||
|
if !strings.Contains(line, code) {
|
||||||
|
t.Errorf("expected %q in %q", code, line)
|
||||||
}
|
}
|
||||||
|
n++
|
||||||
|
|
||||||
|
line = lines[n]
|
||||||
|
|
||||||
|
wantPrefix := "\t" + filePrefix + file
|
||||||
|
if !strings.HasPrefix(line, wantPrefix) {
|
||||||
|
t.Errorf("in line %q, expected prefix %q", line, wantPrefix)
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
|
||||||
|
frame("runtime/debug/stack.go", "runtime/debug.Stack")
|
||||||
|
frame("runtime/debug/stack_test.go", "runtime/debug_test.(*T).ptrmethod")
|
||||||
|
frame("runtime/debug/stack_test.go", "runtime/debug_test.T.method")
|
||||||
|
frame("runtime/debug/stack_test.go", "runtime/debug_test.TestStack")
|
||||||
|
frame("testing/testing.go", "")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue