-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotating_file_test.go
49 lines (43 loc) · 1.16 KB
/
rotating_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
package base
import (
"io/ioutil"
"os"
"path"
"testing"
)
func TestRotatingFile(t *testing.T) {
dirname, err := ioutil.TempDir("", "rotating-file")
if err != nil {
t.Fatalf("ioutil.TempDir failed with %v", err)
}
defer os.RemoveAll(dirname)
logFilename := path.Join(dirname, "log")
logFile, err := NewRotatingFile(logFilename, 0644, NopOpenedCallback)
if err != nil {
t.Fatalf("NewRotatingFile failed with %v", err)
}
defer logFile.Close()
logFile.WriteString("hello, ")
logFile.Rotate()
logFile.WriteString("world!")
oldLogFilename := logFilename + ".old"
if err := os.Rename(logFilename, oldLogFilename); err != nil {
t.Fatalf("Rename failed with %v", err)
}
logFile.WriteString("\n")
logFile.Rotate()
logFile.WriteString("hi!\n")
logFile.Close()
for filename, expectedContents := range map[string]string{
oldLogFilename: "hello, world!\n",
logFilename: "hi!\n",
} {
contents, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("ReadFile(%s) failed with %v", filename, err)
}
if string(contents) != expectedContents {
t.Errorf("Contents of %s were %q, expected %q", filename, string(contents), expectedContents)
}
}
}