|
| 1 | +package debug_test |
| 2 | + |
| 3 | +// Provide 'static type checking' of the templates. This guards against changes is various |
| 4 | +// gopls datastructures causing template execution to fail. The checking is done by |
| 5 | +// the github.com/jba/templatecheck pacakge. Before that is run, the test checks that |
| 6 | +// its list of templates and their arguments corresponds to the arguments in |
| 7 | +// calls to render(). The test assumes that all uses of templates are done through render(). |
| 8 | + |
| 9 | +import ( |
| 10 | + "go/ast" |
| 11 | + "html/template" |
| 12 | + "log" |
| 13 | + "runtime" |
| 14 | + "sort" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + |
| 18 | + "github.com/jba/templatecheck" |
| 19 | + "golang.org/x/tools/go/packages" |
| 20 | + "golang.org/x/tools/internal/lsp/cache" |
| 21 | + "golang.org/x/tools/internal/lsp/debug" |
| 22 | + "golang.org/x/tools/internal/lsp/source" |
| 23 | + "golang.org/x/tools/internal/span" |
| 24 | +) |
| 25 | + |
| 26 | +type tdata struct { |
| 27 | + tmpl *template.Template |
| 28 | + data interface{} // a value of the needed type |
| 29 | +} |
| 30 | + |
| 31 | +var templates = map[string]tdata{ |
| 32 | + "MainTmpl": {debug.MainTmpl, &debug.Instance{}}, |
| 33 | + "DebugTmpl": {debug.DebugTmpl, nil}, |
| 34 | + "RPCTmpl": {debug.RPCTmpl, &debug.Rpcs{}}, |
| 35 | + "TraceTmpl": {debug.TraceTmpl, debug.TraceResults{}}, |
| 36 | + "CacheTmpl": {debug.CacheTmpl, &cache.Cache{}}, |
| 37 | + "SessionTmpl": {debug.SessionTmpl, &cache.Session{}}, |
| 38 | + "ViewTmpl": {debug.ViewTmpl, &cache.View{}}, |
| 39 | + "ClientTmpl": {debug.ClientTmpl, &debug.Client{}}, |
| 40 | + "ServerTmpl": {debug.ServerTmpl, &debug.Server{}}, |
| 41 | + //"FileTmpl": {FileTmpl, source.Overlay{}}, // need to construct a source.Overlay in init |
| 42 | + "InfoTmpl": {debug.InfoTmpl, "something"}, |
| 43 | + "MemoryTmpl": {debug.MemoryTmpl, runtime.MemStats{}}, |
| 44 | +} |
| 45 | + |
| 46 | +// construct a source.Overlay for fileTmpl |
| 47 | +type fakeOverlay struct{} |
| 48 | + |
| 49 | +func (fakeOverlay) Version() float64 { |
| 50 | + return 0 |
| 51 | +} |
| 52 | +func (fakeOverlay) Session() string { |
| 53 | + return "" |
| 54 | +} |
| 55 | +func (fakeOverlay) VersionedFileIdentity() source.VersionedFileIdentity { |
| 56 | + return source.VersionedFileIdentity{} |
| 57 | +} |
| 58 | +func (fakeOverlay) FileIdentity() source.FileIdentity { |
| 59 | + return source.FileIdentity{} |
| 60 | +} |
| 61 | +func (fakeOverlay) Kind() source.FileKind { |
| 62 | + return 0 |
| 63 | +} |
| 64 | +func (fakeOverlay) Read() ([]byte, error) { |
| 65 | + return nil, nil |
| 66 | +} |
| 67 | +func (fakeOverlay) Saved() bool { |
| 68 | + return true |
| 69 | +} |
| 70 | +func (fakeOverlay) URI() span.URI { |
| 71 | + return "" |
| 72 | +} |
| 73 | + |
| 74 | +var _ source.Overlay = fakeOverlay{} |
| 75 | + |
| 76 | +func init() { |
| 77 | + log.SetFlags(log.Lshortfile) |
| 78 | + var v fakeOverlay |
| 79 | + templates["FileTmpl"] = tdata{debug.FileTmpl, v} |
| 80 | +} |
| 81 | + |
| 82 | +func TestTemplates(t *testing.T) { |
| 83 | + cfg := &packages.Config{ |
| 84 | + Mode: packages.NeedTypesInfo | packages.LoadAllSyntax, // figure out what's necessary PJW |
| 85 | + } |
| 86 | + pkgs, err := packages.Load(cfg, "golang.org/x/tools/internal/lsp/debug") |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + if len(pkgs) != 1 { |
| 91 | + t.Fatalf("expected a single package, but got %d", len(pkgs)) |
| 92 | + } |
| 93 | + p := pkgs[0] |
| 94 | + if len(p.Errors) != 0 { |
| 95 | + t.Fatalf("compiler error, e.g. %v", p.Errors[0]) |
| 96 | + } |
| 97 | + // find the calls to render in serve.go |
| 98 | + tree := treeOf(p, "serve.go") |
| 99 | + if tree == nil { |
| 100 | + t.Fatalf("found no syntax tree for %s", "serve.go") |
| 101 | + } |
| 102 | + renders := callsOf(p, tree, "render") |
| 103 | + if len(renders) == 0 { |
| 104 | + t.Fatalf("found no calls to render") |
| 105 | + } |
| 106 | + var found = make(map[string]bool) |
| 107 | + for _, r := range renders { |
| 108 | + if len(r.Args) != 2 { |
| 109 | + // template, func |
| 110 | + t.Fatalf("got %d args, expected 2", len(r.Args)) |
| 111 | + } |
| 112 | + t0, ok := p.TypesInfo.Types[r.Args[0]] |
| 113 | + if !ok || !t0.IsValue() || t0.Type.String() != "*html/template.Template" { |
| 114 | + t.Fatalf("no type info for template") |
| 115 | + } |
| 116 | + if id, ok := r.Args[0].(*ast.Ident); !ok { |
| 117 | + t.Errorf("expected *ast.Ident, got %T", r.Args[0]) |
| 118 | + } else { |
| 119 | + found[id.Name] = true |
| 120 | + } |
| 121 | + } |
| 122 | + // make sure found and templates have the same templates |
| 123 | + for k := range found { |
| 124 | + if _, ok := templates[k]; !ok { |
| 125 | + t.Errorf("code has template %s, but test does not", k) |
| 126 | + } |
| 127 | + } |
| 128 | + for k := range templates { |
| 129 | + if _, ok := found[k]; !ok { |
| 130 | + t.Errorf("test has template %s, code does not", k) |
| 131 | + } |
| 132 | + } |
| 133 | + // now check all the known templates, in alphabetic order, for determinacy |
| 134 | + keys := []string{} |
| 135 | + for k := range templates { |
| 136 | + keys = append(keys, k) |
| 137 | + } |
| 138 | + sort.Strings(keys) |
| 139 | + for _, k := range keys { |
| 140 | + v := templates[k] |
| 141 | + // the FuncMap is an annoyance; should not be necessary |
| 142 | + if err := templatecheck.CheckHTML(v.tmpl, v.data); err != nil { |
| 143 | + t.Errorf("%s: %v", k, err) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func callsOf(p *packages.Package, tree *ast.File, name string) []*ast.CallExpr { |
| 149 | + var ans []*ast.CallExpr |
| 150 | + f := func(n ast.Node) bool { |
| 151 | + x, ok := n.(*ast.CallExpr) |
| 152 | + if !ok { |
| 153 | + return true |
| 154 | + } |
| 155 | + if y, ok := x.Fun.(*ast.Ident); ok { |
| 156 | + if y.Name == name { |
| 157 | + ans = append(ans, x) |
| 158 | + } |
| 159 | + } |
| 160 | + return true |
| 161 | + } |
| 162 | + ast.Inspect(tree, f) |
| 163 | + return ans |
| 164 | +} |
| 165 | +func treeOf(p *packages.Package, fname string) *ast.File { |
| 166 | + for _, tree := range p.Syntax { |
| 167 | + loc := tree.Package |
| 168 | + pos := p.Fset.PositionFor(loc, false) |
| 169 | + if strings.HasSuffix(pos.Filename, fname) { |
| 170 | + return tree |
| 171 | + } |
| 172 | + } |
| 173 | + return nil |
| 174 | +} |
0 commit comments