Documentation
¶
Overview ¶
Package plugintest provides helpers for testing LogLayer plugins.
Use Install to wire a plugin into a fresh logger backed by the in-memory testing transport, then drive scenarios through the logger and assert against the captured transports/testing.LogLine entries.
Use AssertNoMutation to verify a plugin's input-side hook (FieldsHook / MetadataHook) doesn't mutate caller-owned input, which is the contract the framework expects.
Use AssertPanicRecovered to verify the framework recovers a panicking hook and surfaces it via the plugin's [loglayer.ErrorReporter] as a *loglayer.RecoveredPanicError.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertNoMutation ¶
AssertNoMutation verifies that running an input-side hook does not mutate the caller-owned input. It deep-clones the input, runs the hook, and reports a test failure if the original differs from the clone afterward.
Use it for [loglayer.FieldsHook] and [loglayer.MetadataHook] implementations. The hook function is passed in directly rather than via a Plugin, so the test focuses on hook behavior without needing to install or emit through a logger.
red := redact.New(redact.Config{Keys: []string{"password"}})
plugintest.AssertNoMutation(t,
red.(loglayer.MetadataHook).OnMetadataCalled,
map[string]any{"password": "hunter2", "user": "alice"})
func AssertPanicRecovered ¶
func AssertPanicRecovered( t testing.TB, build func(captureFn func(error)) loglayer.Plugin, emit func(*loglayer.LogLayer), ) *loglayer.RecoveredPanicError
AssertPanicRecovered drives a panicking hook through a real *loglayer.LogLayer and asserts the framework caught the panic and forwarded a *loglayer.RecoveredPanicError to the plugin's [loglayer.ErrorReporter].
build receives a captureFn: thread it through to your plugin's OnError (whether that's a Config field or a method) so the framework's recovery path delivers the panic to it. emit drives the operation that triggers the panicking hook.
rpe := plugintest.AssertPanicRecovered(t,
func(captureFn func(error)) loglayer.Plugin {
return myplugin.New(myplugin.Config{
// ... whatever causes a panic in a hook ...
OnError: captureFn,
})
},
func(log *loglayer.LogLayer) { log.Info("trigger") },
)
Returns the captured *RecoveredPanicError so callers can assert on Hook and Value.
func Install ¶
func Install(t testing.TB, p loglayer.Plugin) (*loglayer.LogLayer, *lltest.TestLoggingLibrary)
Install builds a fresh *loglayer.LogLayer with p installed and the in-memory testing transport attached. Returns the logger and the capture library; drive arbitrary scenarios through the logger and pop entries from the library.
log, lib := plugintest.Install(t, myplugin.New(...))
log.WithMetadata(loglayer.Metadata{"k": "v"}).Info("event")
line := lib.PopLine()
if line.Data["enriched"] != "yes" { ... }
DisableFatalExit is set so log.Fatal() doesn't terminate the test process.
Types ¶
This section is empty.