mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
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>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user