OSDN Git Service

libgo: Update to weekly.2012-01-15.
[pf3gnuchains/gcc-fork.git] / libgo / go / exp / types / gcimporter_test.go
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.
4
5 package types
6
7 import (
8         "go/ast"
9         "io/ioutil"
10         "os/exec"
11         "path/filepath"
12         "runtime"
13         "strings"
14         "testing"
15         "time"
16 )
17
18 var gcName, gcPath string // compiler name and path
19
20 func init() {
21         // determine compiler
22         switch runtime.GOARCH {
23         case "386":
24                 gcName = "8g"
25         case "amd64":
26                 gcName = "6g"
27         case "arm":
28                 gcName = "5g"
29         default:
30                 gcName = "unknown-GOARCH-compiler"
31                 gcPath = gcName
32                 return
33         }
34         gcPath, _ = exec.LookPath(gcName)
35 }
36
37 func compile(t *testing.T, dirname, filename string) {
38         cmd := exec.Command(gcPath, filename)
39         cmd.Dir = dirname
40         out, err := cmd.CombinedOutput()
41         if err != nil {
42                 t.Errorf("%s %s failed: %s", gcName, filename, err)
43                 return
44         }
45         t.Logf("%s", string(out))
46 }
47
48 // Use the same global imports map for all tests. The effect is
49 // as if all tested packages were imported into a single package.
50 var imports = make(map[string]*ast.Object)
51
52 func testPath(t *testing.T, path string) bool {
53         _, err := GcImporter(imports, path)
54         if err != nil {
55                 t.Errorf("testPath(%s): %s", path, err)
56                 return false
57         }
58         return true
59 }
60
61 const maxTime = 3 * time.Second
62
63 func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {
64         dirname := filepath.Join(pkgRoot, dir)
65         list, err := ioutil.ReadDir(dirname)
66         if err != nil {
67                 t.Errorf("testDir(%s): %s", dirname, err)
68         }
69         for _, f := range list {
70                 if time.Now().After(endTime) {
71                         t.Log("testing time used up")
72                         return
73                 }
74                 switch {
75                 case !f.IsDir():
76                         // try extensions
77                         for _, ext := range pkgExts {
78                                 if strings.HasSuffix(f.Name(), ext) {
79                                         name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension
80                                         if testPath(t, filepath.Join(dir, name)) {
81                                                 nimports++
82                                         }
83                                 }
84                         }
85                 case f.IsDir():
86                         nimports += testDir(t, filepath.Join(dir, f.Name()), endTime)
87                 }
88         }
89         return
90 }
91
92 func TestGcImport(t *testing.T) {
93         compile(t, "testdata", "exports.go")
94
95         nimports := 0
96         if testPath(t, "./testdata/exports") {
97                 nimports++
98         }
99         nimports += testDir(t, "", time.Now().Add(maxTime)) // installed packages
100         t.Logf("tested %d imports", nimports)
101 }