| 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.resource; | 5 library test.resource; |
| 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 'mocks.dart'; | 10 import 'mocks.dart'; |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 expect(source.shortName, 'test.dart'); | 417 expect(source.shortName, 'test.dart'); |
| 418 }); | 418 }); |
| 419 | 419 |
| 420 test('resolveRelative', () { | 420 test('resolveRelative', () { |
| 421 var relative = source.resolveRelative(new Uri.file('bar/baz.dart')); | 421 var relative = source.resolveRelative(new Uri.file('bar/baz.dart')); |
| 422 expect(relative.fullName, '/foo/bar/baz.dart'); | 422 expect(relative.fullName, '/foo/bar/baz.dart'); |
| 423 }); | 423 }); |
| 424 }); | 424 }); |
| 425 }); | 425 }); |
| 426 }); | 426 }); |
| 427 | |
| 428 group('PhysicalResourceProvider', () { | |
| 429 io.Directory tempDirectory; | |
| 430 String tempPath; | |
| 431 | |
| 432 setUp(() { | |
| 433 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource'); | |
| 434 tempPath = tempDirectory.absolute.path; | |
| 435 }); | |
| 436 | |
| 437 tearDown(() { | |
| 438 tempDirectory.deleteSync(recursive: true); | |
| 439 }); | |
| 440 | |
| 441 group('Watch', () { | |
| 442 | |
| 443 Future delayed(computation()) { | |
| 444 // On Windows, watching the filesystem is accomplished by polling once | |
| 445 // per second. So wait 2 seconds to give time for polling to reliably | |
| 446 // occur. | |
| 447 return new Future.delayed(new Duration(seconds: 2), computation); | |
| 448 } | |
| 449 | |
| 450 watchingFolder(String path, test(List<WatchEvent> changesReceived)) { | |
| 451 // Delay before we start watching the folder. This is necessary | |
| 452 // because on MacOS, file modifications that occur just before we start | |
| 453 // watching are sometimes misclassified as happening just after we | |
| 454 // start watching. | |
| 455 return delayed(() { | |
| 456 Folder folder = PhysicalResourceProvider.INSTANCE.getResource(path); | |
| 457 var changesReceived = <WatchEvent>[]; | |
| 458 var subscription = folder.changes.listen(changesReceived.add); | |
| 459 // Delay running the rest of the test to allow folder.changes to take | |
| 460 // a snapshot of the current directory state. Otherwise it won't be | |
| 461 // able to reliably distinguish new files from modified ones. | |
| 462 return delayed(() => test(changesReceived)).whenComplete(() { | |
| 463 subscription.cancel(); | |
| 464 }); | |
| 465 }); | |
| 466 } | |
| 467 | |
| 468 test('create file', () => watchingFolder(tempPath, (changesReceived) { | |
| 469 expect(changesReceived, hasLength(0)); | |
| 470 var path = join(tempPath, 'foo'); | |
| 471 new io.File(path).writeAsStringSync('contents'); | |
| 472 return delayed(() { | |
| 473 expect(changesReceived, hasLength(1)); | |
| 474 expect(changesReceived[0].type, equals(ChangeType.ADD)); | |
| 475 expect(changesReceived[0].path, equals(path)); | |
| 476 }); | |
| 477 })); | |
| 478 | |
| 479 test('modify file', () { | |
| 480 var path = join(tempPath, 'foo'); | |
| 481 var file = new io.File(path); | |
| 482 file.writeAsStringSync('contents 1'); | |
| 483 return watchingFolder(tempPath, (changesReceived) { | |
| 484 expect(changesReceived, hasLength(0)); | |
| 485 file.writeAsStringSync('contents 2'); | |
| 486 return delayed(() { | |
| 487 expect(changesReceived, hasLength(1)); | |
| 488 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); | |
| 489 expect(changesReceived[0].path, equals(path)); | |
| 490 }); | |
| 491 }); | |
| 492 }); | |
| 493 | |
| 494 test('modify file in subdir', () { | |
| 495 var subdirPath = join(tempPath, 'foo'); | |
| 496 new io.Directory(subdirPath).createSync(); | |
| 497 var path = join(tempPath, 'bar'); | |
| 498 var file = new io.File(path); | |
| 499 file.writeAsStringSync('contents 1'); | |
| 500 return watchingFolder(tempPath, (changesReceived) { | |
| 501 expect(changesReceived, hasLength(0)); | |
| 502 file.writeAsStringSync('contents 2'); | |
| 503 return delayed(() { | |
| 504 expect(changesReceived, hasLength(1)); | |
| 505 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); | |
| 506 expect(changesReceived[0].path, equals(path)); | |
| 507 }); | |
| 508 }); | |
| 509 }); | |
| 510 | |
| 511 test('delete file', () { | |
| 512 var path = join(tempPath, 'foo'); | |
| 513 var file = new io.File(path); | |
| 514 file.writeAsStringSync('contents 1'); | |
| 515 return watchingFolder(tempPath, (changesReceived) { | |
| 516 expect(changesReceived, hasLength(0)); | |
| 517 file.deleteSync(); | |
| 518 return delayed(() { | |
| 519 expect(changesReceived, hasLength(1)); | |
| 520 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); | |
| 521 expect(changesReceived[0].path, equals(path)); | |
| 522 }); | |
| 523 }); | |
| 524 }); | |
| 525 }); | |
| 526 | |
| 527 group('File', () { | |
| 528 String path; | |
| 529 File file; | |
| 530 | |
| 531 setUp(() { | |
| 532 path = join(tempPath, 'file.txt'); | |
| 533 file = PhysicalResourceProvider.INSTANCE.getResource(path); | |
| 534 }); | |
| 535 | |
| 536 test('createSource', () { | |
| 537 new io.File(path).writeAsStringSync('contents'); | |
| 538 var source = file.createSource(UriKind.FILE_URI); | |
| 539 expect(source.uriKind, UriKind.FILE_URI); | |
| 540 expect(source.exists(), isTrue); | |
| 541 expect(source.contents.data, 'contents'); | |
| 542 }); | |
| 543 | |
| 544 group('exists', () { | |
| 545 test('false', () { | |
| 546 expect(file.exists, isFalse); | |
| 547 }); | |
| 548 | |
| 549 test('true', () { | |
| 550 new io.File(path).writeAsStringSync('contents'); | |
| 551 expect(file.exists, isTrue); | |
| 552 }); | |
| 553 }); | |
| 554 | |
| 555 test('fullName', () { | |
| 556 expect(file.path, path); | |
| 557 }); | |
| 558 | |
| 559 test('hashCode', () { | |
| 560 file.hashCode; | |
| 561 }); | |
| 562 | |
| 563 test('shortName', () { | |
| 564 expect(file.shortName, 'file.txt'); | |
| 565 }); | |
| 566 | |
| 567 test('toString', () { | |
| 568 expect(file.toString(), path); | |
| 569 }); | |
| 570 }); | |
| 571 | |
| 572 group('Folder', () { | |
| 573 String path; | |
| 574 Folder folder; | |
| 575 | |
| 576 setUp(() { | |
| 577 path = join(tempPath, 'folder'); | |
| 578 new io.Directory(path).createSync(); | |
| 579 folder = PhysicalResourceProvider.INSTANCE.getResource(path); | |
| 580 }); | |
| 581 | |
| 582 group('getChild', () { | |
| 583 test('does not exist', () { | |
| 584 var child = folder.getChild('no-such-resource'); | |
| 585 expect(child, isNotNull); | |
| 586 expect(child.exists, isFalse); | |
| 587 }); | |
| 588 | |
| 589 test('file', () { | |
| 590 new io.File(join(path, 'myFile')).createSync(); | |
| 591 var child = folder.getChild('myFile'); | |
| 592 expect(child, _isFile); | |
| 593 expect(child.exists, isTrue); | |
| 594 }); | |
| 595 | |
| 596 test('folder', () { | |
| 597 new io.Directory(join(path, 'myFolder')).createSync(); | |
| 598 var child = folder.getChild('myFolder'); | |
| 599 expect(child, _isFolder); | |
| 600 expect(child.exists, isTrue); | |
| 601 }); | |
| 602 }); | |
| 603 | |
| 604 test('getChildren', () { | |
| 605 // create 2 files and 1 folder | |
| 606 new io.File(join(path, 'a.txt')).createSync(); | |
| 607 new io.Directory(join(path, 'bFolder')).createSync(); | |
| 608 new io.File(join(path, 'c.txt')).createSync(); | |
| 609 // prepare 3 children | |
| 610 List<Resource> children = folder.getChildren(); | |
| 611 expect(children, hasLength(3)); | |
| 612 children.sort((a, b) => a.shortName.compareTo(b.shortName)); | |
| 613 // check that each child exists | |
| 614 children.forEach((child) { | |
| 615 expect(child.exists, true); | |
| 616 }); | |
| 617 // check names | |
| 618 expect(children[0].shortName, 'a.txt'); | |
| 619 expect(children[1].shortName, 'bFolder'); | |
| 620 expect(children[2].shortName, 'c.txt'); | |
| 621 // check types | |
| 622 expect(children[0], _isFile); | |
| 623 expect(children[1], _isFolder); | |
| 624 expect(children[2], _isFile); | |
| 625 }); | |
| 626 }); | |
| 627 }); | |
| 628 } | 427 } |
| 629 | 428 |
| 630 var _isFile = new isInstanceOf<File>(); | 429 var _isFile = new isInstanceOf<File>(); |
| 631 var _isFolder = new isInstanceOf<Folder>(); | 430 var _isFolder = new isInstanceOf<Folder>(); |
| 632 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>(); | 431 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>(); |
| OLD | NEW |