| 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'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/context_manager.dart'; | 9 import 'package:analysis_server/src/context_manager.dart'; |
| 10 import 'package:analysis_server/src/source/optimizing_pub_package_map_provider.d
art'; | 10 import 'package:analysis_server/src/source/optimizing_pub_package_map_provider.d
art'; |
| 11 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
| 12 import 'package:analyzer/file_system/memory_file_system.dart'; | 12 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 13 import 'package:analyzer/instrumentation/instrumentation.dart'; | 13 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 14 import 'package:analyzer/source/package_map_resolver.dart'; | 14 import 'package:analyzer/source/package_map_resolver.dart'; |
| 15 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/source_io.dart'; | 17 import 'package:analyzer/src/generated/source_io.dart'; |
| 18 import 'package:path/path.dart'; | 18 import 'package:path/path.dart'; |
| 19 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 19 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 20 import 'package:unittest/unittest.dart'; | 20 import 'package:unittest/unittest.dart'; |
| 21 | 21 |
| 22 import 'mocks.dart'; | 22 import 'mocks.dart'; |
| 23 import 'package:analysis_server/uri/resolver_provider.dart'; |
| 23 | 24 |
| 24 main() { | 25 main() { |
| 25 groupSep = ' | '; | 26 groupSep = ' | '; |
| 26 defineReflectiveTests(ContextManagerTest); | 27 defineReflectiveTests(ContextManagerTest); |
| 27 } | 28 } |
| 28 | 29 |
| 29 @reflectiveTest | 30 @reflectiveTest |
| 30 class ContextManagerTest { | 31 class ContextManagerTest { |
| 31 /** | 32 /** |
| 32 * The name of the 'bin' directory. | 33 * The name of the 'bin' directory. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 52 * The name of the 'test' directory. | 53 * The name of the 'test' directory. |
| 53 */ | 54 */ |
| 54 static const String TEST_NAME = 'test'; | 55 static const String TEST_NAME = 'test'; |
| 55 | 56 |
| 56 TestContextManager manager; | 57 TestContextManager manager; |
| 57 | 58 |
| 58 MemoryResourceProvider resourceProvider; | 59 MemoryResourceProvider resourceProvider; |
| 59 | 60 |
| 60 MockPackageMapProvider packageMapProvider; | 61 MockPackageMapProvider packageMapProvider; |
| 61 | 62 |
| 63 UriResolver packageResolver = null; |
| 64 |
| 62 String projPath = '/my/proj'; | 65 String projPath = '/my/proj'; |
| 63 | 66 |
| 64 String newFile(List<String> pathComponents, [String content = '']) { | 67 String newFile(List<String> pathComponents, [String content = '']) { |
| 65 String filePath = posix.joinAll(pathComponents); | 68 String filePath = posix.joinAll(pathComponents); |
| 66 resourceProvider.newFile(filePath, content); | 69 resourceProvider.newFile(filePath, content); |
| 67 return filePath; | 70 return filePath; |
| 68 } | 71 } |
| 69 | 72 |
| 70 String newFolder(List<String> pathComponents) { | 73 String newFolder(List<String> pathComponents) { |
| 71 String folderPath = posix.joinAll(pathComponents); | 74 String folderPath = posix.joinAll(pathComponents); |
| 72 resourceProvider.newFolder(folderPath); | 75 resourceProvider.newFolder(folderPath); |
| 73 return folderPath; | 76 return folderPath; |
| 74 } | 77 } |
| 75 | 78 |
| 79 UriResolver providePackageResolver(Folder folder) { |
| 80 return packageResolver; |
| 81 } |
| 82 |
| 76 void setUp() { | 83 void setUp() { |
| 77 resourceProvider = new MemoryResourceProvider(); | 84 resourceProvider = new MemoryResourceProvider(); |
| 78 packageMapProvider = new MockPackageMapProvider(); | 85 packageMapProvider = new MockPackageMapProvider(); |
| 79 manager = new TestContextManager(resourceProvider, packageMapProvider); | 86 manager = new TestContextManager(resourceProvider, providePackageResolver, p
ackageMapProvider); |
| 80 resourceProvider.newFolder(projPath); | 87 resourceProvider.newFolder(projPath); |
| 81 } | 88 } |
| 82 | 89 |
| 83 test_ignoreFilesInPackagesFolder() { | 90 test_ignoreFilesInPackagesFolder() { |
| 84 // create a context with a pubspec.yaml file | 91 // create a context with a pubspec.yaml file |
| 85 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 92 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 86 resourceProvider.newFile(pubspecPath, 'pubspec'); | 93 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 87 // create a file in the "packages" folder | 94 // create a file in the "packages" folder |
| 88 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); | 95 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); |
| 89 resourceProvider.newFile(filePath1, 'contents'); | 96 resourceProvider.newFile(filePath1, 'contents'); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 195 } |
| 189 | 196 |
| 190 void test_setRoots_addFolderWithDartFile() { | 197 void test_setRoots_addFolderWithDartFile() { |
| 191 String filePath = posix.join(projPath, 'foo.dart'); | 198 String filePath = posix.join(projPath, 'foo.dart'); |
| 192 resourceProvider.newFile(filePath, 'contents'); | 199 resourceProvider.newFile(filePath, 'contents'); |
| 193 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 200 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 194 // verify | 201 // verify |
| 195 var filePaths = manager.currentContextFilePaths[projPath]; | 202 var filePaths = manager.currentContextFilePaths[projPath]; |
| 196 expect(filePaths, hasLength(1)); | 203 expect(filePaths, hasLength(1)); |
| 197 expect(filePaths, contains(filePath)); | 204 expect(filePaths, contains(filePath)); |
| 205 List<AnalysisContext> contextsInAnalysisRoot = manager.contextsInAnalysisRoo
t(resourceProvider.newFolder(projPath)); |
| 206 expect(contextsInAnalysisRoot, hasLength(1)); |
| 207 AnalysisContext context = contextsInAnalysisRoot[0]; |
| 208 expect(context, isNotNull); |
| 209 Source result = context.sourceFactory.forUri('package:foo/foo.dart'); |
| 210 expect(result, isNotNull); |
| 211 expect(result.exists(), isFalse); |
| 212 } |
| 213 |
| 214 void test_setRoots_packageResolver() { |
| 215 Uri uri = Uri.parse('package:foo/foo.dart'); |
| 216 Source source = new TestSource(); |
| 217 packageResolver = new TestUriResolver({uri : source}); |
| 218 String filePath = posix.join(projPath, 'foo.dart'); |
| 219 resourceProvider.newFile(filePath, 'contents'); |
| 220 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 221 |
| 222 List<AnalysisContext> contextsInAnalysisRoot = manager.contextsInAnalysisRoo
t(resourceProvider.newFolder(projPath)); |
| 223 expect(contextsInAnalysisRoot, hasLength(1)); |
| 224 AnalysisContext context = contextsInAnalysisRoot[0]; |
| 225 expect(context, isNotNull); |
| 226 Source result = context.sourceFactory.forUri2(uri); |
| 227 expect(result, same(source)); |
| 198 } | 228 } |
| 199 | 229 |
| 200 void test_setRoots_addFolderWithDartFileInSubfolder() { | 230 void test_setRoots_addFolderWithDartFileInSubfolder() { |
| 201 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | 231 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 202 resourceProvider.newFile(filePath, 'contents'); | 232 resourceProvider.newFile(filePath, 'contents'); |
| 203 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 233 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 204 // verify | 234 // verify |
| 205 var filePaths = manager.currentContextFilePaths[projPath]; | 235 var filePaths = manager.currentContextFilePaths[projPath]; |
| 206 expect(filePaths, hasLength(1)); | 236 expect(filePaths, hasLength(1)); |
| 207 expect(filePaths, contains(filePath)); | 237 expect(filePaths, contains(filePath)); |
| (...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 final Map<String, Set<Source>> currentContextSources = <String, Set<Source>>{ | 1004 final Map<String, Set<Source>> currentContextSources = <String, Set<Source>>{ |
| 975 }; | 1005 }; |
| 976 | 1006 |
| 977 /** | 1007 /** |
| 978 * Map from context to package URI resolver. | 1008 * Map from context to package URI resolver. |
| 979 */ | 1009 */ |
| 980 final Map<String, UriResolver> currentContextPackageUriResolvers = | 1010 final Map<String, UriResolver> currentContextPackageUriResolvers = |
| 981 <String, UriResolver>{}; | 1011 <String, UriResolver>{}; |
| 982 | 1012 |
| 983 TestContextManager(MemoryResourceProvider resourceProvider, | 1013 TestContextManager(MemoryResourceProvider resourceProvider, |
| 1014 ResolverProvider packageResolverProvider, |
| 984 OptimizingPubPackageMapProvider packageMapProvider) | 1015 OptimizingPubPackageMapProvider packageMapProvider) |
| 985 : super(resourceProvider, packageMapProvider, | 1016 : super(resourceProvider, packageResolverProvider, packageMapProvider, |
| 986 InstrumentationService.NULL_SERVICE); | 1017 InstrumentationService.NULL_SERVICE); |
| 987 | 1018 |
| 988 /** | 1019 /** |
| 989 * Iterable of the paths to contexts that currently exist. | 1020 * Iterable of the paths to contexts that currently exist. |
| 990 */ | 1021 */ |
| 991 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; | 1022 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; |
| 992 | 1023 |
| 993 @override | 1024 @override |
| 994 AnalysisContext addContext(Folder folder, UriResolver packageUriResolver) { | 1025 AnalysisContext addContext(Folder folder, UriResolver packageUriResolver) { |
| 995 String path = folder.path; | 1026 String path = folder.path; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 // causing the analyzer to thrash, just ignore links to non-existent files. | 1090 // causing the analyzer to thrash, just ignore links to non-existent files. |
| 1060 return file.exists; | 1091 return file.exists; |
| 1061 } | 1092 } |
| 1062 | 1093 |
| 1063 @override | 1094 @override |
| 1064 void updateContextPackageUriResolver( | 1095 void updateContextPackageUriResolver( |
| 1065 Folder contextFolder, UriResolver packageUriResolver) { | 1096 Folder contextFolder, UriResolver packageUriResolver) { |
| 1066 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; | 1097 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; |
| 1067 } | 1098 } |
| 1068 } | 1099 } |
| 1100 |
| 1101 class TestUriResolver extends UriResolver { |
| 1102 Map<Uri, Source> uriMap; |
| 1103 |
| 1104 TestUriResolver(this.uriMap); |
| 1105 |
| 1106 @override |
| 1107 Source resolveAbsolute(Uri uri) { |
| 1108 return uriMap[uri]; |
| 1109 } |
| 1110 } |
| 1111 |
| 1112 /** |
| 1113 * A [Source] that knows it's [fullName]. |
| 1114 */ |
| 1115 class TestSource implements Source { |
| 1116 TestSource(); |
| 1117 |
| 1118 @override |
| 1119 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 1120 } |
| OLD | NEW |