Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/syncwriter/syncwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func New(w io.Writer) *Writer {
w: w,

errorf: func(f string, v ...interface{}) {
println(fmt.Sprintf(f, v...))
// Avoid the builtin println, which writes to fd 2
// unsynchronized and can corrupt concurrent go test output.
fmt.Fprintf(os.Stderr, f+"\n", v...)
},
}
}
Expand Down
29 changes: 29 additions & 0 deletions internal/syncwriter/syncwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package syncwriter
import (
"io"
"os"
"strings"
"testing"

"cdr.dev/slog/v3/internal/assert"
Expand Down Expand Up @@ -74,6 +75,34 @@ func TestWriter_Sync(t *testing.T) {
})
}

// The default handler must report through the os.Stderr variable, not the
// builtin println that bypasses it. Reverting to println fails this test.
//
// Not parallel: it swaps the process-wide os.Stderr.
func TestWriter_defaultErrorReportsThroughStderr(t *testing.T) {
r, w, err := os.Pipe()
assert.Success(t, "pipe", err)

tw := New(syncWriter{
wf: func([]byte) (int, error) { return 0, io.EOF },
sf: func() error { return nil },
})

orig := os.Stderr
os.Stderr = w
tw.Write("sinkname", []byte("entry"))
os.Stderr = orig
assert.Success(t, "close", w.Close())

out, err := io.ReadAll(r)
assert.Success(t, "read", err)

got := string(out)
assert.True(t, "reported via stderr", strings.Contains(got, "sinkname: failed to write entry"))
assert.True(t, "single trailing newline", strings.HasSuffix(got, "\n"))
assert.Equal(t, "newline count", 1, strings.Count(got, "\n"))
}

func Test_errorsIsAny(t *testing.T) {
t.Parallel()

Expand Down
Loading