| 1 |
// Copyright 2020 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 debug_test |
| 6 |
|
| 7 |
// Provide 'static type checking' of the templates. This guards against changes in various |
| 8 |
// gopls datastructures causing template execution to fail. The checking is done by |
| 9 |
// the github.com/jba/templatecheck package. Before that is run, the test checks that |
| 10 |
// its list of templates and their arguments corresponds to the arguments in |
| 11 |
// calls to render(). The test assumes that all uses of templates are done through render(). |
| 12 |
|
| 13 |
import ( |
| 14 |
"go/ast" |
| 15 |
"html/template" |
| 16 |
"os" |
| 17 |
"runtime" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/jba/templatecheck" |
| 22 |
"golang.org/x/tools/go/packages" |
| 23 |
"golang.org/x/tools/gopls/internal/cache" |
| 24 |
"golang.org/x/tools/gopls/internal/debug" |
| 25 |
"golang.org/x/tools/gopls/internal/util/moremaps" |
| 26 |
"golang.org/x/tools/internal/testenv" |
| 27 |
) |
| 28 |
|
| 29 |
var templates = map[string]struct { |
| 30 |
tmpl *template.Template |
| 31 |
data any // a value of the needed type |
| 32 |
}{ |
| 33 |
"MainTmpl": {debug.MainTmpl, &debug.Instance{}}, |
| 34 |
"DebugTmpl": {debug.DebugTmpl, nil}, |
| 35 |
"RPCTmpl": {debug.RPCTmpl, &debug.Rpcs{}}, |
| 36 |
"TraceTmpl": {debug.TraceTmpl, debug.TraceResults{}}, |
| 37 |
"CacheTmpl": {debug.CacheTmpl, &cache.Cache{}}, |
| 38 |
"SessionTmpl": {debug.SessionTmpl, &cache.Session{}}, |
| 39 |
"ClientTmpl": {debug.ClientTmpl, &debug.Client{}}, |
| 40 |
"ServerTmpl": {debug.ServerTmpl, &debug.Server{}}, |
| 41 |
"FileTmpl": {debug.FileTmpl, *new(debug.FileWithKind)}, |
| 42 |
"MetadataTmpl": {debug.MetadataTmpl, new(debug.MetadataInfo)}, |
| 43 |
"PackageTmpl": {debug.PackageTmpl, new(debug.PackageInfo)}, |
| 44 |
"InfoTmpl": {debug.InfoTmpl, "something"}, |
| 45 |
"MemoryTmpl": {debug.MemoryTmpl, runtime.MemStats{}}, |
| 46 |
"AnalysisTmpl": {debug.AnalysisTmpl, new(debug.State).Analysis()}, |
| 47 |
} |
| 48 |
|
| 49 |
func TestTemplates(t *testing.T) { |
| 50 |
testenv.NeedsGoPackages(t) |
| 51 |
testenv.NeedsLocalXTools(t) |
| 52 |
|
| 53 |
cfg := &packages.Config{ |
| 54 |
Mode: packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo, |
| 55 |
} |
| 56 |
cfg.Env = os.Environ() |
| 57 |
cfg.Env = append(cfg.Env, |
| 58 |
"GOPACKAGESDRIVER=off", |
| 59 |
"GOWORK=off", // necessary for -mod=mod below |
| 60 |
"GOFLAGS=-mod=mod", |
| 61 |
) |
| 62 |
|
| 63 |
pkgs, err := packages.Load(cfg, "golang.org/x/tools/gopls/internal/debug") |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
if len(pkgs) != 1 { |
| 68 |
t.Fatalf("expected a single package, but got %d", len(pkgs)) |
| 69 |
} |
| 70 |
p := pkgs[0] |
| 71 |
if len(p.Errors) != 0 { |
| 72 |
t.Fatalf("compiler error, e.g. %v", p.Errors[0]) |
| 73 |
} |
| 74 |
// find the calls to render in serve.go |
| 75 |
tree := treeOf(p, "serve.go") |
| 76 |
if tree == nil { |
| 77 |
t.Fatalf("found no syntax tree for %s", "serve.go") |
| 78 |
} |
| 79 |
renders := callsOf(tree, "render") |
| 80 |
if len(renders) == 0 { |
| 81 |
t.Fatalf("found no calls to render") |
| 82 |
} |
| 83 |
var found = make(map[string]bool) |
| 84 |
for _, r := range renders { |
| 85 |
if len(r.Args) != 2 { |
| 86 |
// template, func |
| 87 |
t.Fatalf("got %d args, expected 2", len(r.Args)) |
| 88 |
} |
| 89 |
t0, ok := p.TypesInfo.Types[r.Args[0]] |
| 90 |
if !ok || !t0.IsValue() || t0.Type.String() != "*html/template.Template" { |
| 91 |
t.Fatalf("no type info for template") |
| 92 |
} |
| 93 |
if id, ok := r.Args[0].(*ast.Ident); !ok { |
| 94 |
t.Errorf("expected *ast.Ident, got %T", r.Args[0]) |
| 95 |
} else { |
| 96 |
found[id.Name] = true |
| 97 |
} |
| 98 |
} |
| 99 |
// make sure found and templates have the same templates |
| 100 |
for k := range found { |
| 101 |
if _, ok := templates[k]; !ok { |
| 102 |
t.Errorf("code has template %s, but test does not", k) |
| 103 |
} |
| 104 |
} |
| 105 |
for k := range templates { |
| 106 |
if _, ok := found[k]; !ok { |
| 107 |
t.Errorf("test has template %s, code does not", k) |
| 108 |
} |
| 109 |
} |
| 110 |
// now check all the known templates, in alphabetic order, for determinacy |
| 111 |
for k, v := range moremaps.Sorted(templates) { |
| 112 |
// the FuncMap is an annoyance; should not be necessary |
| 113 |
if err := templatecheck.CheckHTML(v.tmpl, v.data); err != nil { |
| 114 |
t.Errorf("%s: %v", k, err) |
| 115 |
continue |
| 116 |
} |
| 117 |
t.Logf("%s ok", k) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func callsOf(tree *ast.File, name string) []*ast.CallExpr { |
| 122 |
var ans []*ast.CallExpr |
| 123 |
f := func(n ast.Node) bool { |
| 124 |
x, ok := n.(*ast.CallExpr) |
| 125 |
if !ok { |
| 126 |
return true |
| 127 |
} |
| 128 |
if y, ok := x.Fun.(*ast.Ident); ok { |
| 129 |
if y.Name == name { |
| 130 |
ans = append(ans, x) |
| 131 |
} |
| 132 |
} |
| 133 |
return true |
| 134 |
} |
| 135 |
ast.Inspect(tree, f) |
| 136 |
return ans |
| 137 |
} |
| 138 |
|
| 139 |
func treeOf(p *packages.Package, fname string) *ast.File { |
| 140 |
for _, tree := range p.Syntax { |
| 141 |
loc := tree.Package |
| 142 |
pos := p.Fset.PositionFor(loc, false) |
| 143 |
if strings.HasSuffix(pos.Filename, fname) { |
| 144 |
return tree |
| 145 |
} |
| 146 |
} |
| 147 |
return nil |
| 148 |
} |