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 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 event.path.endsWith('file')) { | 22 event.path.endsWith('file')) { |
23 asyncEnd(); | 23 asyncEnd(); |
24 sub.cancel(); | 24 sub.cancel(); |
25 dir.deleteSync(recursive: true); | 25 dir.deleteSync(recursive: true); |
26 } | 26 } |
27 }, onError: (e) { | 27 }, onError: (e) { |
28 dir.deleteSync(recursive: true); | 28 dir.deleteSync(recursive: true); |
29 throw e; | 29 throw e; |
30 }); | 30 }); |
31 | 31 |
32 runAsync(file.createSync); | 32 file.createSync(); |
33 } | 33 } |
34 | 34 |
35 | 35 |
36 void testWatchModifyFile() { | 36 void testWatchModifyFile() { |
37 var dir = new Directory('').createTempSync(); | 37 var dir = new Directory('').createTempSync(); |
38 var file = new File(dir.path + '/file'); | 38 var file = new File(dir.path + '/file'); |
39 file.createSync(); | 39 file.createSync(); |
40 | 40 |
41 var watcher = dir.watch(); | 41 var watcher = dir.watch(); |
42 | 42 |
43 asyncStart(); | 43 asyncStart(); |
44 var sub; | 44 var sub; |
45 sub = watcher.listen((event) { | 45 sub = watcher.listen((event) { |
46 if (event is FileSystemModifyEvent) { | 46 if (event is FileSystemModifyEvent) { |
47 Expect.isTrue(event.path.endsWith('file')); | 47 Expect.isTrue(event.path.endsWith('file')); |
48 sub.cancel(); | 48 sub.cancel(); |
49 asyncEnd(); | 49 asyncEnd(); |
50 dir.deleteSync(recursive: true); | 50 dir.deleteSync(recursive: true); |
51 } | 51 } |
52 }, onError: (e) { | 52 }, onError: (e) { |
53 dir.deleteSync(recursive: true); | 53 dir.deleteSync(recursive: true); |
54 throw e; | 54 throw e; |
55 }); | 55 }); |
56 | 56 |
57 runAsync(() => file.writeAsStringSync('a')); | 57 file.writeAsStringSync('a'); |
58 } | 58 } |
59 | 59 |
60 | 60 |
61 void testWatchMoveFile() { | 61 void testWatchMoveFile() { |
62 var dir = new Directory('').createTempSync(); | 62 var dir = new Directory('').createTempSync(); |
63 var file = new File(dir.path + '/file'); | 63 var file = new File(dir.path + '/file'); |
64 file.createSync(); | 64 file.createSync(); |
65 | 65 |
66 var watcher = dir.watch(); | 66 var watcher = dir.watch(); |
67 | 67 |
68 asyncStart(); | 68 asyncStart(); |
69 var sub; | 69 var sub; |
70 sub = watcher.listen((event) { | 70 sub = watcher.listen((event) { |
71 if (event is FileSystemMoveEvent) { | 71 if (event is FileSystemMoveEvent) { |
72 Expect.isTrue(event.path.endsWith('file')); | 72 Expect.isTrue(event.path.endsWith('file')); |
73 if (event.destination != null) { | 73 if (event.destination != null) { |
74 Expect.isTrue(event.destination.endsWith('file2')); | 74 Expect.isTrue(event.destination.endsWith('file2')); |
75 } | 75 } |
76 sub.cancel(); | 76 sub.cancel(); |
77 asyncEnd(); | 77 asyncEnd(); |
78 dir.deleteSync(recursive: true); | 78 dir.deleteSync(recursive: true); |
79 } | 79 } |
80 }, onError: (e) { | 80 }, onError: (e) { |
81 dir.deleteSync(recursive: true); | 81 dir.deleteSync(recursive: true); |
82 throw e; | 82 throw e; |
83 }); | 83 }); |
84 | 84 |
85 runAsync(() => file.renameSync(dir.path + '/file2')); | 85 file.renameSync(dir.path + '/file2'); |
86 } | 86 } |
87 | 87 |
88 | 88 |
89 void testWatchDeleteFile() { | 89 void testWatchDeleteFile() { |
90 var dir = new Directory('').createTempSync(); | 90 var dir = new Directory('').createTempSync(); |
91 var file = new File(dir.path + '/file'); | 91 var file = new File(dir.path + '/file'); |
92 file.createSync(); | 92 file.createSync(); |
93 | 93 |
94 var watcher = dir.watch(); | 94 var watcher = dir.watch(); |
95 | 95 |
96 asyncStart(); | 96 asyncStart(); |
97 var sub; | 97 var sub; |
98 sub = watcher.listen((event) { | 98 sub = watcher.listen((event) { |
99 if (event is FileSystemDeleteEvent) { | 99 if (event is FileSystemDeleteEvent) { |
100 Expect.isTrue(event.path.endsWith('file')); | 100 Expect.isTrue(event.path.endsWith('file')); |
101 sub.cancel(); | 101 sub.cancel(); |
102 asyncEnd(); | 102 asyncEnd(); |
103 dir.deleteSync(recursive: true); | 103 dir.deleteSync(recursive: true); |
104 } | 104 } |
105 }, onError: (e) { | 105 }, onError: (e) { |
106 dir.deleteSync(recursive: true); | 106 dir.deleteSync(recursive: true); |
107 throw e; | 107 throw e; |
108 }); | 108 }); |
109 | 109 |
110 runAsync(file.deleteSync); | 110 file.deleteSync(); |
111 } | 111 } |
112 | 112 |
113 | 113 |
114 void testWatchOnlyModifyFile() { | 114 void testWatchOnlyModifyFile() { |
115 var dir = new Directory('').createTempSync(); | 115 var dir = new Directory('').createTempSync(); |
116 var file = new File(dir.path + '/file'); | 116 var file = new File(dir.path + '/file'); |
117 | 117 |
118 var watcher = dir.watch(events: FileSystemEvent.MODIFY); | 118 var watcher = dir.watch(events: FileSystemEvent.MODIFY); |
119 | 119 |
120 asyncStart(); | 120 asyncStart(); |
121 var sub; | 121 var sub; |
122 sub = watcher.listen((event) { | 122 sub = watcher.listen((event) { |
123 Expect.isTrue(event is FileSystemModifyEvent); | 123 Expect.isTrue(event is FileSystemModifyEvent); |
124 Expect.isTrue(event.path.endsWith('file')); | 124 Expect.isTrue(event.path.endsWith('file')); |
125 sub.cancel(); | 125 sub.cancel(); |
126 asyncEnd(); | 126 asyncEnd(); |
127 dir.deleteSync(recursive: true); | 127 dir.deleteSync(recursive: true); |
128 }, onError: (e) { | 128 }, onError: (e) { |
129 dir.deleteSync(recursive: true); | 129 dir.deleteSync(recursive: true); |
130 throw e; | 130 throw e; |
131 }); | 131 }); |
132 | 132 |
133 runAsync(() { | 133 file.createSync(); |
134 file.createSync(); | 134 file.writeAsStringSync('a'); |
135 file.writeAsStringSync('a'); | |
136 }); | |
137 } | 135 } |
138 | 136 |
139 | 137 |
140 void testMultipleEvents() { | 138 void testMultipleEvents() { |
141 var dir = new Directory('').createTempSync(); | 139 var dir = new Directory('').createTempSync(); |
142 var file = new File(dir.path + '/file'); | 140 var file = new File(dir.path + '/file'); |
143 var file2 = new File(dir.path + '/file2'); | 141 var file2 = new File(dir.path + '/file2'); |
144 | 142 |
145 var watcher = dir.watch(); | 143 var watcher = dir.watch(); |
146 | 144 |
(...skipping 21 matching lines...) Expand all Loading... |
168 asyncEnd(); | 166 asyncEnd(); |
169 dir.deleteSync(); | 167 dir.deleteSync(); |
170 break; | 168 break; |
171 } | 169 } |
172 if (!Platform.isMacOS) { | 170 if (!Platform.isMacOS) { |
173 if (newState < state) throw "Bad state"; | 171 if (newState < state) throw "Bad state"; |
174 } | 172 } |
175 state = newState; | 173 state = newState; |
176 }); | 174 }); |
177 | 175 |
178 runAsync(() { | 176 file.createSync(); |
179 file.createSync(); | 177 file.writeAsStringSync('a'); |
180 file.writeAsStringSync('a'); | 178 file.renameSync(file2.path); |
181 file.renameSync(file2.path); | 179 file2.deleteSync(); |
182 file2.deleteSync(); | |
183 }); | |
184 } | 180 } |
185 | 181 |
186 | 182 |
187 void testWatchRecursive() { | 183 void testWatchRecursive() { |
188 var dir = new Directory('').createTempSync(); | 184 var dir = new Directory('').createTempSync(); |
189 if (Platform.isLinux) { | 185 if (Platform.isLinux) { |
190 Expect.throws(() => dir.watch(recursive: true)); | 186 Expect.throws(() => dir.watch(recursive: true)); |
191 return; | 187 return; |
192 } | 188 } |
193 var dir2 = new Directory(dir.path + '/dir'); | 189 var dir2 = new Directory(dir.path + '/dir'); |
194 dir2.createSync(); | 190 dir2.createSync(); |
195 var file = new File(dir.path + '/dir/file'); | 191 var file = new File(dir.path + '/dir/file'); |
196 | 192 |
197 var watcher = dir.watch(recursive: true); | 193 var watcher = dir.watch(recursive: true); |
198 | 194 |
199 asyncStart(); | 195 asyncStart(); |
200 var sub; | 196 var sub; |
201 sub = watcher.listen((event) { | 197 sub = watcher.listen((event) { |
202 if (event.path.endsWith('file')) { | 198 if (event.path.endsWith('file')) { |
203 sub.cancel(); | 199 sub.cancel(); |
204 asyncEnd(); | 200 asyncEnd(); |
205 dir.deleteSync(recursive: true); | 201 dir.deleteSync(recursive: true); |
206 } | 202 } |
207 }, onError: (e) { | 203 }, onError: (e) { |
208 dir.deleteSync(recursive: true); | 204 dir.deleteSync(recursive: true); |
209 throw e; | 205 throw e; |
210 }); | 206 }); |
211 | 207 |
212 runAsync(file.createSync()); | 208 file.createSync(); |
213 } | 209 } |
214 | 210 |
215 | 211 |
216 void testWatchNonRecursive() { | 212 void testWatchNonRecursive() { |
217 var dir = new Directory('').createTempSync(); | 213 var dir = new Directory('').createTempSync(); |
218 var dir2 = new Directory(dir.path + '/dir'); | 214 var dir2 = new Directory(dir.path + '/dir'); |
219 dir2.createSync(); | 215 dir2.createSync(); |
220 var file = new File(dir.path + '/dir/file'); | 216 var file = new File(dir.path + '/dir/file'); |
221 | 217 |
222 var watcher = dir.watch(recursive: false); | 218 var watcher = dir.watch(recursive: false); |
223 | 219 |
224 asyncStart(); | 220 asyncStart(); |
225 var sub; | 221 var sub; |
226 sub = watcher.listen((event) { | 222 sub = watcher.listen((event) { |
227 if (event.path.endsWith('file')) { | 223 if (event.path.endsWith('file')) { |
228 throw "File change event not expected"; | 224 throw "File change event not expected"; |
229 } | 225 } |
230 }, onError: (e) { | 226 }, onError: (e) { |
231 dir.deleteSync(recursive: true); | 227 dir.deleteSync(recursive: true); |
232 throw e; | 228 throw e; |
233 }); | 229 }); |
234 | 230 |
235 runAsync(file.createSync); | 231 file.createSync(); |
236 | 232 |
237 new Timer(const Duration(milliseconds: 300), () { | 233 new Timer(const Duration(milliseconds: 300), () { |
238 sub.cancel(); | 234 sub.cancel(); |
239 asyncEnd(); | 235 asyncEnd(); |
240 dir.deleteSync(recursive: true); | 236 dir.deleteSync(recursive: true); |
241 }); | 237 }); |
242 } | 238 } |
243 | 239 |
244 | 240 |
245 void main() { | 241 void main() { |
246 if (!FileSystemEntity.isWatchSupported) return; | 242 if (!FileSystemEntity.isWatchSupported) return; |
247 testWatchCreateFile(); | 243 testWatchCreateFile(); |
248 testWatchModifyFile(); | 244 testWatchModifyFile(); |
249 testWatchMoveFile(); | 245 testWatchMoveFile(); |
250 testWatchDeleteFile(); | 246 testWatchDeleteFile(); |
251 testWatchOnlyModifyFile(); | 247 testWatchOnlyModifyFile(); |
252 testMultipleEvents(); | 248 testMultipleEvents(); |
253 testWatchNonRecursive(); | 249 testWatchNonRecursive(); |
254 } | 250 } |
OLD | NEW |