-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_test.go
More file actions
71 lines (58 loc) · 1.38 KB
/
Copy pathlogging_test.go
File metadata and controls
71 lines (58 loc) · 1.38 KB
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
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"io"
"os"
"strings"
"testing"
)
func captureOutput(f func()) string {
// Backup the original stdout
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Run function that logs
f()
// Close writer, restore stdout, read buffer
_ = w.Close()
os.Stdout = old
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
return buf.String()
}
func TestStdoutLogger_Info(t *testing.T) {
logger := &StdoutLogger{}
output := captureOutput(func() {
logger.Info("hello %s", "world")
})
if !strings.Contains(output, "[INFO] hello world") {
t.Errorf("expected INFO log, got: %s", output)
}
}
func TestStdoutLogger_Warn(t *testing.T) {
logger := &StdoutLogger{}
output := captureOutput(func() {
logger.Warn("watch out")
})
if !strings.Contains(output, "[WARN] watch out") {
t.Errorf("expected WARN log, got: %s", output)
}
}
func TestStdoutLogger_Error(t *testing.T) {
logger := &StdoutLogger{}
output := captureOutput(func() {
logger.Error("something went %s", "wrong")
})
if !strings.Contains(output, "[ERROR] something went wrong") {
t.Errorf("expected ERROR log, got: %s", output)
}
}
func TestStdoutLogger_Debug(t *testing.T) {
logger := &StdoutLogger{}
output := captureOutput(func() {
logger.Debug("debug message %d", 42)
})
if !strings.Contains(output, "[DEBUG] debug message 42") {
t.Errorf("expected DEBUG log, got: %s", output)
}
}