1 // Copyright 2011 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 var gcPath string // Go compiler path
25 switch runtime.GOARCH {
33 gcPath = "unknown-GOARCH-compiler"
36 gcPath = filepath.Join(build.ToolDir, gc)
39 func compile(t *testing.T, dirname, filename string) string {
40 cmd := exec.Command(gcPath, filename)
42 out, err := cmd.CombinedOutput()
45 t.Fatalf("%s %s failed: %s", gcPath, filename, err)
47 archCh, _ := build.ArchChar(runtime.GOARCH)
48 // filename should end with ".go"
49 return filepath.Join(dirname, filename[:len(filename)-2]+archCh)
52 // Use the same global imports map for all tests. The effect is
53 // as if all tested packages were imported into a single package.
54 var imports = make(map[string]*ast.Object)
56 func testPath(t *testing.T, path string) bool {
57 _, err := GcImport(imports, path)
59 t.Errorf("testPath(%s): %s", path, err)
65 const maxTime = 3 * time.Second
67 func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {
68 dirname := filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir)
69 list, err := ioutil.ReadDir(dirname)
71 t.Errorf("testDir(%s): %s", dirname, err)
73 for _, f := range list {
74 if time.Now().After(endTime) {
75 t.Log("testing time used up")
81 for _, ext := range pkgExts {
82 if strings.HasSuffix(f.Name(), ext) {
83 name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension
84 if testPath(t, filepath.Join(dir, name)) {
90 nimports += testDir(t, filepath.Join(dir, f.Name()), endTime)
96 func TestGcImport(t *testing.T) {
97 // On cross-compile builds, the path will not exist.
98 // Need to use GOHOSTOS, which is not available.
99 if _, err := os.Stat(gcPath); err != nil {
100 t.Logf("skipping test: %v", err)
104 if outFn := compile(t, "testdata", "exports.go"); outFn != "" {
105 defer os.Remove(outFn)
109 if testPath(t, "./testdata/exports") {
112 nimports += testDir(t, "", time.Now().Add(maxTime)) // installed packages
113 t.Logf("tested %d imports", nimports)
116 var importedObjectTests = []struct {
121 {"unsafe.Pointer", ast.Typ, "Pointer"},
122 {"math.Pi", ast.Con, "untyped float"},
123 {"io.Reader", ast.Typ, "interface{Read(p []byte) (n int, err error)}"},
124 {"io.ReadWriter", ast.Typ, "interface{Read(p []byte) (n int, err error); Write(p []byte) (n int, err error)}"},
125 {"math.Sin", ast.Fun, "func(x float64) (_ float64)"},
126 // TODO(gri) add more tests
129 func TestGcImportedTypes(t *testing.T) {
130 for _, test := range importedObjectTests {
131 s := strings.Split(test.name, ".")
133 t.Fatal("inconsistent test data")
138 pkg, err := GcImport(imports, importPath)
144 obj := pkg.Data.(*ast.Scope).Lookup(objName)
145 if obj.Kind != test.kind {
146 t.Errorf("%s: got kind = %q; want %q", test.name, obj.Kind, test.kind)
148 typ := typeString(underlying(obj.Type.(Type)))
150 t.Errorf("%s: got type = %q; want %q", test.name, typ, test.typ)