From 697dd320434781b71de60873dfb0819f80c6e917 Mon Sep 17 00:00:00 2001 From: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> Date: Fri, 12 Jul 2019 22:51:05 +0200 Subject: [PATCH] Use `t.Logf` instead of `glog` in tests (#248) * Add code pattern test to ensure glog is not used in tests * Use t.Log instead of glog in tests * Search for the actual function and not for the import when checking code patterns --- hack/verify-code-patterns.sh | 9 +++++++++ integration_test/testutil_test.go | 11 +++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/hack/verify-code-patterns.sh b/hack/verify-code-patterns.sh index 13563967..73701b37 100755 --- a/hack/verify-code-patterns.sh +++ b/hack/verify-code-patterns.sh @@ -24,6 +24,7 @@ if [[ -n "$out" ]]; then exit 1 fi +# use code constant for ".yaml" out="$(grep --include '*.go' \ --exclude "*_test.go" \ --exclude 'constants.go' \ @@ -34,3 +35,11 @@ if [[ -n "$out" ]]; then echo >&2 "$out" exit 1 fi + +# Do not use glog in test code +out="$(grep --include '*_test.go' --exclude-dir 'vendor/' -EIrn '[kg]log\.' || true)" +if [[ -n "$out" ]]; then + echo >&2 "You used glog in tests, use 't.Logf' instead:" + echo >&2 "$out" + exit 1 +fi diff --git a/integration_test/testutil_test.go b/integration_test/testutil_test.go index 442f079e..cad4e0c7 100644 --- a/integration_test/testutil_test.go +++ b/integration_test/testutil_test.go @@ -27,7 +27,6 @@ import ( "testing" "time" - "github.com/golang/glog" "github.com/pkg/errors" "sigs.k8s.io/krew/pkg/constants" @@ -166,7 +165,7 @@ func (it *ITest) WithIndex() *ITest { // WithEnv sets an environment variable for the krew run. func (it *ITest) WithEnv(key string, value interface{}) *ITest { if key == "KREW_ROOT" { - glog.V(1).Infoln("Overriding KREW_ROOT in tests is forbidden") + it.t.Fatal("Overriding KREW_ROOT in tests is forbidden") return it } it.env = append(it.env, fmt.Sprintf("%s=%v", key, value)) @@ -186,14 +185,14 @@ func (it *ITest) Run() error { it.t.Helper() cmd := it.cmd(context.Background()) - glog.V(1).Infoln(cmd.Args) + it.t.Log(cmd.Args) start := time.Now() if err := cmd.Run(); err != nil { return errors.Wrapf(err, "krew %v", it.args) } - glog.V(1).Infoln("Ran in", time.Since(start)) + it.t.Log("Ran in", time.Since(start)) return nil } @@ -203,7 +202,7 @@ func (it *ITest) RunOrFailOutput() []byte { it.t.Helper() cmd := it.cmd(context.Background()) - glog.V(1).Infoln(cmd.Args) + it.t.Log(cmd.Args) start := time.Now() out, err := cmd.CombinedOutput() @@ -211,7 +210,7 @@ func (it *ITest) RunOrFailOutput() []byte { it.t.Fatalf("krew %v: %v, %s", it.args, err, out) } - glog.V(1).Infoln("Ran in", time.Since(start)) + it.t.Log("Ran in", time.Since(start)) return out }