go/packages: make LoadFiles the zero mode
Also: - make a nil *Config equivalent to new(Config). - add a test for nil *Config. - document that Load may return an empty list of packages. Change-Id: I642133abe18553ca8c7f46b7bd2709a03eda0b28 Reviewed-on: https://go-review.googlesource.com/128875 Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
2fda359797
commit
2e5c1e6f75
|
@ -30,11 +30,9 @@ import (
|
||||||
type LoadMode int
|
type LoadMode int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_ LoadMode = iota
|
|
||||||
|
|
||||||
// LoadFiles finds the packages and computes their source file lists.
|
// LoadFiles finds the packages and computes their source file lists.
|
||||||
// Package fields: ID, Name, Errors, GoFiles, OtherFiles.
|
// Package fields: ID, Name, Errors, GoFiles, OtherFiles.
|
||||||
LoadFiles
|
LoadFiles LoadMode = iota
|
||||||
|
|
||||||
// LoadImports adds import information for each package
|
// LoadImports adds import information for each package
|
||||||
// and its dependencies.
|
// and its dependencies.
|
||||||
|
@ -144,6 +142,14 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load and returns the Go packages named by the given patterns.
|
// Load and returns the Go packages named by the given patterns.
|
||||||
|
//
|
||||||
|
// Config specifies loading options;
|
||||||
|
// nil behaves the same as an empty Config.
|
||||||
|
//
|
||||||
|
// Load returns an error if any of the patterns was invalid
|
||||||
|
// as defined by the underlying build system.
|
||||||
|
// It may return an empty list of packages without an error,
|
||||||
|
// for instance for an empty expansion of a valid wildcard.
|
||||||
func Load(cfg *Config, patterns ...string) ([]*Package, error) {
|
func Load(cfg *Config, patterns ...string) ([]*Package, error) {
|
||||||
l := newLoader(cfg)
|
l := newLoader(cfg)
|
||||||
rawCfg := newRawConfig(&l.Config)
|
rawCfg := newRawConfig(&l.Config)
|
||||||
|
@ -247,8 +253,6 @@ func newLoader(cfg *Config) *loader {
|
||||||
ld := &loader{}
|
ld := &loader{}
|
||||||
if cfg != nil {
|
if cfg != nil {
|
||||||
ld.Config = *cfg
|
ld.Config = *cfg
|
||||||
} else {
|
|
||||||
ld.Config.Mode = LoadAllSyntax
|
|
||||||
}
|
}
|
||||||
if ld.Context == nil {
|
if ld.Context == nil {
|
||||||
ld.Context = context.Background()
|
ld.Context = context.Background()
|
||||||
|
|
|
@ -45,6 +45,25 @@ var usesOldGolist = false
|
||||||
// - import cycles are gracefully handled in type checker.
|
// - import cycles are gracefully handled in type checker.
|
||||||
// - test typechecking of generated test main and cgo.
|
// - test typechecking of generated test main and cgo.
|
||||||
|
|
||||||
|
// The zero-value of Config has LoadFiles mode.
|
||||||
|
func TestLoadZeroConfig(t *testing.T) {
|
||||||
|
initial, err := packages.Load(nil, "hash")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(initial) != 1 {
|
||||||
|
t.Fatalf("got %s, want [hash]", initial)
|
||||||
|
}
|
||||||
|
hash := initial[0]
|
||||||
|
// Even though the hash package has imports,
|
||||||
|
// they are not reported.
|
||||||
|
got := fmt.Sprintf("name=%s srcs=%v imports=%v", hash.Name, srcs(hash), hash.Imports)
|
||||||
|
want := "name=hash srcs=[hash.go] imports=map[]"
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("got %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadImportsGraph(t *testing.T) {
|
func TestLoadImportsGraph(t *testing.T) {
|
||||||
tmp, cleanup := makeTree(t, map[string]string{
|
tmp, cleanup := makeTree(t, map[string]string{
|
||||||
"src/a/a.go": `package a; const A = 1`,
|
"src/a/a.go": `package a; const A = 1`,
|
||||||
|
|
Loading…
Reference in New Issue