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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
// Copyright (c) 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
package lint
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"go/types"
"io/ioutil"
"path"
"regexp"
"strconv"
"strings"
"testing"
)
var lintMatch = flag.String("lint.match", "", "restrict testdata matches to this pattern")
func TestAll(t *testing.T) {
l := new(Linter)
rx, err := regexp.Compile(*lintMatch)
if err != nil {
t.Fatalf("Bad -lint.match value %q: %v", *lintMatch, err)
}
baseDir := "testdata"
fis, err := ioutil.ReadDir(baseDir)
if err != nil {
t.Fatalf("ioutil.ReadDir: %v", err)
}
if len(fis) == 0 {
t.Fatalf("no files in %v", baseDir)
}
for _, fi := range fis {
if !rx.MatchString(fi.Name()) {
continue
}
//t.Logf("Testing %s", fi.Name())
src, err := ioutil.ReadFile(path.Join(baseDir, fi.Name()))
if err != nil {
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
}
ins := parseInstructions(t, fi.Name(), src)
if ins == nil {
t.Errorf("Test file %v does not have instructions", fi.Name())
continue
}
ps, err := l.Lint(fi.Name(), src)
if err != nil {
t.Errorf("Linting %s: %v", fi.Name(), err)
continue
}
for _, in := range ins {
ok := false
for i, p := range ps {
if p.Position.Line != in.Line {
continue
}
if in.Match.MatchString(p.Text) {
// check replacement if we are expecting one
if in.Replacement != "" {
// ignore any inline comments, since that would be recursive
r := p.ReplacementLine
if i := strings.Index(r, " //"); i >= 0 {
r = r[:i]
}
if r != in.Replacement {
t.Errorf("Lint failed at %s:%d; got replacement %q, want %q", fi.Name(), in.Line, r, in.Replacement)
}
}
// remove this problem from ps
copy(ps[i:], ps[i+1:])
ps = ps[:len(ps)-1]
//t.Logf("/%v/ matched at %s:%d", in.Match, fi.Name(), in.Line)
ok = true
break
}
}
if !ok {
t.Errorf("Lint failed at %s:%d; /%v/ did not match", fi.Name(), in.Line, in.Match)
}
}
for _, p := range ps {
t.Errorf("Unexpected problem at %s:%d: %v", fi.Name(), p.Position.Line, p.Text)
}
}
}
type instruction struct {
Line int // the line number this applies to
Match *regexp.Regexp // what pattern to match
Replacement string // what the suggested replacement line should be
}
// parseInstructions parses instructions from the comments in a Go source file.
// It returns nil if none were parsed.
func parseInstructions(t *testing.T, filename string, src []byte) []instruction {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
t.Fatalf("Test file %v does not parse: %v", filename, err)
}
var ins []instruction
for _, cg := range f.Comments {
ln := fset.Position(cg.Pos()).Line
raw := cg.Text()
for _, line := range strings.Split(raw, "\n") {
if line == "" || strings.HasPrefix(line, "#") {
continue
}
if line == "OK" && ins == nil {
// so our return value will be non-nil
ins = make([]instruction, 0)
continue
}
if strings.Contains(line, "MATCH") {
rx, err := extractPattern(line)
if err != nil {
t.Fatalf("At %v:%d: %v", filename, ln, err)
}
matchLine := ln
if i := strings.Index(line, "MATCH:"); i >= 0 {
// This is a match for a different line.
lns := strings.TrimPrefix(line[i:], "MATCH:")
lns = lns[:strings.Index(lns, " ")]
matchLine, err = strconv.Atoi(lns)
if err != nil {
t.Fatalf("Bad match line number %q at %v:%d: %v", lns, filename, ln, err)
}
}
var repl string
if r, ok := extractReplacement(line); ok {
repl = r
}
ins = append(ins, instruction{
Line: matchLine,
Match: rx,
Replacement: repl,
})
}
}
}
return ins
}
func extractPattern(line string) (*regexp.Regexp, error) {
a, b := strings.Index(line, "/"), strings.LastIndex(line, "/")
if a == -1 || a == b {
return nil, fmt.Errorf("malformed match instruction %q", line)
}
pat := line[a+1 : b]
rx, err := regexp.Compile(pat)
if err != nil {
return nil, fmt.Errorf("bad match pattern %q: %v", pat, err)
}
return rx, nil
}
func extractReplacement(line string) (string, bool) {
// Look for this: / -> `
// (the end of a match and start of a backtick string),
// and then the closing backtick.
const start = "/ -> `"
a, b := strings.Index(line, start), strings.LastIndex(line, "`")
if a < 0 || a > b {
return "", false
}
return line[a+len(start) : b], true
}
func render(fset *token.FileSet, x interface{}) string {
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, x); err != nil {
panic(err)
}
return buf.String()
}
func TestLine(t *testing.T) {
tests := []struct {
src string
offset int
want string
}{
{"single line file", 5, "single line file"},
{"single line file with newline\n", 5, "single line file with newline\n"},
{"first\nsecond\nthird\n", 2, "first\n"},
{"first\nsecond\nthird\n", 9, "second\n"},
{"first\nsecond\nthird\n", 14, "third\n"},
{"first\nsecond\nthird with no newline", 16, "third with no newline"},
{"first byte\n", 0, "first byte\n"},
}
for _, test := range tests {
got := srcLine([]byte(test.src), token.Position{Offset: test.offset})
if got != test.want {
t.Errorf("srcLine(%q, offset=%d) = %q, want %q", test.src, test.offset, got, test.want)
}
}
}
func TestLintName(t *testing.T) {
tests := []struct {
name, want string
}{
{"foo_bar", "fooBar"},
{"foo_bar_baz", "fooBarBaz"},
{"Foo_bar", "FooBar"},
{"foo_WiFi", "fooWiFi"},
{"id", "id"},
{"Id", "ID"},
{"foo_id", "fooID"},
{"fooId", "fooID"},
{"fooUid", "fooUID"},
{"idFoo", "idFoo"},
{"uidFoo", "uidFoo"},
{"midIdDle", "midIDDle"},
{"APIProxy", "APIProxy"},
{"ApiProxy", "APIProxy"},
{"apiProxy", "apiProxy"},
{"_Leading", "_Leading"},
{"___Leading", "_Leading"},
{"trailing_", "trailing"},
{"trailing___", "trailing"},
{"a_b", "aB"},
{"a__b", "aB"},
{"a___b", "aB"},
{"Rpc1150", "RPC1150"},
{"case3_1", "case3_1"},
{"case3__1", "case3_1"},
{"IEEE802_16bit", "IEEE802_16bit"},
{"IEEE802_16Bit", "IEEE802_16Bit"},
}
for _, test := range tests {
got := lintName(test.name)
if got != test.want {
t.Errorf("lintName(%q) = %q, want %q", test.name, got, test.want)
}
}
}
func TestExportedType(t *testing.T) {
tests := []struct {
typString string
exp bool
}{
{"int", true},
{"string", false}, // references the shadowed builtin "string"
{"T", true},
{"t", false},
{"*T", true},
{"*t", false},
{"map[int]complex128", true},
}
for _, test := range tests {
src := `package foo; type T int; type t int; type string struct{}`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "foo.go", src, 0)
if err != nil {
t.Fatalf("Parsing %q: %v", src, err)
}
// use the package name as package path
config := &types.Config{}
pkg, err := config.Check(file.Name.Name, fset, []*ast.File{file}, nil)
if err != nil {
t.Fatalf("Type checking %q: %v", src, err)
}
tv, err := types.Eval(fset, pkg, token.NoPos, test.typString)
if err != nil {
t.Errorf("types.Eval(%q): %v", test.typString, err)
continue
}
if got := exportedType(tv.Type); got != test.exp {
t.Errorf("exportedType(%v) = %t, want %t", tv.Type, got, test.exp)
}
}
}
func TestIsGenerated(t *testing.T) {
tests := []struct {
source string
generated bool
}{
{"// Code Generated by some tool. DO NOT EDIT.", false},
{"// Code generated by some tool. DO NOT EDIT.", true},
{"// Code generated by some tool. DO NOT EDIT", false},
{"// Code generated DO NOT EDIT.", true},
{"// Code generated DO NOT EDIT.", false},
{"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false},
{"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true},
{"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true},
{"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false},
{"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false},
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true},
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true},
}
for i, test := range tests {
got := isGenerated([]byte(test.source))
if got != test.generated {
t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated)
}
}
}
|