Commit 77c3ef6f authored by Daniel Martí's avatar Daniel Martí

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: 's avatarIan Lance Taylor <iant@golang.org>
parent 041c5d83
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package main package main
import ( import (
"go/build"
"log" "log"
"os" "os"
"path" "path"
...@@ -25,7 +24,7 @@ type Dirs struct { ...@@ -25,7 +24,7 @@ type Dirs struct {
var dirs Dirs var dirs Dirs
func init() { func dirsInit() {
dirs.paths = make([]string, 0, 1000) dirs.paths = make([]string, 0, 1000)
dirs.scan = make(chan string) dirs.scan = make(chan string)
go dirs.walk() go dirs.walk()
...@@ -55,7 +54,7 @@ func (d *Dirs) Next() (string, bool) { ...@@ -55,7 +54,7 @@ func (d *Dirs) Next() (string, bool) {
// walk walks the trees in GOROOT and GOPATH. // walk walks the trees in GOROOT and GOPATH.
func (d *Dirs) walk() { func (d *Dirs) walk() {
d.bfsWalkRoot(build.Default.GOROOT) d.bfsWalkRoot(buildCtx.GOROOT)
for _, root := range splitGopath() { for _, root := range splitGopath() {
d.bfsWalkRoot(root) d.bfsWalkRoot(root)
} }
......
...@@ -7,7 +7,6 @@ package main ...@@ -7,7 +7,6 @@ package main
import ( import (
"bytes" "bytes"
"flag" "flag"
"go/build"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
...@@ -16,6 +15,14 @@ import ( ...@@ -16,6 +15,14 @@ import (
"testing" "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) { func maybeSkip(t *testing.T) {
if strings.HasPrefix(runtime.GOOS, "nacl") { if strings.HasPrefix(runtime.GOOS, "nacl") {
t.Skip("nacl does not have a full file tree") t.Skip("nacl does not have a full file tree")
...@@ -653,7 +660,7 @@ func TestDotSlashLookup(t *testing.T) { ...@@ -653,7 +660,7 @@ func TestDotSlashLookup(t *testing.T) {
t.Fatal(err) 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) t.Fatal(err)
} }
var b bytes.Buffer var b bytes.Buffer
......
...@@ -69,6 +69,7 @@ func usage() { ...@@ -69,6 +69,7 @@ func usage() {
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
log.SetPrefix("doc: ") log.SetPrefix("doc: ")
dirsInit()
err := do(os.Stdout, flag.CommandLine, os.Args[1:]) err := do(os.Stdout, flag.CommandLine, os.Args[1:])
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
...@@ -355,9 +356,11 @@ func findPackage(pkg string) (string, bool) { ...@@ -355,9 +356,11 @@ func findPackage(pkg string) (string, bool) {
} }
} }
var buildCtx = build.Default
// splitGopath splits $GOPATH into a list of roots. // splitGopath splits $GOPATH into a list of roots.
func splitGopath() []string { func splitGopath() []string {
return filepath.SplitList(build.Default.GOPATH) return filepath.SplitList(buildCtx.GOPATH)
} }
// pwd returns the current directory. // pwd returns the current directory.
......
...@@ -62,7 +62,7 @@ func (pkg *Package) prettyPath() string { ...@@ -62,7 +62,7 @@ func (pkg *Package) prettyPath() string {
// Also convert everything to slash-separated paths for uniform handling. // Also convert everything to slash-separated paths for uniform handling.
path = filepath.Clean(filepath.ToSlash(pkg.build.Dir)) path = filepath.Clean(filepath.ToSlash(pkg.build.Dir))
// Can we find a decent prefix? // 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 { if p, ok := trim(path, filepath.ToSlash(goroot)); ok {
return p return p
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment