mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/doc: use empty GOPATH when running the tests
Otherwise, a populated GOPATH might result in failures such as: $ go test [...] no buildable Go source files in [...]/gopherjs/compiler/natives/src/crypto/rand exit status 1 Move the initialization of the dirs walker out of the init func, so that we can control its behavior in the tests. Updates #24464. Change-Id: I4b26a7d3d6809bdd8e9b6b0556d566e7855f80fe Reviewed-on: https://go-review.googlesource.com/101836 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
041c5d8348
commit
77c3ef6f6f
4 changed files with 16 additions and 7 deletions
|
|
@ -5,7 +5,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"go/build"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
|
@ -25,7 +24,7 @@ type Dirs struct {
|
|||
|
||||
var dirs Dirs
|
||||
|
||||
func init() {
|
||||
func dirsInit() {
|
||||
dirs.paths = make([]string, 0, 1000)
|
||||
dirs.scan = make(chan string)
|
||||
go dirs.walk()
|
||||
|
|
@ -55,7 +54,7 @@ func (d *Dirs) Next() (string, bool) {
|
|||
|
||||
// walk walks the trees in GOROOT and GOPATH.
|
||||
func (d *Dirs) walk() {
|
||||
d.bfsWalkRoot(build.Default.GOROOT)
|
||||
d.bfsWalkRoot(buildCtx.GOROOT)
|
||||
for _, root := range splitGopath() {
|
||||
d.bfsWalkRoot(root)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"go/build"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
|
@ -16,6 +15,14 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// otherwise the tests are brittle, as they may give unexpected
|
||||
// output or errors when a suffix match with GOPATH takes place
|
||||
buildCtx.GOPATH = ""
|
||||
dirsInit()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func maybeSkip(t *testing.T) {
|
||||
if strings.HasPrefix(runtime.GOOS, "nacl") {
|
||||
t.Skip("nacl does not have a full file tree")
|
||||
|
|
@ -653,7 +660,7 @@ func TestDotSlashLookup(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
if err := os.Chdir(filepath.Join(build.Default.GOROOT, "src", "text")); err != nil {
|
||||
if err := os.Chdir(filepath.Join(buildCtx.GOROOT, "src", "text")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var b bytes.Buffer
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ func usage() {
|
|||
func main() {
|
||||
log.SetFlags(0)
|
||||
log.SetPrefix("doc: ")
|
||||
dirsInit()
|
||||
err := do(os.Stdout, flag.CommandLine, os.Args[1:])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
@ -355,9 +356,11 @@ func findPackage(pkg string) (string, bool) {
|
|||
}
|
||||
}
|
||||
|
||||
var buildCtx = build.Default
|
||||
|
||||
// splitGopath splits $GOPATH into a list of roots.
|
||||
func splitGopath() []string {
|
||||
return filepath.SplitList(build.Default.GOPATH)
|
||||
return filepath.SplitList(buildCtx.GOPATH)
|
||||
}
|
||||
|
||||
// pwd returns the current directory.
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ func (pkg *Package) prettyPath() string {
|
|||
// Also convert everything to slash-separated paths for uniform handling.
|
||||
path = filepath.Clean(filepath.ToSlash(pkg.build.Dir))
|
||||
// Can we find a decent prefix?
|
||||
goroot := filepath.Join(build.Default.GOROOT, "src")
|
||||
goroot := filepath.Join(buildCtx.GOROOT, "src")
|
||||
if p, ok := trim(path, filepath.ToSlash(goroot)); ok {
|
||||
return p
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue