Repository navigation
Expand file tree
/
Copy pathsourcecode_edge_test.go
More file actions
164 lines (149 loc) · 5.07 KB
/
Copy pathsourcecode_edge_test.go
File metadata and controls
164 lines (149 loc) · 5.07 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
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
153
154
155
156
157
158
159
160
161
162
163
164
package moduledoc
import (
"go/ast"
"go/parser"
"os"
"testing"
"golang.org/x/tools/go/packages"
)
// loadFixturePackage loads one local testdata package with full type info.
func loadFixturePackage(t *testing.T, pattern string) *packages.Package {
t.Helper()
if testing.Short() {
t.Skip("requires the Go toolchain")
}
cfg := &packages.Config{
Mode: packagesLoadMode,
Env: append(os.Environ(), "CGO_ENABLED=0"),
}
pkgs, err := packages.Load(cfg, pattern)
if err != nil {
t.Fatalf("loading %s: %v", pattern, err)
}
if len(pkgs) != 1 {
t.Fatalf("expected 1 package for %s, got %d", pattern, len(pkgs))
}
for _, e := range pkgs[0].Errors {
t.Fatalf("fixture package %s has errors: %v", pattern, e)
}
return pkgs[0]
}
func identsByTypeName(t *testing.T, d *Driver, pkg *packages.Package) map[string]string {
t.Helper()
idents, err := d.findCaddyModuleIdents(pkg)
if err != nil {
t.Fatalf("finding module idents: %v", err)
}
byName := make(map[string]string, len(idents))
for ident, id := range idents {
byName[ident.Name] = id
}
return byName
}
func TestFindModuleIdentsPointerAndValuePatterns(t *testing.T) {
d := New(newMemStorage())
// registration via new() with pointer receiver
pkg := loadFixturePackage(t, "./testdata")
got := identsByTypeName(t, d, pkg)
if got["Gizmo"] != "app.namespace.gizmo" {
t.Errorf("expected Gizmo module, got %#v", got)
}
// registration via composite literal with value receiver
pkg = loadFixturePackage(t, "./testdata/valuerecv")
got = identsByTypeName(t, d, pkg)
if got["Sprocket"] != "app.namespace.sprocket" {
t.Errorf("expected Sprocket module, got %#v", got)
}
pkg = loadFixturePackage(t, "./testdata/addrofliteral")
got = identsByTypeName(t, d, pkg)
if len(got) != 1 || got["Widget"] != "app.namespace.addr_widget" {
t.Errorf("expected Widget module registered via address-of composite literal, got %#v", got)
}
}
func TestFindModuleRegistrationArgumentForms(t *testing.T) {
tests := []struct {
name string
arg string
want string
wantErr bool
}{
{name: "value literal", arg: "Widget{}", want: "Widget"},
{name: "new", arg: "new(Widget)", want: "Widget"},
{name: "address-of literal", arg: "&Widget{}", want: "Widget"},
{name: "initialized address-of literal", arg: "&Widget{Enabled: true}", want: "Widget"},
{name: "qualified address-of literal", arg: "&other.Widget{}"},
{name: "address-of variable", arg: "&widget", wantErr: true},
{name: "other unary operator", arg: "!Widget{}", wantErr: true},
}
d := New(nil)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expr, err := parser.ParseExpr("RegisterModule(" + tt.arg + ")")
if err != nil {
t.Fatal(err)
}
call, ok := expr.(*ast.CallExpr)
if !ok {
t.Fatalf("expected call expression, got %T", expr)
}
ident, err := d.findModuleRegistration(&packages.Package{}, call)
if (err != nil) != tt.wantErr {
t.Fatalf("findModuleRegistration() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.want == "" {
if ident != nil {
t.Errorf("expected no identifier, got %s", ident.Name)
}
} else if ident == nil || ident.Name != tt.want {
t.Errorf("expected identifier %s, got %v", tt.want, ident)
}
})
}
}
func TestModuleIDFromConstant(t *testing.T) {
d := New(newMemStorage())
pkg := loadFixturePackage(t, "./testdata/constid")
got := identsByTypeName(t, d, pkg)
if got["ConstWidget"] != "app.namespace.const_widget" {
t.Errorf("module with ID declared as a package constant should be discovered, got %#v", got)
}
}
func TestModuleIDFromConcatenation(t *testing.T) {
d := New(newMemStorage())
pkg := loadFixturePackage(t, "./testdata/exprid")
got := identsByTypeName(t, d, pkg)
if got["ExprWidget"] != "app.namespace.expr_widget" {
t.Errorf("module with ID built from constant string concatenation should be discovered, got %#v", got)
}
}
func TestUnregisteredModuleFailsPackage(t *testing.T) {
d := New(newMemStorage())
pkg := loadFixturePackage(t, "./testdata/unregistered")
// one unregistered module type fails the whole package,
// including its valid sibling module (current strict contract)
if _, err := d.findCaddyModuleIdents(pkg); err == nil {
t.Error("expected error for package containing an unregistered module type")
}
}
func TestRegistrationWithoutLocalImplementation(t *testing.T) {
d := New(newMemStorage())
pkg := loadFixturePackage(t, "./testdata/noimpl")
if _, err := d.findCaddyModuleIdents(pkg); err == nil {
t.Error("expected error for registration whose CaddyModule method is not declared in the package")
}
}
func TestFindModuleIdentsUnusualAST(t *testing.T) {
d := New(newMemStorage())
pkg := loadFixturePackage(t, "./testdata/crosspkg")
defer func() {
if r := recover(); r != nil {
t.Fatalf("registration of a qualified type from another package must not panic: %v", r)
}
}()
// either outcome is acceptable as long as it does not panic:
// an error (no local implementation) or an empty result
idents, err := d.findCaddyModuleIdents(pkg)
if err == nil && len(idents) > 0 {
t.Logf("cross-package registration produced idents: %v", idents)
}
}