-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfile_test.go
85 lines (74 loc) · 2.03 KB
/
file_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package clog
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_fileLogger(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping testing on Windows")
}
testName := "Test_fileLogger"
defer Remove(DefaultFileName)
defer Remove(testName)
tests := []struct {
name string
mode string
config interface{}
wantLevel Level
wantErr error
}{
{
name: "nil config",
mode: DefaultFileName,
wantErr: nil,
},
{
name: "valid config",
mode: DefaultFileName,
config: FileConfig{
Level: LevelInfo,
Filename: filepath.Join(os.TempDir(), "Test_ModeFile"),
},
wantErr: nil,
},
{
name: "custom name",
mode: testName,
wantErr: nil,
},
{
name: "invalid filename",
config: FileConfig{
Level: LevelInfo,
},
wantErr: errors.New(`initialize logger: init file "": open file "": open : no such file or directory`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantErr, NewFileWithName(tt.mode, 10, tt.config))
})
}
assert.Equal(t, 2, mgr.len())
assert.Equal(t, DefaultFileName, mgr.loggers[0].Name())
assert.Equal(t, LevelInfo, mgr.loggers[0].Level())
assert.Equal(t, testName, mgr.loggers[1].Name())
assert.Equal(t, LevelTrace, mgr.loggers[1].Level())
}
func Test_rotateFilename(t *testing.T) {
_ = os.MkdirAll("test", os.ModePerm)
defer os.RemoveAll("test")
filename := rotateFilename("test/Test_rotateFilename.log", "2017-03-05")
assert.Equal(t, "test/Test_rotateFilename.log.2017-03-05", filename)
assert.Nil(t, ioutil.WriteFile(filename, []byte(""), os.ModePerm))
filename = rotateFilename("test/Test_rotateFilename.log", "2017-03-05")
assert.Equal(t, "test/Test_rotateFilename.log.2017-03-05.001", filename)
assert.Nil(t, ioutil.WriteFile(filename, []byte(""), os.ModePerm))
filename = rotateFilename("test/Test_rotateFilename.log", "2017-03-05")
assert.Equal(t, "test/Test_rotateFilename.log.2017-03-05.002", filename)
}