-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_test.go
56 lines (47 loc) · 1.17 KB
/
log_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
package main
import (
"bytes"
"os"
"strings"
"testing"
)
func TestLog(t *testing.T) {
Cfg = &Config{Verbose: true}
buf := bytes.NewBuffer(nil)
logger.SetOutput(buf)
Verbose("a", "b")
assertTrue(t, strings.HasSuffix(buf.String(), ": ab\n"), "val", buf.String())
buf.Reset()
Cfg.Verbose = false
Verbose("a", "b")
assertEqual(t, buf.Len(), 0)
buf.Reset()
Error("c", "d")
assertTrue(t, strings.HasSuffix(buf.String(), ": cd\n"), "val", buf.String())
}
func TestSetLoggerFile(t *testing.T) {
Cfg = &Config{}
// Invalid file
err := SetLoggerFile("a/b.log")
assertTrue(t, err != nil)
// Opening valid file
bytes := make([]byte, 100)
f, err := os.CreateTemp("", "abc")
assertEqual(t, err, nil)
defer func() {
_ = f.Close()
_ = os.Remove(f.Name())
}()
assertTrue(t, err == nil)
assertTrue(t, SetLoggerFile(f.Name()) == nil)
Error("a", "b")
_, err = f.Read(bytes)
assertEqual(t, err, nil)
assertTrue(t, strings.Contains(string(bytes), ": ab"))
assertTrue(t, CloseLoggerFile(os.Stderr) == nil)
// File must be closed now.
assertTrue(t, logFile.Close() != nil)
stderr, ok := logger.Writer().(*os.File)
assertTrue(t, ok)
assertEqual(t, stderr, os.Stderr)
}