OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / rjeczalik / notify / watcher_inotify_test.go
1 // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4
5 // +build linux
6
7 package notify
8
9 import (
10         "os"
11         "path/filepath"
12         "testing"
13 )
14
15 func icreate(w *W, path string) WCase {
16         cas := create(w, path)
17         cas.Events = append(cas.Events,
18                 &Call{P: path, E: InCreate},
19         )
20         return cas
21 }
22
23 func iremove(w *W, path string) WCase {
24         cas := remove(w, path)
25         cas.Events = append(cas.Events,
26                 &Call{P: path, E: InDelete},
27         )
28         return cas
29 }
30
31 func iopen(w *W, path string) WCase {
32         return WCase{
33                 Action: func() {
34                         f, err := os.OpenFile(filepath.Join(w.root, path), os.O_RDWR, 0644)
35                         if err != nil {
36                                 w.Fatalf("OpenFile(%q)=%v", path, err)
37                         }
38                         if err := f.Close(); err != nil {
39                                 w.Fatalf("Close(%q)=%v", path, err)
40                         }
41                 },
42                 Events: []EventInfo{
43                         &Call{P: path, E: InAccess},
44                         &Call{P: path, E: InOpen},
45                         &Call{P: path, E: InCloseNowrite},
46                 },
47         }
48 }
49
50 func iread(w *W, path string, p []byte) WCase {
51         return WCase{
52                 Action: func() {
53                         f, err := os.OpenFile(filepath.Join(w.root, path), os.O_RDWR, 0644)
54                         if err != nil {
55                                 w.Fatalf("OpenFile(%q)=%v", path, err)
56                         }
57                         if _, err := f.Read(p); err != nil {
58                                 w.Fatalf("Read(%q)=%v", path, err)
59                         }
60                         if err := f.Close(); err != nil {
61                                 w.Fatalf("Close(%q)=%v", path, err)
62                         }
63                 },
64                 Events: []EventInfo{
65                         &Call{P: path, E: InAccess},
66                         &Call{P: path, E: InOpen},
67                         &Call{P: path, E: InModify},
68                         &Call{P: path, E: InCloseNowrite},
69                 },
70         }
71 }
72
73 func iwrite(w *W, path string, p []byte) WCase {
74         cas := write(w, path, p)
75         path = cas.Events[0].Path()
76         cas.Events = append(cas.Events,
77                 &Call{P: path, E: InAccess},
78                 &Call{P: path, E: InOpen},
79                 &Call{P: path, E: InModify},
80                 &Call{P: path, E: InCloseWrite},
81         )
82         return cas
83 }
84
85 func irename(w *W, path string) WCase {
86         const ext = ".notify"
87         return WCase{
88                 Action: func() {
89                         file := filepath.Join(w.root, path)
90                         if err := os.Rename(file, file+ext); err != nil {
91                                 w.Fatalf("Rename(%q, %q)=%v", path, path+ext, err)
92                         }
93                 },
94                 Events: []EventInfo{
95                         &Call{P: path, E: InMovedFrom},
96                         &Call{P: path + ext, E: InMovedTo},
97                         &Call{P: path, E: InOpen},
98                         &Call{P: path, E: InAccess},
99                         &Call{P: path, E: InCreate},
100                 },
101         }
102 }
103
104 var events = []Event{
105         InAccess,
106         InModify,
107         InAttrib,
108         InCloseWrite,
109         InCloseNowrite,
110         InOpen,
111         InMovedFrom,
112         InMovedTo,
113         InCreate,
114         InDelete,
115         InDeleteSelf,
116         InMoveSelf,
117 }
118
119 func TestWatcherInotify(t *testing.T) {
120         w := NewWatcherTest(t, "testdata/vfs.txt", events...)
121         defer w.Close()
122
123         cases := [...]WCase{
124                 iopen(w, "src/github.com/rjeczalik/fs/fs.go"),
125                 iwrite(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")),
126                 iread(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")),
127                 iremove(w, "src/github.com/ppknap/link/README.md"),
128                 irename(w, "src/github.com/rjeczalik/fs/LICENSE"),
129         }
130
131         w.ExpectAny(cases[:])
132 }