package main
import (
- "go/build"
"log"
"os"
"path"
var dirs Dirs
-func init() {
+func dirsInit() {
dirs.paths = make([]string, 0, 1000)
dirs.scan = make(chan string)
go dirs.walk()
// 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)
}
import (
"bytes"
"flag"
- "go/build"
"os"
"path/filepath"
"regexp"
"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")
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
func main() {
log.SetFlags(0)
log.SetPrefix("doc: ")
+ dirsInit()
err := do(os.Stdout, flag.CommandLine, os.Args[1:])
if err != nil {
log.Fatal(err)
}
}
+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.
// 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
}