1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 const maxTime
= 2 * time
.Second
22 var importer
= New(&build
.Default
, token
.NewFileSet(), make(map[string]*types
.Package
))
24 func doImport(t
*testing
.T
, path
, srcDir
string) {
26 if _
, err
:= importer
.ImportFrom(path
, srcDir
, 0); err
!= nil {
27 // don't report an error if there's no buildable Go files
28 if _
, nogo
:= err
.(*build
.NoGoError
); !nogo
{
29 t
.Errorf("import %q failed (%v)", path
, err
)
33 t
.Logf("import %q: %v", path
, time
.Since(t0
))
36 // walkDir imports the all the packages with the given path
37 // prefix recursively. It returns the number of packages
38 // imported and whether importing was aborted because time
39 // has passed endTime.
40 func walkDir(t
*testing
.T
, path
string, endTime time
.Time
) (int, bool) {
41 if time
.Now().After(endTime
) {
42 t
.Log("testing time used up")
46 // ignore fake packages and testdata directories
47 if path
== "builtin" || path
== "unsafe" || strings
.HasSuffix(path
, "testdata") {
51 list
, err
:= ioutil
.ReadDir(filepath
.Join(runtime
.GOROOT(), "src", path
))
53 t
.Fatalf("walkDir %s failed (%v)", path
, err
)
58 for _
, f
:= range list
{
60 n
, abort
:= walkDir(t
, filepath
.Join(path
, f
.Name()), endTime
)
65 } else if strings
.HasSuffix(f
.Name(), ".go") {
75 return nimports
, false
78 func TestImportStdLib(t
*testing
.T
) {
79 if !testenv
.HasSrc() {
80 t
.Skip("no source code available")
84 if testing
.Short() && testenv
.Builder() == "" {
85 dt
= 500 * time
.Millisecond
87 nimports
, _
:= walkDir(t
, "", time
.Now().Add(dt
)) // installed packages
88 t
.Logf("tested %d imports", nimports
)
91 var importedObjectTests
= []struct {
95 {"flag.Bool", "func Bool(name string, value bool, usage string) *bool"},
96 {"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"},
97 {"io.ReadWriter", "type ReadWriter interface{Reader; Writer}"}, // go/types.gcCompatibilityMode is off => interface not flattened
98 {"math.Pi", "const Pi untyped float"},
99 {"math.Sin", "func Sin(x float64) float64"},
100 {"math/big.Int", "type Int struct{neg bool; abs nat}"},
101 {"golang_org/x/text/unicode/norm.MaxSegmentSize", "const MaxSegmentSize untyped int"},
104 func TestImportedTypes(t
*testing
.T
) {
105 if !testenv
.HasSrc() {
106 t
.Skip("no source code available")
109 for _
, test
:= range importedObjectTests
{
110 s
:= strings
.Split(test
.name
, ".")
112 t
.Fatal("invalid test data format")
117 pkg
, err
:= importer
.ImportFrom(importPath
, ".", 0)
123 obj
:= pkg
.Scope().Lookup(objName
)
125 t
.Errorf("%s: object not found", test
.name
)
129 got
:= types
.ObjectString(obj
, types
.RelativeTo(pkg
))
130 if got
!= test
.want
{
131 t
.Errorf("%s: got %q; want %q", test
.name
, got
, test
.want
)
136 func TestReimport(t
*testing
.T
) {
137 if !testenv
.HasSrc() {
138 t
.Skip("no source code available")
141 // Reimporting a partially imported (incomplete) package is not supported (see issue #19337).
142 // Make sure we recognize the situation and report an error.
144 mathPkg
:= types
.NewPackage("math", "math") // incomplete package
145 importer
:= New(&build
.Default
, token
.NewFileSet(), map[string]*types
.Package
{mathPkg
.Path(): mathPkg
})
146 _
, err
:= importer
.ImportFrom("math", ".", 0)
147 if err
== nil ||
!strings
.HasPrefix(err
.Error(), "reimport") {
148 t
.Errorf("got %v; want reimport error", err
)