| 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 'dart:collection'; |
| 8 |
| 7 import 'package:analysis_server/src/context_manager.dart'; | 9 import 'package:analysis_server/src/context_manager.dart'; |
| 8 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| 9 import 'package:analyzer/file_system/memory_file_system.dart'; | 11 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 10 import 'package:analyzer/source/package_map_provider.dart'; | 12 import 'package:analyzer/source/package_map_provider.dart'; |
| 11 import 'package:analyzer/source/package_map_resolver.dart'; | 13 import 'package:analyzer/source/package_map_resolver.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 14 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:analyzer/src/generated/source_io.dart'; | 16 import 'package:analyzer/src/generated/source_io.dart'; |
| 15 import 'package:path/path.dart'; | 17 import 'package:path/path.dart'; |
| 16 import 'package:unittest/unittest.dart'; | 18 import 'package:unittest/unittest.dart'; |
| 17 | 19 |
| 18 import 'mocks.dart'; | 20 import 'mocks.dart'; |
| 19 import 'reflective_tests.dart'; | 21 import 'reflective_tests.dart'; |
| 20 | 22 |
| 21 | 23 |
| 22 main() { | 24 main() { |
| 23 groupSep = ' | '; | 25 groupSep = ' | '; |
| 24 runReflectiveTests(ContextManagerTest); | 26 runReflectiveTests(ContextManagerTest); |
| 25 } | 27 } |
| 26 | 28 |
| 27 | 29 |
| 28 @reflectiveTest | 30 @reflectiveTest |
| 29 class ContextManagerTest { | 31 class ContextManagerTest { |
| 32 /** |
| 33 * The name of the 'bin' directory. |
| 34 */ |
| 35 static const String BIN_NAME = 'bin'; |
| 36 /** |
| 37 * The name of the 'lib' directory. |
| 38 */ |
| 39 static const String LIB_NAME = 'lib'; |
| 40 /** |
| 41 * The name of the 'src' directory. |
| 42 */ |
| 43 static const String SRC_NAME = 'src'; |
| 44 |
| 45 /** |
| 46 * The name of the 'test' directory. |
| 47 */ |
| 48 static const String TEST_NAME = 'test'; |
| 49 |
| 30 TestContextManager manager; | 50 TestContextManager manager; |
| 51 |
| 31 MemoryResourceProvider resourceProvider; | 52 MemoryResourceProvider resourceProvider; |
| 53 |
| 32 MockPackageMapProvider packageMapProvider; | 54 MockPackageMapProvider packageMapProvider; |
| 33 | 55 |
| 34 String projPath = '/my/proj'; | 56 String projPath = '/my/proj'; |
| 35 | 57 |
| 58 String newFile(List<String> pathComponents, [String content = '']) { |
| 59 String filePath = posix.joinAll(pathComponents); |
| 60 resourceProvider.newFile(filePath, content); |
| 61 return filePath; |
| 62 } |
| 63 |
| 64 String newFolder(List<String> pathComponents) { |
| 65 String folderPath = posix.joinAll(pathComponents); |
| 66 resourceProvider.newFolder(folderPath); |
| 67 return folderPath; |
| 68 } |
| 69 |
| 36 void setUp() { | 70 void setUp() { |
| 37 resourceProvider = new MemoryResourceProvider(); | 71 resourceProvider = new MemoryResourceProvider(); |
| 38 packageMapProvider = new MockPackageMapProvider(); | 72 packageMapProvider = new MockPackageMapProvider(); |
| 39 manager = new TestContextManager(resourceProvider, packageMapProvider); | 73 manager = new TestContextManager(resourceProvider, packageMapProvider); |
| 40 resourceProvider.newFolder(projPath); | 74 resourceProvider.newFolder(projPath); |
| 41 } | 75 } |
| 42 | 76 |
| 43 test_ignoreFilesInPackagesFolder() { | 77 test_ignoreFilesInPackagesFolder() { |
| 44 // create a context with a pubspec.yaml file | 78 // create a context with a pubspec.yaml file |
| 45 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 79 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 void test_setRoots_addFolderWithPubspec() { | 201 void test_setRoots_addFolderWithPubspec() { |
| 168 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 202 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 169 resourceProvider.newFile(pubspecPath, 'pubspec'); | 203 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 170 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 204 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 171 // verify | 205 // verify |
| 172 expect(manager.currentContextPaths, hasLength(1)); | 206 expect(manager.currentContextPaths, hasLength(1)); |
| 173 expect(manager.currentContextPaths, contains(projPath)); | 207 expect(manager.currentContextPaths, contains(projPath)); |
| 174 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | 208 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 175 } | 209 } |
| 176 | 210 |
| 211 void test_setRoots_addFolderWithPubspecAndLib() { |
| 212 String binPath = newFolder([projPath, BIN_NAME]); |
| 213 String libPath = newFolder([projPath, LIB_NAME]); |
| 214 String srcPath = newFolder([libPath, SRC_NAME]); |
| 215 String testPath = newFolder([projPath, TEST_NAME]); |
| 216 |
| 217 newFile([projPath, PUBSPEC_NAME]); |
| 218 String appPath = newFile([binPath, 'app.dart']); |
| 219 newFile([libPath, 'main.dart']); |
| 220 newFile([srcPath, 'internal.dart']); |
| 221 String testFilePath = newFile([testPath, 'main_test.dart']); |
| 222 |
| 223 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 224 Set<Source> sources = manager.currentContextSources[projPath]; |
| 225 |
| 226 expect(manager.currentContextPaths, hasLength(1)); |
| 227 expect(manager.currentContextPaths, contains(projPath)); |
| 228 expect(sources, hasLength(4)); |
| 229 List<String> uris = |
| 230 sources.map((Source source) => source.uri.toString()).toList(); |
| 231 expect(uris, contains('file://$appPath')); |
| 232 expect(uris, contains('package:proj/main.dart')); |
| 233 expect(uris, contains('package:proj/src/internal.dart')); |
| 234 expect(uris, contains('file://$testFilePath')); |
| 235 } |
| 236 |
| 177 void test_setRoots_addFolderWithPubspecFolders() { | 237 void test_setRoots_addFolderWithPubspecFolders() { |
| 178 // prepare paths | 238 // prepare paths |
| 179 String root = '/root'; | 239 String root = '/root'; |
| 180 String rootFile = '$root/root.dart'; | 240 String rootFile = '$root/root.dart'; |
| 181 String subProjectA = '$root/sub/aaa'; | 241 String subProjectA = '$root/sub/aaa'; |
| 182 String subProjectB = '$root/sub/sub2/bbb'; | 242 String subProjectB = '$root/sub/sub2/bbb'; |
| 183 String subProjectA_file = '$subProjectA/bin/a.dart'; | 243 String subProjectA_file = '$subProjectA/bin/a.dart'; |
| 184 String subProjectB_file = '$subProjectB/bin/b.dart'; | 244 String subProjectB_file = '$subProjectB/bin/b.dart'; |
| 185 // create files | 245 // create files |
| 186 resourceProvider.newFile('$subProjectA/pubspec.yaml', 'pubspec'); | 246 resourceProvider.newFile('$subProjectA/pubspec.yaml', 'pubspec'); |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 */ | 845 */ |
| 786 Map<String, int> currentContextTimestamps = <String, int>{}; | 846 Map<String, int> currentContextTimestamps = <String, int>{}; |
| 787 | 847 |
| 788 /** | 848 /** |
| 789 * Map from context to (map from file path to timestamp of last event). | 849 * Map from context to (map from file path to timestamp of last event). |
| 790 */ | 850 */ |
| 791 final Map<String, Map<String, int>> currentContextFilePaths = <String, | 851 final Map<String, Map<String, int>> currentContextFilePaths = <String, |
| 792 Map<String, int>>{}; | 852 Map<String, int>>{}; |
| 793 | 853 |
| 794 /** | 854 /** |
| 855 * A map from the paths of contexts to a set of the sources that should be |
| 856 * explicitly analyzed in those contexts. |
| 857 */ |
| 858 final Map<String, Set<Source>> currentContextSources = <String, |
| 859 Set<Source>>{}; |
| 860 |
| 861 /** |
| 795 * Map from context to package URI resolver. | 862 * Map from context to package URI resolver. |
| 796 */ | 863 */ |
| 797 final Map<String, UriResolver> currentContextPackageUriResolvers = <String, | 864 final Map<String, UriResolver> currentContextPackageUriResolvers = <String, |
| 798 UriResolver>{}; | 865 UriResolver>{}; |
| 799 | 866 |
| 800 TestContextManager(MemoryResourceProvider resourceProvider, | 867 TestContextManager(MemoryResourceProvider resourceProvider, |
| 801 PackageMapProvider packageMapProvider) | 868 PackageMapProvider packageMapProvider) |
| 802 : super(resourceProvider, packageMapProvider); | 869 : super(resourceProvider, packageMapProvider); |
| 803 | 870 |
| 804 /** | 871 /** |
| 805 * Iterable of the paths to contexts that currently exist. | 872 * Iterable of the paths to contexts that currently exist. |
| 806 */ | 873 */ |
| 807 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; | 874 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; |
| 808 | 875 |
| 809 @override | 876 @override |
| 810 void addContext(Folder folder, UriResolver packageUriResolver) { | 877 AnalysisContext addContext(Folder folder, UriResolver packageUriResolver) { |
| 811 String path = folder.path; | 878 String path = folder.path; |
| 812 expect(currentContextPaths, isNot(contains(path))); | 879 expect(currentContextPaths, isNot(contains(path))); |
| 813 currentContextTimestamps[path] = now; | 880 currentContextTimestamps[path] = now; |
| 814 currentContextFilePaths[path] = <String, int>{}; | 881 currentContextFilePaths[path] = <String, int>{}; |
| 882 currentContextSources[path] = new HashSet<Source>(); |
| 815 currentContextPackageUriResolvers[path] = packageUriResolver; | 883 currentContextPackageUriResolvers[path] = packageUriResolver; |
| 884 return null; |
| 816 } | 885 } |
| 817 | 886 |
| 818 @override | 887 @override |
| 819 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { | 888 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { |
| 820 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; | 889 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; |
| 890 Set<Source> sources = currentContextSources[contextFolder.path]; |
| 891 |
| 821 for (Source source in changeSet.addedSources) { | 892 for (Source source in changeSet.addedSources) { |
| 822 expect(filePaths, isNot(contains(source.fullName))); | 893 expect(filePaths, isNot(contains(source.fullName))); |
| 823 filePaths[source.fullName] = now; | 894 filePaths[source.fullName] = now; |
| 895 sources.add(source); |
| 824 } | 896 } |
| 825 for (Source source in changeSet.removedSources) { | 897 for (Source source in changeSet.removedSources) { |
| 826 expect(filePaths, contains(source.fullName)); | 898 expect(filePaths, contains(source.fullName)); |
| 827 filePaths.remove(source.fullName); | 899 filePaths.remove(source.fullName); |
| 900 sources.remove(source); |
| 828 } | 901 } |
| 829 for (Source source in changeSet.changedSources) { | 902 for (Source source in changeSet.changedSources) { |
| 830 expect(filePaths, contains(source.fullName)); | 903 expect(filePaths, contains(source.fullName)); |
| 831 filePaths[source.fullName] = now; | 904 filePaths[source.fullName] = now; |
| 832 } | 905 } |
| 833 } | 906 } |
| 834 | 907 |
| 835 void assertContextFiles(String contextPath, List<String> expectedFiles) { | 908 void assertContextFiles(String contextPath, List<String> expectedFiles) { |
| 836 var actualFiles = currentContextFilePaths[contextPath].keys; | 909 var actualFiles = currentContextFilePaths[contextPath].keys; |
| 837 expect(actualFiles, unorderedEquals(expectedFiles)); | 910 expect(actualFiles, unorderedEquals(expectedFiles)); |
| 838 } | 911 } |
| 839 | 912 |
| 840 void assertContextPaths(List<String> expected) { | 913 void assertContextPaths(List<String> expected) { |
| 841 expect(currentContextPaths, unorderedEquals(expected)); | 914 expect(currentContextPaths, unorderedEquals(expected)); |
| 842 } | 915 } |
| 843 | 916 |
| 844 @override | 917 @override |
| 845 void removeContext(Folder folder) { | 918 void removeContext(Folder folder) { |
| 846 String path = folder.path; | 919 String path = folder.path; |
| 847 expect(currentContextPaths, contains(path)); | 920 expect(currentContextPaths, contains(path)); |
| 848 currentContextTimestamps.remove(path); | 921 currentContextTimestamps.remove(path); |
| 849 currentContextFilePaths.remove(path); | 922 currentContextFilePaths.remove(path); |
| 923 currentContextSources.remove(path); |
| 850 currentContextPackageUriResolvers.remove(path); | 924 currentContextPackageUriResolvers.remove(path); |
| 851 } | 925 } |
| 852 | 926 |
| 853 @override | 927 @override |
| 854 void updateContextPackageUriResolver(Folder contextFolder, | 928 void updateContextPackageUriResolver(Folder contextFolder, |
| 855 UriResolver packageUriResolver) { | 929 UriResolver packageUriResolver) { |
| 856 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; | 930 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; |
| 857 } | 931 } |
| 858 } | 932 } |
| OLD | NEW |