-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsourcecode_test.go
More file actions
85 lines (80 loc) · 2.34 KB
/
Copy pathsourcecode_test.go
File metadata and controls
85 lines (80 loc) · 2.34 KB
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
package moduledoc
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"testing"
"golang.org/x/tools/go/packages"
)
func TestFindModuleRegistrationIgnoresMethods(t *testing.T) {
for _, tc := range []struct {
name string
method string
call string
}{
{"two arguments", "func (*State) RegisterModule(string, map[string]func()) {}", `state.RegisterModule("_G", nil)`},
{"one argument", "func (*State) RegisterModule(any) {}", `state.RegisterModule(App{})`},
} {
t.Run(tc.name, func(t *testing.T) {
fset := token.NewFileSet()
// Type-check a minimal Caddy package so the real Go type checker
// distinguishes an imported package name from a method receiver.
caddyFile, err := parser.ParseFile(fset, "caddy.go", `package caddy
func RegisterModule(any) {}`, 0)
if err != nil {
t.Fatal(err)
}
caddyPkg, err := new(types.Config).Check(caddyCorePackagePath, fset, []*ast.File{caddyFile}, nil)
if err != nil {
t.Fatal(err)
}
src := `package example
import caddycore "github.com/caddyserver/caddy/v2"
type App struct{}
type State struct{}
` + tc.method + `
func init() {
state := new(State)
` + tc.call + `
caddycore.RegisterModule(App{})
}`
file, err := parser.ParseFile(fset, "example.go", src, 0)
if err != nil {
t.Fatal(err)
}
info := &types.Info{Uses: make(map[*ast.Ident]types.Object)}
cfg := types.Config{Importer: registrationTestImporter{caddyPkg}}
if _, err := cfg.Check("example.com/plugin", fset, []*ast.File{file}, info); err != nil {
t.Fatal(err)
}
pkg := &packages.Package{TypesInfo: info}
var registered []string
ast.Inspect(file, func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
ident, err := new(Driver).findModuleRegistration(pkg, call)
if err != nil {
t.Errorf("%s: %v", fset.Position(call.Pos()), err)
}
if ident != nil {
registered = append(registered, ident.Name)
}
return true
})
if len(registered) != 1 || registered[0] != "App" {
t.Fatalf("registered types = %v; want only the Caddy registration of App", registered)
}
})
}
}
type registrationTestImporter struct{ pkg *types.Package }
func (i registrationTestImporter) Import(path string) (*types.Package, error) {
if path != i.pkg.Path() {
return nil, fmt.Errorf("unexpected import %q", path)
}
return i.pkg, nil
}