Skip to content

Commit

Permalink
runtime/metrics: simplify test to support more environments
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dmitshur committed Dec 9, 2020
1 parent db6032d commit 5627a4d
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/runtime/metrics/description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ package metrics_test
import (
"bufio"
"os"
"path/filepath"
"regexp"
"runtime"
"runtime/metrics"
"strings"
"testing"
Expand All @@ -26,17 +24,9 @@ func TestDescriptionNameFormat(t *testing.T) {
}

func extractMetricDocs(t *testing.T) map[string]string {
if runtime.GOOS == "android" {
t.Skip("no access to Go source on android")
}

// Get doc.go.
_, filename, _, _ := runtime.Caller(0)
filename = filepath.Join(filepath.Dir(filename), "doc.go")

f, err := os.Open(filename)
f, err := os.Open("doc.go")
if err != nil {
t.Fatal(err)
t.Fatalf("failed to open doc.go in runtime/metrics package: %v", err)
}
const (
stateSearch = iota // look for list of metrics
Expand Down Expand Up @@ -90,7 +80,7 @@ func extractMetricDocs(t *testing.T) map[string]string {
}
}
if state == stateSearch {
t.Fatalf("failed to find supported metrics docs in %s", filename)
t.Fatalf("failed to find supported metrics docs in %s", f.Name())
}
return result
}
Expand Down

0 comments on commit 5627a4d

Please sign in to comment.