go/loader: fix tests to work at go1.5 and tip
Change-Id: Icc80f112a7ec4b64519220c57b6ccb2612166656 Reviewed-on: https://go-review.googlesource.com/18197 Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
b14dd1a5b7
commit
2b78567943
|
@ -23,15 +23,21 @@ func TestContainingPackage(t *testing.T) {
|
||||||
goroot := runtime.GOROOT()
|
goroot := runtime.GOROOT()
|
||||||
gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
|
gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
|
||||||
|
|
||||||
for _, test := range [][2]string{
|
tests := [][2]string{
|
||||||
{goroot + "/src/fmt/print.go", "fmt"},
|
{goroot + "/src/fmt/print.go", "fmt"},
|
||||||
{goroot + "/src/encoding/json/foo.go", "encoding/json"},
|
{goroot + "/src/encoding/json/foo.go", "encoding/json"},
|
||||||
{goroot + "/src/encoding/missing/foo.go", "(not found)"},
|
{goroot + "/src/encoding/missing/foo.go", "(not found)"},
|
||||||
{gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go",
|
{gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go",
|
||||||
"golang.org/x/tools/go/buildutil"},
|
"golang.org/x/tools/go/buildutil"},
|
||||||
{gopath + "/src/vendor/golang.org/x/net/http2/hpack/hpack.go",
|
}
|
||||||
"vendor/golang.org/x/net/http2/hpack"},
|
// TODO(adonovan): simplify after Go 1.6.
|
||||||
} {
|
if buildutil.AllowVendor != 0 {
|
||||||
|
tests = append(tests, [2]string{
|
||||||
|
gopath + "/src/vendor/golang.org/x/net/http2/hpack/hpack.go",
|
||||||
|
"vendor/golang.org/x/net/http2/hpack",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
file, want := test[0], test[1]
|
file, want := test[0], test[1]
|
||||||
bp, err := buildutil.ContainingPackage(&build.Default, ".", file)
|
bp, err := buildutil.ContainingPackage(&build.Default, ".", file)
|
||||||
got := bp.ImportPath
|
got := bp.ImportPath
|
||||||
|
|
|
@ -0,0 +1,177 @@
|
||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build go1.5,!go1.6
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package loader_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/token"
|
||||||
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/loader"
|
||||||
|
)
|
||||||
|
|
||||||
|
func printProgram(prog *loader.Program) {
|
||||||
|
// Created packages are the initial packages specified by a call
|
||||||
|
// to CreateFromFilenames or CreateFromFiles.
|
||||||
|
var names []string
|
||||||
|
for _, info := range prog.Created {
|
||||||
|
names = append(names, info.Pkg.Path())
|
||||||
|
}
|
||||||
|
fmt.Printf("created: %s\n", names)
|
||||||
|
|
||||||
|
// Imported packages are the initial packages specified by a
|
||||||
|
// call to Import or ImportWithTests.
|
||||||
|
names = nil
|
||||||
|
for _, info := range prog.Imported {
|
||||||
|
if strings.Contains(info.Pkg.Path(), "internal") {
|
||||||
|
continue // skip, to reduce fragility
|
||||||
|
}
|
||||||
|
names = append(names, info.Pkg.Path())
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
fmt.Printf("imported: %s\n", names)
|
||||||
|
|
||||||
|
// InitialPackages contains the union of created and imported.
|
||||||
|
names = nil
|
||||||
|
for _, info := range prog.InitialPackages() {
|
||||||
|
names = append(names, info.Pkg.Path())
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
fmt.Printf("initial: %s\n", names)
|
||||||
|
|
||||||
|
// AllPackages contains all initial packages and their dependencies.
|
||||||
|
names = nil
|
||||||
|
for pkg := range prog.AllPackages {
|
||||||
|
names = append(names, pkg.Path())
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
fmt.Printf("all: %s\n", names)
|
||||||
|
}
|
||||||
|
|
||||||
|
func printFilenames(fset *token.FileSet, info *loader.PackageInfo) {
|
||||||
|
var names []string
|
||||||
|
for _, f := range info.Files {
|
||||||
|
names = append(names, filepath.Base(fset.File(f.Pos()).Name()))
|
||||||
|
}
|
||||||
|
fmt.Printf("%s.Files: %s\n", info.Pkg.Path(), names)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This example loads a set of packages and all of their dependencies
|
||||||
|
// from a typical command-line. FromArgs parses a command line and
|
||||||
|
// makes calls to the other methods of Config shown in the examples that
|
||||||
|
// follow.
|
||||||
|
func ExampleConfig_FromArgs() {
|
||||||
|
args := []string{"mytool", "unicode/utf8", "errors", "runtime", "--", "foo", "bar"}
|
||||||
|
const wantTests = false
|
||||||
|
|
||||||
|
var conf loader.Config
|
||||||
|
rest, err := conf.FromArgs(args[1:], wantTests)
|
||||||
|
prog, err := conf.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("rest: %s\n", rest)
|
||||||
|
printProgram(prog)
|
||||||
|
// Output:
|
||||||
|
// rest: [foo bar]
|
||||||
|
// created: []
|
||||||
|
// imported: [errors runtime unicode/utf8]
|
||||||
|
// initial: [errors runtime unicode/utf8]
|
||||||
|
// all: [errors runtime unicode/utf8]
|
||||||
|
}
|
||||||
|
|
||||||
|
// This example creates and type-checks a single package (without tests)
|
||||||
|
// from a list of filenames, and loads all of its dependencies.
|
||||||
|
func ExampleConfig_CreateFromFilenames() {
|
||||||
|
var conf loader.Config
|
||||||
|
filename := filepath.Join(runtime.GOROOT(), "src/container/heap/heap.go")
|
||||||
|
conf.CreateFromFilenames("container/heap", filename)
|
||||||
|
prog, err := conf.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
printProgram(prog)
|
||||||
|
// Output:
|
||||||
|
// created: [container/heap]
|
||||||
|
// imported: []
|
||||||
|
// initial: [container/heap]
|
||||||
|
// all: [container/heap sort]
|
||||||
|
}
|
||||||
|
|
||||||
|
// In the examples below, for stability, the chosen packages are
|
||||||
|
// relatively small, platform-independent, and low-level (and thus
|
||||||
|
// infrequently changing).
|
||||||
|
// The strconv package has internal and external tests.
|
||||||
|
|
||||||
|
const hello = `package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Hello, world.")
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
// This example creates and type-checks a package from a list of
|
||||||
|
// already-parsed files, and loads all its dependencies.
|
||||||
|
func ExampleConfig_CreateFromFiles() {
|
||||||
|
var conf loader.Config
|
||||||
|
f, err := conf.ParseFile("hello.go", hello)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
conf.CreateFromFiles("hello", f)
|
||||||
|
prog, err := conf.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
printProgram(prog)
|
||||||
|
printFilenames(prog.Fset, prog.Package("strconv"))
|
||||||
|
// Output:
|
||||||
|
// created: [hello]
|
||||||
|
// imported: []
|
||||||
|
// initial: [hello]
|
||||||
|
// all: [errors fmt hello io math os reflect runtime strconv sync sync/atomic syscall time unicode/utf8]
|
||||||
|
// strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go]
|
||||||
|
}
|
||||||
|
|
||||||
|
// This example imports three packages, including the tests for one of
|
||||||
|
// them, and loads all their dependencies.
|
||||||
|
func ExampleConfig_Import() {
|
||||||
|
// ImportWithTest("strconv") causes strconv to include
|
||||||
|
// internal_test.go, and creates an external test package,
|
||||||
|
// strconv_test.
|
||||||
|
// (Compare with the example of CreateFromFiles.)
|
||||||
|
|
||||||
|
var conf loader.Config
|
||||||
|
conf.Import("unicode/utf8")
|
||||||
|
conf.Import("errors")
|
||||||
|
conf.ImportWithTests("strconv")
|
||||||
|
prog, err := conf.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
printProgram(prog)
|
||||||
|
printFilenames(prog.Fset, prog.Package("strconv"))
|
||||||
|
printFilenames(prog.Fset, prog.Package("strconv_test"))
|
||||||
|
// Output:
|
||||||
|
// created: [strconv_test]
|
||||||
|
// imported: [errors strconv unicode/utf8]
|
||||||
|
// initial: [errors strconv strconv_test unicode/utf8]
|
||||||
|
// all: [bufio bytes errors flag fmt io log math math/rand os reflect runtime runtime/pprof runtime/trace sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8]
|
||||||
|
// strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go internal_test.go]
|
||||||
|
// strconv_test.Files: [atob_test.go atof_test.go atoi_test.go decimal_test.go example_test.go fp_test.go ftoa_test.go itoa_test.go quote_test.go strconv_test.go]
|
||||||
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// +build go1.5
|
// +build go1.6
|
||||||
// +build !windows
|
// +build !windows
|
||||||
|
|
||||||
package loader_test
|
package loader_test
|
||||||
|
@ -14,6 +14,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/tools/go/loader"
|
"golang.org/x/tools/go/loader"
|
||||||
)
|
)
|
||||||
|
@ -31,6 +32,9 @@ func printProgram(prog *loader.Program) {
|
||||||
// call to Import or ImportWithTests.
|
// call to Import or ImportWithTests.
|
||||||
names = nil
|
names = nil
|
||||||
for _, info := range prog.Imported {
|
for _, info := range prog.Imported {
|
||||||
|
if strings.Contains(info.Pkg.Path(), "internal") {
|
||||||
|
continue // skip, to reduce fragility
|
||||||
|
}
|
||||||
names = append(names, info.Pkg.Path())
|
names = append(names, info.Pkg.Path())
|
||||||
}
|
}
|
||||||
sort.Strings(names)
|
sort.Strings(names)
|
||||||
|
|
|
@ -394,6 +394,11 @@ func TestCwd(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoad_vendor(t *testing.T) {
|
func TestLoad_vendor(t *testing.T) {
|
||||||
|
if buildutil.AllowVendor == 0 {
|
||||||
|
// Vendoring requires Go 1.6.
|
||||||
|
// TODO(adonovan): delete in due course.
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
pkgs := map[string]string{
|
pkgs := map[string]string{
|
||||||
"a": `package a; import _ "x"`,
|
"a": `package a; import _ "x"`,
|
||||||
"a/vendor": ``, // mkdir a/vendor
|
"a/vendor": ``, // mkdir a/vendor
|
||||||
|
|
Loading…
Reference in New Issue