first commit
This commit is contained in:
commit
8c4dcae475
89 changed files with 24623 additions and 0 deletions
464
tests/tmplcheck_test.go
Normal file
464
tests/tmplcheck_test.go
Normal file
|
|
@ -0,0 +1,464 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"text/template/parse"
|
||||
)
|
||||
|
||||
// Every template is checked against the struct it is executed with, without
|
||||
// executing it: each field or method it reads must exist on the type it is
|
||||
// read from - in every branch, including the ones no test request takes.
|
||||
// html/template only finds a wrong name when the action runs.
|
||||
//
|
||||
// The checker follows the data through range, with, variables and template
|
||||
// calls. Where a type is not known statically it does not guess: a value
|
||||
// behind an interface{} (a dict's missing key, the argument of a partial
|
||||
// called with a literal) is accepted whatever is read from it.
|
||||
func TestTemplatesMatchPages(t *testing.T) {
|
||||
buildFixture(t)
|
||||
for _, p := range pageTypes {
|
||||
name := p.templateName()
|
||||
set, err := parsePage(name) // a fresh parse: executing rewrites the trees
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
c := &tmplCheck{seen: map[string]bool{}, records: map[reflect.Type]bool{}}
|
||||
c.lookup = func(n string) *parse.Tree {
|
||||
if tt := set.Lookup(n); tt != nil {
|
||||
return tt.Tree
|
||||
}
|
||||
return nil
|
||||
}
|
||||
c.call(name, reflect.TypeOf(p), 0)
|
||||
sort.Strings(c.errs)
|
||||
for _, e := range c.errs {
|
||||
t.Errorf("%s (%T): %s", name, p, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Each template file is some page's, and each page's template exists.
|
||||
func TestEveryTemplateHasAPage(t *testing.T) {
|
||||
buildFixture(t)
|
||||
files, err := fs.Glob(templateFS, "*.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
byName := map[string]bool{}
|
||||
for _, p := range pageTypes {
|
||||
if byName[p.templateName()] {
|
||||
t.Errorf("%s is the template of two page types", p.templateName())
|
||||
}
|
||||
byName[p.templateName()] = true
|
||||
}
|
||||
for _, f := range files {
|
||||
if f == "base.html" || f == "partials.html" {
|
||||
continue
|
||||
}
|
||||
if !byName[f] {
|
||||
t.Errorf("%s has no page type in pageTypes", f)
|
||||
}
|
||||
delete(byName, f)
|
||||
}
|
||||
for n := range byName {
|
||||
t.Errorf("page type for %s, which is not a template file", n)
|
||||
}
|
||||
}
|
||||
|
||||
// The checker itself: a misspelled field is found in a branch that would
|
||||
// not run, behind a range, a variable and a partial; a dict's keys carry
|
||||
// their types into the partial they are passed to.
|
||||
func TestTemplateCheckerFindsWrongNames(t *testing.T) {
|
||||
type row struct{ Name string }
|
||||
type page struct {
|
||||
Rows []row
|
||||
Doc *Document
|
||||
M map[string]int
|
||||
}
|
||||
for src, want := range map[string]string{
|
||||
`{{ if .Nope }}x{{ end }}`: "Nope",
|
||||
`{{ if false }}{{ range .Rows }}{{ .Nmae }}{{ end }}{{ end }}`: "Nmae",
|
||||
`{{ range $i, $r := .Rows }}{{ $r.Name }}{{ $.Rowz }}{{ end }}`: "Rowz",
|
||||
`{{ with .Doc }}{{ .Title }}{{ .DocMeta.Creatr }}{{ end }}`: "Creatr",
|
||||
`{{ $d := .Doc }}{{ $d.Titel }}`: "Titel",
|
||||
`{{ template "p" .Doc }}{{ define "p" }}{{ .ID }}{{ .Idd }}{{ end }}`: "Idd",
|
||||
`{{ template "p" (dict "D" .Doc) }}{{ define "p" }}{{ .D.Idd }}{{ end }}`: "Idd",
|
||||
`{{ index .M "a" }}{{ (index .Rows 0).Nmae }}`: "Nmae",
|
||||
`{{ .Rows.Name }}`: "Name",
|
||||
`{{ range .Rows }}{{ .Name }}{{ else }}{{ .Name }}{{ end }}`: "Name",
|
||||
`{{ template "p" (dict "D" .Doc) }}{{ define "p" }}{{ .Missing.X }}{{ end }}`: "",
|
||||
`{{ range .Rows }}{{ .Name }}{{ end }}{{ .Doc.Title }}{{ len .M }}`: "",
|
||||
`{{ $x := "" }}{{ if .Doc }}{{ $x = .Doc }}{{ end }}{{ $x.Anything }}`: "",
|
||||
} {
|
||||
trees, err := parse.Parse("t", src, "", "", checkerFuncs())
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", src, err)
|
||||
}
|
||||
c := &tmplCheck{seen: map[string]bool{}, records: map[reflect.Type]bool{}}
|
||||
c.lookup = func(n string) *parse.Tree { return trees[n] }
|
||||
c.call("t", reflect.TypeOf(page{}), 0)
|
||||
got := strings.Join(c.errs, "; ")
|
||||
if (want == "") != (got == "") || !strings.Contains(got, want) {
|
||||
t.Errorf("%s\n errors: %q, want one naming %q", src, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkerFuncs is funcMap as the parser wants it, with the builtins the
|
||||
// templates use.
|
||||
func checkerFuncs() map[string]any {
|
||||
m := map[string]any{}
|
||||
for k, v := range funcMap {
|
||||
m[k] = v
|
||||
}
|
||||
for _, b := range []string{"and", "or", "not", "eq", "ne", "lt", "le", "gt", "ge", "len", "index",
|
||||
"slice", "print", "printf", "println", "html", "js", "urlquery", "call"} {
|
||||
m[b] = true
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
type tmplCheck struct {
|
||||
lookup func(name string) *parse.Tree
|
||||
seen map[string]bool // template name + argument type, already walked
|
||||
records map[reflect.Type]bool // types made from a dict call: a missing key is nil, not an error
|
||||
errs []string
|
||||
where string // the template being walked
|
||||
}
|
||||
|
||||
type tmplVar struct {
|
||||
name string
|
||||
typ reflect.Type // nil: not known
|
||||
}
|
||||
|
||||
var (
|
||||
stringType = reflect.TypeOf("")
|
||||
intType = reflect.TypeOf(0)
|
||||
boolType = reflect.TypeOf(false)
|
||||
floatType = reflect.TypeOf(0.0)
|
||||
anyType = reflect.TypeOf((*any)(nil)).Elem()
|
||||
)
|
||||
|
||||
func (c *tmplCheck) errorf(n parse.Node, format string, a ...any) {
|
||||
loc := c.where
|
||||
if tree := c.lookup(c.where); tree != nil {
|
||||
loc, _ = tree.ErrorContext(n)
|
||||
}
|
||||
c.errs = append(c.errs, fmt.Sprintf("%s (in %q): %s", loc, c.where, fmt.Sprintf(format, a...)))
|
||||
}
|
||||
|
||||
// call walks the named template with dot of type arg.
|
||||
func (c *tmplCheck) call(name string, arg reflect.Type, depth int) {
|
||||
key := name + "|"
|
||||
if arg != nil {
|
||||
key += arg.String()
|
||||
if c.records[arg] {
|
||||
key += fmt.Sprintf("|%p", arg) // records print alike only when they are alike; keep them apart anyway
|
||||
}
|
||||
}
|
||||
if c.seen[key] || depth > 20 {
|
||||
return
|
||||
}
|
||||
c.seen[key] = true
|
||||
tree := c.lookup(name)
|
||||
if tree == nil || tree.Root == nil {
|
||||
c.errs = append(c.errs, fmt.Sprintf("template %q is not defined", name))
|
||||
return
|
||||
}
|
||||
prev := c.where
|
||||
c.where = name
|
||||
vars := []tmplVar{{"$", arg}}
|
||||
c.walk(tree.Root, arg, &vars, depth)
|
||||
c.where = prev
|
||||
}
|
||||
|
||||
func (c *tmplCheck) walk(n parse.Node, dot reflect.Type, vars *[]tmplVar, depth int) {
|
||||
switch n := n.(type) {
|
||||
case *parse.ListNode:
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
for _, x := range n.Nodes {
|
||||
c.walk(x, dot, vars, depth)
|
||||
}
|
||||
case *parse.ActionNode:
|
||||
c.pipe(n.Pipe, dot, vars)
|
||||
case *parse.IfNode:
|
||||
mark := len(*vars)
|
||||
c.pipe(n.Pipe, dot, vars)
|
||||
c.walk(n.List, dot, vars, depth)
|
||||
c.walk(n.ElseList, dot, vars, depth)
|
||||
*vars = (*vars)[:mark]
|
||||
case *parse.WithNode:
|
||||
mark := len(*vars)
|
||||
t := c.pipe(n.Pipe, dot, vars)
|
||||
c.walk(n.List, t, vars, depth)
|
||||
c.walk(n.ElseList, dot, vars, depth)
|
||||
*vars = (*vars)[:mark]
|
||||
case *parse.RangeNode:
|
||||
mark := len(*vars)
|
||||
t := c.cmds(n.Pipe, dot, vars)
|
||||
key, elem := rangeTypes(t)
|
||||
switch len(n.Pipe.Decl) {
|
||||
case 1:
|
||||
*vars = append(*vars, tmplVar{n.Pipe.Decl[0].Ident[0], elem})
|
||||
case 2:
|
||||
*vars = append(*vars, tmplVar{n.Pipe.Decl[0].Ident[0], key}, tmplVar{n.Pipe.Decl[1].Ident[0], elem})
|
||||
}
|
||||
c.walk(n.List, elem, vars, depth)
|
||||
*vars = (*vars)[:mark]
|
||||
c.walk(n.ElseList, dot, vars, depth)
|
||||
case *parse.TemplateNode:
|
||||
var arg reflect.Type
|
||||
if n.Pipe != nil {
|
||||
arg = c.pipe(n.Pipe, dot, vars)
|
||||
}
|
||||
c.call(n.Name, arg, depth+1)
|
||||
}
|
||||
}
|
||||
|
||||
func rangeTypes(t reflect.Type) (key, elem reflect.Type) {
|
||||
t = indirectType(t)
|
||||
if t == nil {
|
||||
return nil, nil
|
||||
}
|
||||
switch t.Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
return intType, known(t.Elem())
|
||||
case reflect.Map:
|
||||
return known(t.Key()), known(t.Elem())
|
||||
case reflect.Int:
|
||||
return intType, intType
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// known is t, or nil when t says nothing about the value (an interface).
|
||||
func known(t reflect.Type) reflect.Type {
|
||||
if t != nil && t.Kind() == reflect.Interface {
|
||||
return nil
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func indirectType(t reflect.Type) reflect.Type {
|
||||
for t != nil && t.Kind() == reflect.Pointer {
|
||||
t = t.Elem()
|
||||
}
|
||||
return known(t)
|
||||
}
|
||||
|
||||
// pipe is the type a pipeline yields, declaring or assigning its variable.
|
||||
func (c *tmplCheck) pipe(p *parse.PipeNode, dot reflect.Type, vars *[]tmplVar) reflect.Type {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
t := c.cmds(p, dot, vars)
|
||||
for _, d := range p.Decl {
|
||||
name := d.Ident[0]
|
||||
if !p.IsAssign {
|
||||
*vars = append(*vars, tmplVar{name, t})
|
||||
continue
|
||||
}
|
||||
for i := len(*vars) - 1; i >= 0; i-- {
|
||||
if (*vars)[i].name == name {
|
||||
if (*vars)[i].typ != t {
|
||||
(*vars)[i].typ = nil // assigned two types: whichever branch ran decides
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (c *tmplCheck) cmds(p *parse.PipeNode, dot reflect.Type, vars *[]tmplVar) reflect.Type {
|
||||
var t reflect.Type
|
||||
for i, cmd := range p.Cmds {
|
||||
t = c.cmd(cmd, dot, vars, i > 0, t)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (c *tmplCheck) cmd(cmd *parse.CommandNode, dot reflect.Type, vars *[]tmplVar, piped bool, prev reflect.Type) reflect.Type {
|
||||
first := cmd.Args[0]
|
||||
if id, ok := first.(*parse.IdentifierNode); ok {
|
||||
if id.Ident == "dict" {
|
||||
return c.dict(cmd, dot, vars)
|
||||
}
|
||||
args := make([]reflect.Type, 0, len(cmd.Args))
|
||||
for _, a := range cmd.Args[1:] {
|
||||
args = append(args, c.arg(a, dot, vars))
|
||||
}
|
||||
if piped {
|
||||
args = append(args, prev)
|
||||
}
|
||||
return c.fn(id, args)
|
||||
}
|
||||
for _, a := range cmd.Args[1:] { // a method's arguments
|
||||
c.arg(a, dot, vars)
|
||||
}
|
||||
return c.arg(first, dot, vars)
|
||||
}
|
||||
|
||||
func (c *tmplCheck) arg(n parse.Node, dot reflect.Type, vars *[]tmplVar) reflect.Type {
|
||||
switch n := n.(type) {
|
||||
case *parse.DotNode:
|
||||
return dot
|
||||
case *parse.FieldNode:
|
||||
return c.fields(n, dot, n.Ident)
|
||||
case *parse.VariableNode:
|
||||
for i := len(*vars) - 1; i >= 0; i-- {
|
||||
if (*vars)[i].name == n.Ident[0] {
|
||||
return c.fields(n, (*vars)[i].typ, n.Ident[1:])
|
||||
}
|
||||
}
|
||||
c.errorf(n, "undefined variable %s", n.Ident[0])
|
||||
case *parse.ChainNode:
|
||||
return c.fields(n, c.arg(n.Node, dot, vars), n.Field)
|
||||
case *parse.PipeNode:
|
||||
return c.pipe(n, dot, vars)
|
||||
case *parse.IdentifierNode:
|
||||
return c.fn(n, nil)
|
||||
case *parse.StringNode:
|
||||
return stringType
|
||||
case *parse.BoolNode:
|
||||
return boolType
|
||||
case *parse.NumberNode:
|
||||
if n.IsInt {
|
||||
return intType
|
||||
}
|
||||
return floatType
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fields follows a chain of names from t.
|
||||
func (c *tmplCheck) fields(n parse.Node, t reflect.Type, names []string) reflect.Type {
|
||||
for _, name := range names {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
// A method, on the value or on its address.
|
||||
recv := t
|
||||
if recv.Kind() != reflect.Pointer && recv.Kind() != reflect.Interface {
|
||||
recv = reflect.PointerTo(recv)
|
||||
}
|
||||
if m, ok := recv.MethodByName(name); ok {
|
||||
t = nil
|
||||
if m.Type.NumOut() > 0 {
|
||||
t = known(m.Type.Out(0))
|
||||
}
|
||||
continue
|
||||
}
|
||||
s := indirectType(t)
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
switch s.Kind() {
|
||||
case reflect.Struct:
|
||||
f, ok := s.FieldByName(name)
|
||||
if !ok || !f.IsExported() {
|
||||
if c.records[s] {
|
||||
return nil // a key this dict call did not pass
|
||||
}
|
||||
c.errorf(n, "no field or method %s in %s", name, s)
|
||||
return nil
|
||||
}
|
||||
t = known(f.Type)
|
||||
case reflect.Map:
|
||||
if s.Key().Kind() != reflect.String {
|
||||
c.errorf(n, "%s read from %s, a map without string keys", name, s)
|
||||
return nil
|
||||
}
|
||||
t = known(s.Elem())
|
||||
default:
|
||||
c.errorf(n, "%s read from %s, which has no fields", name, s)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// dict is the record a dict call builds: each literal key with the type of
|
||||
// the value passed for it.
|
||||
func (c *tmplCheck) dict(cmd *parse.CommandNode, dot reflect.Type, vars *[]tmplVar) reflect.Type {
|
||||
var fs []reflect.StructField
|
||||
args := cmd.Args[1:]
|
||||
for i := 0; i+1 < len(args); i += 2 {
|
||||
k, ok := args[i].(*parse.StringNode)
|
||||
t := c.arg(args[i+1], dot, vars)
|
||||
if !ok {
|
||||
return nil // a computed key: nothing is known about the record
|
||||
}
|
||||
if t == nil {
|
||||
t = anyType
|
||||
}
|
||||
fs = append(fs, reflect.StructField{Name: k.Text, Type: t})
|
||||
}
|
||||
defer func() { recover() }() // a key that is not a Go identifier: no record
|
||||
rt := reflect.StructOf(fs)
|
||||
c.records[rt] = true
|
||||
return rt
|
||||
}
|
||||
|
||||
// fn is the type a function call yields.
|
||||
func (c *tmplCheck) fn(id *parse.IdentifierNode, args []reflect.Type) reflect.Type {
|
||||
switch id.Ident {
|
||||
case "not", "eq", "ne", "lt", "le", "gt", "ge":
|
||||
return boolType
|
||||
case "len":
|
||||
return intType
|
||||
case "print", "printf", "println", "html", "js", "urlquery":
|
||||
return stringType
|
||||
case "and", "or":
|
||||
for _, a := range args {
|
||||
if a == nil || a != args[0] {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if len(args) > 0 {
|
||||
return args[0]
|
||||
}
|
||||
return nil
|
||||
case "slice":
|
||||
if len(args) > 0 {
|
||||
return args[0]
|
||||
}
|
||||
return nil
|
||||
case "index":
|
||||
if len(args) == 0 {
|
||||
return nil
|
||||
}
|
||||
t := args[0]
|
||||
for range args[1:] {
|
||||
t = indirectType(t)
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
switch t.Kind() {
|
||||
case reflect.Map, reflect.Slice, reflect.Array:
|
||||
t = known(t.Elem())
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return t
|
||||
case "call":
|
||||
return nil
|
||||
}
|
||||
f, ok := funcMap[id.Ident]
|
||||
if !ok {
|
||||
return nil // the parser has already refused an unknown function
|
||||
}
|
||||
ft := reflect.TypeOf(f)
|
||||
if ft.NumOut() == 0 {
|
||||
return nil
|
||||
}
|
||||
return known(ft.Out(0))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue