-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging_test.go
54 lines (40 loc) · 955 Bytes
/
logging_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package fauna_test
import (
"io"
"os"
"testing"
"github.com/fauna/fauna-go/v3"
"github.com/stretchr/testify/require"
)
func pipeStdOut(handler func()) ([]byte, error) {
storeStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
handler()
_ = w.Close()
out, _ := io.ReadAll(r)
os.Stdout = storeStdout
return out, nil
}
func TestLogger(t *testing.T) {
t.Run("should not log by default", func(t *testing.T) {
out, outErr := pipeStdOut(func() {
logger := fauna.DefaultLogger()
logger.Info("testing")
})
require.NoError(t, outErr)
require.Empty(t, string(out))
})
t.Run("should write to stdout", func(t *testing.T) {
logMessage := "now you see me"
out, outErr := pipeStdOut(func() {
t.Setenv("FAUNA_DEBUG", "0")
logger := fauna.DefaultLogger()
logger.Info(logMessage)
})
require.NoError(t, outErr)
outStr := string(out)
require.Contains(t, outStr, logMessage)
t.Logf("out: %s", outStr)
})
}