...
1 package testutil_test
2
3 import (
4 "testing"
5
6 "github.com/cybertec-postgresql/pgwatch/v6/internal/log"
7 "github.com/cybertec-postgresql/pgwatch/v6/internal/testutil"
8 "github.com/sirupsen/logrus"
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestNewTestLogger(t *testing.T) {
13 t.Run("captures output at configured level", func(t *testing.T) {
14 ctx, out := testutil.NewTestLogger(t, logrus.WarnLevel)
15 log.GetLogger(ctx).Warn("hello warning")
16 assert.Contains(t, out.String(), "hello warning")
17 })
18
19 t.Run("suppresses output below configured level", func(t *testing.T) {
20 ctx, out := testutil.NewTestLogger(t, logrus.WarnLevel)
21 log.GetLogger(ctx).Debug("debug noise")
22 assert.Empty(t, out.String())
23 })
24
25 t.Run("returns non-nil logger in context", func(t *testing.T) {
26 ctx, _ := testutil.NewTestLogger(t, logrus.InfoLevel)
27 assert.NotNil(t, log.GetLogger(ctx))
28 })
29
30 t.Run("each call returns independent buffer", func(t *testing.T) {
31 ctx1, out1 := testutil.NewTestLogger(t, logrus.InfoLevel)
32 _, out2 := testutil.NewTestLogger(t, logrus.InfoLevel)
33 log.GetLogger(ctx1).Info("only in one")
34 assert.Contains(t, out1.String(), "only in one")
35 assert.Empty(t, out2.String())
36 })
37 }
38