OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "dart:async"; | 5 import "dart:async"; |
6 import "dart:io"; | 6 import "dart:io"; |
7 | 7 |
8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 import "package:path/path.dart"; |
10 | 11 |
11 | 12 |
12 void testWatchCreateFile() { | 13 void testWatchCreateFile() { |
13 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 14 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
14 var file = new File(dir.path + '/file'); | 15 var file = new File(join(dir.path, 'file')); |
15 | 16 |
16 var watcher = dir.watch(); | 17 var watcher = dir.watch(); |
17 | 18 |
18 asyncStart(); | 19 asyncStart(); |
19 var sub; | 20 var sub; |
20 sub = watcher.listen((event) { | 21 sub = watcher.listen((event) { |
21 if (event is FileSystemCreateEvent && | 22 if (event is FileSystemCreateEvent && |
22 event.path.endsWith('file')) { | 23 event.path.endsWith('file')) { |
| 24 Expect.isFalse(event.isDirectory); |
23 asyncEnd(); | 25 asyncEnd(); |
24 sub.cancel(); | 26 sub.cancel(); |
25 dir.deleteSync(recursive: true); | 27 dir.deleteSync(recursive: true); |
26 } | 28 } |
27 }, onError: (e) { | 29 }, onError: (e) { |
28 dir.deleteSync(recursive: true); | 30 dir.deleteSync(recursive: true); |
29 throw e; | 31 throw e; |
30 }); | 32 }); |
31 | 33 |
32 file.createSync(); | 34 file.createSync(); |
33 } | 35 } |
34 | 36 |
35 | 37 |
| 38 void testWatchCreateDir() { |
| 39 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
| 40 var subdir = new Directory(join(dir.path, 'dir')); |
| 41 |
| 42 var watcher = dir.watch(); |
| 43 |
| 44 asyncStart(); |
| 45 var sub; |
| 46 sub = watcher.listen((event) { |
| 47 if (event is FileSystemCreateEvent && |
| 48 event.path.endsWith('dir')) { |
| 49 Expect.isTrue(event.isDirectory); |
| 50 asyncEnd(); |
| 51 sub.cancel(); |
| 52 dir.deleteSync(recursive: true); |
| 53 } |
| 54 }, onError: (e) { |
| 55 dir.deleteSync(recursive: true); |
| 56 throw e; |
| 57 }); |
| 58 |
| 59 subdir.createSync(); |
| 60 } |
| 61 |
| 62 |
36 void testWatchModifyFile() { | 63 void testWatchModifyFile() { |
37 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 64 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
38 var file = new File(dir.path + '/file'); | 65 var file = new File(join(dir.path, 'file')); |
39 file.createSync(); | 66 file.createSync(); |
40 | 67 |
41 var watcher = dir.watch(); | 68 var watcher = dir.watch(); |
42 | 69 |
43 asyncStart(); | 70 asyncStart(); |
44 var sub; | 71 var sub; |
45 sub = watcher.listen((event) { | 72 sub = watcher.listen((event) { |
46 if (event is FileSystemModifyEvent) { | 73 if (event is FileSystemModifyEvent) { |
47 Expect.isTrue(event.path.endsWith('file')); | 74 Expect.isTrue(event.path.endsWith('file')); |
48 sub.cancel(); | 75 sub.cancel(); |
49 asyncEnd(); | 76 asyncEnd(); |
50 dir.deleteSync(recursive: true); | 77 dir.deleteSync(recursive: true); |
51 } | 78 } |
52 }, onError: (e) { | 79 }, onError: (e) { |
53 dir.deleteSync(recursive: true); | 80 dir.deleteSync(recursive: true); |
54 throw e; | 81 throw e; |
55 }); | 82 }); |
56 | 83 |
57 file.writeAsStringSync('a'); | 84 file.writeAsStringSync('a'); |
58 } | 85 } |
59 | 86 |
60 | 87 |
61 void testWatchMoveFile() { | 88 void testWatchMoveFile() { |
62 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 89 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
63 var file = new File(dir.path + '/file'); | 90 var file = new File(join(dir.path, 'file')); |
64 file.createSync(); | 91 file.createSync(); |
65 | 92 |
66 var watcher = dir.watch(); | 93 var watcher = dir.watch(); |
67 | 94 |
68 asyncStart(); | 95 asyncStart(); |
69 var sub; | 96 var sub; |
70 sub = watcher.listen((event) { | 97 sub = watcher.listen((event) { |
71 if (event is FileSystemMoveEvent) { | 98 if (event is FileSystemMoveEvent) { |
72 Expect.isTrue(event.path.endsWith('file')); | 99 Expect.isTrue(event.path.endsWith('file')); |
73 if (event.destination != null) { | 100 if (event.destination != null) { |
74 Expect.isTrue(event.destination.endsWith('file2')); | 101 Expect.isTrue(event.destination.endsWith('file2')); |
75 } | 102 } |
76 sub.cancel(); | 103 sub.cancel(); |
77 asyncEnd(); | 104 asyncEnd(); |
78 dir.deleteSync(recursive: true); | 105 dir.deleteSync(recursive: true); |
79 } | 106 } |
80 }, onError: (e) { | 107 }, onError: (e) { |
81 dir.deleteSync(recursive: true); | 108 dir.deleteSync(recursive: true); |
82 throw e; | 109 throw e; |
83 }); | 110 }); |
84 | 111 |
85 file.renameSync(dir.path + '/file2'); | 112 file.renameSync(join(dir.path, 'file2')); |
86 } | 113 } |
87 | 114 |
88 | 115 |
89 void testWatchDeleteFile() { | 116 void testWatchDeleteFile() { |
90 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 117 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
91 var file = new File(dir.path + '/file'); | 118 var file = new File(join(dir.path, 'file')); |
92 file.createSync(); | 119 file.createSync(); |
93 | 120 |
94 var watcher = dir.watch(); | 121 var watcher = dir.watch(); |
95 | 122 |
96 asyncStart(); | 123 asyncStart(); |
97 var sub; | 124 var sub; |
98 sub = watcher.listen((event) { | 125 sub = watcher.listen((event) { |
99 if (event is FileSystemDeleteEvent) { | 126 if (event is FileSystemDeleteEvent) { |
100 Expect.isTrue(event.path.endsWith('file')); | 127 Expect.isTrue(event.path.endsWith('file')); |
101 sub.cancel(); | 128 sub.cancel(); |
(...skipping 22 matching lines...) Expand all Loading... |
124 }, onDone: () { | 151 }, onDone: () { |
125 asyncEnd(); | 152 asyncEnd(); |
126 }); | 153 }); |
127 | 154 |
128 dir.deleteSync(); | 155 dir.deleteSync(); |
129 } | 156 } |
130 | 157 |
131 | 158 |
132 void testWatchOnlyModifyFile() { | 159 void testWatchOnlyModifyFile() { |
133 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 160 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
134 var file = new File(dir.path + '/file'); | 161 var file = new File(join(dir.path, 'file')); |
135 | 162 |
136 var watcher = dir.watch(events: FileSystemEvent.MODIFY); | 163 var watcher = dir.watch(events: FileSystemEvent.MODIFY); |
137 | 164 |
138 asyncStart(); | 165 asyncStart(); |
139 var sub; | 166 var sub; |
140 sub = watcher.listen((event) { | 167 sub = watcher.listen((event) { |
141 Expect.isTrue(event is FileSystemModifyEvent); | 168 Expect.isTrue(event is FileSystemModifyEvent); |
142 Expect.isTrue(event.path.endsWith('file')); | 169 Expect.isTrue(event.path.endsWith('file')); |
143 sub.cancel(); | 170 sub.cancel(); |
144 asyncEnd(); | 171 asyncEnd(); |
145 dir.deleteSync(recursive: true); | 172 dir.deleteSync(recursive: true); |
146 }, onError: (e) { | 173 }, onError: (e) { |
147 dir.deleteSync(recursive: true); | 174 dir.deleteSync(recursive: true); |
148 throw e; | 175 throw e; |
149 }); | 176 }); |
150 | 177 |
151 file.createSync(); | 178 file.createSync(); |
152 file.writeAsStringSync('a'); | 179 file.writeAsStringSync('a'); |
153 } | 180 } |
154 | 181 |
155 | 182 |
156 void testMultipleEvents() { | 183 void testMultipleEvents() { |
157 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 184 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
158 var file = new File(dir.path + '/file'); | 185 var file = new File(join(dir.path, 'file')); |
159 var file2 = new File(dir.path + '/file2'); | 186 var file2 = new File(join(dir.path, 'file2')); |
160 | 187 |
161 var watcher = dir.watch(); | 188 var watcher = dir.watch(); |
162 | 189 |
163 asyncStart(); | 190 asyncStart(); |
164 int state = 0; | 191 int state = 0; |
165 var sub; | 192 var sub; |
166 sub = watcher.listen((event) { | 193 sub = watcher.listen((event) { |
167 int newState = 0; | 194 int newState = 0; |
168 switch (event.type) { | 195 switch (event.type) { |
169 case FileSystemEvent.CREATE: | 196 case FileSystemEvent.CREATE: |
(...skipping 27 matching lines...) Expand all Loading... |
197 file2.deleteSync(); | 224 file2.deleteSync(); |
198 } | 225 } |
199 | 226 |
200 | 227 |
201 void testWatchRecursive() { | 228 void testWatchRecursive() { |
202 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 229 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
203 if (Platform.isLinux) { | 230 if (Platform.isLinux) { |
204 Expect.throws(() => dir.watch(recursive: true)); | 231 Expect.throws(() => dir.watch(recursive: true)); |
205 return; | 232 return; |
206 } | 233 } |
207 var dir2 = new Directory(dir.path + '/dir'); | 234 var dir2 = new Directory(join(dir.path, 'dir')); |
208 dir2.createSync(); | 235 dir2.createSync(); |
209 var file = new File(dir.path + '/dir/file'); | 236 var file = new File(join(dir.path, 'dir/file')); |
210 | 237 |
211 var watcher = dir.watch(recursive: true); | 238 var watcher = dir.watch(recursive: true); |
212 | 239 |
213 asyncStart(); | 240 asyncStart(); |
214 var sub; | 241 var sub; |
215 sub = watcher.listen((event) { | 242 sub = watcher.listen((event) { |
216 if (event.path.endsWith('file')) { | 243 if (event.path.endsWith('file')) { |
217 sub.cancel(); | 244 sub.cancel(); |
218 asyncEnd(); | 245 asyncEnd(); |
219 dir.deleteSync(recursive: true); | 246 dir.deleteSync(recursive: true); |
220 } | 247 } |
221 }, onError: (e) { | 248 }, onError: (e) { |
222 dir.deleteSync(recursive: true); | 249 dir.deleteSync(recursive: true); |
223 throw e; | 250 throw e; |
224 }); | 251 }); |
225 | 252 |
226 file.createSync(); | 253 file.createSync(); |
227 } | 254 } |
228 | 255 |
229 | 256 |
230 void testWatchNonRecursive() { | 257 void testWatchNonRecursive() { |
231 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); | 258 var dir = Directory.systemTemp.createTempSync('dart_file_system_watcher'); |
232 var dir2 = new Directory(dir.path + '/dir'); | 259 var dir2 = new Directory(join(dir.path, 'dir')); |
233 dir2.createSync(); | 260 dir2.createSync(); |
234 var file = new File(dir.path + '/dir/file'); | 261 var file = new File(join(dir.path, 'dir/file')); |
235 | 262 |
236 var watcher = dir.watch(recursive: false); | 263 var watcher = dir.watch(recursive: false); |
237 | 264 |
238 asyncStart(); | 265 asyncStart(); |
239 var sub; | 266 var sub; |
240 sub = watcher.listen((event) { | 267 sub = watcher.listen((event) { |
241 if (event.path.endsWith('file')) { | 268 if (event.path.endsWith('file')) { |
242 throw "File change event not expected"; | 269 throw "File change event not expected"; |
243 } | 270 } |
244 }, onError: (e) { | 271 }, onError: (e) { |
(...skipping 21 matching lines...) Expand all Loading... |
266 }, onError: (e) { | 293 }, onError: (e) { |
267 asyncEnd(); | 294 asyncEnd(); |
268 Expect.isTrue(e is FileSystemException); | 295 Expect.isTrue(e is FileSystemException); |
269 }); | 296 }); |
270 } | 297 } |
271 | 298 |
272 | 299 |
273 void main() { | 300 void main() { |
274 if (!FileSystemEntity.isWatchSupported) return; | 301 if (!FileSystemEntity.isWatchSupported) return; |
275 testWatchCreateFile(); | 302 testWatchCreateFile(); |
| 303 testWatchCreateDir(); |
276 testWatchModifyFile(); | 304 testWatchModifyFile(); |
277 testWatchMoveFile(); | 305 testWatchMoveFile(); |
278 testWatchDeleteFile(); | 306 testWatchDeleteFile(); |
279 testWatchDeleteDir(); | 307 testWatchDeleteDir(); |
280 testWatchOnlyModifyFile(); | 308 testWatchOnlyModifyFile(); |
281 testMultipleEvents(); | 309 testMultipleEvents(); |
282 testWatchNonRecursive(); | 310 testWatchNonRecursive(); |
283 testWatchNonExisting(); | 311 testWatchNonExisting(); |
284 } | 312 } |
OLD | NEW |