1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:14:40 -07:00

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:
Alan Donovan 2018-08-09 14:35:13 -04:00
parent 2fda359797
commit 2e5c1e6f75
2 changed files with 28 additions and 5 deletions

View File

@ -30,11 +30,9 @@ import (
type LoadMode int
const (
_ LoadMode = iota
// LoadFiles finds the packages and computes their source file lists.
// Package fields: ID, Name, Errors, GoFiles, OtherFiles.
LoadFiles
LoadFiles LoadMode = iota
// LoadImports adds import information for each package
// and its dependencies.
@ -144,6 +142,14 @@ type Config struct {
}
// 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) {
l := newLoader(cfg)
rawCfg := newRawConfig(&l.Config)
@ -247,8 +253,6 @@ func newLoader(cfg *Config) *loader {
ld := &loader{}
if cfg != nil {
ld.Config = *cfg
} else {
ld.Config.Mode = LoadAllSyntax
}
if ld.Context == nil {
ld.Context = context.Background()

View File

@ -45,6 +45,25 @@ var usesOldGolist = false
// - import cycles are gracefully handled in type checker.
// - 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) {
tmp, cleanup := makeTree(t, map[string]string{
"src/a/a.go": `package a; const A = 1`,