aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2020-01-28 19:07:11 +0000
committerGravatar GitHub <noreply@github.com> 2020-01-28 19:07:11 +0000
commitf66c2bac25d6367e9053a68d137a5fad45a4c386 (patch)
treecd155225a60a2799c218f62ae4b4b8134d7d1796
parent40d0fd8598e6d8c824491ba9cb5119c9f07b56d6 (diff)
downloadcoredns-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>
-rwxr-xr-x.presubmit/import-testing9
-rwxr-xr-x.presubmit/test-lowercase19
-rw-r--r--Makefile8
-rw-r--r--test/presubmit_test.go133
4 files changed, 134 insertions, 35 deletions
diff --git a/.presubmit/import-testing b/.presubmit/import-testing
deleted file mode 100755
index ad33bda90..000000000
--- a/.presubmit/import-testing
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/usr/bin/env bash
-
-echo "** presubmit/$(basename $0)"
-
-# Check to make sure "testing" is not imported
-if [[ $(go list -f '{{ join .Deps " "}}') == *" testing "* ]]; then
- echo "** presubmit/$(basename $0): please remove any 'import \"testing\"'"
- exit 1
-fi
diff --git a/.presubmit/test-lowercase b/.presubmit/test-lowercase
deleted file mode 100755
index 8eaadb514..000000000
--- a/.presubmit/test-lowercase
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env bash
-
-echo "** presubmit/$(basename $0)"
-
-# Get the tests that call t.* without capitalizing the first char - seems we standardized on that.
-if egrep -r '\bt\.Fatal.?\("[a-z]' "$@"; then
- echo "** presubmit/$(basename $0): please start with an upper case letter when using t.Fatal*()"
- exit 1
-fi
-
-if egrep -r '\bt\.Error.?\("[a-z]' "$@"; then
- echo "** presubmit/$(basename $0): please start with an upper case letter when using t.Error*()"
- exit 1
-fi
-
-if egrep -r '\bt\.Log.?\("[a-z]' "$@"; then
- echo "** presubmit/$(basename $0): please start with an upper case letter when using t.Log*()"
- exit 1
-fi
diff --git a/Makefile b/Makefile
index e82ffc7e9..b07ace86f 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,6 @@ SYSTEM:=
CHECKS:=check
BUILDOPTS:=-v
GOPATH?=$(HOME)/go
-PRESUBMIT:=core coremain plugin test request
MAKEPWD:=$(dir $(realpath $(firstword $(MAKEFILE_LIST))))
CGO_ENABLED:=0
@@ -17,7 +16,7 @@ coredns: $(CHECKS)
CGO_ENABLED=$(CGO_ENABLED) $(SYSTEM) go build $(BUILDOPTS) -ldflags="-s -w -X github.com/coredns/coredns/coremain.GitCommit=$(GITCOMMIT)" -o $(BINARY)
.PHONY: check
-check: presubmit core/plugin/zplugin.go core/dnsserver/zdirectives.go
+check: core/plugin/zplugin.go core/dnsserver/zdirectives.go
.PHONY: travis
travis:
@@ -69,11 +68,6 @@ gen:
pb:
$(MAKE) -C pb
-# Presubmit runs all scripts in .presubmit; any non 0 exit code will fail the build.
-.PHONY: presubmit
-presubmit:
- @for pre in $(MAKEPWD)/.presubmit/* ; do "$$pre" $(PRESUBMIT) || exit 1 ; done
-
.PHONY: clean
clean:
go clean
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
+}