-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpkg_test.go
128 lines (115 loc) · 2.71 KB
/
pkg_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
package analysisutil_test
import (
"fmt"
"go/types"
"path/filepath"
"testing"
"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/packages/packagestest"
)
func TestRemoveVendor(t *testing.T) {
tests := []struct {
path string
want string
}{
{"vendor/path/to/pkg", "path/to/pkg"},
{"path/to/pkg", "path/to/pkg"},
{"", ""},
{"a/vendor/path/to/pkg", "path/to/pkg"},
{"pkg", "pkg"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.path, func(t *testing.T) {
t.Parallel()
got := analysisutil.RemoveVendor(tt.path)
if got != tt.want {
t.Errorf("want %s but got %s", tt.want, got)
}
})
}
}
func TestLookupFromImports(t *testing.T) {
tests := []struct {
path, name string
found bool
}{
{"fmt", "Println", true},
{"b", "Msg", true},
{"strings", "Join", false},
{"b", "Func", false},
}
testdata := filepath.Join("testdata", "lookupfromimports")
pkg := loadPkg(t, testdata, "a")
for _, tt := range tests {
tt := tt
name := fmt.Sprintf("%s.%s", tt.path, tt.name)
t.Run(name, func(t *testing.T) {
obj := analysisutil.LookupFromImports(pkg.Imports(), tt.path, tt.name)
switch {
case obj == nil && tt.found:
t.Error("not found")
case obj != nil && !tt.found:
t.Error("want not found but found:", obj)
}
})
}
}
func loadPkg(t *testing.T, testdata, pkg string) *types.Package {
t.Helper()
exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
Name: pkg,
Files: packagestest.MustCopyFileTree(testdata),
}})
defer exported.Cleanup()
conf := exported.Config
conf.Mode = packages.LoadAllSyntax
pkgs, err := packages.Load(conf, pkg)
if err != nil {
t.Fatal("unexpected error:", err)
}
if len(pkgs) == 0 {
t.Fatal("cannot load package", pkg)
}
if len(pkgs[0].Errors) != 0 {
t.Fatal("unexpected error:", pkgs[0].Errors)
}
return pkgs[0].Types
}
func TestImported(t *testing.T) {
tests := []struct {
path string
used bool
}{
{"fmt", true},
{"b", true},
{"a", false},
{"log", true},
}
run := func(pass *analysis.Pass) (interface{}, error) {
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
used := analysisutil.Imported(tt.path, pass)
if used && !tt.used {
t.Error("not used")
} else if !used && tt.used {
t.Error("used")
}
})
}
return nil, nil
}
var analyzer = &analysis.Analyzer{
Run: run,
RunDespiteErrors: true,
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
},
}
testdata := analysistest.TestData()
analysistest.Run(t, testdata, analyzer, "pkgused")
}