1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:56:16 -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/token"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"code.google.com/p/go.tools/go/buildutil"
"code.google.com/p/go.tools/go/loader"
"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) {
runtime.GC()
t0 := time.Now()
@ -61,8 +36,10 @@ func TestStdlib(t *testing.T) {
alloc := memstats.Alloc
// Load, parse and type-check the program.
var conf loader.Config
for _, path := range allPackages() {
ctxt := build.Default // copy
ctxt.GOPATH = "" // disable GOPATH
conf := loader.Config{Build: &ctxt}
for _, path := range buildutil.AllPackagesList(conf.Build) {
if err := conf.ImportWithTests(path); err != nil {
t.Error(err)
}

View File

@ -14,14 +14,12 @@ package pointer
import (
"flag"
"go/build"
"go/token"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"code.google.com/p/go.tools/go/buildutil"
"code.google.com/p/go.tools/go/loader"
"code.google.com/p/go.tools/go/ssa"
"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")
// 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) {
if !*runStdlibTest {
t.Skip("skipping (slow) stdlib test (use --stdlib)")
}
// Load, parse and type-check the program.
var conf loader.Config
conf.SourceImports = true
if _, err := conf.FromArgs(allPackages(), true); err != nil {
ctxt := build.Default // copy
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)
return
}

View File

@ -10,51 +10,30 @@ package ssa_test
// Run test with GOMAXPROCS=8.
import (
"go/build"
"go/token"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"code.google.com/p/go.tools/go/buildutil"
"code.google.com/p/go.tools/go/loader"
"code.google.com/p/go.tools/go/ssa"
"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) {
// Load, parse and type-check the program.
t0 := time.Now()
var conf loader.Config
conf.SourceImports = true
if _, err := conf.FromArgs(allPackages(), true); err != nil {
// Load, parse and type-check the program.
ctxt := build.Default // copy
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)
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.
// Derived from from go/ssa/stdlib_test.go
// root must end with os.PathSeparator.
//
// TODO(adonovan): use buildutil.AllPackages when the tree thaws.
func allPackages(root string) []string {
var pkgs []string
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {