Skip to content

Commit

Permalink
klogr: use test.InitKlog
Browse files Browse the repository at this point in the history
Besides sharing code, it's also better in two regards:
- restores state after test
- avoids triple printing of errors
  • Loading branch information
pohly authored and dims committed Jan 19, 2023
1 parent 125ecfe commit 34a9807
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions klogr/klogr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"encoding/json"
"errors"
"flag"
"strings"
"testing"

"k8s.io/klog/v2"
"k8s.io/klog/v2/test"

"github.com/go-logr/logr"
)
Expand Down Expand Up @@ -169,14 +169,9 @@ func testOutput(t *testing.T, format string) {
klogr: new().V(0),
text: "test",
err: errors.New("whoops"),
// The message is printed to three different log files (info, warning, error), so we see it three times in our output buffer.
expectedOutput: ` "msg"="test" "error"="whoops"
"msg"="test" "error"="whoops"
"msg"="test" "error"="whoops"
`,
expectedKlogOutput: `"test" err="whoops"
"test" err="whoops"
"test" err="whoops"
`,
},
}
Expand Down Expand Up @@ -209,13 +204,8 @@ func testOutput(t *testing.T, format string) {
}

func TestOutput(t *testing.T) {
klog.InitFlags(nil)
flag.CommandLine.Set("v", "10")
flag.CommandLine.Set("skip_headers", "true")
flag.CommandLine.Set("logtostderr", "false")
flag.CommandLine.Set("alsologtostderr", "false")
flag.CommandLine.Set("stderrthreshold", "10")
flag.Parse()
fs := test.InitKlog(t)
fs.Set("skip_headers", "true")

formats := []string{
formatNew,
Expand Down

0 comments on commit 34a9807

Please sign in to comment.