diff options
author | 2020-01-28 19:07:11 +0000 | |
---|---|---|
committer | 2020-01-28 19:07:11 +0000 | |
commit | f66c2bac25d6367e9053a68d137a5fad45a4c386 (patch) | |
tree | cd155225a60a2799c218f62ae4b4b8134d7d1796 /test | |
parent | 40d0fd8598e6d8c824491ba9cb5119c9f07b56d6 (diff) | |
download | coredns-f66c2bac25d6367e9053a68d137a5fad45a4c386.tar.gz coredns-f66c2bac25d6367e9053a68d137a5fad45a4c386.tar.zst coredns-f66c2bac25d6367e9053a68d137a5fad45a4c386.zip |
Remove all shell presubmits (#3631)
This ports the shell presubmit to Go and makes them better; esp the
Errorf/Logf/Fatalf one because it actually parses the code and works of
the AST.
These error will now show up in the travis/circle CI runs where they
belong.
Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'test')
-rw-r--r-- | test/presubmit_test.go | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/test/presubmit_test.go b/test/presubmit_test.go index 392cbe5fb..8243c4c0d 100644 --- a/test/presubmit_test.go +++ b/test/presubmit_test.go @@ -5,6 +5,9 @@ package test import ( "bufio" "fmt" + "go/ast" + "go/parser" + "go/token" "os" "path/filepath" "strings" @@ -75,3 +78,133 @@ func hasHyphen(path string, info os.FileInfo, _ error) error { return nil } + +// Test if error messages start with an upper case. +func TestLowercaseLog(t *testing.T) { + err := filepath.Walk("..", hasLowercase) + if err != nil { + t.Fatal(err) + } +} + +func hasLowercase(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.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 { + return 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) { + l.err = fmt.Errorf("test error message %s doesn't start with an uppercase", bl.Value) + return nil + } + if i == 1 { + break + } + } + return l +} + +func TestImportTesting(t *testing.T) { + err := filepath.Walk("..", hasImportTesting) + if err != nil { + t.Fatal(err) + } +} + +func hasImportTesting(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.HasSuffix(path, "_test.go") { + return nil + } + + if strings.HasSuffix(path, ".go") { + fs := token.NewFileSet() + f, err := parser.ParseFile(fs, path, nil, parser.AllErrors) + if err != nil { + return err + } + for _, im := range f.Imports { + if im.Path.Value == `"testing"` { + return fmt.Errorf("file %q is importing %q", path, "testing") + } + } + } + return nil +} |