Skip to content

Commit 5627a4d

Browse files
committed
runtime/metrics: simplify test to support more environments
go test sets the working directory to that of the package being tested, so opening one of the package source files can be done in a simpler way. This also allows the test to run in more environments, for example when GOROOT_FINAL¹ is set. Also remove the testenv.HasSrc-like check for Go source. The doc.go file is a part of the package being built and tested, so it's expected to be available. If it's important for this test to handle when a test binary is built with go test -c and executed elsewhere without package source files, something more than testenv.HasSrc would be needed. ¹ https://golang.org/cmd/go/#hdr-Environment_variables Fixes #43085. Change-Id: Ie6ade395a8fc7beebdadbad6f4873800138dfc26 Reviewed-on: https://go-review.googlesource.com/c/go/+/276452 Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Trust: Dmitri Shuralyov <[email protected]>
1 parent db6032d commit 5627a4d

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

src/runtime/metrics/description_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ package metrics_test
77
import (
88
"bufio"
99
"os"
10-
"path/filepath"
1110
"regexp"
12-
"runtime"
1311
"runtime/metrics"
1412
"strings"
1513
"testing"
@@ -26,17 +24,9 @@ func TestDescriptionNameFormat(t *testing.T) {
2624
}
2725

2826
func extractMetricDocs(t *testing.T) map[string]string {
29-
if runtime.GOOS == "android" {
30-
t.Skip("no access to Go source on android")
31-
}
32-
33-
// Get doc.go.
34-
_, filename, _, _ := runtime.Caller(0)
35-
filename = filepath.Join(filepath.Dir(filename), "doc.go")
36-
37-
f, err := os.Open(filename)
27+
f, err := os.Open("doc.go")
3828
if err != nil {
39-
t.Fatal(err)
29+
t.Fatalf("failed to open doc.go in runtime/metrics package: %v", err)
4030
}
4131
const (
4232
stateSearch = iota // look for list of metrics
@@ -90,7 +80,7 @@ func extractMetricDocs(t *testing.T) map[string]string {
9080
}
9181
}
9282
if state == stateSearch {
93-
t.Fatalf("failed to find supported metrics docs in %s", filename)
83+
t.Fatalf("failed to find supported metrics docs in %s", f.Name())
9484
}
9585
return result
9686
}

0 commit comments

Comments
 (0)