@@ -12,8 +12,10 @@ import (
1212 "errors"
1313 "flag"
1414 "fmt"
15+ "os/exec"
1516 "regexp"
1617 "runtime"
18+ "strings"
1719 "sync"
1820 "testing"
1921 "time"
@@ -136,9 +138,36 @@ func TestInfo(t *testing.T) {
136138 }
137139}
138140
139- func TestCallDepth (t * testing.T ) {
140- logger := ktesting .NewLogger (t , ktesting .NewConfig ())
141+ // TestCallDepthOutput produces output which gets checked by TestCallDepthVerify.
142+ func TestCallDepthOutput (t * testing.T ) {
143+ logger := ktesting .NewLogger (t , ktesting .DefaultConfig )
141144 logger .Info ("hello world" )
145+ logger .V (1 ).Info ("you shouldn't see this with -testing.v=0" )
146+ callDepthHelper (logger .V (1 ), "you should see this with -testing.v=0 -testing.vmodule=helper_test=1" )
147+ }
148+
149+ // TestCallDepthVerify runs TestCallDepth with appropriate flags and checks the output.
150+ //
151+ // Invoking `go test` is necessary because we want to verify that it correctly unwinds the stack,
152+ // which means that we have to let it print output. We cannot intercept that log output from
153+ // within a test.
154+ func TestCallDepthVerify (t * testing.T ) {
155+ cmd , args := "go" , "test -v -run=TestCallDepthOutput k8s.io/klog/v2/ktesting -args -testing.v=0 -testing.vmodule=helper_test=1"
156+ output , err := exec .Command ("go" , strings .Split (args , " " )... ).CombinedOutput ()
157+ if err != nil {
158+ t .Fatalf ("failed to %s %s: %v" , cmd , args , err )
159+ }
160+ t .Log ("Got output:\n " , string (output ))
161+
162+ expect := `(?m)^[[:space:]]*testinglogger_test.go:.*hello world$
163+ ^[[:space:]]*testinglogger_test.go:.*you should see this`
164+ matched , err := regexp .MatchString (expect , string (output ))
165+ if err != nil {
166+ t .Fatalf ("failed to parse regexp: %v\n \n %s" , err , expect )
167+ }
168+ if ! matched {
169+ t .Fatalf ("expected output\n %s\n \n to match regexp\n %s" , string (output ), expect )
170+ }
142171}
143172
144173type logToBuf struct {
0 commit comments