forked from lomik/zapwriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_test.go
79 lines (63 loc) · 1.34 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
package zapwriter
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func fileOpen(t *testing.T) (f *FileOutput, path string, dir string, teadDown func()) {
teadDown = func() {}
var err error
dir, err = ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
teadDown = func() { os.RemoveAll(dir) }
path = filepath.Join(dir, "test.log")
f, err = File(path)
if err != nil {
t.Fatal(err)
}
n, err := f.Write([]byte("hello world\n"))
if err != nil || n != 12 {
t.FailNow()
}
c, err := ioutil.ReadFile(path)
if err != nil || string(c) != "hello world\n" {
t.FailNow()
}
return
}
func TestFileWrite(t *testing.T) {
f, _, _, tearDown := fileOpen(t)
defer tearDown()
err := f.Close()
if err != nil {
t.FailNow()
}
}
func TestFileMove(t *testing.T) {
f, path, dir, tearDown := fileOpen(t)
defer tearDown()
os.Rename(path, filepath.Join(dir, "test_bak.log"))
time.Sleep(2 * time.Second)
f.Write([]byte("new message\n"))
c, err := ioutil.ReadFile(path)
if err != nil || string(c) != "new message\n" {
t.FailNow()
}
f.Close()
}
func TestFileDelete(t *testing.T) {
f, path, _, tearDown := fileOpen(t)
defer tearDown()
os.Remove(path)
time.Sleep(2 * time.Second)
f.Write([]byte("new message\n"))
c, err := ioutil.ReadFile(path)
if err != nil || string(c) != "new message\n" {
t.FailNow()
}
f.Close()
}