-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger_test.go
60 lines (50 loc) · 1.27 KB
/
logger_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
55
56
57
58
59
60
package rbac
import (
"bytes"
"io"
"os"
"testing"
)
func TestConsoleLogger(t *testing.T) {
var buf bytes.Buffer
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
logger := NewConsoleLogger()
logger.Debugf("debug")
logger.Errorf("error")
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = old // restoring the real stdout
_ = <-outC
expected := "[DEBUG] debug\n[ERROR] error\n"
if string(buf.Bytes()) != expected {
t.Fatalf("logger output is not compatible, expected: `%s`, got: `%s`", expected, buf.Bytes())
}
}
func TestNullLogger(t *testing.T) {
var buf bytes.Buffer
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
logger := NewNullLogger()
logger.Debugf("TEST")
logger.Errorf("TEST2")
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = old // restoring the real stdout
_ = <-outC
if string(buf.Bytes()) != "" {
t.Fatalf("logger output is not compatible, expected: `%s`, got: `%s`", "", buf.Bytes())
}
}