1
0
mirror of https://github.com/golang/go synced 2024-11-05 17:16:10 -07:00

go.tools: eliminate three copies of the allPackages utility.

The one in godoc/analysis will have to wait.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/142860043
This commit is contained in:
Alan Donovan 2014-09-11 14:14:53 -04:00
parent 7de4da029c
commit 78aabae27e
4 changed files with 26 additions and 93 deletions

View File

@ -17,42 +17,17 @@ import (
"go/build" "go/build"
"go/token" "go/token"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
"code.google.com/p/go.tools/go/buildutil"
"code.google.com/p/go.tools/go/loader" "code.google.com/p/go.tools/go/loader"
"code.google.com/p/go.tools/go/types" "code.google.com/p/go.tools/go/types"
) )
func allPackages() []string {
var pkgs []string
root := filepath.Join(runtime.GOROOT(), "src") + string(os.PathSeparator)
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// Prune the search if we encounter any of these names:
switch filepath.Base(path) {
case "testdata", ".hg":
return filepath.SkipDir
}
if info.IsDir() {
pkg := filepath.ToSlash(strings.TrimPrefix(path, root))
switch pkg {
case "builtin", "pkg":
return filepath.SkipDir // skip these subtrees
case "":
return nil // ignore root of tree
}
pkgs = append(pkgs, pkg)
}
return nil
})
return pkgs
}
func TestStdlib(t *testing.T) { func TestStdlib(t *testing.T) {
runtime.GC() runtime.GC()
t0 := time.Now() t0 := time.Now()
@ -61,8 +36,10 @@ func TestStdlib(t *testing.T) {
alloc := memstats.Alloc alloc := memstats.Alloc
// Load, parse and type-check the program. // Load, parse and type-check the program.
var conf loader.Config ctxt := build.Default // copy
for _, path := range allPackages() { ctxt.GOPATH = "" // disable GOPATH
conf := loader.Config{Build: &ctxt}
for _, path := range buildutil.AllPackagesList(conf.Build) {
if err := conf.ImportWithTests(path); err != nil { if err := conf.ImportWithTests(path); err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -14,14 +14,12 @@ package pointer
import ( import (
"flag" "flag"
"go/build"
"go/token" "go/token"
"os"
"path/filepath"
"runtime"
"strings"
"testing" "testing"
"time" "time"
"code.google.com/p/go.tools/go/buildutil"
"code.google.com/p/go.tools/go/loader" "code.google.com/p/go.tools/go/loader"
"code.google.com/p/go.tools/go/ssa" "code.google.com/p/go.tools/go/ssa"
"code.google.com/p/go.tools/go/ssa/ssautil" "code.google.com/p/go.tools/go/ssa/ssautil"
@ -29,42 +27,19 @@ import (
var runStdlibTest = flag.Bool("stdlib", false, "Run the (slow) stdlib test") var runStdlibTest = flag.Bool("stdlib", false, "Run the (slow) stdlib test")
// TODO(adonovan): move this to go/buildutil package since we have four copies:
// go/{loader,pointer,ssa}/stdlib_test.go and godoc/analysis/analysis.go.
func allPackages() []string {
var pkgs []string
root := filepath.Join(runtime.GOROOT(), "src") + string(os.PathSeparator)
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// Prune the search if we encounter any of these names:
switch filepath.Base(path) {
case "testdata", ".hg":
return filepath.SkipDir
}
if info.IsDir() {
pkg := filepath.ToSlash(strings.TrimPrefix(path, root))
switch pkg {
case "builtin", "pkg":
return filepath.SkipDir // skip these subtrees
case "":
return nil // ignore root of tree
}
pkgs = append(pkgs, pkg)
}
return nil
})
return pkgs
}
func TestStdlib(t *testing.T) { func TestStdlib(t *testing.T) {
if !*runStdlibTest { if !*runStdlibTest {
t.Skip("skipping (slow) stdlib test (use --stdlib)") t.Skip("skipping (slow) stdlib test (use --stdlib)")
} }
// Load, parse and type-check the program. // Load, parse and type-check the program.
var conf loader.Config ctxt := build.Default // copy
conf.SourceImports = true ctxt.GOPATH = "" // disable GOPATH
if _, err := conf.FromArgs(allPackages(), true); err != nil { conf := loader.Config{
SourceImports: true,
Build: &ctxt,
}
if _, err := conf.FromArgs(buildutil.AllPackagesList(conf.Build), true); err != nil {
t.Errorf("FromArgs failed: %v", err) t.Errorf("FromArgs failed: %v", err)
return return
} }

View File

@ -10,51 +10,30 @@ package ssa_test
// Run test with GOMAXPROCS=8. // Run test with GOMAXPROCS=8.
import ( import (
"go/build"
"go/token" "go/token"
"os"
"path/filepath"
"runtime" "runtime"
"strings"
"testing" "testing"
"time" "time"
"code.google.com/p/go.tools/go/buildutil"
"code.google.com/p/go.tools/go/loader" "code.google.com/p/go.tools/go/loader"
"code.google.com/p/go.tools/go/ssa" "code.google.com/p/go.tools/go/ssa"
"code.google.com/p/go.tools/go/ssa/ssautil" "code.google.com/p/go.tools/go/ssa/ssautil"
) )
func allPackages() []string {
var pkgs []string
root := filepath.Join(runtime.GOROOT(), "src") + string(os.PathSeparator)
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// Prune the search if we encounter any of these names:
switch filepath.Base(path) {
case "testdata", ".hg":
return filepath.SkipDir
}
if info.IsDir() {
pkg := filepath.ToSlash(strings.TrimPrefix(path, root))
switch pkg {
case "builtin", "pkg":
return filepath.SkipDir // skip these subtrees
case "":
return nil // ignore root of tree
}
pkgs = append(pkgs, pkg)
}
return nil
})
return pkgs
}
func TestStdlib(t *testing.T) { func TestStdlib(t *testing.T) {
// Load, parse and type-check the program. // Load, parse and type-check the program.
t0 := time.Now() t0 := time.Now()
var conf loader.Config // Load, parse and type-check the program.
conf.SourceImports = true ctxt := build.Default // copy
if _, err := conf.FromArgs(allPackages(), true); err != nil { ctxt.GOPATH = "" // disable GOPATH
conf := loader.Config{
SourceImports: true,
Build: &ctxt,
}
if _, err := conf.FromArgs(buildutil.AllPackagesList(conf.Build), true); err != nil {
t.Errorf("FromArgs failed: %v", err) t.Errorf("FromArgs failed: %v", err)
return return
} }

View File

@ -575,6 +575,8 @@ func (a linksByStart) Len() int { return len(a) }
// specified package root directory, e.g. $GOROOT/src or $GOPATH/src. // specified package root directory, e.g. $GOROOT/src or $GOPATH/src.
// Derived from from go/ssa/stdlib_test.go // Derived from from go/ssa/stdlib_test.go
// root must end with os.PathSeparator. // root must end with os.PathSeparator.
//
// TODO(adonovan): use buildutil.AllPackages when the tree thaws.
func allPackages(root string) []string { func allPackages(root string) []string {
var pkgs []string var pkgs []string
filepath.Walk(root, func(path string, info os.FileInfo, err error) error { filepath.Walk(root, func(path string, info os.FileInfo, err error) error {