forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresubmit_test.go
327 lines (292 loc) · 7.08 KB
/
presubmit_test.go
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
318
319
320
321
322
323
324
325
326
327
package test
// These tests check for meta level items, like trailing whitespace, correct file naming etc.
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"unicode"
)
func TestFileNameHyphen(t *testing.T) {
walker := hasHyphenWalker{}
err := filepath.Walk("..", walker.walk)
if err != nil {
t.Fatal(err)
}
if len(walker.Errors) > 0 {
for _, err = range walker.Errors {
t.Error(err)
}
}
}
type hasHyphenWalker struct {
Errors []error
}
func (w *hasHyphenWalker) walk(path string, info os.FileInfo, _ error) error {
// only for regular files, not starting with a . and those that are go files.
if !info.Mode().IsRegular() {
return nil
}
if strings.HasPrefix(path, "../.") {
return nil
}
if strings.Contains(path, "/vendor") {
return nil
}
if filepath.Ext(path) != ".go" {
return nil
}
if strings.Index(path, "-") > 0 {
absPath, _ := filepath.Abs(path)
w.Errors = append(w.Errors, fmt.Errorf("file %q has a hyphen, please use underscores in file names", absPath))
}
return nil
}
// Test if error messages start with an upper case.
func TestLowercaseLog(t *testing.T) {
walker := hasLowercaseWalker{}
err := filepath.Walk("..", walker.walk)
if err != nil {
t.Fatal(err)
}
if len(walker.Errors) > 0 {
for _, err = range walker.Errors {
t.Error(err)
}
}
}
type hasLowercaseWalker struct {
Errors []error
}
func (w *hasLowercaseWalker) walk(path string, info os.FileInfo, _ error) error {
// only for regular files, not starting with a . and those that are go files.
if !info.Mode().IsRegular() {
return nil
}
if strings.HasPrefix(path, "../.") {
return nil
}
if strings.Contains(path, "/vendor") {
return nil
}
if !strings.HasSuffix(path, "_test.go") {
return nil
}
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, path, nil, parser.AllErrors)
if err != nil {
return err
}
l := &logfmt{}
ast.Walk(l, f)
if l.err != nil {
w.Errors = append(w.Errors, l.err)
}
return nil
}
type logfmt struct {
err error
}
func (l logfmt) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
ce, ok := n.(*ast.CallExpr)
if !ok {
return l
}
se, ok := ce.Fun.(*ast.SelectorExpr)
if !ok {
return l
}
id, ok := se.X.(*ast.Ident)
if !ok {
return l
}
if id.Name != "t" { // t *testing.T
return l
}
switch se.Sel.Name {
case "Errorf":
case "Logf":
case "Log":
case "Fatalf":
case "Fatal":
default:
return l
}
// Check first arg, that should have basic lit with capital
if len(ce.Args) < 1 {
return l
}
bl, ok := ce.Args[0].(*ast.BasicLit)
if !ok {
return l
}
if bl.Kind != token.STRING {
return l
}
if strings.HasPrefix(bl.Value, "\"%s") || strings.HasPrefix(bl.Value, "\"%d") {
return l
}
if strings.HasPrefix(bl.Value, "\"%v") || strings.HasPrefix(bl.Value, "\"%+v") {
return l
}
for i, u := range bl.Value {
// disregard "
if i == 1 && !unicode.IsUpper(u) {
return nil
}
if i == 1 {
break
}
}
return l
}
func TestImportTesting(t *testing.T) {
walker := hasLowercaseWalker{}
err := filepath.Walk("..", walker.walk)
if err != nil {
t.Fatal(err)
}
if len(walker.Errors) > 0 {
for _, err = range walker.Errors {
t.Error(err)
}
}
}
func TestImportOrdering(t *testing.T) {
walker := testImportOrderingWalker{}
err := filepath.Walk("..", walker.walk)
if err != nil {
t.Fatal(err)
}
if len(walker.Errors) > 0 {
for _, err = range walker.Errors {
t.Error(err)
}
}
}
type testImportOrderingWalker struct {
Errors []error
}
func (w *testImportOrderingWalker) walk(path string, info os.FileInfo, _ error) error {
if !info.Mode().IsRegular() {
return nil
}
if strings.HasPrefix(path, "../.") {
return nil
}
if strings.Contains(path, "/vendor") {
return nil
}
if filepath.Ext(path) != ".go" {
return nil
}
// pb files are autogenerated by protoc
if strings.HasPrefix(path, "../pb/") {
return nil
}
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, path, nil, parser.AllErrors)
if err != nil {
return err
}
if len(f.Imports) == 0 {
return nil
}
// 3 blocks total, if
// 3 blocks: std + coredns + 3rd party
// 2 blocks: std + coredns, std + 3rd party, coredns + 3rd party
// 1 block: std, coredns, 3rd party
// first entry in a block specifies the type (std, coredns, 3rd party)
// we want: std, coredns, 3rd party
// more than 3 blocks as an error
blocks := [3][]*ast.ImportSpec{}
prevpos := 0
bl := 0
for _, im := range f.Imports {
line := fs.Position(im.Path.Pos()).Line
if line-prevpos > 1 && prevpos > 0 {
bl++
}
if bl > 2 {
absPath, _ := filepath.Abs(path)
w.Errors = append(w.Errors, fmt.Errorf("more than %d import blocks in %q", bl, absPath))
}
blocks[bl] = append(blocks[bl], im)
prevpos = line
}
// if it:
// contains strings github.com/coredns -> coredns
// contains dots -> 3rd
// no dots -> std
ip := [3]string{} // type per block, just string, either std, coredns, 3rd
for i := 0; i <= bl; i++ {
ip[i] = importtype(blocks[i][0].Path.Value)
}
// Ok, now that we have the type, let's see if all members adhere to it.
// After that we check if they are in the right order.
for i := 0; i <= bl; i++ {
for _, p := range blocks[i] {
t := importtype(p.Path.Value)
if t != ip[i] {
absPath, _ := filepath.Abs(path)
w.Errors = append(w.Errors, fmt.Errorf("import path for %s is not of the same type %q in %q", p.Path.Value, ip[i], absPath))
}
}
}
// check order
switch bl {
case 0:
// we don't care
case 1:
if ip[0] == "std" && ip[1] == "coredns" {
break // OK
}
if ip[0] == "std" && ip[1] == "3rd" {
break // OK
}
if ip[0] == "coredns" && ip[1] == "3rd" {
break // OK
}
absPath, _ := filepath.Abs(path)
w.Errors = append(w.Errors, fmt.Errorf("import path in %q are not in the right order (std -> coredns -> 3rd)", absPath))
case 2:
if ip[0] == "std" && ip[1] == "coredns" && ip[2] == "3rd" {
break // OK
}
absPath, _ := filepath.Abs(path)
w.Errors = append(w.Errors, fmt.Errorf("import path in %q are not in the right order (std -> coredns -> 3rd)", absPath))
}
return nil
}
func importtype(s string) string {
if strings.Contains(s, "github.com/coredns") {
return "coredns"
}
if strings.Contains(s, ".") {
return "3rd"
}
return "std"
}
// TestMetricNaming tests the imports path used for metrics. It depends on faillint to be installed: go install github.com/fatih/faillint
func TestPrometheusImports(t *testing.T) {
if _, err := exec.LookPath("faillint"); err != nil {
fmt.Fprintf(os.Stderr, "Not executing TestPrometheusImports: faillint not found\n")
return
}
// make this multiline?
p := `github.com/prometheus/client_golang/prometheus.{NewCounter,NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}=github.com/prometheus/client_golang/prometheus/promauto.{NewCounter,NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}`
cmd := exec.Command("faillint", "-paths", p, "./...")
cmd.Dir = ".."
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed: %s\n%s", err, out)
}
}