mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
fix(testutil): ensure FakeSink does not swallow logs (#25185)
`FakeSink` was silently capturing log entries without forwarding them to `testing.TB.Log`. This made debugging test failures harder because logs were invisible in `go test -v` output. Store `testing.TB` in `FakeSink` and call `t.Log` on each entry, guarded by a check to avoid logging after the test has finished. Split out from #25012. > 🤖 Generated with [Coder Agents](https://coder.com)
This commit is contained in:
+15
-3
@@ -12,21 +12,33 @@ import (
|
||||
// tests can assert on what was logged. It requires a testing.TB
|
||||
// as it is only meant for use in tests.
|
||||
type FakeSink struct {
|
||||
t testing.TB
|
||||
mu sync.RWMutex
|
||||
entries []slog.SinkEntry
|
||||
tDone bool
|
||||
}
|
||||
|
||||
// NewFakeSink returns a FakeSink ready for use.
|
||||
func NewFakeSink(_ testing.TB) *FakeSink {
|
||||
return &FakeSink{}
|
||||
func NewFakeSink(t testing.TB) *FakeSink {
|
||||
fs := &FakeSink{t: t}
|
||||
t.Cleanup(func() {
|
||||
fs.mu.Lock()
|
||||
fs.tDone = true
|
||||
fs.mu.Unlock()
|
||||
})
|
||||
return fs
|
||||
}
|
||||
|
||||
// LogEntry implements slog.Sink. It appends the entry to the
|
||||
// internal slice.
|
||||
func (s *FakeSink) LogEntry(_ context.Context, e slog.SinkEntry) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.entries = append(s.entries, e)
|
||||
shouldLog := !s.tDone
|
||||
s.mu.Unlock()
|
||||
if shouldLog {
|
||||
s.t.Log(e.Message, e.Fields)
|
||||
}
|
||||
}
|
||||
|
||||
// Sync implements slog.Sink.
|
||||
|
||||
Reference in New Issue
Block a user