1 // Copyright 2013 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.
7 // This file enumerates all packages beneath $GOROOT, loads them, plus
8 // their external tests if any, runs the type checker on them, and
9 // prints some summary information.
25 "golang.org/x/tools/go/buildutil"
26 "golang.org/x/tools/go/loader"
27 "golang.org/x/tools/internal/testenv"
30 func TestStdlib(t *testing.T) {
31 if runtime.GOOS == "android" {
32 t.Skipf("incomplete std lib on %s", runtime.GOOS)
35 t.Skip("skipping in short mode; uses tons of memory (https://golang.org/issue/14113)")
37 testenv.NeedsTool(t, "go")
41 var memstats runtime.MemStats
42 runtime.ReadMemStats(&memstats)
43 alloc := memstats.Alloc
45 // Load, parse and type-check the program.
46 ctxt := build.Default // copy
47 ctxt.GOPATH = "" // disable GOPATH
48 conf := loader.Config{Build: &ctxt}
49 for _, path := range buildutil.AllPackages(conf.Build) {
50 conf.ImportWithTests(path)
53 prog, err := conf.Load()
55 t.Fatalf("Load failed: %v", err)
60 runtime.ReadMemStats(&memstats)
62 numPkgs := len(prog.AllPackages)
63 if want := 205; numPkgs < want {
64 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
67 // Dump package members.
69 for pkg := range prog.AllPackages {
70 fmt.Printf("Package %s:\n", pkg.Path())
72 qualifier := types.RelativeTo(pkg)
73 for _, name := range scope.Names() {
74 if ast.IsExported(name) {
75 fmt.Printf("\t%s\n", types.ObjectString(scope.Lookup(name), qualifier))
82 // Check that Test functions for regexp and compress/bzip2 are
83 // simultaneously present. The apparent cycle formed when augmenting
84 // these packages by their tests (together with io/ioutil's test, which is now
85 // an xtest) was the original motivation or reporting golang.org/issue/7114.
87 // compress/bzip2.TestBitReader in bzip2_test.go imports io/ioutil
88 // io/ioutil.TestTempFile in tempfile_test.go imports regexp (no longer exists)
89 // regexp.TestRE2Search in exec_test.go imports compress/bzip2
90 for _, test := range []struct{ pkg, fn string }{
91 {"regexp", "TestRE2Search"},
92 {"compress/bzip2", "TestBitReader"},
94 info := prog.Imported[test.pkg]
96 t.Errorf("failed to load package %q", test.pkg)
99 obj, _ := info.Pkg.Scope().Lookup(test.fn).(*types.Func)
101 t.Errorf("package %q has no func %q", test.pkg, test.fn)
106 // Dump some statistics.
108 // determine line count
110 prog.Fset.Iterate(func(f *token.File) bool {
111 lineCount += f.LineCount()
115 t.Log("GOMAXPROCS: ", runtime.GOMAXPROCS(0))
116 t.Log("#Source lines: ", lineCount)
117 t.Log("Load/parse/typecheck: ", t1.Sub(t0))
118 t.Log("#MB: ", int64(memstats.Alloc-alloc)/1000000)
121 func TestCgoOption(t *testing.T) {
123 t.Skip("skipping in short mode; uses tons of memory (https://golang.org/issue/14113)")
125 switch runtime.GOOS {
126 // On these systems, the net and os/user packages don't use cgo
127 // or the std library is incomplete (Android).
128 case "android", "plan9", "solaris", "windows":
129 t.Skipf("no cgo or incomplete std lib on %s", runtime.GOOS)
131 // In nocgo builds (e.g. linux-amd64-nocgo),
132 // there is no "runtime/cgo" package,
133 // so cgo-generated Go files will have a failing import.
134 if !build.Default.CgoEnabled {
137 testenv.NeedsTool(t, "go")
139 // Test that we can load cgo-using packages with
140 // CGO_ENABLED=[01], which causes go/build to select pure
141 // Go/native implementations, respectively, based on build
144 // Each entry specifies a package-level object and the generic
145 // file expected to define it when cgo is disabled.
146 // When cgo is enabled, the exact file is not specified (since
147 // it varies by platform), but must differ from the generic one.
149 // The test also loads the actual file to verify that the
150 // object is indeed defined at that location.
151 for _, test := range []struct {
152 pkg, name, genericFile string
154 {"net", "cgoLookupHost", "cgo_stub.go"},
155 {"os/user", "current", "lookup_stubs.go"},
157 ctxt := build.Default
158 for _, ctxt.CgoEnabled = range []bool{false, true} {
159 conf := loader.Config{Build: &ctxt}
160 conf.Import(test.pkg)
161 prog, err := conf.Load()
163 t.Errorf("Load failed: %v", err)
166 info := prog.Imported[test.pkg]
168 t.Errorf("package %s not found", test.pkg)
171 obj := info.Pkg.Scope().Lookup(test.name)
173 t.Errorf("no object %s.%s", test.pkg, test.name)
176 posn := prog.Fset.Position(obj.Pos())
177 t.Logf("%s: %s (CgoEnabled=%t)", posn, obj, ctxt.CgoEnabled)
179 gotFile := filepath.Base(posn.Filename)
180 filesMatch := gotFile == test.genericFile
182 if ctxt.CgoEnabled && filesMatch {
183 t.Errorf("CGO_ENABLED=1: %s found in %s, want native file",
185 } else if !ctxt.CgoEnabled && !filesMatch {
186 t.Errorf("CGO_ENABLED=0: %s found in %s, want %s",
187 obj, gotFile, test.genericFile)
190 // Load the file and check the object is declared at the right place.
191 b, err := ioutil.ReadFile(posn.Filename)
193 t.Errorf("can't read %s: %s", posn.Filename, err)
196 line := string(bytes.Split(b, []byte("\n"))[posn.Line-1])
197 ident := line[posn.Column-1:]
198 if !strings.HasPrefix(ident, test.name) {
199 t.Errorf("%s: %s not declared here (looking at %q)", posn, obj, ident)