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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2018 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon_test
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"gopkg.in/check.v1"
)
func countCommandDecls(c *check.C, comment check.CommentInterface) int {
n := 0
fns, _ := filepath.Glob("*.go")
for _, fn := range fns {
if !strings.HasSuffix(fn, "_test.go") {
n += countCommandDeclsIn(c, fn, comment)
}
}
return n
}
func countCommandDeclsIn(c *check.C, filename string, comment check.CommentInterface) int {
// NOTE: there's probably a
// better/easier way of doing this (patches welcome)
//
// Another note: the code below will find any and all variable
// declaration that have a &Command{} on the right hand. This
// is what we're currently using to declare the handlers that
// fill the api list; it also counts Command{}, and
// multi-variable (var foo, ... = Command{}, ...) just to
// future-proof it a little bit, but it's still supposed to be
// very restrictive. In particular I can think of different
// ways of doing things that won't be counted by the code
// below, i.e. the code below can still give false positives
// by counting too few command instances, e.g. if they're
// added directly to the api list, or if they're declared in a
// function or secondary slice or ... but as it stands I can't
// think of a way for it to give false negatives.
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, 0)
c.Assert(err, check.IsNil, comment)
found := 0
ast.Inspect(f, func(n ast.Node) bool {
var vs *ast.ValueSpec
switch n := n.(type) {
case *ast.ValueSpec:
// a ValueSpec is a constant or variable
// child of GenDecl
vs = n
case *ast.File:
// yes we want to recurse into the file
return true
case *ast.GenDecl:
// and we recurse into the toplevel GenDecls
// (note a GenDecl can't contain a GenDecl)
return true
default:
// don't recurse into anything else
return false
}
// foo, bar = Command{}, Command{} -> two v.Values
for i, v := range vs.Values {
// note we loop over values, so empty declarations aren't counted
if vs.Names[i].Name == "_" {
// don't count "var _ = &Command{}"
continue
}
// a Command{} is a composite literal; check for that
x, ok := v.(*ast.CompositeLit)
if !ok {
// it might be a &Command{} instead
// the & in &foo{} is an unary expression
y, ok := v.(*ast.UnaryExpr)
// (and yes the & in &foo{} is token.AND)
if !ok || y.Op != token.AND {
continue
}
// again check for Command{} (composite literal)
x, ok = y.X.(*ast.CompositeLit)
if !ok {
continue
}
}
// ok, x is a composite literal, ie foo{}.
// the foo in foo{} is an Ident
z, ok := x.Type.(*ast.Ident)
if !ok {
continue
}
if z.Name == "Command" {
// gotcha!
found++
}
}
return false
})
return found
}
type cmdCounterSuite struct{}
var _ = check.Suite(&cmdCounterSuite{})
type commandDeclCounterTableT struct {
desc string
count int
content string
}
var commandDeclCounterTable = []commandDeclCounterTableT{
{"counts top-level vars", 4, `
var won, too = &Command{}, Command{}
var tri = &Command{}
var foh = Command{}
`},
{"count top-level vars in groups", 4, `
var (
won, too = &Command{}, Command{}
tri = &Command{}
foh = Command{}
)
`},
{"does *not* count these (should it?)", 0, `
var wonP, tooP *Command
var wonD, tooD Command
var triP *Command
var triD Command
`},
{"not in groups either", 0, `
var (
wonP, tooP *Command
wonD, tooD Command
triP *Command
triD Command
)
`},
{"does not count empty decls", 0, `
var _, _ = &Command{}, Command{}
var _ = &Command{}
var _ = Command{}
`},
{"does not count empty decls in groups", 0, `
var (
_, _ = &Command{}, Command{}
_ = &Command{}
_ = Command{}
)
`},
{"does not count things built in functions", 0, `
func won() *Command {
return &Command{}
}
func too() *Command {
var x = &Command{}
return x
}
func tri() Command {
return Command{}
}
func foh() Command {
var x = Command{}
return x
}
`},
{"does not count things built in lists", 0, `
var won = []Command{{}, {}, {}}
var too = []Command{Command{}, Command{}}
var tri = []*Command{nil, nil, nil}
var foh = []*Command{{}, {}, {}}
var fai = []*Command{&Command{}, &Command{}}
`},
{"does not count things built in lists in groups", 0, `
var (
won = []Command{{}, {}, {}}
too = []Command{Command{}, Command{}}
tri = []*Command{nil, nil, nil}
foh = []*Command{{}, {}, {}}
fai = []*Command{&Command{}, &Command{}}
)
`},
}
func (cmdCounterSuite) TestCommandDeclCounter(c *check.C) {
d := c.MkDir()
for i, t := range commandDeclCounterTable {
fn := filepath.Join(d, fmt.Sprintf("a_%02d.go", i))
comm := check.Commentf(t.desc)
c.Assert(os.WriteFile(fn, []byte("package huh"+t.content), 0644), check.IsNil, comm)
n := countCommandDeclsIn(c, fn, comm)
c.Check(n, check.Equals, t.count, comm)
}
}
|