go/loader: name unnamed packages so errors make sense
Add tests for: - this naming - missing file in created package Change-Id: I07c66b66e845c52d4685509c362b34f1f0c92648 Reviewed-on: https://go-review.googlesource.com/6310 Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
264bffc00c
commit
8913eaef75
|
@ -729,8 +729,12 @@ func (conf *Config) Load() (*Program, error) {
|
||||||
files = append(files, cp.Files...)
|
files = append(files, cp.Files...)
|
||||||
|
|
||||||
path := cp.Path
|
path := cp.Path
|
||||||
if path == "" && len(files) > 0 {
|
if path == "" {
|
||||||
path = files[0].Name.Name
|
if len(files) > 0 {
|
||||||
|
path = files[0].Name.Name
|
||||||
|
} else {
|
||||||
|
path = "(unnamed)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
createPkg(path, files, errs)
|
createPkg(path, files, errs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package loader_test
|
package loader_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"go/build"
|
"go/build"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -139,6 +140,49 @@ func TestLoad_MissingInitialPackage_AllowErrors(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateUnnamedPackage(t *testing.T) {
|
||||||
|
var conf loader.Config
|
||||||
|
conf.CreateFromFilenames("")
|
||||||
|
prog, err := conf.Load()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Load failed: %v", err)
|
||||||
|
}
|
||||||
|
if got, want := fmt.Sprint(prog.InitialPackages()), "[(unnamed)]"; got != want {
|
||||||
|
t.Errorf("InitialPackages = %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoad_MissingFileInCreatedPackage(t *testing.T) {
|
||||||
|
var conf loader.Config
|
||||||
|
conf.CreateFromFilenames("", "missing.go")
|
||||||
|
|
||||||
|
const wantErr = "couldn't load packages due to errors: (unnamed)"
|
||||||
|
|
||||||
|
prog, err := conf.Load()
|
||||||
|
if prog != nil {
|
||||||
|
t.Errorf("Load unexpectedly returned a Program")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Load succeeded unexpectedly, want %q", wantErr)
|
||||||
|
}
|
||||||
|
if err.Error() != wantErr {
|
||||||
|
t.Fatalf("Load failed with wrong error %q, want %q", err, wantErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoad_MissingFileInCreatedPackage_AllowErrors(t *testing.T) {
|
||||||
|
conf := loader.Config{AllowErrors: true}
|
||||||
|
conf.CreateFromFilenames("", "missing.go")
|
||||||
|
|
||||||
|
prog, err := conf.Load()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Load failed: %v", err)
|
||||||
|
}
|
||||||
|
if got, want := fmt.Sprint(prog.InitialPackages()), "[(unnamed)]"; got != want {
|
||||||
|
t.Fatalf("InitialPackages = %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoad_ParseError(t *testing.T) {
|
func TestLoad_ParseError(t *testing.T) {
|
||||||
var conf loader.Config
|
var conf loader.Config
|
||||||
conf.CreateFromFilenames("badpkg", "testdata/badpkgdecl.go")
|
conf.CreateFromFilenames("badpkg", "testdata/badpkgdecl.go")
|
||||||
|
@ -146,14 +190,15 @@ func TestLoad_ParseError(t *testing.T) {
|
||||||
const wantErr = "couldn't load packages due to errors: badpkg"
|
const wantErr = "couldn't load packages due to errors: badpkg"
|
||||||
|
|
||||||
prog, err := conf.Load()
|
prog, err := conf.Load()
|
||||||
if err == nil {
|
|
||||||
t.Errorf("Load succeeded unexpectedly, want %q", wantErr)
|
|
||||||
} else if err.Error() != wantErr {
|
|
||||||
t.Errorf("Load failed with wrong error %q, want %q", err, wantErr)
|
|
||||||
}
|
|
||||||
if prog != nil {
|
if prog != nil {
|
||||||
t.Errorf("Load unexpectedly returned a Program")
|
t.Errorf("Load unexpectedly returned a Program")
|
||||||
}
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Load succeeded unexpectedly, want %q", wantErr)
|
||||||
|
}
|
||||||
|
if err.Error() != wantErr {
|
||||||
|
t.Fatalf("Load failed with wrong error %q, want %q", err, wantErr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoad_ParseError_AllowErrors(t *testing.T) {
|
func TestLoad_ParseError_AllowErrors(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue