Skip to content

Commit

Permalink
Use t.Logf instead of glog in tests (kubernetes-sigs#248)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
corneliusweig authored and k8s-ci-robot committed Jul 12, 2019
1 parent fa7f844 commit 697dd32
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 9 additions & 0 deletions hack/verify-code-patterns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand All @@ -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
11 changes: 5 additions & 6 deletions integration_test/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"testing"
"time"

"github.com/golang/glog"
"github.com/pkg/errors"

"sigs.k8s.io/krew/pkg/constants"
Expand Down Expand Up @@ -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))
Expand All @@ -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
}

Expand All @@ -203,15 +202,15 @@ 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()
if err != nil {
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
}

Expand Down

0 comments on commit 697dd32

Please sign in to comment.