...

Source file src/github.com/launchdarkly/go-server-sdk/v6/testhelpers/client_context.go

Documentation: github.com/launchdarkly/go-server-sdk/v6/testhelpers

     1  package testhelpers
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
     7  	"github.com/launchdarkly/go-server-sdk/v6/internal/sharedtest"
     8  	"github.com/launchdarkly/go-server-sdk/v6/subsystems"
     9  )
    10  
    11  // Fallible is a general interface for anything with a Failed method. This is used by test helpers to
    12  // generalize between *testing.T, assert.T, etc. when all that we care about is detecting test failure.
    13  type Fallible interface {
    14  	Failed() bool
    15  }
    16  
    17  // WithMockLoggingContext creates a ClientContext that writes to a MockLogger, executes the specified
    18  // action, and then dumps the captured output to the console only if there's been a test failure.
    19  func WithMockLoggingContext(t Fallible, action func(subsystems.ClientContext)) {
    20  	mockLog := ldlogtest.NewMockLog()
    21  	context := sharedtest.NewTestContext("", nil,
    22  		&subsystems.LoggingConfiguration{Loggers: mockLog.Loggers})
    23  	defer func() {
    24  		if t.Failed() {
    25  			mockLog.Dump(os.Stdout)
    26  		}
    27  		// There's already a similar DumpLogIfTestFailed defined in the ldlogtest package, but it requires
    28  		// specifically a *testing.T.
    29  	}()
    30  	action(context)
    31  }
    32  

View as plain text