TestDebugFileSink_SurvivesAddMode is the regression test for the empty-per-test-set-file bug: AddMode rebuilds the core from scratch and the file-sink tee was being silently discarded. Without reattachDebugFileSink in AddMode, the second log record would not appear in the file.
(t *testing.T)
| 385 | // reattachDebugFileSink in AddMode, the second log record would not |
| 386 | // appear in the file. |
| 387 | func TestDebugFileSink_SurvivesAddMode(t *testing.T) { |
| 388 | SetRedactor(nil) |
| 389 | console := &syncBuffer{} |
| 390 | logger := newConsoleLogger(console, zap.InfoLevel) |
| 391 | |
| 392 | tmp, err := os.CreateTemp(t.TempDir(), "addmode-*.log") |
| 393 | if err != nil { |
| 394 | t.Fatalf("create temp: %v", err) |
| 395 | } |
| 396 | defer tmp.Close() |
| 397 | wrapped, sink := AddDebugFileSink(logger, tmp, 0) |
| 398 | SetDebugFileSink(sink) |
| 399 | defer SetDebugFileSink(nil) |
| 400 | |
| 401 | wrapped.Debug("before-addmode") |
| 402 | |
| 403 | // Simulate the agent CLI's Validate path: AddMode mutates the live |
| 404 | // logger via *logger = *new (the same pointee-overwrite pattern |
| 405 | // the codebase uses everywhere). |
| 406 | rebuilt, err := AddMode("agent") |
| 407 | if err != nil { |
| 408 | t.Fatalf("AddMode: %v", err) |
| 409 | } |
| 410 | *wrapped = *rebuilt |
| 411 | |
| 412 | wrapped.Debug("after-addmode") |
| 413 | |
| 414 | if err := sink.Flush(); err != nil { |
| 415 | t.Fatalf("flush: %v", err) |
| 416 | } |
| 417 | contents, _ := os.ReadFile(tmp.Name()) |
| 418 | got := string(contents) |
| 419 | if !strings.Contains(got, "before-addmode") { |
| 420 | t.Errorf("before-addmode missing from file: %s", got) |
| 421 | } |
| 422 | if !strings.Contains(got, "after-addmode") { |
| 423 | t.Errorf("after-addmode missing from file (the regression): %s", got) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // TestDebugFileSink_RotateAfterAddMode confirms that rotation still |
| 428 | // works after the logger has been rebuilt — the buffered+capped |
nothing calls this directly
no test coverage detected