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.context.directory.manager; | 5 library test.context.directory.manager; |
6 | 6 |
7 import 'package:analysis_server/src/context_manager.dart'; | 7 import 'package:analysis_server/src/context_manager.dart'; |
8 import 'reflective_tests.dart'; | 8 import 'reflective_tests.dart'; |
9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
10 import 'package:analyzer/file_system/memory_file_system.dart'; | 10 import 'package:analyzer/file_system/memory_file_system.dart'; |
11 import 'package:analyzer/source/package_map_provider.dart'; | 11 import 'package:analyzer/source/package_map_provider.dart'; |
| 12 import 'package:analyzer/source/package_map_resolver.dart'; |
12 import 'package:analyzer/src/generated/engine.dart'; | 13 import 'package:analyzer/src/generated/engine.dart'; |
13 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:analyzer/src/generated/source_io.dart'; |
14 import 'package:path/path.dart'; | 16 import 'package:path/path.dart'; |
15 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
16 | 18 |
17 import 'mocks.dart'; | 19 import 'mocks.dart'; |
18 | 20 |
19 | 21 |
20 main() { | 22 main() { |
21 groupSep = ' | '; | 23 groupSep = ' | '; |
22 runReflectiveTests(ContextManagerTest); | 24 runReflectiveTests(ContextManagerTest); |
23 } | 25 } |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 }, | 185 }, |
184 }; | 186 }; |
185 // set roots | 187 // set roots |
186 manager.setRoots(<String>[root], <String>[], <String, String>{}); | 188 manager.setRoots(<String>[root], <String>[], <String, String>{}); |
187 manager.assertContextPaths([root, subProjectA, subProjectB]); | 189 manager.assertContextPaths([root, subProjectA, subProjectB]); |
188 // verify files | 190 // verify files |
189 manager.assertContextFiles(root, [rootFile]); | 191 manager.assertContextFiles(root, [rootFile]); |
190 manager.assertContextFiles(subProjectA, [subProjectA_file]); | 192 manager.assertContextFiles(subProjectA, [subProjectA_file]); |
191 manager.assertContextFiles(subProjectB, [subProjectB_file]); | 193 manager.assertContextFiles(subProjectB, [subProjectB_file]); |
192 // verify package maps | 194 // verify package maps |
193 expect( | 195 _checkPackageMap(root, equals(packageMapProvider.packageMaps[root])); |
194 manager.currentContextPackageMaps[root], | 196 _checkPackageMap(subProjectA, |
195 equals(packageMapProvider.packageMaps[root])); | |
196 expect( | |
197 manager.currentContextPackageMaps[subProjectA], | |
198 equals(packageMapProvider.packageMaps[subProjectA])); | 197 equals(packageMapProvider.packageMaps[subProjectA])); |
199 expect( | 198 _checkPackageMap(subProjectB, |
200 manager.currentContextPackageMaps[subProjectB], | |
201 equals(packageMapProvider.packageMaps[subProjectB])); | 199 equals(packageMapProvider.packageMaps[subProjectB])); |
202 } | 200 } |
203 | 201 |
204 void test_setRoots_addFolderWithoutPubspec() { | 202 void test_setRoots_addFolderWithoutPubspec() { |
205 packageMapProvider.packageMap = null; | 203 packageMapProvider.packageMap = null; |
206 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 204 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
207 // verify | 205 // verify |
208 expect(manager.currentContextPaths, hasLength(1)); | 206 expect(manager.currentContextPaths, hasLength(1)); |
209 expect(manager.currentContextPaths, contains(projPath)); | 207 expect(manager.currentContextPaths, contains(projPath)); |
210 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | 208 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
211 } | 209 } |
212 | 210 |
| 211 void test_setRoots_addPackageRoot() { |
| 212 String packagePathFoo = '/package1/foo'; |
| 213 String packageRootPath = '/package2/foo'; |
| 214 Folder packageFolder = resourceProvider.newFolder(packagePathFoo); |
| 215 packageMapProvider.packageMap = { |
| 216 'foo': [packageFolder] |
| 217 }; |
| 218 List<String> includedPaths = <String>[projPath]; |
| 219 List<String> excludedPaths = <String>[]; |
| 220 manager.setRoots(includedPaths, excludedPaths, <String, String>{}); |
| 221 _checkPackageMap(projPath, equals(packageMapProvider.packageMap)); |
| 222 manager.setRoots(includedPaths, excludedPaths, |
| 223 <String, String>{ projPath: packageRootPath }); |
| 224 _checkPackageRoot(projPath, equals(packageRootPath)); |
| 225 } |
| 226 |
| 227 void test_setRoots_changePackageRoot() { |
| 228 String packageRootPath1 = '/package1'; |
| 229 String packageRootPath2 = '/package2'; |
| 230 List<String> includedPaths = <String>[projPath]; |
| 231 List<String> excludedPaths = <String>[]; |
| 232 manager.setRoots(includedPaths, excludedPaths, |
| 233 <String, String>{ projPath: packageRootPath1 }); |
| 234 _checkPackageRoot(projPath, equals(packageRootPath1)); |
| 235 manager.setRoots(includedPaths, excludedPaths, |
| 236 <String, String>{ projPath: packageRootPath2 }); |
| 237 _checkPackageRoot(projPath, equals(packageRootPath2)); |
| 238 } |
| 239 |
213 void test_setRoots_exclude_newRoot_withExcludedFile() { | 240 void test_setRoots_exclude_newRoot_withExcludedFile() { |
214 // prepare paths | 241 // prepare paths |
215 String project = '/project'; | 242 String project = '/project'; |
216 String file1 = '$project/file1.dart'; | 243 String file1 = '$project/file1.dart'; |
217 String file2 = '$project/file2.dart'; | 244 String file2 = '$project/file2.dart'; |
218 // create files | 245 // create files |
219 resourceProvider.newFile(file1, '// 1'); | 246 resourceProvider.newFile(file1, '// 1'); |
220 resourceProvider.newFile(file2, '// 2'); | 247 resourceProvider.newFile(file2, '// 2'); |
221 // set roots | 248 // set roots |
222 manager.setRoots(<String>[project], <String>[file1], <String, String>{}); | 249 manager.setRoots(<String>[project], <String>[file1], <String, String>{}); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 manager.assertContextFiles(subProject, [subFile]); | 363 manager.assertContextFiles(subProject, [subFile]); |
337 } | 364 } |
338 | 365 |
339 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { | 366 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { |
340 String packagePath = '/package/foo'; | 367 String packagePath = '/package/foo'; |
341 Folder packageFolder = resourceProvider.newFolder(packagePath); | 368 Folder packageFolder = resourceProvider.newFolder(packagePath); |
342 packageMapProvider.packageMap = { | 369 packageMapProvider.packageMap = { |
343 'foo': [packageFolder] | 370 'foo': [packageFolder] |
344 }; | 371 }; |
345 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 372 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
346 expect( | 373 _checkPackageMap(projPath, equals(packageMapProvider.packageMap)); |
347 manager.currentContextPackageMaps[projPath], | 374 } |
348 equals(packageMapProvider.packageMap)); | 375 |
| 376 void test_setRoots_newFolderWithPackageRoot() { |
| 377 String packageRootPath = '/package'; |
| 378 manager.setRoots(<String>[projPath], <String>[], |
| 379 <String, String>{ projPath: packageRootPath }); |
| 380 _checkPackageRoot(projPath, equals(packageRootPath)); |
349 } | 381 } |
350 | 382 |
351 void test_setRoots_removeFolderWithPubspec() { | 383 void test_setRoots_removeFolderWithPubspec() { |
352 // create a pubspec | 384 // create a pubspec |
353 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 385 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
354 resourceProvider.newFile(pubspecPath, 'pubspec'); | 386 resourceProvider.newFile(pubspecPath, 'pubspec'); |
355 // add one root - there is a context | 387 // add one root - there is a context |
356 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 388 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
357 expect(manager.currentContextPaths, hasLength(1)); | 389 expect(manager.currentContextPaths, hasLength(1)); |
358 // set empty roots - no contexts | 390 // set empty roots - no contexts |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 packageMapProvider.packageMap = null; | 431 packageMapProvider.packageMap = null; |
400 // add one root - there is a context | 432 // add one root - there is a context |
401 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 433 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
402 expect(manager.currentContextPaths, hasLength(1)); | 434 expect(manager.currentContextPaths, hasLength(1)); |
403 // set empty roots - no contexts | 435 // set empty roots - no contexts |
404 manager.setRoots(<String>[], <String>[], <String, String>{}); | 436 manager.setRoots(<String>[], <String>[], <String, String>{}); |
405 expect(manager.currentContextPaths, hasLength(0)); | 437 expect(manager.currentContextPaths, hasLength(0)); |
406 expect(manager.currentContextFilePaths, hasLength(0)); | 438 expect(manager.currentContextFilePaths, hasLength(0)); |
407 } | 439 } |
408 | 440 |
| 441 void test_setRoots_removePackageRoot() { |
| 442 String packagePathFoo = '/package1/foo'; |
| 443 String packageRootPath = '/package2/foo'; |
| 444 Folder packageFolder = resourceProvider.newFolder(packagePathFoo); |
| 445 packageMapProvider.packageMap = { |
| 446 'foo': [packageFolder] |
| 447 }; |
| 448 List<String> includedPaths = <String>[projPath]; |
| 449 List<String> excludedPaths = <String>[]; |
| 450 manager.setRoots(includedPaths, excludedPaths, |
| 451 <String, String>{ projPath: packageRootPath }); |
| 452 _checkPackageRoot(projPath, equals(packageRootPath)); |
| 453 manager.setRoots(includedPaths, excludedPaths, <String, String>{}); |
| 454 _checkPackageMap(projPath, equals(packageMapProvider.packageMap)); |
| 455 } |
| 456 |
409 test_watch_addDummyLink() { | 457 test_watch_addDummyLink() { |
410 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 458 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
411 // empty folder initially | 459 // empty folder initially |
412 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 460 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
413 expect(filePaths, isEmpty); | 461 expect(filePaths, isEmpty); |
414 // add link | 462 // add link |
415 String filePath = posix.join(projPath, 'foo.dart'); | 463 String filePath = posix.join(projPath, 'foo.dart'); |
416 resourceProvider.newDummyLink(filePath); | 464 resourceProvider.newDummyLink(filePath); |
417 // the link was ignored | 465 // the link was ignored |
418 return pumpEventQueue().then((_) { | 466 return pumpEventQueue().then((_) { |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 test_watch_modifyPackageMapDependency() { | 671 test_watch_modifyPackageMapDependency() { |
624 // create a dependency file | 672 // create a dependency file |
625 String dependencyPath = posix.join(projPath, 'dep'); | 673 String dependencyPath = posix.join(projPath, 'dep'); |
626 resourceProvider.newFile(dependencyPath, 'contents'); | 674 resourceProvider.newFile(dependencyPath, 'contents'); |
627 packageMapProvider.dependencies.add(dependencyPath); | 675 packageMapProvider.dependencies.add(dependencyPath); |
628 // create a Dart file | 676 // create a Dart file |
629 String dartFilePath = posix.join(projPath, 'main.dart'); | 677 String dartFilePath = posix.join(projPath, 'main.dart'); |
630 resourceProvider.newFile(dartFilePath, 'contents'); | 678 resourceProvider.newFile(dartFilePath, 'contents'); |
631 // the created context has the expected empty package map | 679 // the created context has the expected empty package map |
632 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 680 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
633 expect(manager.currentContextPackageMaps[projPath], isEmpty); | 681 _checkPackageMap(projPath, isEmpty); |
634 // configure package map | 682 // configure package map |
635 String packagePath = '/package/foo'; | 683 String packagePath = '/package/foo'; |
636 resourceProvider.newFolder(packagePath); | 684 resourceProvider.newFolder(packagePath); |
637 packageMapProvider.packageMap = { | 685 packageMapProvider.packageMap = { |
638 'foo': projPath | 686 'foo': projPath |
639 }; | 687 }; |
640 // Changing a .dart file in the project shouldn't cause a new | 688 // Changing a .dart file in the project shouldn't cause a new |
641 // package map to be picked up. | 689 // package map to be picked up. |
642 resourceProvider.modifyFile(dartFilePath, 'new contents'); | 690 resourceProvider.modifyFile(dartFilePath, 'new contents'); |
643 return pumpEventQueue().then((_) { | 691 return pumpEventQueue().then((_) { |
644 expect(manager.currentContextPackageMaps[projPath], isEmpty); | 692 _checkPackageMap(projPath, isEmpty); |
645 // However, changing the package map dependency should. | 693 // However, changing the package map dependency should. |
646 resourceProvider.modifyFile(dependencyPath, 'new contents'); | 694 resourceProvider.modifyFile(dependencyPath, 'new contents'); |
647 return pumpEventQueue().then((_) { | 695 return pumpEventQueue().then((_) { |
648 expect( | 696 _checkPackageMap(projPath, equals(packageMapProvider.packageMap)); |
649 manager.currentContextPackageMaps[projPath], | |
650 equals(packageMapProvider.packageMap)); | |
651 }); | 697 }); |
652 }); | 698 }); |
653 } | 699 } |
654 | 700 |
655 test_watch_modifyPackageMapDependency_fail() { | 701 test_watch_modifyPackageMapDependency_fail() { |
656 // create a dependency file | 702 // create a dependency file |
657 String dependencyPath = posix.join(projPath, 'dep'); | 703 String dependencyPath = posix.join(projPath, 'dep'); |
658 resourceProvider.newFile(dependencyPath, 'contents'); | 704 resourceProvider.newFile(dependencyPath, 'contents'); |
659 packageMapProvider.dependencies.add(dependencyPath); | 705 packageMapProvider.dependencies.add(dependencyPath); |
660 // create a Dart file | 706 // create a Dart file |
661 String dartFilePath = posix.join(projPath, 'main.dart'); | 707 String dartFilePath = posix.join(projPath, 'main.dart'); |
662 resourceProvider.newFile(dartFilePath, 'contents'); | 708 resourceProvider.newFile(dartFilePath, 'contents'); |
663 // the created context has the expected empty package map | 709 // the created context has the expected empty package map |
664 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 710 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
665 expect(manager.currentContextPackageMaps[projPath], isEmpty); | 711 _checkPackageMap(projPath, isEmpty); |
666 // Change the package map dependency so that the packageMapProvider is | 712 // Change the package map dependency so that the packageMapProvider is |
667 // re-run, and arrange for it to return null from computePackageMap(). | 713 // re-run, and arrange for it to return null from computePackageMap(). |
668 packageMapProvider.packageMap = null; | 714 packageMapProvider.packageMap = null; |
669 resourceProvider.modifyFile(dependencyPath, 'new contents'); | 715 resourceProvider.modifyFile(dependencyPath, 'new contents'); |
670 return pumpEventQueue().then((_) { | 716 return pumpEventQueue().then((_) { |
671 // The package map should have been changed to null. | 717 // The package map should have been changed to null. |
672 expect(manager.currentContextPackageMaps[projPath], isNull); | 718 _checkPackageMap(projPath, isNull); |
673 }); | 719 }); |
674 } | 720 } |
| 721 |
| 722 /** |
| 723 * Verify that package URI's for source files in [path] will be resolved |
| 724 * using a package map matching [expectation]. |
| 725 */ |
| 726 void _checkPackageMap(String path, expectation) { |
| 727 UriResolver resolver = manager.currentContextPackageUriResolvers[path]; |
| 728 expect(resolver, new isInstanceOf<PackageMapUriResolver>()); |
| 729 PackageMapUriResolver packageMapUriResolver = resolver; |
| 730 expect(packageMapUriResolver.packageMap, expectation); |
| 731 } |
| 732 |
| 733 /** |
| 734 * Verify that package URI's for source files in [path] will be resolved |
| 735 * using a package root maching [expectation]. |
| 736 */ |
| 737 void _checkPackageRoot(String path, expectation) { |
| 738 UriResolver resolver = manager.currentContextPackageUriResolvers[path]; |
| 739 expect(resolver, new isInstanceOf<PackageUriResolver>()); |
| 740 PackageUriResolver packageUriResolver = resolver; |
| 741 expect(packageUriResolver.packagesDirectory_forTesting, expectation); |
| 742 } |
675 } | 743 } |
676 | 744 |
677 | 745 |
678 class TestContextManager extends ContextManager { | 746 class TestContextManager extends ContextManager { |
679 /** | 747 /** |
680 * Source of timestamps stored in [currentContextFilePaths]. | 748 * Source of timestamps stored in [currentContextFilePaths]. |
681 */ | 749 */ |
682 int now = 0; | 750 int now = 0; |
683 | 751 |
684 /** | 752 /** |
685 * Map from context to the timestamp when the context was created. | 753 * Map from context to the timestamp when the context was created. |
686 */ | 754 */ |
687 Map<String, int> currentContextTimestamps = <String, int>{}; | 755 Map<String, int> currentContextTimestamps = <String, int>{}; |
688 | 756 |
689 /** | 757 /** |
690 * Iterable of the paths to contexts that currently exist. | 758 * Iterable of the paths to contexts that currently exist. |
691 */ | 759 */ |
692 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; | 760 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; |
693 | 761 |
694 /** | 762 /** |
695 * Map from context to (map from file path to timestamp of last event). | 763 * Map from context to (map from file path to timestamp of last event). |
696 */ | 764 */ |
697 final Map<String, Map<String, int>> currentContextFilePaths = <String, | 765 final Map<String, Map<String, int>> currentContextFilePaths = <String, |
698 Map<String, int>>{}; | 766 Map<String, int>>{}; |
699 | 767 |
700 /** | 768 /** |
701 * Map from context to package map. | 769 * Map from context to package URI resolver. |
702 */ | 770 */ |
703 final Map<String, Map<String, List<Folder>>> currentContextPackageMaps = | 771 final Map<String, UriResolver> currentContextPackageUriResolvers = |
704 <String, Map<String, List<Folder>>>{}; | 772 <String, UriResolver>{}; |
705 | 773 |
706 TestContextManager(MemoryResourceProvider resourceProvider, | 774 TestContextManager(MemoryResourceProvider resourceProvider, |
707 PackageMapProvider packageMapProvider) | 775 PackageMapProvider packageMapProvider) |
708 : super(resourceProvider, packageMapProvider); | 776 : super(resourceProvider, packageMapProvider); |
709 | 777 |
710 @override | 778 @override |
711 void addContext(Folder folder, Map<String, List<Folder>> packageMap) { | 779 void addContext(Folder folder, UriResolver packageUriResolver) { |
712 String path = folder.path; | 780 String path = folder.path; |
713 expect(currentContextPaths, isNot(contains(path))); | 781 expect(currentContextPaths, isNot(contains(path))); |
714 currentContextTimestamps[path] = now; | 782 currentContextTimestamps[path] = now; |
715 currentContextFilePaths[path] = <String, int>{}; | 783 currentContextFilePaths[path] = <String, int>{}; |
716 currentContextPackageMaps[path] = packageMap; | 784 currentContextPackageUriResolvers[path] = packageUriResolver; |
717 } | 785 } |
718 | 786 |
719 @override | 787 @override |
720 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { | 788 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { |
721 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; | 789 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; |
722 for (Source source in changeSet.addedSources) { | 790 for (Source source in changeSet.addedSources) { |
723 expect(filePaths, isNot(contains(source.fullName))); | 791 expect(filePaths, isNot(contains(source.fullName))); |
724 filePaths[source.fullName] = now; | 792 filePaths[source.fullName] = now; |
725 } | 793 } |
726 for (Source source in changeSet.removedSources) { | 794 for (Source source in changeSet.removedSources) { |
(...skipping 14 matching lines...) Expand all Loading... |
741 void assertContextPaths(List<String> expected) { | 809 void assertContextPaths(List<String> expected) { |
742 expect(currentContextPaths, unorderedEquals(expected)); | 810 expect(currentContextPaths, unorderedEquals(expected)); |
743 } | 811 } |
744 | 812 |
745 @override | 813 @override |
746 void removeContext(Folder folder) { | 814 void removeContext(Folder folder) { |
747 String path = folder.path; | 815 String path = folder.path; |
748 expect(currentContextPaths, contains(path)); | 816 expect(currentContextPaths, contains(path)); |
749 currentContextTimestamps.remove(path); | 817 currentContextTimestamps.remove(path); |
750 currentContextFilePaths.remove(path); | 818 currentContextFilePaths.remove(path); |
751 currentContextPackageMaps.remove(path); | 819 currentContextPackageUriResolvers.remove(path); |
752 } | 820 } |
753 | 821 |
754 @override | 822 @override |
755 void updateContextPackageMap(Folder contextFolder, Map<String, | 823 void updateContextPackageUriResolver(Folder contextFolder, |
756 List<Folder>> packageMap) { | 824 UriResolver packageUriResolver) { |
757 currentContextPackageMaps[contextFolder.path] = packageMap; | 825 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; |
758 } | 826 } |
759 } | 827 } |
OLD | NEW |