]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: use empty GOPATH when running the tests
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 21 Mar 2018 12:36:58 +0000 (12:36 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 21 Mar 2018 13:43:22 +0000 (13:43 +0000)
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>
src/cmd/doc/dirs.go
src/cmd/doc/doc_test.go
src/cmd/doc/main.go
src/cmd/doc/pkg.go

index a4ef8d2379d66b7631ecb5e65b05c3ec36155db6..5088f880e14222130feb7b3ec302b6c946405b2d 100644 (file)
@@ -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)
        }
index 12ed52bace240e98958c9fa1013f85cf680ff57f..c60e93743f26cd20d162dff1c00717c1eefd075c 100644 (file)
@@ -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
index a91c3b79cd8242d6c7b10ea4afe5409b7b23b6a8..9f947146a494a647fbacfe028c68bdce4f84261b 100644 (file)
@@ -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.
index d1a844ea76f92343643e93b1d1ee8fc98efb5f8a..8ff9ff57acfee90d61281b85478a369498298138 100644 (file)
@@ -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
        }