This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgoimporter_test.go
152 lines (124 loc) · 3.07 KB
/
vgoimporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package vgoimporter_test
import (
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"go/types"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"
"myitcv.io/vgoimporter"
)
const (
testPkg = "myitcv.io/vgoimporter/_example"
testPkgName = "hello"
)
func TestPrePostGoInstall(t *testing.T) {
if !t.Run("pre", basicTest) {
return
}
cmd := exec.Command("vgo", "test", "myitcv.io/vgoimporter/_example")
cmd.Dir = filepath.Join(".", "_example")
want := `# myitcv.io/vgoimporter/_example
./example.go:10:13: undefined: Test
./example.go:19:27: undefined: asdf
FAIL myitcv.io/vgoimporter/_example [build failed]
`
out, err := cmd.CombinedOutput()
if err != nil {
if _, ok := err.(*exec.ExitError); !ok {
t.Fatalf("unexpected error: %v", err)
}
}
if got := string(out); got != want {
t.Fatalf("unexpected output; got\n%q\nwanted:\n%q", got, want)
}
t.Run("post", basicTest)
}
func basicTest(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get absolute working dir: %v", err)
}
egDir := filepath.Join(wd, path.Base(testPkg))
// cmd := exec.Command("vgo", "list", "-json", ".")
// cmd.Dir = egDir
// out, err := cmd.CombinedOutput()
// if err != nil {
// t.Fatalf("failed to run %v: %v\n%s", strings.Join(cmd.Args, " "), err, out)
// }
// var p pkgInfo
// if err := json.Unmarshal(out, &p); err != nil {
// t.Fatalf("failed to json.Unmarshal: %v\n%s", err, out)
// }
fset := token.NewFileSet()
isGo := func(fi os.FileInfo) bool {
return strings.HasSuffix(fi.Name(), ".go")
}
pkgs, err := parser.ParseDir(fset, egDir, isGo, 0)
if err != nil {
fatalf("failed to parse %v in dir %v: %v", testPkg, egDir, err)
}
pkg, ok := pkgs[testPkgName]
if !ok {
fatalf("failed to find %v in %#v", testPkgName, pkgs)
}
var files []*ast.File
for _, f := range pkg.Files {
files = append(files, f)
}
ni := &constFinder{n: "Name"}
ast.Walk(ni, pkg)
if ni.i == nil {
t.Fatalf("failed to find Name constant")
}
imp, err := vgoimporter.New(&build.Default, fset, egDir)
if err != nil {
fatalf("failed to create importer: %v", err)
}
info := &types.Info{
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Types: make(map[ast.Expr]types.TypeAndValue),
}
conf := types.Config{
Importer: imp,
Error: func(err error) {},
}
_, err = conf.Check(testPkg, fset, files, info)
if err != nil {
if _, ok := err.(types.Error); !ok {
fatalf("unexpected error type checking: %v", err)
}
}
tn, ok := info.Defs[ni.i]
if !ok {
fatalf("failed to find type for %v", ni.i)
}
want := types.UntypedString
if got, ok := tn.Type().(*types.Basic); !ok || got.Kind() != want {
t.Fatalf("type of %v: got %v, wanted %v", ni.i, got, want)
}
}
type constFinder struct {
n string
i *ast.Ident
}
func (c *constFinder) Visit(n ast.Node) ast.Visitor {
switch i := n.(type) {
case *ast.Ident:
if i.Obj != nil && i.Obj.Kind == ast.Con && i.Name == c.n {
c.i = i
return nil
}
}
return c
}
func fatalf(format string, args ...interface{}) {
panic(fmt.Errorf(format, args...))
}