Debian Codegraph @

gopls/internal/debug/template_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 t.Skip("golang-github-jba-templatecheck-dev has not been packaged yet, skipping test")
51 testenv.NeedsGoPackages(t)
52 testenv.NeedsLocalXTools(t)
53
54 cfg := &packages.Config{
55 Mode: packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
56 }
57 cfg.Env = os.Environ()
58 cfg.Env = append(cfg.Env,
59 "GOPACKAGESDRIVER=off",
60 "GOWORK=off", // necessary for -mod=mod below
61 "GOFLAGS=-mod=mod",
62 )
63
64 pkgs, err := packages.Load(cfg, "golang.org/x/tools/gopls/internal/debug")
65 if err != nil {
66 t.Fatal(err)
67 }
68 if len(pkgs) != 1 {
69 t.Fatalf("expected a single package, but got %d", len(pkgs))
70 }
71 p := pkgs[0]
72 if len(p.Errors) != 0 {
73 t.Fatalf("compiler error, e.g. %v", p.Errors[0])
74 }
75 // find the calls to render in serve.go
76 tree := treeOf(p, "serve.go")
77 if tree == nil {
78 t.Fatalf("found no syntax tree for %s", "serve.go")
79 }
80 renders := callsOf(tree, "render")
81 if len(renders) == 0 {
82 t.Fatalf("found no calls to render")
83 }
84 var found = make(map[string]bool)
85 for _, r := range renders {
86 if len(r.Args) != 2 {
87 // template, func
88 t.Fatalf("got %d args, expected 2", len(r.Args))
89 }
90 t0, ok := p.TypesInfo.Types[r.Args[0]]
91 if !ok || !t0.IsValue() || t0.Type.String() != "*html/template.Template" {
92 t.Fatalf("no type info for template")
93 }
94 if id, ok := r.Args[0].(*ast.Ident); !ok {
95 t.Errorf("expected *ast.Ident, got %T", r.Args[0])
96 } else {
97 found[id.Name] = true
98 }
99 }
100 // make sure found and templates have the same templates
101 for k := range found {
102 if _, ok := templates[k]; !ok {
103 t.Errorf("code has template %s, but test does not", k)
104 }
105 }
106 for k := range templates {
107 if _, ok := found[k]; !ok {
108 t.Errorf("test has template %s, code does not", k)
109 }
110 }
111 // now check all the known templates, in alphabetic order, for determinacy
112 for k, v := range moremaps.Sorted(templates) {
113 // the FuncMap is an annoyance; should not be necessary
•114 // if err := templatecheck.CheckHTML(v.tmpl, v.data); err != nil {
115 // t.Errorf("%s: %v", k, err)
116 // continue
117 // }
118 t.Logf("%s ok", k)
119 }
120 }
121
122 func callsOf(tree *ast.File, name string) []*ast.CallExpr {
123 var ans []*ast.CallExpr
124 f := func(n ast.Node) bool {
125 x, ok := n.(*ast.CallExpr)
126 if !ok {
127 return true
128 }
129 if y, ok := x.Fun.(*ast.Ident); ok {
130 if y.Name == name {
131 ans = append(ans, x)
132 }
133 }
134 return true
135 }
136 ast.Inspect(tree, f)
137 return ans
138 }
139
140 func treeOf(p *packages.Package, fname string) *ast.File {
141 for _, tree := range p.Syntax {
142 loc := tree.Package
143 pos := p.Fset.PositionFor(loc, false)
144 if strings.HasSuffix(pos.Filename, fname) {
145 return tree
146 }
147 }
148 return nil
149 }