OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library test.physical_file_system; | 5 library test.physical_file_system; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
9 | 9 |
10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 String idOne = 'one'; | 279 String idOne = 'one'; |
280 Folder folderOne = provider.getStateLocation(idOne); | 280 Folder folderOne = provider.getStateLocation(idOne); |
281 expect(folderOne, isNotNull); | 281 expect(folderOne, isNotNull); |
282 String idTwo = 'two'; | 282 String idTwo = 'two'; |
283 Folder folderTwo = provider.getStateLocation(idTwo); | 283 Folder folderTwo = provider.getStateLocation(idTwo); |
284 expect(folderTwo, isNotNull); | 284 expect(folderTwo, isNotNull); |
285 expect(folderTwo, isNot(equals(folderOne))); | 285 expect(folderTwo, isNot(equals(folderOne))); |
286 expect(provider.getStateLocation(idOne), equals(folderOne)); | 286 expect(provider.getStateLocation(idOne), equals(folderOne)); |
287 } | 287 } |
288 | 288 |
289 test_watch_createFile() { | 289 test_watchFile_delete() { |
| 290 var path = join(tempPath, 'foo'); |
| 291 var file = new io.File(path); |
| 292 file.writeAsStringSync('contents 1'); |
| 293 return _watchingFile(path, (changesReceived) { |
| 294 expect(changesReceived, hasLength(0)); |
| 295 file.deleteSync(); |
| 296 return _delayed(() { |
| 297 expect(changesReceived, hasLength(1)); |
| 298 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); |
| 299 expect(changesReceived[0].path, equals(path)); |
| 300 }); |
| 301 }); |
| 302 } |
| 303 |
| 304 test_watchFile_modify() { |
| 305 var path = join(tempPath, 'foo'); |
| 306 var file = new io.File(path); |
| 307 file.writeAsStringSync('contents 1'); |
| 308 return _watchingFile(path, (changesReceived) { |
| 309 expect(changesReceived, hasLength(0)); |
| 310 file.writeAsStringSync('contents 2'); |
| 311 return _delayed(() { |
| 312 expect(changesReceived, hasLength(1)); |
| 313 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); |
| 314 expect(changesReceived[0].path, equals(path)); |
| 315 }); |
| 316 }); |
| 317 } |
| 318 |
| 319 test_watchFolder_createFile() { |
290 return _watchingFolder(tempPath, (changesReceived) { | 320 return _watchingFolder(tempPath, (changesReceived) { |
291 expect(changesReceived, hasLength(0)); | 321 expect(changesReceived, hasLength(0)); |
292 var path = join(tempPath, 'foo'); | 322 var path = join(tempPath, 'foo'); |
293 new io.File(path).writeAsStringSync('contents'); | 323 new io.File(path).writeAsStringSync('contents'); |
294 return _delayed(() { | 324 return _delayed(() { |
295 // There should be an "add" event indicating that the file was added. | 325 // There should be an "add" event indicating that the file was added. |
296 // Depending on how long it took to write the contents, it may be | 326 // Depending on how long it took to write the contents, it may be |
297 // followed by "modify" events. | 327 // followed by "modify" events. |
298 expect(changesReceived, isNotEmpty); | 328 expect(changesReceived, isNotEmpty); |
299 expect(changesReceived[0].type, equals(ChangeType.ADD)); | 329 expect(changesReceived[0].type, equals(ChangeType.ADD)); |
300 expect(changesReceived[0].path, equals(path)); | 330 expect(changesReceived[0].path, equals(path)); |
301 for (int i = 1; i < changesReceived.length; i++) { | 331 for (int i = 1; i < changesReceived.length; i++) { |
302 expect(changesReceived[i].type, equals(ChangeType.MODIFY)); | 332 expect(changesReceived[i].type, equals(ChangeType.MODIFY)); |
303 expect(changesReceived[i].path, equals(path)); | 333 expect(changesReceived[i].path, equals(path)); |
304 } | 334 } |
305 }); | 335 }); |
306 }); | 336 }); |
307 } | 337 } |
308 | 338 |
309 test_watch_deleteFile() { | 339 test_watchFolder_deleteFile() { |
310 var path = join(tempPath, 'foo'); | 340 var path = join(tempPath, 'foo'); |
311 var file = new io.File(path); | 341 var file = new io.File(path); |
312 file.writeAsStringSync('contents 1'); | 342 file.writeAsStringSync('contents 1'); |
313 return _watchingFolder(tempPath, (changesReceived) { | 343 return _watchingFolder(tempPath, (changesReceived) { |
314 expect(changesReceived, hasLength(0)); | 344 expect(changesReceived, hasLength(0)); |
315 file.deleteSync(); | 345 file.deleteSync(); |
316 return _delayed(() { | 346 return _delayed(() { |
317 expect(changesReceived, hasLength(1)); | 347 expect(changesReceived, hasLength(1)); |
318 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); | 348 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); |
319 expect(changesReceived[0].path, equals(path)); | 349 expect(changesReceived[0].path, equals(path)); |
320 }); | 350 }); |
321 }); | 351 }); |
322 } | 352 } |
323 | 353 |
324 test_watch_modifyFile() { | 354 test_watchFolder_modifyFile() { |
325 var path = join(tempPath, 'foo'); | 355 var path = join(tempPath, 'foo'); |
326 var file = new io.File(path); | 356 var file = new io.File(path); |
327 file.writeAsStringSync('contents 1'); | 357 file.writeAsStringSync('contents 1'); |
328 return _watchingFolder(tempPath, (changesReceived) { | 358 return _watchingFolder(tempPath, (changesReceived) { |
329 expect(changesReceived, hasLength(0)); | 359 expect(changesReceived, hasLength(0)); |
330 file.writeAsStringSync('contents 2'); | 360 file.writeAsStringSync('contents 2'); |
331 return _delayed(() { | 361 return _delayed(() { |
332 expect(changesReceived, hasLength(1)); | 362 expect(changesReceived, hasLength(1)); |
333 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); | 363 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); |
334 expect(changesReceived[0].path, equals(path)); | 364 expect(changesReceived[0].path, equals(path)); |
335 }); | 365 }); |
336 }); | 366 }); |
337 } | 367 } |
338 | 368 |
339 test_watch_modifyFile_inSubDir() { | 369 test_watchFolder_modifyFile_inSubDir() { |
340 var subdirPath = join(tempPath, 'foo'); | 370 var subdirPath = join(tempPath, 'foo'); |
341 new io.Directory(subdirPath).createSync(); | 371 new io.Directory(subdirPath).createSync(); |
342 var path = join(tempPath, 'bar'); | 372 var path = join(tempPath, 'bar'); |
343 var file = new io.File(path); | 373 var file = new io.File(path); |
344 file.writeAsStringSync('contents 1'); | 374 file.writeAsStringSync('contents 1'); |
345 return _watchingFolder(tempPath, (changesReceived) { | 375 return _watchingFolder(tempPath, (changesReceived) { |
346 expect(changesReceived, hasLength(0)); | 376 expect(changesReceived, hasLength(0)); |
347 file.writeAsStringSync('contents 2'); | 377 file.writeAsStringSync('contents 2'); |
348 return _delayed(() { | 378 return _delayed(() { |
349 expect(changesReceived, hasLength(1)); | 379 expect(changesReceived, hasLength(1)); |
350 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); | 380 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); |
351 expect(changesReceived[0].path, equals(path)); | 381 expect(changesReceived[0].path, equals(path)); |
352 }); | 382 }); |
353 }); | 383 }); |
354 } | 384 } |
355 | 385 |
356 Future _delayed(computation()) { | 386 Future _delayed(computation()) { |
357 // Give the tests 1 second to detect the changes. While it may only | 387 // Give the tests 1 second to detect the changes. While it may only |
358 // take up to a few hundred ms, a whole second gives a good margin | 388 // take up to a few hundred ms, a whole second gives a good margin |
359 // for when running tests. | 389 // for when running tests. |
360 return new Future.delayed(new Duration(seconds: 1), computation); | 390 return new Future.delayed(new Duration(seconds: 1), computation); |
361 } | 391 } |
362 | 392 |
| 393 _watchingFile(String path, test(List<WatchEvent> changesReceived)) { |
| 394 // Delay before we start watching the file. This is necessary |
| 395 // because on MacOS, file modifications that occur just before we |
| 396 // start watching are sometimes misclassified as happening just after |
| 397 // we start watching. |
| 398 return _delayed(() { |
| 399 File file = PhysicalResourceProvider.INSTANCE.getResource(path); |
| 400 var changesReceived = <WatchEvent>[]; |
| 401 var subscription = file.changes.listen(changesReceived.add); |
| 402 // Delay running the rest of the test to allow file.changes propogate. |
| 403 return _delayed(() => test(changesReceived)).whenComplete(() { |
| 404 subscription.cancel(); |
| 405 }); |
| 406 }); |
| 407 } |
| 408 |
363 _watchingFolder(String path, test(List<WatchEvent> changesReceived)) { | 409 _watchingFolder(String path, test(List<WatchEvent> changesReceived)) { |
364 // Delay before we start watching the folder. This is necessary | 410 // Delay before we start watching the folder. This is necessary |
365 // because on MacOS, file modifications that occur just before we | 411 // because on MacOS, file modifications that occur just before we |
366 // start watching are sometimes misclassified as happening just after | 412 // start watching are sometimes misclassified as happening just after |
367 // we start watching. | 413 // we start watching. |
368 return _delayed(() { | 414 return _delayed(() { |
369 Folder folder = PhysicalResourceProvider.INSTANCE.getResource(path); | 415 Folder folder = PhysicalResourceProvider.INSTANCE.getResource(path); |
370 var changesReceived = <WatchEvent>[]; | 416 var changesReceived = <WatchEvent>[]; |
371 var subscription = folder.changes.listen(changesReceived.add); | 417 var subscription = folder.changes.listen(changesReceived.add); |
372 // Delay running the rest of the test to allow folder.changes to | 418 // Delay running the rest of the test to allow folder.changes to |
(...skipping 13 matching lines...) Expand all Loading... |
386 | 432 |
387 setUp() { | 433 setUp() { |
388 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource'); | 434 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource'); |
389 tempPath = tempDirectory.absolute.path; | 435 tempPath = tempDirectory.absolute.path; |
390 } | 436 } |
391 | 437 |
392 tearDown() { | 438 tearDown() { |
393 tempDirectory.deleteSync(recursive: true); | 439 tempDirectory.deleteSync(recursive: true); |
394 } | 440 } |
395 } | 441 } |
OLD | NEW |