Test_WithContextTemplateFailureOmitsPartialContext locks in the scratch-buffer guarantee from M8: a tag that writes some bytes before erroring must not leak its prefix into the real log line. Instead, the render-error marker stands in for the entire context block.
(t *testing.T)
| 276 | // leak its prefix into the real log line. Instead, the render-error marker |
| 277 | // stands in for the entire context block. |
| 278 | func Test_WithContextTemplateFailureOmitsPartialContext(t *testing.T) { |
| 279 | initDefaultLogger() |
| 280 | |
| 281 | templateErr := errors.New("template failure") |
| 282 | require.NoError(t, SetContextTemplate(ContextConfig{ |
| 283 | Format: "[${broken}] ", |
| 284 | CustomTags: map[string]ContextTagFunc{ |
| 285 | "broken": func(output Buffer, _ any, _ *ContextData, _ string) (int, error) { |
| 286 | if _, err := output.WriteString("partial"); err != nil { |
| 287 | return 0, err |
| 288 | } |
| 289 | return 0, templateErr |
| 290 | }, |
| 291 | }, |
| 292 | })) |
| 293 | t.Cleanup(func() { MustSetContextTemplate(ContextConfig{}) }) |
| 294 | |
| 295 | var w byteSliceWriter |
| 296 | SetOutput(&w) |
| 297 | |
| 298 | WithContext(context.Background()).Info("start") |
| 299 | |
| 300 | out := string(w.b) |
| 301 | require.NotContains(t, out, "partial", "scratch buffer must isolate partial writes from the live log line") |
| 302 | require.Contains(t, out, "ctx-render-error", "render error must be surfaced rather than silently dropped") |
| 303 | require.Contains(t, out, "start", "log payload must still reach the writer") |
| 304 | } |
| 305 | |
| 306 | func Test_LogfKeyAndValues(t *testing.T) { |
| 307 | tests := []struct { |
nothing calls this directly
no test coverage detected